fanforce-cli 1.7.1 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,512 +0,0 @@
1
- class Fanforce::CLI
2
- require 'fanforce/cli/commands_support'
3
-
4
- def list_apps
5
- puts "\n---------------------------------------------------------------------------------------------------------------\n"
6
- puts "#{Apps.count} APPS".format(:bold,:green) + ' IN CURRENT DIRECTORY'
7
- puts "---------------------------------------------------------------------------------------------------------------\n"
8
- Apps.each do |app, cur_count, total|
9
- printf "- %s\n", app.dir_name.format(:green)
10
- end
11
-
12
- puts "---------------------------------------------------------------------------------------------------------------\n\n" if Apps.count > 0
13
- end
14
-
15
- def create_app(app_id)
16
- dir_name = "app-#{app_id}"
17
- dir = "#{$HomeDir}/#{dir_name}"
18
-
19
- if File.exists?("#{dir}/config.ru")
20
- puts '---------------------------------------------------------------------------------------------------------------'
21
- puts 'ERROR... '.format(:red,:bold) + "#{dir_name} already exists. You should run: ".format(:red) + "fanforce update #{dir_name}".format(:green)
22
- puts '---------------------------------------------------------------------------------------------------------------'
23
- return
24
- end
25
-
26
- puts "\n---------------------------------------------------------------------------------------------------------------"
27
- puts 'CREATING FILES...'
28
- Dir.mkdir(dir) if !File.directory?(dir)
29
- Dir.chdir(dir)
30
- puts "#{'Created'.format(:bold, :green)} #{dir_name}/"
31
- app = App.load(dir)
32
- setup_files(app)
33
-
34
- puts "\n---------------------------------------------------------------------------------------------------------------"
35
- puts 'RUNNING BUNDLER...'
36
- Run.bundle_install(app.dir)
37
-
38
- puts "\n---------------------------------------------------------------------------------------------------------------"
39
- puts 'CREATING POW DOMAINS...'
40
- setup_pow(app)
41
-
42
- puts "\n---------------------------------------------------------------------------------------------------------------"
43
- puts 'CREATING LOCAL GIT REPOSITORY...'
44
- Run.git_init and puts '- git init'
45
- Run.git_add and puts '- git add .'
46
- Run.git_first_commit and puts '- git commit -m "initial fanforce commit"'
47
-
48
- #puts "\n---------------------------------------------------------------------------------------------------------------"
49
- #puts 'CREATING BITBUCKET REPOSITORY...'
50
- #setup_bitbucket app
51
- #
52
- #puts "\n---------------------------------------------------------------------------------------------------------------"
53
- #puts 'CREATING HEROKU APPS...'
54
- #setup_heroku app, :staging
55
- #setup_heroku app, :production
56
-
57
- puts "\n---------------------------------------------------------------------------------------------------------------"
58
- puts 'CREATING ENV VARIABLES...'
59
- setup_envs(app, :development)
60
-
61
- puts "\n---------------------------------------------------------------------------------------------------------------"
62
- puts 'DONE!'.format(:bold,:green)
63
- puts "---------------------------------------------------------------------------------------------------------------\n"
64
- end
65
-
66
- def update_app(app_id, command)
67
- app_dir_name = "app-#{app_id}"
68
- app_dir = "#{$HomeDir}/#{app_dir_name}"
69
-
70
- if !File.exists?("#{app_dir}/config.ru")
71
- puts '---------------------------------------------------------------------------------------------------------------'
72
- puts 'ERROR... '.format(:red,:bold) + "#{app_dir_name} does not exist. You should run: ".format(:red) + "fanforce create #{app_dir_name}".format(:green)
73
- puts '---------------------------------------------------------------------------------------------------------------'
74
- return
75
- end
76
-
77
- run_update(app_dir, 1, 1, command)
78
-
79
- puts "\n---------------------------------------------------------------------------------------------------------------"
80
- puts 'DONE!'.format(:bold,:green)
81
- puts "---------------------------------------------------------------------------------------------------------------\n"
82
- end
83
-
84
- ######################################################################################################################
85
-
86
- def delete_app(app_id)
87
- app_dir_name = "app-#{app_id}"
88
- app_dir = "#{$HomeDir}/#{app_dir_name}"
89
-
90
- if !File.directory?("#{app_dir}")
91
- puts '---------------------------------------------------------------------------------------------------------------'
92
- puts 'OOPS... '.format(:red,:bold) + "#{app_dir_name} does not exist and therefore cannot be deleted!"
93
- puts '---------------------------------------------------------------------------------------------------------------'
94
- return
95
- end
96
-
97
- app = App.load(app_dir)
98
-
99
- puts "\n---------------------------------------------------------------------------------------------------------------"
100
- puts 'DELETING HEROKU APPS...'
101
- [:staging,:production].each do |environment|
102
- next if $Config[:heroku].blank? or $Config[:heroku][environment].blank?
103
- heroku = auth_heroku(environment)
104
- heroku_app_name = get_heroku_app_name(app, environment)
105
- begin
106
- heroku.delete_app(heroku_app_name)
107
- puts "#{'Removed '.format(:green,:bold)}" + " #{heroku_app_name}"
108
- rescue Exception => e
109
- puts "#{'Already Removed'.format(:green,:bold)}" + " #{heroku_app_name}"
110
- end
111
- end
112
-
113
- puts "\n---------------------------------------------------------------------------------------------------------------"
114
- puts 'DELETING BITBUCKET REPOSITORY...'
115
- if $Config[:bitbucket].present?
116
- bitbucket = BitBucket.new(login: $Config[:bitbucket][:user], password: $Config[:bitbucket][:password])
117
- begin
118
- repo = bitbucket.repos.find($Config[:bitbucket][:user], app.dir_name)
119
- RestClient.delete("https://#{$Config[:bitbucket][:user]}:#{$Config[:bitbucket][:password]}@api.bitbucket.org/1.0/repositories/#{$Config[:bitbucket][:user]}/#{app.dir_name}")
120
- puts "#{'Removed'.format(:green,:bold)}" + " #{app.dir_name}"
121
- rescue
122
- puts "#{'Already Removed'.format(:green,:bold)}" + " #{app.dir_name}"
123
- end
124
- end
125
-
126
- puts "\n---------------------------------------------------------------------------------------------------------------"
127
- puts 'DELETING POW DOMAINS...'
128
- Run.pow_destroy(app, app.root_domain)
129
- Run.pow_destroy(app, Fanforce.default_short_domain)
130
-
131
- puts "\n---------------------------------------------------------------------------------------------------------------"
132
- puts 'DELETING FILES...'
133
- if File.directory?(app_dir)
134
- FileUtils.rm_rf(app_dir)
135
- puts "#{'Removed'.format(:bold, :green)} #{app_dir_name}/"
136
- else
137
- puts "#{'Already Removed'.format(:bold, :green)} #{app_dir_name}/"
138
- end
139
-
140
- puts "\n---------------------------------------------------------------------------------------------------------------"
141
- puts 'DONE! (note: iron workers were not deleted)'
142
- puts '---------------------------------------------------------------------------------------------------------------'
143
-
144
- end
145
-
146
- ######################################################################################################################
147
-
148
- def run_update(app_dir, processed_count, total_count, command)
149
- app = App.load(app_dir)
150
- puts "\n---------------------------------------------------------------------------------------------------------------"
151
- puts "#{'app'.upcase.format(:bold)} - #{app._id.upcase.gsub('-',' ').format(:bold)} (#{processed_count} of #{total_count})... "
152
-
153
- Dir.chdir(app_dir) do
154
- if [:all,:files].include?(command)
155
- puts "\nUPDATING FILES..."
156
- setup_files(app)
157
- end
158
-
159
- if [:all,:bundle].include?(command)
160
- puts "\nRUNNING BUNDLER..."
161
- Run.bundle_install(app.dir)
162
- end
163
-
164
- if [:all,:pow].include?(command)
165
- puts "\nUPDATING POW DOMAINS..."
166
- setup_pow(app)
167
- end
168
-
169
- if [:all,:git].include?(command)
170
- puts "\nUPDATING LOCAL GIT REPOSITORY..."
171
- setup_local_git(app)
172
- end
173
-
174
- if [:all,:env].include?(command)
175
- puts "\nUPDATING ENV VARIABLES..."
176
- setup_envs(app, :development)
177
- end
178
-
179
- print "\nRESTARTING POW... "
180
- restart(app, :development)
181
- puts 'DONE'
182
- end
183
-
184
- end
185
-
186
- ######################################################################################################################
187
-
188
- def run_push(app_dir, processed_count, total_count, environment, command)
189
- app = App.load(app_dir)
190
- puts "\n---------------------------------------------------------------------------------------------------------------"
191
- puts "#{'app'.upcase.format(:bold)} - #{app._id.upcase.gsub('-',' ').format(:bold)} (#{processed_count} of #{total_count})... "
192
-
193
- Dir.chdir(app_dir) do
194
- if [:all,:bitbucket].include?(command) and $Config[:bitbucket].present?
195
- if !(`git remote`).split(/\r?\n/).include?('bitbucket')
196
- setup_bitbucket(app)
197
- else
198
- puts "\n#{'Pushing '.format(:green,:bold)}" + "latest commit to Bitbucket (#{$Config[:bitbucket][:user]})..."
199
- Run.command('git push bitbucket --all')
200
- end
201
- end
202
-
203
- environments = (environment==:all) ? [:staging,:production] : [environment]
204
- environments.each do |environment|
205
- if [:all,:heroku].include?(command)
206
- remote_name = "#{env(environment)}-heroku"
207
- setup_heroku(app, environment) if !(`git remote`).split(/\r?\n/).include?(remote_name)
208
-
209
- vars = Env.vars_by_app(environment)[app.dir_name] || {}
210
- puts "\n#{"Updating Env Vars".format(:green,:bold)} on Heroku #{environment.to_s.titleize} (#{vars.size} variables)..."
211
- push_env_to(environment, app, vars)
212
-
213
- puts "\n#{'Pushing '.format(:green,:bold)}" + "latest commit to Heroku #{environment.to_s.titleize} (#{remote_name})..."
214
- Run.command("git push #{remote_name} master")
215
- end
216
-
217
- if [:all,:iron].include?(command)
218
- puts ''
219
- upload_iron(app, command, [environment])
220
- end
221
- end
222
-
223
- end
224
- end
225
-
226
- ######################################################################################################################
227
-
228
- def count
229
- puts "\n---------------------------------------------------------------------------------------------------------------"
230
- puts "#{Apps.count} APPS".format(:bold,:green) + ' IN CURRENT DIRECTORY'
231
- puts '---------------------------------------------------------------------------------------------------------------'
232
- puts ''
233
- end
234
-
235
- ######################################################################################################################
236
-
237
- def run_restart(app_dir, processed_count, total_count, environment)
238
- app = App.load(app_dir)
239
- puts "---------------------------------------------------------------------------------------------------------------"
240
- Dir.chdir(app_dir) do
241
- if [:all, :development].include?(environment)
242
- puts "#{'DEVELOPMENT '.format(:bold)}#{app.dir_name.format(:green)} restarted" if restart(app, :development)
243
- end
244
- if [:all, :staging].include?(environment)
245
- puts "#{'STAGING '.format(:bold)}#{app.dir_name.format(:green)} restarted" if restart(app, :staging)
246
- end
247
- if [:all, :production].include?(environment)
248
- puts "#{'PRODUCTION '.format(:bold)}#{app.dir_name.format(:green)} restarted" if restart(app, :production)
249
- end
250
- end
251
- end
252
-
253
- def restart(app, environment)
254
- if environment == :development
255
- File.mkdir("#{app.dir}/tmp") if !File.directory?("#{app.dir}/tmp")
256
- FileUtils.touch("#{app.dir}/tmp/restart.txt")
257
- elsif [:production, :staging].include?(environment)
258
- if $Config[:heroku].blank? or $Config[:heroku][environment].blank?
259
- puts "#{'OOPS...'.format(:red,:bold)} #{environment} has not been setup on heroku"
260
- return false
261
- end
262
- heroku = auth_heroku(environment)
263
- heroku.post_ps_restart get_heroku_app_name(app, environment)
264
- end
265
- return true
266
- end
267
-
268
- ######################################################################################################################
269
-
270
- def run_bundle(app_dir, processed_count, total_count, arg, extra_args)
271
- app = App.load(app_dir)
272
- puts "\n---------------------------------------------------------------------------------------------------------------"
273
- puts "#{'app'.upcase.format(:bold)} - #{app._id.upcase.gsub('-',' ').format(:bold)} (#{processed_count} of #{total_count})... "
274
-
275
- Dir.chdir(app_dir) do
276
- if Gem::Specification::find_all_by_name('bundler').empty?
277
- puts 'Installing Bundler... '
278
- `gem install bundler`
279
- puts 'DONE'
280
- end
281
-
282
- app.update_file(:gemfile)
283
- Run.command("bundle #{arg} #{extra_args.join(' ')}")
284
- restart(app, :development)
285
- puts 'DONE'.format(:green)
286
- end
287
- end
288
-
289
- ######################################################################################################################
290
-
291
- def run_cleanorgs(app_dir, processed_count, total_count, environment, supercore_api_key)
292
- ENV['RACK_ENV'] = environment.to_s
293
- app = App.load(app_dir)
294
- puts "\n---------------------------------------------------------------------------------------------------------------"
295
- puts "#{'app'.upcase.format(:bold)} - #{app._id.upcase.gsub('-',' ').format(:bold)} (#{processed_count} of #{total_count})... "
296
-
297
- require 'redis'
298
- require 'fanforce/api'
299
- ff = Fanforce::API.new(supercore_api_key)
300
-
301
- Dir.chdir(app_dir) do
302
- app = App.load(app_dir)
303
- vars = Env.vars_by_app(environment)[app.dir_name] || {}
304
- redis = Redis.new(url: vars['REDIS_URL'])
305
-
306
- installs = redis.keys("installed:#{app.dir_name}:*")
307
- organizations = {}
308
- redis.multi do
309
- organizations = installs.inject({}) do |result, key|
310
- next result if key !~ /^(installed:#{app.dir_name}:(.+))$/
311
- result.update $2 => redis.get($1)
312
- end
313
- end
314
- puts "#{organizations.size} installs found..."
315
- processed_count = 0
316
- organizations.each do |organization_id, api_key|
317
- print "- checking organization #{processed_count+=1}... "
318
- if ff.get("/organizations/#{organization_id}", fields: '_id')
319
- puts 'VALID'
320
- else
321
- print 'INVALID... '
322
- redis.del("installed:#{app.dir_name}:#{organization_id}")
323
- puts 'UNINSTALLED'
324
- end
325
- end
326
-
327
- end
328
- end
329
-
330
- ######################################################################################################################
331
-
332
- def preprocess_git_overview
333
- puts ''
334
- length = Apps.dir_names.inject(0) {|length, dir_name| dir_name.length > length ? dir_name.length : length }
335
- puts sprintf("%-#{length+3}s %-20s %s", 'Directory', 'Plugin Type', 'Status').format(:bold)
336
- length
337
- end
338
-
339
- def postprocess_git_overview
340
- puts '------------------------------------------------------------------------'
341
- puts 'DONE'
342
- end
343
-
344
- def run_git_overview(app_dir, processed_count, total_count, length)
345
- app = App.load(app_dir)
346
- puts '------------------------------------------------------------------------'
347
- Dir.chdir(app_dir) do
348
- committed = `git status`.include?('nothing to commit') ? true : false
349
- printf "%-#{length+3}s %s\n", app.dir_name, committed ? 'Committed'.format(:green) : 'Uncommitted'.format(:red)
350
- end
351
- end
352
-
353
- def run_git(app_dir, processed_count, total_count, extra_args)
354
- app = App.load(app_dir)
355
- puts "\n---------------------------------------------------------------------------------------------------------------"
356
- puts "#{'app'.upcase.format(:bold)} - #{app._id.upcase.gsub('-',' ').format(:bold)} (#{processed_count} of #{total_count})... "
357
-
358
- extra_args = extra_args.map {|c| c.include?(' ') ? c.to_json : c }.join(' ')
359
- extra_args = '--no-pager ' + extra_args if extra_args !~ /--no-pager/
360
-
361
- Dir.chdir(app_dir) do
362
- Run.command("git #{extra_args}")
363
- puts 'DONE'.format(:green)
364
- end
365
- end
366
-
367
- ######################################################################################################################
368
-
369
- def run_iron(app_dir, processed_count, total_count, command, environment)
370
- environments = environment == :all ? [:development, :staging, :production] : [environment]
371
- app = App.load(app_dir)
372
- puts "\n---------------------------------------------------------------------------------------------------------------"
373
- puts "#{'app'.upcase.format(:bold)} - #{app._id.upcase.gsub('-',' ').format(:bold)} (#{processed_count} of #{total_count})... "
374
-
375
- upload_iron(app, command, environments)
376
- end
377
-
378
- def upload_iron(app, command, environments)
379
- if !File.directory?("#{app.dir}/workers")
380
- return puts "#{'Skipped '.format(:bold)} no workers folder was found"
381
- end
382
-
383
- environments.each do |environment|
384
- vars = Env.vars_by_app(environment)[app.dir_name] || {}
385
- next puts "#{'Skipped '.format(:bold)} #{environment.to_s.titleize} is missing IRON_TOKEN and/or IRON_PROJECT_ID env variables" if vars['IRON_TOKEN'].blank? or vars['IRON_PROJECT_ID'].blank?
386
-
387
- puts "#{'Updating Env'.format(:green,:bold)} #{environment.to_s.titleize}... and workers have #{vars.size} env variables"
388
- push_env_to(environment, app, vars, true)
389
-
390
- iron_worker = IronWorkerNG::Client.new(:token => vars['IRON_TOKEN'], :project_id => vars['IRON_PROJECT_ID'])
391
- Dir.chdir("#{app.dir}/workers") do
392
- workers = Dir['*.worker']
393
- next puts "#{'Skipped '.format(:bold)} #{environment.to_s.titleize} has 0 workers" if workers.size == 0
394
-
395
- upload_processes = []
396
- FileUtils.cp("#{app.dir}/workers/.env/#{environment}.rb", "#{app.dir}/workers/.env/.#{environment}env.rb")
397
- workers.each do |filename|
398
- code_name = filename.gsub('.worker', '')
399
- remove_single_worker(code_name, iron_worker, environment) if command == :reset
400
-
401
- puts "#{'Uploading'.format(:green,:bold)} #{filename.gsub('.worker', '')} to #{environment.to_s.titleize}..."
402
- code = IronWorkerNG::Code::Base.new(:workerfile => "#{app.dir}/workers/#{filename}")
403
- code.remote
404
- code.name = code_name
405
- code.file("#{app.dir}/workers/.env/.#{environment}env.rb")
406
-
407
- upload_processes << upload_iron_worker(app, iron_worker, code, filename, environment)
408
- end
409
- upload_processes.each { |pid| Process.waitpid(pid) }
410
- FileUtils.remove_file("#{app.dir}/workers/.env/.#{environment}env.rb")
411
- end
412
- end
413
- end
414
-
415
- def upload_iron_worker(app, iron_worker, code, filename, environment)
416
- fork do
417
- begin
418
- iron_worker.codes.create(code, max_concurrency: 5)
419
- rescue Exception => e
420
- puts "#{'Aborted '.format(:red,:bold)} upload of #{filename.gsub('.worker', '')} to #{environment.to_s.titleize}..."
421
- puts e.message
422
- puts e.backtrace
423
- puts ''
424
- end
425
- end
426
- end
427
-
428
- def remove_single_worker(code_name, iron_worker, environment)
429
- iron_worker.codes.list.each do |code|
430
- next if code.name != code_name
431
- iron_worker.codes.delete(code.id)
432
- puts "#{'Removing '.format(:green,:bold)} #{code_name} from #{environment.to_s.titleize}..."
433
- return true
434
- end
435
- end
436
-
437
- ######################################################################################################################
438
-
439
- def delete_all_iron_workers(environment)
440
- puts ''
441
- environments = environment == :all ? [:development, :staging, :production] : [environment]
442
-
443
- iron_auths = {}
444
- Apps.each do |app, cur_count, total|
445
- environments.each do |environment|
446
- vars = Env.vars_by_app(environment)[app.dir_name]
447
- iron_auths[environment] ||= {}
448
- iron_auths[environment][vars['IRON_PROJECT_ID']] = vars['IRON_TOKEN'] if vars['IRON_PROJECT_ID'].present?
449
- end
450
- end
451
-
452
- environments.each do |environment|
453
- puts '---------------------------------------------------------------------------------------------------------------'
454
- puts "REMOVING WORKERS IN #{environment.to_s.upcase}..."
455
-
456
- workers_found = false
457
- iron_auths[environment].each do |iron_project_id, iron_token|
458
- iron_worker = IronWorkerNG::Client.new(:token => iron_token, :project_id => iron_project_id)
459
- iron_worker.codes.list.each do |code|
460
- workers_found = true
461
- iron_worker.codes.delete(code.id)
462
- puts "#{'Removed'.format(:red,:bold)} #{code.name}"
463
- end
464
- end
465
- puts 'No workers were found' if !workers_found
466
- end
467
-
468
- puts '---------------------------------------------------------------------------------------------------------------'
469
- end
470
-
471
- ######################################################################################################################
472
-
473
- def run_upgrade(app_dir, processed_count, total_count)
474
- environment = :qa
475
- app = App.load(app_dir)
476
- puts "\n---------------------------------------------------------------------------------------------------------------"
477
- puts "#{'app'.upcase.format(:bold)} - #{app._id.upcase.gsub('-',' ').format(:bold)} (#{processed_count} of #{total_count})... "
478
-
479
- #heroku = auth_heroku(:staging)
480
- #heroku_app_name = get_heroku_app_qa_name(app, environment)
481
- #begin
482
- # heroku.delete_app(heroku_app_name)
483
- # puts "#{'Deleted '.format(:green,:bold)}" + "#{environment} app on heroku (#{heroku_app_name})"
484
- #rescue
485
- # heroku.post_app(name: heroku_app_name)
486
- # puts "#{'Not Found '.format(:green,:bold)}" + "#{environment} on heroku (#{heroku_app_name})"
487
- #end
488
-
489
- if (`git remote`).split(/\r?\n/).include?($Config[:bitbucket][:user])
490
- puts "#{'Removed '.format(:red,:bold)}" + "git remote for #{$Config[:bitbucket][:user]}"
491
- `git remote rm #{$Config[:bitbucket][:user]}`
492
- end
493
- if (`git remote`).split(/\r?\n/).include?('qa-heroku')
494
- puts "#{'Removed '.format(:red,:bold)}" + "git remote for qa-heroku"
495
- `git remote rm qa-heroku`
496
- end
497
- if (`git remote`).split(/\r?\n/).include?('bitbucket')
498
- puts "#{'Updated '.format(:green,:bold)}" + "git remote for bitbucket"
499
- `git remote rm bitbucket`
500
- `git remote add bitbucket git@bitbucket.org:#{$Config[:bitbucket][:user]}/#{app.dir_name}.git`
501
- else
502
- `git remote add bitbucket git@bitbucket.org:#{$Config[:bitbucket][:user]}/#{app.dir_name}.git`
503
- puts "#{'Created '.format(:green,:bold)}" + "git remote for bitbucket"
504
- end
505
-
506
- `git config --remove-section branch.master`
507
- `git push --set-upstream bitbucket master`
508
-
509
- end
510
-
511
-
512
- end