Unpack tar from GCS bucket

This commit is contained in:
Priya Wadhwa
2018-03-26 13:59:56 -07:00
parent d9c31044b9
commit 85bbb6edff
6 changed files with 186 additions and 20 deletions
+10
View File
@@ -26,7 +26,17 @@ const (
// WorkspaceDir is the path to the workspace directory
WorkspaceDir = "/workspace"
//KbuildDir is the path to the kbuild directory
KbuildDir = "/kbuild"
WhitelistPath = "/proc/self/mountinfo"
Author = "kbuild"
// KbuildTar is the default name of the tar uploaded to GCS buckets
KbuildTar = "kbuild.tar"
// BuildContextDir is the directory a build context will be unpacked into,
// for example, a tarball from a GCS bucket will be unpacked here
BuildContextDir = "/kbuild/buildcontext/"
)
+73
View File
@@ -0,0 +1,73 @@
/*
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
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"
"os"
"path/filepath"
)
// UnpackTarFromGCSBucket unpacks the kbuild.tar file in the given bucket to the given directory
func UnpackTarFromGCSBucket(bucketName, directory string) error {
// Get the tar from the bucket
tarPath, err := getTarFromBucket(bucketName, directory)
if err != nil {
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 {
return err
}
// Remove the tar so it doesn't interfere with subsequent commands
logrus.Debugf("Deleting %s", tarPath)
return os.Remove(tarPath)
}
// getTarFromBucket gets kbuild.tar from the GCS bucket and saves it to the filesystem
// It returns the path to the tar file
func getTarFromBucket(bucketName, directory string) (string, error) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return "", err
}
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)
if err != nil {
return "", err
}
defer reader.Close()
tarPath := filepath.Join(directory, constants.KbuildTar)
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)
return tarPath, nil
}