-
Notifications
You must be signed in to change notification settings - Fork 0
/
createCustomNode.js
133 lines (111 loc) · 3.21 KB
/
createCustomNode.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var async = require('async');
var fs = require('fs');
var util = require('util');
var ShippableAdapter = require('./ShippableAdapter.js');
function createClusterNode() {
var bag = {
apiUrl: "https://api.shippable.com"
};
async.series([
_readConfigs.bind(null, bag),
_getDefaultClusterIdFromSubscription.bind(null, bag),
_postClusterNode.bind(null, bag),
_getNodeInitScript.bind(null, bag),
_saveInitScript.bind(null, bag)
],
function (err) {
if (err)
console.log('Failed to post clusterNode with error: ', err);
else {
console.log('Created clusterNode object in Shippable. Run initScript.sh on the node to complete initialization');
}
}
);
}
function _readConfigs(bag, next) {
console.log('Reading config.json file');
fs.readFile('config.json',
function (err, data) {
if (err)
return next(err);
data = data.toString();
var error = null;
// parse data
try {
bag.configs = JSON.parse(data);
} catch (e) {
error = 'Unable to parse config data. ' +
'Please check if config.json is a valid JSON file.';
}
if (error)
return next(error);
else
return next();
}
);
}
function _getDefaultClusterIdFromSubscription(bag, next) {
bag.shippableAdapter = new ShippableAdapter(bag.configs.SHIPPABLE_API_TOKEN,
bag.apiUrl);
// skip if user supplied the clusterId
if (bag.configs.CLUSTER_ID) return next();
console.log('fetching default cluster for subscription');
bag.shippableAdapter.getSubscriptionById(bag.configs.SUBSCRIPTION_ID,
function (err, sub) {
if (err)
return next(err);
try {
bag.sub = JSON.parse(sub);
} catch (e) {
return next('Unable to parse response');
}
if (!bag.sub.defaultClusterId)
return next('No default cluster present in subscription.');
bag.configs.CLUSTER_ID = bag.sub.defaultClusterId;
return next();
}
);
}
function _postClusterNode(bag, next) {
console.log('creating clusterNode');
var clusterNodeBody = {
friendlyName: bag.configs.FRIENDLY_NAME,
isShippableInitialized: bag.configs.IS_SHIPPABLE_INITIALIZED,
location: bag.configs.NODE_LOCATION,
nodeTypeCode: bag.configs.NODE_TYPE_CODE,
sshPort: bag.configs.SSH_PORT,
nodeInitScript: bag.configs.NODE_INIT_SCRIPT,
subscriptionId: bag.configs.SUBSCRIPTION_ID,
clusterId: bag.configs.CLUSTER_ID
};
bag.shippableAdapter.postClusterNode(clusterNodeBody,
function (err, clusterNode) {
if (err)
return next(err);
bag.clusterNode = clusterNode;
return next();
}
);
}
function _getNodeInitScript(bag, next) {
console.log('getting node initialization script');
bag.shippableAdapter.getNodeInitScript(bag.clusterNode.id,
function (err, initScript) {
if (err)
return next(err);
bag.initScript = initScript;
return next();
}
);
}
function _saveInitScript(bag, next) {
console.log('saving initScript in initScript.sh');
fs.writeFile('initScript.sh', bag.initScript,
function (err) {
if (err)
return next(err);
return next();
}
);
}
createClusterNode();