This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dotenv = require('dotenv'); | |
const process = require('process'); | |
dotenv.config(); | |
import express from 'express'; | |
import {InlineProgramArgs, LocalWorkspace} from "@pulumi/pulumi/automation"; | |
import * as gcp from "@pulumi/gcp"; | |
let gcpAccessToken = process.env.GOOGLE_APPLICATION_CREDENTIALS; // was GCP_CREDENTIALS | |
let pulumiAccessToken = process.env.PULUMI_ACCESS_TOKEN; | |
console.log(gcpAccessToken, pulumiAccessToken); | |
if (! gcpAccessToken || ! pulumiAccessToken) { | |
throw Error('Not set'); | |
} | |
const {spawn} = require('child_process') | |
const app = express(); | |
app.get('/', async (req : any, res : any) => { | |
res.status(200).send('ok'); | |
}); | |
app.post('/success', async (req : any, res : any) => { | |
console.log('Checkpoint 1', req.body); | |
const stackName = `cf2`; | |
const projectName = 'cf-marketplace-hub-1'; | |
const serviceUniqueName = 'test3'; | |
const run = async () => { | |
const pulumiProgram = async () => { | |
const myProject = gcp.organizations.Project.get("org", projectName, { | |
billingAccount: '01F9AB-2C105C-7DB8E5', | |
orgId: '275465548596' | |
}); | |
const container = new gcp.projects.Service("container", { | |
project: myProject.id, | |
disableDependentServices: true, | |
service: "container.googleapis.com" | |
}); | |
const containerRegistry = new gcp.projects.Service("containerregistry", { | |
project: myProject.id, | |
disableDependentServices: true, | |
service: "containerregistry.googleapis.com" | |
}); | |
const serviceusage = new gcp.projects.Service("serviceusage", { | |
project: myProject.id, | |
disableDependentServices: true, | |
service: "serviceusage.googleapis.com" | |
}); | |
const cloudbuild = new gcp.projects.Service("cloudbuild", { | |
project: myProject.id, | |
disableDependentServices: true, | |
service: "cloudbuild.googleapis.com" | |
}); | |
const compute = new gcp.projects.Service("compute", { | |
project: myProject.id, | |
disableDependentServices: true, | |
service: "compute.googleapis.com" | |
}); | |
const registry = new gcp.container.Registry("my-registry", { | |
project: myProject.id, | |
location: 'US' | |
}, {dependsOn: [containerRegistry]}); | |
const enableCloudRun = new gcp.projects.Service("EnableCloudRun", { | |
service: "run.googleapis.com", | |
project: myProject.id | |
}); | |
const appService = new gcp.cloudrun.Service(serviceUniqueName, { | |
location: "us-central1", | |
project: projectName, | |
name: serviceUniqueName, | |
autogenerateRevisionName: true, | |
template: { | |
spec: { | |
containers: [ | |
{ | |
image: 'gcr.io/chainfuse/marketplace', | |
} | |
], | |
containerConcurrency: 50 | |
} | |
}, | |
traffics: [ | |
{ | |
latestRevision: true, | |
percent: 100 | |
} | |
] | |
}, {dependsOn: enableCloudRun}); | |
const noauthIAMPolicy = gcp.organizations.getIAMPolicy({ | |
bindings: [ | |
{ | |
role: "roles/run.invoker", | |
members: ["allUsers"] | |
} | |
] | |
}); | |
const noauthIamPolicy = new gcp.cloudrun.IamPolicy("noauthIamPolicy", { | |
location: appService.location, | |
project: appService.project, | |
service: appService.name, | |
policyData: noauthIAMPolicy.then(noauthIAMPolicy => noauthIAMPolicy.policyData) | |
}); | |
}; | |
const args: InlineProgramArgs = { | |
stackName, | |
projectName: projectName, | |
program: pulumiProgram | |
}; | |
const stack = await LocalWorkspace.createOrSelectStack(args); | |
console.log('stack', stack); | |
stack.setAllConfig({}); | |
// await stack.refresh({ onOutput: console.info }); | |
await stack.up({onOutput: console.info}); | |
res.status(200).send('OK'); | |
return; | |
}; | |
await run().catch((err) => console.log(err)); | |
}); | |
app.listen(8080, () => { | |
console.log(`Example app listening on port ${8080}`) | |
}); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment