-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·178 lines (147 loc) · 5.75 KB
/
index.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
var program = require('commander'),
colors = require('colors'),
Cluster = require('coreos-cluster').Cluster,
Table = require('easy-table'),
_ = require('lodash');
var pkg = require('./package.json');
program
.version(pkg.version)
.option('--basename [name]', 'prefixed name of cluster', 'cluster-')
.option('-t --type [type]', 'type of cluster [performance]', 'performance')
.option('-r --release [release]', 'coreos release [stable]', 'stable')
.option('-f --flavor [flavor]', 'flavor for the coreos cluster [performance1-1]')
.option('--num-nodes [number]', 'number of nodes to create (or add)', parseInt)
.option('--discovery-service-url [url]', 'url for an existing cluster discovery service')
.option('--private-network [guid]', 'guid for an optional private network')
.option('--monitoring-token [guid]', 'guid for optional rackspace cloud monitoring')
.option('--ephemeral', 'optional use data disk for Docker storage')
.option('--key-name [ssh keyname]', 'optional ssh keyname')
.option('--updateGroup [group]', 'optional update group')
.option('--updateServer [server]', 'optional endpoint for updates')
.option('--username [username]', 'required or via RACKSPACE_USERNAME env variable')
.option('--apiKey [apiKey]', 'required or via RACKSPACE_APIKEY env variable')
.option('--region [region]', 'required or via RACKSPACE_REGION env variable')
.parse(process.argv);
var options = {
type: program.type,
release: program.release,
update: {},
credentials: {
username: program.username || process.env.RACKSPACE_USERNAME,
apiKey: program.apiKey || process.env.RACKSPACE_APIKEY,
region: program.region || process.env.RACKSPACE_REGION
}
};
if (!options.credentials.region ||
!options.credentials.username ||
!options.credentials.apiKey) {
program.help();
process.exit(2);
}
if (program.basename) {
options.basename = program.basename
}
if (program.flavor) {
options.flavor = program.flavor;
}
if (program.keyName) {
options.keyname = program.keyName;
}
if (program.discoveryServiceUrl) {
options.discoveryServiceUrl = program.discoveryServiceUrl;
}
if (program.privateNetwork) {
options.privateNetwork = program.privateNetwork;
}
if (program.monitoringToken) {
options.monitoringToken = program.monitoringToken;
}
options.ephemeral = false;
if (program.ephemeral) {
options.ephemeral = program.ephemeral;
}
if (program.updateGroup) {
options.update.group = program.updateGroup;
}
if (program.updateServer) {
options.update.server = program.updateServer;
}
var cluster, interval;
try {
cluster = new Cluster(options);
}
catch (e) {
terminate(e, true);
}
console.log(colors.green('Starting coreos-cluster create...'));
console.log(colors.yellow(' Type: ') + colors.green(cluster.type));
console.log(colors.yellow(' Release: ') + colors.green(cluster.release));
console.log(colors.yellow(' Flavor: ') + colors.green(cluster.flavor));
console.log(colors.yellow(' Number of Nodes: ') + colors.green(program.numNodes));
process.stdout.write(colors.yellow('\ncoreos-cluster initializing...'));
cluster.initialize(function(err) {
if (err) {
terminate(err);
}
process.stdout.write(colors.green('done: \n'));
console.log(colors.yellow(' SSH Key: ') + colors.green(cluster.keyname));
console.log(colors.yellow(' Service Discovery URL: ') + colors.green(cluster.discoveryServiceUrl));
console.log(colors.yellow(' Docker storage on data disk: ') + colors.green(cluster.ephemeral));
console.log('\n' + colors.blue('Validating cluster options and total number of nodes...'));
cluster.validateNodeOptions(function(err, currentNodes) {
if (err) {
terminate(err);
}
console.log(colors.blue(' Found ') + colors.green(currentNodes + ' current nodes') + colors.blue(' for cluster ' + cluster._clusterToken));
console.log(colors.blue(' Adding ') + colors.green(program.numNodes + ' nodes') + colors.blue(' to cluster ' + cluster._clusterToken + '\n'));
if (currentNodes + program.numNodes < 3) {
terminate('Total number of nodes must be at least 3');
}
if (currentNodes) {
console.log(colors.red('\nAdd New Servers To Cluster...') + colors.gray(' this may take a few minutes...\n'));
}
else {
console.log(colors.red('\nCreating Servers For New Cluster...') + colors.gray(' this may take a few minutes...\n'));
}
interval = setInterval(function() {
process.stdout.write(colors.gray('.'));
}, 2500);
cluster.addNodes(program.numNodes, handleProvision)
});
function handleProvision(err) {
clearInterval(interval);
process.stdout.write(colors.gray('done!\n\n'));
if (err) {
terminate(err);
}
var t = new Table;
Object.keys(cluster._servers).forEach(function(name) {
var server = cluster._servers[name];
t.cell('Name', server.name);
t.cell('IP', _.find(server.addresses.public, function(ip) {
return ip.version === 4;
}).addr);
t.cell('Status', server.status);
t.cell('ID', server.id);
t.newRow();
});
t.sort(['Name']);
console.log(t.toString());
console.log(colors.yellow(' Service Discovery URL: ') + colors.green(cluster.discoveryServiceUrl));
console.log(colors.yellow(' SSH Key: ') + colors.green(cluster.keyname));
if (cluster._keypair) {
console.log(colors.yellow(' Private Key: ') + colors.green(cluster._keypair.private_key));
console.log(colors.yellow(' Public Key: ') + colors.green(cluster._keypair.public_key));
console.log(colors.red('WARNING: You must save your ssh private key, this will not be displayed again'));
}
console.log(colors.green('\nSUCCESS!'));
}
});
function terminate(reason, help) {
console.error(colors.red(reason));
if (help) {
program.help();
}
process.exit(1);
}