Vendor in latest containers/buildah

Switch from projectatomic/buildah to containers/buildah

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2018-09-18 15:31:54 -04:00
parent c3a0874222
commit 92b28a88d8
60 changed files with 882 additions and 382 deletions

View File

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,145 @@
package imagebuildah
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/containers/storage/pkg/reexec"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
const (
symlinkChrootedCommand = "chrootsymlinks-resolve"
maxSymlinksResolved = 40
)
func init() {
reexec.Register(symlinkChrootedCommand, resolveChrootedSymlinks)
}
func resolveChrootedSymlinks() {
status := 0
flag.Parse()
if len(flag.Args()) < 1 {
os.Exit(1)
}
// Our first parameter is the directory to chroot into.
if err := unix.Chdir(flag.Arg(0)); err != nil {
fmt.Fprintf(os.Stderr, "chdir(): %v\n", err)
os.Exit(1)
}
if err := unix.Chroot(flag.Arg(0)); err != nil {
fmt.Fprintf(os.Stderr, "chroot(): %v\n", err)
os.Exit(1)
}
// Our second parameter is the path name to evaluate for symbolic links
symLink, err := getSymbolicLink(flag.Arg(0), flag.Arg(1))
if err != nil {
fmt.Fprintf(os.Stderr, "error getting symbolic links: %v\n", err)
os.Exit(1)
}
if _, err := os.Stdout.WriteString(symLink); err != nil {
fmt.Fprintf(os.Stderr, "error writing string to stdout: %v\n", err)
os.Exit(1)
}
os.Exit(status)
}
func resolveSymLink(rootdir, filename string) (string, error) {
// The child process expects a chroot and one path that
// will be consulted relative to the chroot directory and evaluated
// for any symbolic links present.
cmd := reexec.Command(symlinkChrootedCommand, rootdir, filename)
output, err := cmd.CombinedOutput()
if err != nil {
return "", errors.Wrapf(err, string(output))
}
// Hand back the resolved symlink, will be "" if a symlink is not found
return string(output), nil
}
// getSymbolic link goes through each part of the path and continues resolving symlinks as they appear.
// Returns what the whole target path for what "path" resolves to.
func getSymbolicLink(rootdir, path string) (string, error) {
var (
symPath string
symLinksResolved int
)
// Splitting path as we need to resolve each parth of the path at a time
splitPath := strings.Split(path, "/")
if splitPath[0] == "" {
splitPath = splitPath[1:]
symPath = "/"
}
for _, p := range splitPath {
// If we have resolved 40 symlinks, that means something is terribly wrong
// will return an error and exit
if symLinksResolved >= maxSymlinksResolved {
return "", errors.Errorf("have resolved %q symlinks, something is terribly wrong!", maxSymlinksResolved)
}
symPath = filepath.Join(symPath, p)
isSymlink, resolvedPath, err := hasSymlink(symPath)
if err != nil {
return "", errors.Wrapf(err, "error checking symlink for %q", symPath)
}
// if isSymlink is true, check if resolvedPath is potentially another symlink
// keep doing this till resolvedPath is not a symlink and isSymlink is false
for isSymlink == true {
// Need to keep track of number of symlinks resolved
// Will also return an error if the symlink points to itself as that will exceed maxSymlinksResolved
if symLinksResolved >= maxSymlinksResolved {
return "", errors.Errorf("have resolved %q symlinks, something is terribly wrong!", maxSymlinksResolved)
}
isSymlink, resolvedPath, err = hasSymlink(resolvedPath)
if err != nil {
return "", errors.Wrapf(err, "error checking symlink for %q", resolvedPath)
}
symLinksResolved++
}
// Assign resolvedPath to symPath. The next part of the loop will append the next part of the original path
// and continue resolving
symPath = resolvedPath
symLinksResolved++
}
return symPath, nil
}
// hasSymlink returns true and the target if path is symlink
// otherwise it returns false and path
func hasSymlink(path string) (bool, string, error) {
info, err := os.Lstat(path)
if os.IsNotExist(err) {
if err = os.MkdirAll(path, 0755); err != nil {
return false, "", errors.Wrapf(err, "error ensuring volume path %q exists", path)
}
info, err = os.Lstat(path)
if err != nil {
return false, "", errors.Wrapf(err, "error running lstat on %q", path)
}
}
// Return false and path as path is not a symlink
if info.Mode()&os.ModeSymlink != os.ModeSymlink {
return false, path, nil
}
// Read the symlink to get what it points to
targetDir, err := os.Readlink(path)
if err != nil {
return false, "", errors.Wrapf(err, "error reading link %q", path)
}
// if the symlink points to a relative path, prepend the path till now to the resolved path
if !filepath.IsAbs(targetDir) {
targetDir = filepath.Join(path, targetDir)
}
// run filepath.Clean to remove the ".." from relative paths
return true, filepath.Clean(targetDir), nil
}

View File

@@ -0,0 +1,113 @@
package imagebuildah
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/containers/buildah"
"github.com/containers/storage/pkg/chrootarchive"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func cloneToDirectory(url, dir string) error {
if !strings.HasPrefix(url, "git://") {
url = "git://" + url
}
logrus.Debugf("cloning %q to %q", url, dir)
cmd := exec.Command("git", "clone", url, dir)
return cmd.Run()
}
func downloadToDirectory(url, dir string) error {
logrus.Debugf("extracting %q to %q", url, dir)
resp, err := http.Get(url)
if err != nil {
return errors.Wrapf(err, "error getting %q", url)
}
defer resp.Body.Close()
if resp.ContentLength == 0 {
return errors.Errorf("no contents in %q", url)
}
if err := chrootarchive.Untar(resp.Body, dir, nil); err != nil {
resp1, err := http.Get(url)
if err != nil {
return errors.Wrapf(err, "error getting %q", url)
}
defer resp1.Body.Close()
body, err := ioutil.ReadAll(resp1.Body)
if err != nil {
return errors.Wrapf(err, "Failed to read %q", url)
}
dockerfile := filepath.Join(dir, "Dockerfile")
// Assume this is a Dockerfile
if err := ioutil.WriteFile(dockerfile, body, 0600); err != nil {
return errors.Wrapf(err, "Failed to write %q to %q", url, dockerfile)
}
}
return nil
}
// TempDirForURL checks if the passed-in string looks like a URL. If it is,
// TempDirForURL creates a temporary directory, arranges for its contents to be
// the contents of that URL, and returns the temporary directory's path, along
// with the name of a subdirectory which should be used as the build context
// (which may be empty or "."). Removal of the temporary directory is the
// responsibility of the caller. If the string doesn't look like a URL,
// TempDirForURL returns empty strings and a nil error code.
func TempDirForURL(dir, prefix, url string) (name string, subdir string, err error) {
if !strings.HasPrefix(url, "http://") &&
!strings.HasPrefix(url, "https://") &&
!strings.HasPrefix(url, "git://") &&
!strings.HasPrefix(url, "github.com/") {
return "", "", nil
}
name, err = ioutil.TempDir(dir, prefix)
if err != nil {
return "", "", errors.Wrapf(err, "error creating temporary directory for %q", url)
}
if strings.HasPrefix(url, "git://") {
err = cloneToDirectory(url, name)
if err != nil {
if err2 := os.Remove(name); err2 != nil {
logrus.Debugf("error removing temporary directory %q: %v", name, err2)
}
return "", "", err
}
return name, "", nil
}
if strings.HasPrefix(url, "github.com/") {
ghurl := url
url = fmt.Sprintf("https://%s/archive/master.tar.gz", ghurl)
logrus.Debugf("resolving url %q to %q", ghurl, url)
subdir = path.Base(ghurl) + "-master"
}
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
err = downloadToDirectory(url, name)
if err != nil {
if err2 := os.Remove(name); err2 != nil {
logrus.Debugf("error removing temporary directory %q: %v", name, err2)
}
return "", subdir, err
}
return name, subdir, nil
}
logrus.Debugf("don't know how to retrieve %q", url)
if err2 := os.Remove(name); err2 != nil {
logrus.Debugf("error removing temporary directory %q: %v", name, err2)
}
return "", "", errors.Errorf("unreachable code reached")
}
// InitReexec is a wrapper for buildah.InitReexec(). It should be called at
// the start of main(), and if it returns true, main() should return
// immediately.
func InitReexec() bool {
return buildah.InitReexec()
}