beekeeper-util
Version:
CLI utility for managing the beekeeper-service
200 lines (175 loc) • 6.87 kB
text/coffeescript
_ = require 'lodash'
get = require 'lodash/fp/get'
replace = require 'lodash/fp/replace'
isEqual = require 'lodash/fp/isEqual'
url = require 'url'
colors = require 'colors'
moment = require 'moment'
path = require 'path'
request = require 'request'
cliClear = require 'cli-clear'
notifier = require 'node-notifier'
BeekeeperService = require './beekeeper-service'
getVersion = replace('v', '')
class StatusService
constructor: ({ options, }) ->
{
serviceUrl
} = options
= serviceUrl
= new BeekeeperService { }
getServiceUrl: (serviceUrl) =>
return unless serviceUrl?
unless _.startsWith serviceUrl, 'http'
serviceUrl = "https://#{serviceUrl}"
urlParts = url.parse serviceUrl, true
urlParts.pathname = '/version'
urlParts.protocol ?= 'https'
urlParts.slashes = true
return url.format urlParts
run: =>
()
.getTag { , , , }, (error, deployment, latest) =>
return error if error?
return deployment if
"#{@owner}/#{@repo}:#{@tag}" unless deployment?
return () unless deployment?
{ latest, deployment }
if deployment.ci_passing? and not deployment.ci_passing
return deployment, 'CI failed'
if deployment.ci_passing and deployment.docker_url?
return deployment, 'CI passed but no docker build'
deployment
clear: =>
return unless
cliClear()
console.log '[refreshed at] ', colors.cyan moment().toString()
end: ({ exitCode, pending, passing, notFound }) =>
return _.delay , 3000 if ? && passing
return _.delay , 10000 if and pending
{ passing, notFound }
process.exit exitCode if pending
doNotify: ({ passing, notFound }) =>
return unless
message = 'Service Deployed!'
sound = 'Purr'
unless passing
message = 'Deployment Failed!'
sound = 'Basso'
if notFound
message = 'Deployment Not Found!'
sound = 'Funk'
notifier.notify {
title: 'Beekeeper'
subtitle: "#{@repo}:#{@tag}"
message: message
icon: path.join(__dirname, '..', 'assets', 'beekeeper.png')
sound: sound
open:
timeout: 10
}
waitForVersion: (successCount=0) =>
()
(error, matches, currentVersion) =>
return error if error?
successCount++ if matches
{ currentVersion }
return _.delay , 3000, successCount if successCount <= 3
{ passing: true }
process.exit 0
checkVersion: (callback) =>
request.get , { json: true }, (error, response, body) =>
return callback error if error?
currentVersion = getVersion(get('version', body))
matches = isEqual(getVersion(), currentVersion)
callback null, matches, currentVersion
printJSON: (deployment) =>
console.log JSON.stringify deployment, null, 2
{ exitCode: 0 }
printHeader: (slug) =>
console.log ''
console.log "#{colors.cyan('Deployment:')}", slug
printDeployHeader: ({ latest, deployment }) =>
return unless deployment?
latestSlug = "#{latest.owner_name}/#{latest.repo_name}:#{latest.tag}" if latest?
deploymentSlug = "#{deployment.owner_name}/#{deployment.repo_name}:#{deployment.tag}"
console.log ''
colorTitle = (str='') =>
return colors.cyan(str)
colorValue = (str='') =>
return colors.white(str)
colorList = (arr=[]) =>
return colors.italic(_.join(arr, ', '))
if ?
console.log colors.gray('Service: '), colors.gray()
unless latest?
console.log colorTitle('There is no latest version, maybe it has never been deployed?')
console.log colorTitle('Desired'), colorValue(deploymentSlug)
else if deployment.tag == latest.tag
console.log colorTitle('Running'), colorValue(deploymentSlug)
else
console.log colorTitle('Running'), colorValue(latestSlug)
console.log colorTitle('Desired'), colorValue(deploymentSlug)
unless _.isEmpty deployment.tags
console.log colorTitle('Tag '), colorList(deployment.tags)
printPassing: ({ created_at, docker_url }) =>
console.log ''
console.log "#{colors.bold('Build')} ", colors.green('Passing')
console.log "#{colors.bold('Docker')} ", colors.underline(docker_url)
console.log "#{colors.bold('Created')} ", (created_at)
console.log ''
{ exitCode: 0, passing: true }
printVersionResult: ({ currentVersion }) =>
console.log ''
message = "Service is running v#{currentVersion}!"
if getVersion(currentVersion) == getVersion()
console.log colors.green(message)
else
console.log colors.white(message)
console.log colors.yellow("Waiting for service to run #{@tag}...")
console.log ''
printPending: ({ ci_passing, docker_url, created_at, updated_at }) =>
waitlist = []
waitlist.push '"CI Build"' unless ci_passing?
waitlist.push '"Docker URL"' unless docker_url?
console.log ''
console.log "#{colors.bold('Build')} ", colors.yellow('Pending...')
console.log "#{colors.bold('Waiting on')}", colors.magenta(waitlist.join(', '))
console.log "#{colors.bold('Created')} ", (created_at)
updated_at
console.log ''
{ exitCode: 0, pending: true }
printFailed: ({ created_at, updated_at }, reason) =>
console.log ''
console.log "#{colors.bold('Build')} ", colors.red('Failed')
console.log "#{colors.bold('Created')} ", (created_at)
console.log "#{colors.bold('Reason')} ", "'#{reason}'"
console.log ''
updated_at
console.log ''
{ exitCode: 1, passing: false }
printNotFound: =>
console.log ''
console.log "#{colors.bold('Build')} ", colors.yellow.underline('Not found!')
console.log ''
{ exitCode: 1, notFound: true, passing: false }
printComponents: (components) =>
return console.log("#{colors.bold('No completed components')}") if _.isEmpty components
console.log "#{colors.bold('Completed Components')} "
_.each components, (date, key) =>
console.log " #{colors.cyan(key)} ", (date)
prettyDate: (date) =>
return "@ #{colors.gray(moment(date).fromNow())}"
die: (error) =>
return process.exit(0) unless error?
console.error error.stack
process.exit 1
module.exports = StatusService