Skip to content

Commit

Permalink
fix(appengine): Properly account for GCS object version in deploy (#3806
Browse files Browse the repository at this point in the history
)

The appengine deploy stage does not account for the fact that GCS
objects can have a version (such as gs://bucket/file.tar#123).
Currently sending a file such as the preceding breaks as the
provider doesn't recognize it as a tar file (and also includes 123
as part of the file name rather than as an object generation).

Fix this so that the version is parsed out of the file path and
is properly sent to the GCS API.
  • Loading branch information
ezimanyi committed Jun 20, 2019
1 parent dcd46c6 commit c778d18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import com.netflix.spinnaker.clouddriver.appengine.AppengineJobExecutor
import com.netflix.spinnaker.clouddriver.appengine.artifacts.GcsStorageService
import com.netflix.spinnaker.clouddriver.appengine.model.AppengineRepositoryClient
import com.netflix.spinnaker.clouddriver.artifacts.ArtifactUtils
import groovy.transform.CompileStatic
import groovy.transform.TupleConstructor
import groovy.util.logging.Slf4j
import org.apache.commons.io.FileUtils
import org.apache.commons.io.IOUtils

@CompileStatic
@Slf4j
@TupleConstructor
class AppengineGcsRepositoryClient implements AppengineRepositoryClient {
Expand Down Expand Up @@ -57,6 +59,16 @@ class AppengineGcsRepositoryClient implements AppengineRepositoryClient {
def slash = fullPath.indexOf("/")
def bucketName = fullPath.substring(0, slash)
def bucketPath = fullPath.substring(slash + 1)
Long version = null

def versionSeparator = bucketPath.indexOf("#")
if (versionSeparator >= 0) {
String versionString = bucketPath.substring(versionSeparator + 1)
if (!versionString.isEmpty()) {
version = Long.parseLong(versionString)
}
bucketPath = bucketPath.substring(0, versionSeparator)
}

// Start with a clean directory for each deployment.
File targetDirectory = new File(targetDirectoryPath)
Expand All @@ -67,8 +79,8 @@ class AppengineGcsRepositoryClient implements AppengineRepositoryClient {
throw new IllegalArgumentException("GAE staging directory resolved to a file: ${}, failing...")
}

if (fullPath.endsWith(".tar")) {
InputStream tas = storage.openObjectStream(bucketName, bucketPath)
if (bucketPath.endsWith(".tar")) {
InputStream tas = storage.openObjectStream(bucketName, bucketPath, version)

// NOTE: We write the tar file out to an intermediate temp file because the tar input stream
// directly from openObjectStream() closes unexpectedly when accessed from untarStreamToPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ public GcsStorageService(Storage storage) {
storage_ = storage;
}

public InputStream openObjectStream(String bucketName, String path) throws IOException {
public InputStream openObjectStream(String bucketName, String path, Long generation)
throws IOException {
Storage.Objects.Get get = storage_.objects().get(bucketName, path);
if (generation != null) {
get.setGeneration(generation);
}
return get.executeMediaAsInputStream();
}

Expand Down Expand Up @@ -143,7 +147,7 @@ public void visitObjects(String bucketName, VisitorOperation op) throws IOExcept

public void downloadStorageObjectRelative(
StorageObject obj, String ignorePrefix, String baseDirectory) throws IOException {
InputStream stream = openObjectStream(obj.getBucket(), obj.getName());
InputStream stream = openObjectStream(obj.getBucket(), obj.getName(), obj.getGeneration());
String objPath = obj.getName();
if (!ignorePrefix.isEmpty()) {
ignorePrefix += File.separator;
Expand Down

0 comments on commit c778d18

Please sign in to comment.