1 | _ = require 'lodash'
|
2 | colors = require 'colors'
|
3 | request = require 'request'
|
4 | commander = require 'commander'
|
5 | debug = require('debug')('deployinator:deploy')
|
6 |
|
7 | class DeployinatorDeploy
|
8 | parseOptions: =>
|
9 | commander
|
10 | .usage '[options] <project name> <tag>'
|
11 | .option '-u, --user <user>', '(optional) Docker image user [octoblu]'
|
12 | .parse process.argv
|
13 |
|
14 | @project_name = _.first commander.args
|
15 | @tag = commander.args[1]
|
16 | @user = commander.user ? 'octoblu'
|
17 | @USERNAME = process.env.DEPLOYINATOR_UUID
|
18 | @PASSWORD = process.env.DEPLOYINATOR_TOKEN
|
19 | @DOCKER_PASS = process.env.DEPLOYINATOR_DOCKER_PASS
|
20 | @HOST = process.env.DEPLOYINATOR_HOST
|
21 |
|
22 | run: =>
|
23 | @parseOptions()
|
24 |
|
25 | return @die new Error('Missing DEPLOYINATOR_UUID in environment') unless @USERNAME?
|
26 | return @die new Error('Missing DEPLOYINATOR_TOKEN in environment') unless @PASSWORD?
|
27 | return @die new Error('Missing DEPLOYINATOR_DOCKER_PASS in environment') unless @DOCKER_PASS?
|
28 | return @die new Error('Missing DEPLOYINATOR_HOST in environment') unless @HOST?
|
29 | return commander.outputHelp() unless @project_name? && @tag?
|
30 |
|
31 | @deploy()
|
32 |
|
33 | deploy: =>
|
34 | console.log ""
|
35 | console.log "=>", @project_name
|
36 | console.log ""
|
37 |
|
38 | requestOptions =
|
39 | json:
|
40 | repository: "#{@user}/#{@project_name}"
|
41 | docker_url: "quay.io/#{@user}/#{@project_name}"
|
42 | updated_tags:
|
43 | "#{@tag}": "#{@DOCKER_PASS}"
|
44 | method: 'POST'
|
45 | uri: "https://#{@HOST}/deploy"
|
46 | auth:
|
47 | user: @USERNAME
|
48 | password: @PASSWORD
|
49 | debug 'requestOptions', requestOptions
|
50 | request requestOptions, (error, response, body) =>
|
51 | return @die error if error?
|
52 | return @die new Error("Deploy failed") if response.statusCode >= 400
|
53 | debug 'response', body
|
54 | console.log 'Deployed', colors.yellow @tag
|
55 | console.log ""
|
56 |
|
57 | die: (error) =>
|
58 | if 'Error' == typeof error
|
59 | console.error colors.red error.message
|
60 | else
|
61 | console.error colors.red arguments...
|
62 | process.exit 1
|
63 |
|
64 | new DeployinatorDeploy().run()
|