1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | {resolve} = require "path"
|
13 |
|
14 |
|
15 | {map, pluck, remove, read, shuffle} = require "fairmont"
|
16 | {parse} = require "c50n"
|
17 |
|
18 |
|
19 | async = (require "when/generator").lift
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | catch_fail = (f) ->
|
28 | try
|
29 | f()
|
30 | catch e
|
31 | throw e
|
32 |
|
33 |
|
34 |
|
35 | select_random = (list) ->
|
36 | list = shuffle list
|
37 | return list[0]
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | module.exports =
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | build_create_cluster: async (config, argv) ->
|
50 | catch_fail ->
|
51 |
|
52 | if argv.length == 1
|
53 |
|
54 | cluster_name = argv[0]
|
55 | else
|
56 |
|
57 | {adjectives, nouns} = parse( yield read( resolve( __dirname, "names.cson")))
|
58 | cluster_name = "#{select_random(adjectives)}-#{select_random(nouns)}"
|
59 |
|
60 |
|
61 | return {
|
62 |
|
63 | aws: config.aws
|
64 | key_name: config.aws.key_name
|
65 | availability_zone: config.aws.availability_zone
|
66 | cluster_name: cluster_name
|
67 | public_domain: config.public_domain
|
68 | private_domain: "#{cluster_name}.cluster"
|
69 |
|
70 |
|
71 | channel: config.channel || 'stable'
|
72 | cluster_size: config.cluster_size || 3
|
73 | instance_type: config.instance_type || "m1.medium"
|
74 | public_keys: config.public_keys || []
|
75 | region: config.region if config.region?
|
76 | formation_service_templates:
|
77 | config.extra_storage || true
|
78 | spot_price: config.spot_price if config.spot_price?
|
79 | virtualization: config.virtualization || "pv"
|
80 |
|
81 |
|
82 | url: config.huxley.url
|
83 | secret_token: config.huxley.secret_token
|
84 | email: config.huxley.email
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 | check_create_cluster: async (config, argv) ->
|
90 | catch_fail ->
|
91 | if config.clusters?
|
92 | clusters = yield map ((x) -> pluck "name", x), config.clusters
|
93 | if argv[0] in clusters
|
94 | message = "There is already a cluster named #{argv[0]} registered with your account \n " +
|
95 | "Please use select another name or use \"huxley cluster delete #{argv[0]}\" to delete the current cluster."
|
96 | throw message
|
97 |
|
98 |
|
99 | update_create_cluster: async (config, options, api_response) ->
|
100 | catch_fail ->
|
101 | unless config.data.clusters?
|
102 | config.data.clusters = []
|
103 |
|
104 | config.data.clusters.push {
|
105 | name: options.cluster_name
|
106 | id: api_response.cluster_id
|
107 | }
|
108 |
|
109 | yield config.save()
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | build_delete_cluster: async (config, argv) ->
|
118 | catch_fail ->
|
119 | clusters = yield map ((x) -> pluck "name", x), config.clusters
|
120 | index = clusters.indexOf argv[0]
|
121 |
|
122 | return {
|
123 | cluster_id: config.clusters[index].id
|
124 | url: config.huxley.url
|
125 | secret_token: config.huxley.secret_token
|
126 | email: config.huxley.email
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 | check_delete_cluster: async (config, argv) ->
|
132 | catch_fail ->
|
133 | if config.clusters?
|
134 | clusters = yield map ((x) -> pluck "name", x), config.clusters
|
135 | unless argv[0] in clusters
|
136 | throw "Error: The cluster \"#{argv[0]}\" is not registered with your account. Nothing to remove."
|
137 | else
|
138 | throw "Error: The cluster \"#{argv[0]}\" is not registered with your account. Nothing to remove."
|
139 |
|
140 |
|
141 |
|
142 | update_delete_cluster: async (config, argv) ->
|
143 | catch_fail ->
|
144 | clusters = yield map ((x) -> pluck "name", x), config.data.clusters
|
145 | index = clusters.indexOf argv[0]
|
146 | config.data.clusters[index..index] = []
|
147 |
|
148 | yield config.save()
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | build_poll_cluster: async (config, argv) ->
|
156 | catch_fail ->
|
157 | clusters = yield map ((x) -> pluck "name", x), config.clusters
|
158 | index = clusters.indexOf argv[0]
|
159 |
|
160 | return {
|
161 | cluster_id: config.clusters[index].id
|
162 | url: config.huxley.url
|
163 | secret_token: config.huxley.secret_token
|
164 | email: config.huxley.email
|
165 | }
|
166 |
|
167 |
|
168 |
|
169 | check_poll_cluster: async (config, argv) ->
|
170 | catch_fail ->
|
171 | if config.clusters?
|
172 | clusters = yield map ((x) -> pluck "name", x), config.clusters
|
173 | unless argv[0] in clusters
|
174 | throw "Error: The cluster \"#{argv[0]}\" is not registered with your account. Nothing to poll."
|
175 | else
|
176 | throw "Error: The cluster \"#{argv[0]}\" is not registered with your account. Nothing to poll."
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | build_add_remote: async (config, argv) ->
|
184 | catch_fail ->
|
185 | return yield {
|
186 | cluster_name: argv[0]
|
187 | cluster_address: "core@#{argv[0]}.#{config.public_domain}"
|
188 | repo_name: config.app_name
|
189 | hook_address: "root@#{argv[0]}.#{config.public_domain}:3000"
|
190 | url: config.huxley.url
|
191 | secret_token: config.huxley.secret_token
|
192 | email: config.huxley.email
|
193 | }
|
194 |
|
195 |
|
196 | check_add_remote: async (config, argv) ->
|
197 | catch_fail ->
|
198 | if config.remotes?
|
199 | remotes = yield map ((x) -> pluck "name", x), config.remotes
|
200 | if argv[0] in remotes
|
201 | message = "There is already a remote named #{argv[0]} registered with this application. \n " +
|
202 | "Please use \"huxley remote rm #{argv[0]}\" to delete and try again."
|
203 | throw message
|
204 |
|
205 |
|
206 |
|
207 | update_add_remote: async (config, argv, api_response) ->
|
208 | catch_fail ->
|
209 | unless config.data.remotes?
|
210 | config.data.remotes = []
|
211 |
|
212 | config.data.remotes.push {
|
213 | name: argv[0]
|
214 | id: api_response.remote_id
|
215 | }
|
216 |
|
217 | yield config.save()
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | build_rm_remote: async (config, argv) ->
|
224 | catch_fail ->
|
225 | remotes = yield map ((x) -> pluck "name", x), config.remotes
|
226 | index = remotes.indexOf argv[0]
|
227 |
|
228 | return {
|
229 | remote_id: config.remotes[index].id
|
230 | url: config.huxley.url
|
231 | secret_token: config.huxley.secret_token
|
232 | email: config.huxley.email
|
233 | }
|
234 |
|
235 |
|
236 |
|
237 | check_rm_remote: async (config, argv) ->
|
238 | catch_fail ->
|
239 | if config.remotes?
|
240 | remotes = yield map ((x) -> pluck "name", x), config.remotes
|
241 | unless argv[0] in remotes
|
242 | throw "Error: The remote #{argv[0]} is not registered with your account. Nothing to remove."
|
243 | else
|
244 | throw "Error: The remote #{argv[0]} is not registered with your account. Nothing to remove."
|
245 |
|
246 |
|
247 | update_rm_remote: async (config, argv) ->
|
248 | catch_fail ->
|
249 | remotes = yield map ((x) -> pluck "name", x), config.data.remotes
|
250 | index = remotes.indexOf argv[0]
|
251 | config.data.remotes[index..index] = []
|
252 |
|
253 | yield config.save()
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | run_passive_remote: async (config, argv) ->
|
261 | catch_fail ->
|
262 |
|
263 | command =
|
264 | "ssh -A -o \"StrictHostKeyChecking no\" -o \"UserKnownHostsFile=/dev/null\" " +
|
265 | "-p 3000 root@#{argv[0]}.#{config.public_domain} << EOF\n" +
|
266 | "cd /root/passive \n " +
|
267 | "if [ -d #{config.repo_name}.git ]; then \n " +
|
268 | " rm -rf #{config.repo_name}.git \n" +
|
269 | "fi \n " +
|
270 | "mkdir #{config.repo_name}.git \n " +
|
271 | "cd #{config.repo_name}.git \n " +
|
272 | "/usr/bin/git init --bare \n " +
|
273 | "EOF"
|
274 |
|
275 | yield shell command
|
276 |
|
277 |
|
278 | yield force shell, "git remote rm #{argv[0]}"
|
279 | yield shell "git remote add #{argv[0]} ssh://root@#{argv[0]}.#{config.public_domain}:3000/root/passive/#{config.repo_name}.git"
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | command =
|
285 | "cd #{process.cwd()} &&" +
|
286 | "git init; " +
|
287 | "git add -A; "+
|
288 | "git commit -m 'Repo commited by Huxley to place on cluster.'; " +
|
289 | "git push #{config.repo_name} master"
|
290 |
|
291 | yield shell command
|