1 | _ = require 'lodash'
|
2 | url = require 'url'
|
3 | async = require 'async'
|
4 | request = require 'request'
|
5 | moment = require 'moment'
|
6 | debug = require('debug')('deploy-state-util:governator-service')
|
7 |
|
8 | class GovernatorService
|
9 | constructor: ({ config }) ->
|
10 | @governators = config['governators']
|
11 |
|
12 | getStatuses: ({ repo, owner, tag }, callback) =>
|
13 | _getStatus = async.apply(@getStatus, { repo, owner, tag })
|
14 | async.mapValues @governators, _getStatus, callback
|
15 |
|
16 | getStatus: ({ repo, owner, tag }, { hostname, username, password }, cluster, callback) =>
|
17 | baseUrl = url.format {
|
18 | hostname: hostname
|
19 | protocol: 'https'
|
20 | slashes: true
|
21 | }
|
22 | options = {
|
23 | uri: '/status',
|
24 | baseUrl,
|
25 | json: true,
|
26 | auth: {
|
27 | username,
|
28 | password,
|
29 | },
|
30 | }
|
31 | debug 'get governator', options
|
32 | request.get options, (error, response, body) =>
|
33 | debug 'got governator', { baseUrl, body, error, statusCode: response?.statusCode }
|
34 | return callback error if error?
|
35 |
|
36 | deploys = _.pickBy body, (value, key) =>
|
37 | startsWith = _.startsWith key, "governator:/#{owner}/#{repo}"
|
38 | endsWith = true
|
39 | endsWith = _.endsWith key, ":#{tag}" if tag?
|
40 | return startsWith and endsWith
|
41 |
|
42 | debug 'deploys', deploys
|
43 | callback null, @formatAll deploys
|
44 |
|
45 | formatAll: (deployments) =>
|
46 | deployments = _.map deployments, @format
|
47 | deployments = _.sortBy deployments, 'deployAtFull'
|
48 | return deployments
|
49 |
|
50 | format: (deployment) =>
|
51 | deployment = _.cloneDeep deployment
|
52 | deployment.deployAtSince = moment.unix(deployment.deployAt).fromNow()
|
53 | [govKey, etcdKey, dockerUrl, tag ] = deployment.key.split(':')
|
54 | slug = dockerUrl.replace 'quay.io/', ''
|
55 | deployment.deploymentKey = "#{slug}:#{tag}"
|
56 | return deployment
|
57 |
|
58 | module.exports = GovernatorService
|