Unpack context.tar.gz from bucket

This commit is contained in:
Priya Wadhwa 2018-04-03 14:38:17 -07:00
parent 911a9a9e39
commit 74c4a6629d
No known key found for this signature in database
GPG Key ID: 0D0DAFD8F7AA73AE
6 changed files with 34 additions and 18 deletions

2
Gopkg.lock generated
View File

@ -620,6 +620,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "8538050f7de37808cc15a8a347740aa73ccb80f0302b9dda86c929a6d8b0db35"
inputs-digest = "51fdf15812acb4ba731695ce038965ca38f136449addbebf03a6f13cd91d4077"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -28,6 +28,7 @@ import (
"github.com/spf13/cobra"
"io/ioutil"
"os"
"path/filepath"
)
var (
@ -39,7 +40,7 @@ var (
)
func init() {
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "/workspace/Dockerfile", "Path to the dockerfile to be built.")
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "Dockerfile", "Path to the dockerfile to be built.")
RootCmd.PersistentFlags().StringVarP(&srcContext, "context", "c", "", "Path to the dockerfile build context.")
RootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.")
RootCmd.PersistentFlags().StringVarP(&destination, "destination", "d", "", "Registry the final image should be pushed to (ex: gcr.io/test/example:latest)")
@ -82,6 +83,11 @@ func resolveSourceContext() error {
}
logrus.Debugf("Unpacked tar from %s to path %s", bucket, buildContextPath)
srcContext = buildContextPath
// If path to dockerfile doesn't exist, assume it is in the unpacked tar
if !util.FilepathExists(dockerfilePath) {
logrus.Debugf("Expecting dockerfile to be located at %s within the tar build context", dockerfilePath)
dockerfilePath = filepath.Join(srcContext, dockerfilePath)
}
return nil
}

View File

@ -24,7 +24,6 @@ import (
const (
executorImage = "executor-image"
executorCommand = "/kbuild/executor"
dockerImage = "gcr.io/cloud-builders/docker"
ubuntuImage = "ubuntu"
testRepo = "gcr.io/kbuild-test/"
@ -163,11 +162,11 @@ func main() {
GCSBucketTarBuildContext := step{
Name: ubuntuImage,
Args: []string{"tar", "-C", "/workspace/integration_tests/", "-cf", "/workspace/kbuild.tar", "."},
Args: []string{"tar", "-C", "/workspace/integration_tests/", "-zcvf", "/workspace/context.tar.gz", "."},
}
uploadTarBuildContext := step{
Name: "gcr.io/cloud-builders/gsutil",
Args: []string{"cp", "/workspace/kbuild.tar", "gs://kbuild-test-bucket/"},
Args: []string{"cp", "/workspace/context.tar.gz", "gs://kbuild-test-bucket/"},
}
// Build executor image
@ -238,7 +237,7 @@ func main() {
kbuildImage := testRepo + kbuildPrefix + test.repo
kbuild := step{
Name: executorImage,
Args: []string{"--destination", kbuildImage, "--dockerfile", test.dockerfilePath},
Args: []string{"--destination", kbuildImage, "--dockerfile", test.dockerfilePath, "--context", test.kbuildContext},
}
// Pull the kbuild image
pullKbuildImage := step{

View File

@ -33,8 +33,8 @@ const (
Author = "kbuild"
// KbuildTar is the default name of the tar uploaded to GCS buckets
KbuildTar = "kbuild.tar"
// ContextTar is the default name of the tar uploaded to GCS buckets
ContextTar = "context.tar.gz"
// BuildContextDir is the directory a build context will be unpacked into,
// for example, a tarball from a GCS bucket will be unpacked here

View File

@ -18,7 +18,6 @@ package util
import (
"cloud.google.com/go/storage"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/constants"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
@ -34,12 +33,7 @@ func UnpackTarFromGCSBucket(bucketName, directory string) error {
return err
}
logrus.Debug("Unpacking source context tar...")
// Now, unpack the tar to a build context, and return the path to the build context
file, err := os.Open(tarPath)
if err != nil {
return err
}
if err := pkgutil.UnTar(file, directory, nil); err != nil {
if err := UnpackCompressedTar(tarPath, directory); err != nil {
return err
}
// Remove the tar so it doesn't interfere with subsequent commands
@ -57,15 +51,15 @@ func getTarFromBucket(bucketName, directory string) (string, error) {
}
bucket := client.Bucket(bucketName)
// Get the tarfile kbuild.tar from the GCS bucket, and save it to a tar object
reader, err := bucket.Object(constants.KbuildTar).NewReader(ctx)
reader, err := bucket.Object(constants.ContextTar).NewReader(ctx)
if err != nil {
return "", err
}
defer reader.Close()
tarPath := filepath.Join(directory, constants.KbuildTar)
tarPath := filepath.Join(directory, constants.ContextTar)
if err := CreateFile(tarPath, reader, 0600); err != nil {
return "", err
}
logrus.Debugf("Copied tarball %s from GCS bucket %s to %s", constants.KbuildTar, bucketName, tarPath)
logrus.Debugf("Copied tarball %s from GCS bucket %s to %s", constants.ContextTar, bucketName, tarPath)
return tarPath, nil
}

View File

@ -18,6 +18,8 @@ package util
import (
"archive/tar"
"compress/gzip"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/sirupsen/logrus"
"io"
"os"
@ -86,3 +88,18 @@ func checkHardlink(p string, i os.FileInfo) (bool, string) {
}
return hardlink, linkDst
}
// UnpackCompressedTar unpacks the compressed tar at path to dir
func UnpackCompressedTar(path, dir string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
gzr, err := gzip.NewReader(file)
if err != nil {
return err
}
defer gzr.Close()
return pkgutil.UnTar(gzr, dir, nil)
}