Skip to content

Commit

Permalink
fix(bake/manifests): delete templates when request finishes (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander committed Apr 13, 2018
1 parent 2896d13 commit 49a3fda
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
2 changes: 2 additions & 0 deletions rosco-manifests/rosco-manifests.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dependencies {
compileOnly spinnaker.dependency("lombok")

compile group: 'commons-io', name: 'commons-io', version: '2.4'

compile project(":rosco-core")
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public class BakeManifestRequest {
List<Artifact> values;
Map<String, Object> overrides;

public BakeRecipe convertToRecipe(TemplateUtils utils) {
return utils.buildBakeRecipe(this);
}

enum TemplateRenderer {
HELM2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.netflix.spinnaker.rosco.jobs.BakeRecipe;
import com.netflix.spinnaker.rosco.jobs.JobExecutor;
import com.netflix.spinnaker.rosco.jobs.JobRequest;
import com.netflix.spinnaker.rosco.manifests.TemplateUtils.BakeManifestEnvironment;
import com.netflix.spinnaker.rosco.manifests.helm.HelmTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -35,24 +36,30 @@ TemplateUtils templateUtils(BakeManifestRequest request) {

public Artifact bake(BakeManifestRequest request) {
TemplateUtils utils = templateUtils(request);
BakeRecipe recipe = utils.buildBakeRecipe(request);
BakeManifestEnvironment env = new BakeManifestEnvironment();
BakeRecipe recipe = utils.buildBakeRecipe(env, request);
BakeStatus bakeStatus;

JobRequest jobRequest = new JobRequest(recipe.getCommand(), new ArrayList<>(), UUID.randomUUID().toString());
String jobId = jobExecutor.startJob(jobRequest);
try {
JobRequest jobRequest = new JobRequest(recipe.getCommand(), new ArrayList<>(), UUID.randomUUID().toString());
String jobId = jobExecutor.startJob(jobRequest);

BakeStatus bakeStatus = jobExecutor.updateJob(jobId);
bakeStatus = jobExecutor.updateJob(jobId);

while (bakeStatus == null || bakeStatus.getState() == BakeStatus.State.RUNNING) {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
while (bakeStatus == null || bakeStatus.getState() == BakeStatus.State.RUNNING) {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}

bakeStatus = jobExecutor.updateJob(jobId);
}
bakeStatus = jobExecutor.updateJob(jobId);
}

if (bakeStatus.getResult() != BakeStatus.Result.SUCCESS) {
throw new IllegalStateException("Bake of " + request + " failed: " + bakeStatus.getLogsContent());
if (bakeStatus.getResult() != BakeStatus.Result.SUCCESS) {
throw new IllegalStateException("Bake of " + request + " failed: " + bakeStatus.getLogsContent());
}
} finally {
env.cleanup();
}

return Artifact.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.netflix.spinnaker.kork.core.RetrySupport;
import com.netflix.spinnaker.rosco.jobs.BakeRecipe;
import com.netflix.spinnaker.rosco.services.ClouddriverService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import retrofit.client.Response;
Expand All @@ -15,13 +18,15 @@
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@Component
@Slf4j
public abstract class TemplateUtils {
@Autowired
ClouddriverService clouddriverService;

public BakeRecipe buildBakeRecipe(BakeManifestRequest request) {
public BakeRecipe buildBakeRecipe(BakeManifestEnvironment env, BakeManifestRequest request) {
BakeRecipe result = new BakeRecipe();
result.setName(request.getOutputName());
return result;
Expand All @@ -34,8 +39,8 @@ private String nameFromReference(String reference) {
.replace(":", "_");
}

public Path downloadArtifactToTmpFile(Artifact artifact) throws IOException {
Path path = Paths.get(System.getProperty("java.io.tmpdir"), nameFromReference(artifact.getReference()));
protected Path downloadArtifactToTmpFile(BakeManifestEnvironment env, Artifact artifact) throws IOException {
Path path = Paths.get(env.getStagingPath().toString(), nameFromReference(artifact.getReference()));
OutputStream outputStream = new FileOutputStream(path.toString());

Response response = retrySupport.retry(() -> clouddriverService.fetchArtifact(artifact), 5, 1000, true);
Expand All @@ -47,4 +52,24 @@ public Path downloadArtifactToTmpFile(Artifact artifact) throws IOException {

return path;
}

public static class BakeManifestEnvironment {
@Getter
final private Path stagingPath = Paths.get(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

public BakeManifestEnvironment() {
boolean success = stagingPath.toFile().mkdirs();
if (!success) {
log.warn("Failed to make directory " + stagingPath + "...");
}
}

public void cleanup() {
try {
FileUtils.deleteDirectory(stagingPath.toFile());
} catch (IOException e) {
throw new RuntimeException("Failed to cleanup bake manifest environment: " + e.getMessage(), e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
@Component
public class HelmTemplateUtils extends TemplateUtils {
@Override
public BakeRecipe buildBakeRecipe(BakeManifestRequest request) {
BakeRecipe result = super.buildBakeRecipe(request);
public BakeRecipe buildBakeRecipe(BakeManifestEnvironment env, BakeManifestRequest request) {
BakeRecipe result = super.buildBakeRecipe(env, request);
Path templatePath;
try {
templatePath = downloadArtifactToTmpFile(request.getInputArtifact());
templatePath = downloadArtifactToTmpFile(env, request.getInputArtifact());
} catch (IOException e) {
throw new IllegalStateException("Failed to fetch helm template: " + e.getMessage(), e);
}
Expand Down

0 comments on commit 49a3fda

Please sign in to comment.