@bowtie/sls
Version:
Serverless helpers & utilities
83 lines (61 loc) • 1.88 kB
JavaScript
const { Deploy, Build } = require('../models')
const BaseController = require('./BaseController')
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max)) + 1
}
const compareLength = (a, b) => {
if (a.length < b.length) return 1
if (b.length < a.length) return -1
return 0
}
const compareCreated = (a, b) => {
if (a['createdAt'] < b['createdAt']) return 1
if (b['createdAt'] < a['createdAt']) return -1
return 0
}
class DeploysController extends BaseController {
constructor () {
super({
model: Deploy,
defaultSort: 'createdAt'
})
}
async stacks(event, context) {
const authorize = await this._authorize('stacks', event, context)
if (authorize !== true) {
return authorize
}
const deploys = await Deploy.scanAll()
const stacks = [];
deploys.sort(compareCreated)
deploys.forEach(deploy => {
if (!stacks.includes(deploy.stack)) {
stacks.push(deploy.stack);
}
});
return this._ok(stacks);
}
async approve(event, context) {
if (!event.pathParameters || !event.pathParameters['id'] || !event.pathParameters['stack']) {
return this._bad()
}
const { stack } = event.pathParameters
const action = 'deploy'
const envAlias = event.helpers.getEnvAlias(stack)
const authorize = (await this._authorize(envAlias, event, context)) || (await this._authorize(action, event, context))
if (authorize !== true) {
return authorize
}
const deploy = await Deploy.get(event.pathParameters['id'])
if (!build) {
return this._not_found()
}
try {
const deploy = await build.deploy(Object.assign({}, event.pathParameters, event.queryStringParameters || {}))
return this._created(deploy)
} catch(err) {
return this._bad(err)
}
}
}
module.exports = DeploysController