move formats pkg to and vendor from buildah

Signed-off-by: Qi Wang <qiwan@redhat.com>
This commit is contained in:
Qi Wang
2019-03-11 12:14:29 -04:00
parent 6421208e0f
commit e3d8e79d95
21 changed files with 74 additions and 100 deletions

View File

@ -1,7 +1,6 @@
package buildah
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
@ -272,36 +271,6 @@ func addRlimits(ulimit []string, g *generate.Generator) error {
return nil
}
func addHosts(hosts []string, w io.Writer) error {
buf := bufio.NewWriter(w)
for _, host := range hosts {
values := strings.SplitN(host, ":", 2)
if len(values) != 2 {
return errors.Errorf("unable to parse host entry %q: incorrect format", host)
}
if values[0] == "" {
return errors.Errorf("hostname in host entry %q is empty", host)
}
if values[1] == "" {
return errors.Errorf("IP address in host entry %q is empty", host)
}
fmt.Fprintf(buf, "%s\t%s\n", values[1], values[0])
}
return buf.Flush()
}
func addHostsToFile(hosts []string, filename string) error {
if len(hosts) == 0 {
return nil
}
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
return errors.Wrapf(err, "error creating hosts file %q", filename)
}
defer file.Close()
return addHosts(hosts, file)
}
func addCommonOptsToSpec(commonOpts *CommonBuildOptions, g *generate.Generator) error {
// Resources - CPU
if commonOpts.CPUPeriod != 0 {
@ -638,6 +607,59 @@ func (b *Builder) addNetworkConfig(rdir, hostPath string, chownOpts *idtools.IDP
return cfile, nil
}
// generateHosts creates a containers hosts file
func (b *Builder) generateHosts(rdir, hostname string, addHosts []string, chownOpts *idtools.IDPair) (string, error) {
hostPath := "/etc/hosts"
stat, err := os.Stat(hostPath)
if err != nil {
return "", errors.Wrapf(err, "error statting %q for container %q", hostPath, b.ContainerID)
}
hosts := bytes.NewBufferString("# Generated by Buildah\n")
orig, err := ioutil.ReadFile(hostPath)
if err != nil {
return "", errors.Wrapf(err, "unable to read %s", hostPath)
}
hosts.Write(orig)
for _, host := range addHosts {
// verify the host format
values := strings.SplitN(host, ":", 2)
if len(values) != 2 {
return "", errors.Errorf("unable to parse host entry %q: incorrect format", host)
}
if values[0] == "" {
return "", errors.Errorf("hostname in host entry %q is empty", host)
}
if values[1] == "" {
return "", errors.Errorf("IP address in host entry %q is empty", host)
}
hosts.Write([]byte(fmt.Sprintf("%s\t%s\n", values[1], values[0])))
}
if hostname != "" {
hosts.Write([]byte(fmt.Sprintf("127.0.0.1 %s\n", hostname)))
hosts.Write([]byte(fmt.Sprintf("::1 %s\n", hostname)))
}
cfile := filepath.Join(rdir, filepath.Base(hostPath))
if err = ioutils.AtomicWriteFile(cfile, hosts.Bytes(), stat.Mode().Perm()); err != nil {
return "", errors.Wrapf(err, "error writing /etc/hosts into the container")
}
uid := int(stat.Sys().(*syscall.Stat_t).Uid)
gid := int(stat.Sys().(*syscall.Stat_t).Gid)
if chownOpts != nil {
uid = chownOpts.UID
gid = chownOpts.GID
}
if err = os.Chown(cfile, uid, gid); err != nil {
return "", errors.Wrapf(err, "error chowning file %q for container %q", cfile, b.ContainerID)
}
if err := label.Relabel(cfile, b.MountLabel, false); err != nil {
return "", errors.Wrapf(err, "error relabeling %q in container %q", cfile, b.ContainerID)
}
return cfile, nil
}
func setupMaskedPaths(g *generate.Generator) {
for _, mp := range []string{
"/proc/acpi",
@ -1081,15 +1103,11 @@ func (b *Builder) Run(command []string, options RunOptions) error {
volumes := b.Volumes()
if !contains(volumes, "/etc/hosts") {
hostFile, err := b.addNetworkConfig(path, "/etc/hosts", rootIDPair)
hostFile, err := b.generateHosts(path, spec.Hostname, b.CommonBuildOpts.AddHost, rootIDPair)
if err != nil {
return err
}
bindFiles["/etc/hosts"] = hostFile
if err := addHostsToFile(b.CommonBuildOpts.AddHost, hostFile); err != nil {
return err
}
}
if !contains(volumes, "/etc/resolv.conf") {