@bowtie/sls
Version:
Serverless helpers & utilities
147 lines (107 loc) • 3.4 kB
JavaScript
const Convert = require('ansi-to-html')
const { Deploy, Build } = require('../models')
const BaseController = require('./BaseController')
const convert = new Convert({
newline: true
})
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 BuildsController extends BaseController {
constructor () {
super({
model: Build,
defaultSort: 'createdAt',
defaultLimit: 2
})
}
async tags(event, context) {
const authorize = await this._authorize('tags', event, context)
if (authorize !== true) {
return authorize
}
const builds = await Build.scanAll()
const tags = []
builds.sort(compareCreated)
builds.forEach(build => {
if (build.build_tags && Array.isArray(build.build_tags)) {
build.build_tags.sort(compareLength)
build.build_tags.forEach(tag => {
if (!tags.includes(tag)) {
tags.push(tag);
}
});
}
});
return this._ok(tags);
}
async deploy(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 build = await Build.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)
}
}
async logs(event, context) {
const authorize = await this._authorize('logs', event, context)
if (authorize !== true) {
return authorize
}
if (!event.pathParameters || !event.pathParameters['id']) {
return this._bad()
}
const build = await Build.get(event.pathParameters['id'])
if (!build) {
return this._not_found()
}
const logs = await build.logs()
return this._ok({ logs })
}
// async viewLogs(event, context) {
// if (!event.pathParameters || !event.pathParameters['id']) {
// return this._bad()
// }
// const build = await Build.get(event.pathParameters['id'])
// if (!build) {
// return this._not_found()
// }
// let html = ''
// html += '<div class="logs" style="position: absolute; top: 0; left: 0; bottom: 0; right: 0; background: black; color: white; padding: 25px; overflow: scroll;">'
// html += `<h1>${build.service_name} #${build.build_number} - ${build.build_status}</h1><hr>`
// html += '<pre>'
// const logs = await build.logs()
// const lines = logs.split("\n")
// lines.forEach(line => {
// html += convert.toHtml(line) + '<br />'
// })
// html += '</pre></div>'
// return this._ok(html, {
// headers: {
// 'Content-Type': 'text/html'
// }
// })
// }
}
module.exports = BuildsController