-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (85 loc) · 2.76 KB
/
index.js
File metadata and controls
92 lines (85 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const request = require('request')
function getAuthorizationHeader() {
if (!process.env.RANCHER_ACCESS_KEY || !process.env.RANCHER_SECRET_KEY) {
throw new Error('Rancher credentials not available in environment')
}
const credentials = `${ process.env.RANCHER_ACCESS_KEY }:${ process.env.RANCHER_SECRET_KEY }`
const encoded = new Buffer(credentials).toString('base64')
return `Basic ${ encoded }`
}
function doREST(options) {
return new Promise((resolve, reject) => {
if (typeof options === 'string') options = { url: options };
const params = Object.assign({}, {
method: 'GET',
headers: {
authorization: getAuthorizationHeader()
}
}, options)
console.log('about to request', params)
request(params, function(err, response, body) {
if (err) return reject(err);
if (response.statusCode >= 300) {
throw new Error(`get, unexpected statusCode ${ response.statusCode }, body=${ body }`)
}
const parsed = JSON.parse(body)
console.log('parsed response', parsed)
return resolve(parsed)
})
})
}
function get(url) {
return doREST(url)
}
function post(options) {
if (typeof options === 'string') options = { url: options };
options = Object.assign({}, options, { method: 'POST' })
return doREST(options)
}
function waitUntilUpgradeAvailable(serviceUrl) {
return get(serviceUrl)
.then(status => {
if (status.actions.upgrade) {
return status
} else {
return new Promise((resolve) => {
setTimeout(() => resolve(waitUntilUpgradeAvailable(serviceUrl)), 2000)
})
}
})
}
// The optional "startFist" parameter is described here:
// https://rancher.com/docs/rancher/v1.6/en/cattle/upgrading/#in-service-upgrade
// The default value - if not provided - is true.
// You'll only want to set it to false if you are reusing the service IP address in Rancher.
function upgrade(serviceUrl, startFirst) {
return get(serviceUrl)
.then(status => { // finish previous upgrade, if necessary
console.log('launchConfig', status.launchConfig)
if (status.actions.finishupgrade) {
return post({ url: status.actions.finishupgrade })
.then(r => waitUntilUpgradeAvailable(serviceUrl))
.then(r => get(serviceUrl))
} else {
return status
}
})
.then(status => {
if (!status.actions.upgrade) {
throw new Error(`Upgrade action not available for ${ status.links.self }. Is service active?`)
}
return status
})
.then(status => post({
url: status.actions.upgrade,
body: JSON.stringify({
inServiceStrategy: {
startFirst: (typeof startFirst !== 'undefined') ? startFirst : true,
launchConfig: status.launchConfig
},
toServiceStrategy: {
}
})
}))
}
module.exports = upgrade