most-lighter-95902
01/02/2022, 6:31 PMimport * as express from 'express'
import { LocalWorkspace } from '@pulumi/pulumi/automation'
import clusterApi from './cluster'
const router = express.Router()
const ensurePlugins = async () => {
const ws = await LocalWorkspace.create({})
await ws.installPlugin('aws', 'v4.15.0')
await ws.installPlugin('awsx', 'v0.31.0')
await ws.installPlugin('eks', 'v0.32.0')
await ws.installPlugin('kubernetes', 'v3.12.1')
await ws.installPlugin('kubernetes-cert-manager', 'v0.0.1')
}
// install necessary plugins once upon boot
ensurePlugins()
// API routes
router.use('/cluster', clusterApi)
export default router
thousands-hairdresser-72380
01/03/2022, 3:31 PMensurePlugins
function.
a. There will be a period of time where the plugins are installing and you won't be notified when it's done.
2. You're not returning and using the workspace reference.
a. At the end of ensurePlugins
add return ws
It looks like you've correctly created the workspace, but you will probably need a reference that after creation.
Something like:
const workspace = await ensurePlugins() // or rename it to something like `createWorkspace` or `loadWorkspace`
Another option:
Use the static method createOrSelectStack
from LocalWorkspace
, then call installPlugin
on the stack.workspace
reference.
See example here: https://github.com/pulumi/automation-api-examples/blob/80afd678f6adc51eb9f0279ca62408b7f1e4623c/nodejs/inlineProgram-ts/index.ts#L73most-lighter-95902
01/03/2022, 4:44 PMws
reference works - in the Automation via REST API example code, it shows:const ws = await LocalWorkspace.create({ projectSettings: { name: projectName, runtime: "nodejs" } });
const stacks = await ws.listStacks();
GET /cluster
for exampleconst stack = await LocalWorkspace.createStack({
stackName,
projectName,
// generate our pulumi program on the fly from the POST body
program: createPulumiProgram(content),
})
POST /cluster
ensurePlugins
only once somewhere in my server and I don’t need to return and use ws
in my endpoints?thousands-hairdresser-72380
01/03/2022, 5:17 PMLocalWorkspace
is a execution context containing a single Pulumi project, a program, and multiple stacks
So it really depends on what you're spinning up.
Is it a custom project each time (for example created from an inline program
) or is it a static project spun up for many users?
If it's custom, you'll want a new workspace for each project.
If it's just spinning up the same project for many users, you can share a workspace and split up the ownership by stack.
Also, I'm no pro, I'm still learning as well.
Anyone else here, please feel free to jump in and correct me.most-lighter-95902
01/03/2022, 5:48 PMname
field i the projectSettings
argument? Either way, the pulumiOverHttps-ts tutorial seems to error out for me when I include the ensurePlugins
functionthousands-hairdresser-72380
01/03/2022, 7:30 PM// types.ts
export interface System {
workspace: LocalWorkspace
}
// workspace.ts
export interface Plugin {
name: string
version: string
}
export async function createWorkspace(plugins: Plugin[]) {
const workspace = await LocalWorkspace.create({})
// or if it's always the same plugins, just reference them directly here like in your first example
for(const { name, version } of plugins) {
await workspace.installPlugin(name, version)
}
return workspace
}
// router.ts
import express from 'express'
import asyncHandler from 'express-async-handler'
export default function createRouter({ workspace }: System) {
const app = express.Router()
app.get('/stacks', asyncHandler((req, res) => {
const stacks = await workspace.listStacks()
res.json(stacks)
}))
return app
}
// app.ts
import express from 'express'
import createRouter from './router'
async function main() {
const defaultPlugins = [...]
export default workspace = await createWorkspace(defaultPlugins)
const app = express()
app.use(createRouter({ workspace }))
// app.listen, etc.
}
most-lighter-95902
01/04/2022, 6:07 PMthousands-hairdresser-72380
01/04/2022, 6:07 PMmost-lighter-95902
01/04/2022, 6:11 PMthousands-hairdresser-72380
01/18/2022, 2:39 PMmost-lighter-95902
01/18/2022, 5:12 PM