Skip to content

Commit

Permalink
Create stack if it doesn't exist in node driver
Browse files Browse the repository at this point in the history
  • Loading branch information
komalali committed Nov 3, 2022
1 parent 94e6ab2 commit 3a85dae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
34 changes: 20 additions & 14 deletions deployment-drivers/nodejs/typescript-driver/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let org = "pulumi";
let stack = "dev"
const backendURL = "http://api.pulumi.com/api"
let org: string;
let stack: string;
const backendURL = "https://api.pulumi.com/api"

type SupportedProject = "simple-resource" | "bucket-time" | "go-bucket" | "lambda-template" | "yamlcaml";
type Operation = "update" | "preview" | "destroy" | "refresh";
Expand Down Expand Up @@ -33,7 +33,17 @@ const makePulumiAPICall = async (method: string, urlSuffix: string, payload?: an
const createDeployment = async (project: string, payload: any) => {
const urlSuffix = `preview/${org}/${project}/${stack}/deployments`;

return await makePulumiAPICall('POST', urlSuffix, payload);
try {
return await makePulumiAPICall('POST', urlSuffix, payload);
} catch (e: any) {
// Create the stack if it doesn't exist
if (e.message.includes("404")) {
const createStackPayload = { "stackName": stack };
await makePulumiAPICall('POST', `stacks/${org}/${project}`, createStackPayload);
return await makePulumiAPICall('POST', urlSuffix, payload);
}
throw e;
}
}

const createSimpleDeployment = async (op: Operation) => {
Expand All @@ -42,10 +52,7 @@ const createSimpleDeployment = async (op: Operation) => {
git: {
repoURL: "https://github.com/pulumi/deploy-demos.git",
branch: "refs/heads/main",
repoDir: "pulumi-programs/simple-resource",
gitAuth: {
accessToken: process.env.GITHUB_ACCESS_TOKEN,
}
repoDir: "pulumi-programs/simple-resource"
}
},
operationContext: {
Expand Down Expand Up @@ -237,7 +244,7 @@ const getDeploymentLogs = async (deployment: DeploymentAction) => {
}

const query = `job=${currentJob}&step=${currentStep}&offset=${nextOffset}`;
const logsResponse = await makePulumiAPICall("GET", `preview/${org}/${deployment.project}/${stack}/deployments/${deployment.id}/logs?${query}`);
const logsResponse: any = await makePulumiAPICall("GET", `preview/${org}/${deployment.project}/${stack}/deployments/${deployment.id}/logs?${query}`);
const logLines = (logsResponse.lines || []).map((l:any) => `${l.timestamp}: ${l.line}`);
logs.push(...logLines);
if (logsResponse.nextOffset !== undefined) {
Expand Down Expand Up @@ -303,8 +310,8 @@ const queryDeployment = async (deployment: DeploymentAction) => {
};
}

const deploymentStatusResult = await getDeploymentStatus(deployment);
deployment.status = deploymentStatusResult.status;
const deploymentStatusResult: any = await getDeploymentStatus(deployment);
deployment.status = deploymentStatusResult.status;

// we only have enough state about the deployment to query for logs once it reaches "running" state
// https://github.com/pulumi/pulumi-service/issues/10266
Expand All @@ -325,7 +332,6 @@ const queryDeployment = async (deployment: DeploymentAction) => {
}

const monitorDeployments = async (deployments: DeploymentAction[]) => {

let deploymentIDs = deployments.map(d => d.id!);

while(deploymentIDs.length) {
Expand All @@ -338,7 +344,7 @@ const monitorDeployments = async (deployments: DeploymentAction[]) => {
}
// filter out completed deployments for the next pass
deploymentIDs = deploymentIDs.filter(x => completedDeployments.indexOf(x) === -1);
console.log(`Finished polling deployments: ${completedDeployments.length} out of ${deploymentIDs.length} complete.`);
console.log(`Finished polling deployments. Completed: ${completedDeployments.length}. Remaining: ${deploymentIDs.length}.`);
await delay(2000);
}
};
Expand All @@ -347,7 +353,7 @@ const execDeployments = async (deployments: DeploymentAction[]) => {
let deploymentNumber = 1;
for(let deployment of deployments) {
console.log(`executing deployment ${deploymentNumber}`)
const deploymentResult = await createProjectDeployment(deployment.project, deployment.op);
const deploymentResult: any = await createProjectDeployment(deployment.project, deployment.op);
console.log(deploymentResult);
deployment.id = deploymentResult.id;
deploymentNumber++;
Expand Down
3 changes: 3 additions & 0 deletions deployment-drivers/nodejs/typescript-driver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"dependencies": {
"@types/node": "^18.7.13"
},
"devDependencies": {
"typescript": "^4.8.4"
}
}

0 comments on commit 3a85dae

Please sign in to comment.