-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapps.rb
1231 lines (1032 loc) · 37.9 KB
/
apps.rb
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'digest/sha1'
require 'fileutils'
require 'pathname'
require 'tempfile'
require 'tmpdir'
require 'set'
require "uuidtools"
require 'socket'
module VMC::Cli::Command
class Apps < Base
include VMC::Cli::ServicesHelper
include VMC::Cli::ManifestHelper
include VMC::Cli::TunnelHelper
include VMC::Cli::ConsoleHelper
include VMC::Cli::FileHelper
def list
apps = client.apps
apps.sort! {|a, b| a[:name] <=> b[:name] }
return display JSON.pretty_generate(apps || []) if @options[:json]
display "\n"
return display "No Applications" if apps.nil? || apps.empty?
infra_supported = !apps.detect { |a| a[:infra] }.nil?
apps_table = table do |t|
t.headings = 'Application', '# ', 'Health', 'URLS', 'Services'
t.headings << 'In' if infra_supported
apps.each do |app|
a = [app[:name], app[:instances], health(app), app[:uris].join(', '), app[:services].join(', ')]
if infra_supported
a << ( app[:infra] ? app[:infra][:provider] : " " )
end
t << a
end
end
display apps_table
end
alias :apps :list
SLEEP_TIME = 1
LINE_LENGTH = 80
# Numerators are in secs
TICKER_TICKS = 25/SLEEP_TIME
HEALTH_TICKS = 5/SLEEP_TIME
TAIL_TICKS = 45/SLEEP_TIME
GIVEUP_TICKS = 120/SLEEP_TIME
def info(what, default=nil)
@options[what] || (@app_info && @app_info[what.to_s]) || default
end
def console(appname, interactive=true)
app = client.app_info(appname)
infra_name = app[:infra] ? app[:infra][:name] : 'aws' # FIXME
unless defined? Caldecott
display "To use `vmc rails-console', you must first install Caldecott:"
display ""
display "\tgem install caldecott"
display ""
display "Note that you'll need a C compiler. If you're on OS X, Xcode"
display "will provide one. If you're on Windows, try DevKit."
display ""
display "This manual step will be removed in the future."
display ""
err "Caldecott is not installed."
end
#Make sure there is a console we can connect to first
conn_info = console_connection_info appname
port = pick_tunnel_port(@options[:port] || 20000)
raise VMC::Client::AuthError unless client.logged_in?
if not tunnel_pushed?(infra_name)
display "Deploying tunnel application '#{tunnel_appname(infra_name)}'."
auth = UUIDTools::UUID.random_create.to_s
push_caldecott(auth,infra_name)
start_caldecott(infra_name)
else
auth = tunnel_auth(infra_name)
end
if not tunnel_healthy?(auth,infra_name)
display "Redeploying tunnel application '#{tunnel_appname(infra_name)}'."
# We don't expect caldecott not to be running, so take the
# most aggressive restart method.. delete/re-push
client.delete_app(tunnel_appname(infra_name))
invalidate_tunnel_app_info(infra_name)
push_caldecott(auth,infra_name)
start_caldecott(infra_name)
end
start_tunnel(port, conn_info, auth, infra_name)
wait_for_tunnel_start(port)
start_local_console(port, appname) if interactive
port
end
def start(appname=nil, push=false)
if appname
do_start(appname, push)
else
each_app do |name|
do_start(name, push)
end
end
end
def stop(appname=nil)
if appname
do_stop(appname)
else
reversed = []
each_app do |name|
reversed.unshift name
end
reversed.each do |name|
do_stop(name)
end
end
end
def restart(appname=nil)
stop(appname)
start(appname)
end
def mem(appname, memsize=nil)
app = client.app_info(appname)
mem = current_mem = mem_quota_to_choice(app[:resources][:memory])
memsize = normalize_mem(memsize) if memsize
memsize ||= ask(
"Update Memory Reservation?",
:default => current_mem,
:choices => mem_choices
)
mem = mem_choice_to_quota(mem)
memsize = mem_choice_to_quota(memsize)
current_mem = mem_choice_to_quota(current_mem)
display "Updating Memory Reservation to #{mem_quota_to_choice(memsize)}: ", false
# check memsize here for capacity
check_has_capacity_for((memsize - mem) * app[:instances])
mem = memsize
if (mem != current_mem)
app[:resources][:memory] = mem
client.update_app(appname, app)
display 'OK'.green
restart appname if app[:state] == 'STARTED'
else
display 'OK'.green
end
end
def map(appname, url)
app = client.app_info(appname)
uris = app[:uris] || []
uris << url
app[:uris] = uris
client.update_app(appname, app)
display "Successfully mapped url".green
end
def unmap(appname, url)
app = client.app_info(appname)
uris = app[:uris] || []
url = url.gsub(/^http(s*):\/\//i, '')
deleted = uris.delete(url)
err "Invalid url" unless deleted
app[:uris] = uris
client.update_app(appname, app)
display "Successfully unmapped url".green
end
def delete(appname=nil)
force = @options[:force]
if @options[:all]
if no_prompt || force || ask("Delete ALL applications?", :default => false)
apps = client.apps
apps.each { |app| delete_app(app[:name], force) }
end
else
err 'No valid appname given' unless appname
delete_app(appname, force)
end
end
def files(appname, path='/')
return all_files(appname, path) if @options[:all] && !@options[:instance]
instance = @options[:instance] || '0'
content = client.app_files(appname, path, instance)
display content
rescue VMC::Client::NotFound, VMC::Client::TargetError
err 'No such file or directory'
end
def download(appname, path=nil)
path = File.expand_path(path || "#{appname}.zip" )
banner = "Downloading last pushed source code to #{path}: "
display banner, false
client.app_download(appname, path)
display 'OK'.green
end
def pull(appname, path=nil)
path = File.expand_path(path || appname)
banner = "Pulling last pushed source code: "
display banner, false
client.app_pull(appname, path)
display 'OK'.green
end
def clone(src_appname, dest_appname, dest_infra=nil)
# FIXME need to ask for dest_appname if nil
err "Application '#{dest_appname}' already exists" if app_exists?(dest_appname)
app = client.app_info(src_appname)
if client.infra_supported?
dest_infra = @options[:infra] || client.infra_name_for_description(
ask("Select Infrastructure",:indexed => true, :choices => client.infra_descriptions))
client.infra = dest_infra
end
url_template = "#{dest_appname}.${target-base}"
url_resolved = url_template.dup
resolve_lexically(url_resolved)
url = @options[:url] || ask("Application Deployed URL", :default => url_resolved)
Dir.mktmpdir do |dir|
zip_path = File.join(dir,src_appname)
pull(src_appname,zip_path)
display "Cloning '#{src_appname}' to '#{dest_appname}': "
manifest = {
:name => "#{dest_appname}",
:staging => app[:staging],
:uris => [ url ],
:instances => app[:instances],
:resources => app[:resources]
}
manifest[:staging][:command] = app[:staging][:command] if app[:staging][:command]
manifest[:infra] = { :provider => dest_infra } if dest_infra
client.create_app(dest_appname, manifest)
# Stage and upload the app bits.
upload_app_bits(dest_appname, zip_path, dest_infra)
# Clone services
client.services.select { |s| app[:services].include?(s[:name])}.each do |service|
display "Exporting data from #{service[:name]}: ", false
export_info = client.export_service(service[:name])
if export_info
display 'OK'.green
else
err "Export data from '#{service}': failed"
end
cloned_service_name = generate_cloned_service_name(src_appname,dest_appname,service[:name],dest_infra)
display "Creating service #{cloned_service_name}: ", false
client.create_service(dest_infra, service[:vendor], cloned_service_name)
display 'OK'.green
display "Binding service #{cloned_service_name}: ", false
client.bind_service(cloned_service_name, dest_appname)
display 'OK'.green
display "Importing data to #{cloned_service_name}: ", false
import_info = client.import_service(cloned_service_name,export_info[:uri])
if import_info
display 'OK'.green
else
err "Import data into '#{service}' failed"
end
end
no_start = @options[:nostart]
start(dest_appname, true) unless no_start
end
end
def logs(appname)
# Check if we have an app before progressing further
client.app_info(appname)
return grab_all_logs(appname) if @options[:all] && !@options[:instance]
instance = @options[:instance] || '0'
grab_logs(appname, instance)
end
def crashes(appname, print_results=true, since=0)
crashed = client.app_crashes(appname)[:crashes]
crashed.delete_if { |c| c[:since] < since }
instance_map = {}
# return display JSON.pretty_generate(apps) if @options[:json]
counter = 0
crashed = crashed.to_a.sort { |a,b| a[:since] - b[:since] }
crashed_table = table do |t|
t.headings = 'Name', 'Instance ID', 'Crashed Time'
crashed.each do |crash|
name = "#{appname}-#{counter += 1}"
instance_map[name] = crash[:instance]
t << [name, crash[:instance], Time.at(crash[:since]).strftime("%m/%d/%Y %I:%M%p")]
end
end
VMC::Cli::Config.store_instances(instance_map)
if @options[:json]
return display JSON.pretty_generate(crashed)
elsif print_results
display "\n"
if crashed.empty?
display "No crashed instances for [#{appname}]" if print_results
else
display crashed_table if print_results
end
end
crashed
end
def crashlogs(appname)
instance = @options[:instance] || '0'
grab_crash_logs(appname, instance)
end
def instances(appname, num=nil)
if num
change_instances(appname, num)
else
get_instances(appname)
end
end
def stats(appname=nil)
if appname
display "\n", false
do_stats(appname)
else
each_app do |n|
display "\n#{n}:"
do_stats(n)
end
end
end
def update(appname=nil)
if appname
app = client.app_info(appname)
if @options[:canary]
display "[--canary] is deprecated and will be removed in a future version".yellow
end
infra = app[:infra] ? app[:infra][:provider] : nil
upload_app_bits(appname, @path, infra)
restart appname if app[:state] == 'STARTED'
else
each_app do |name|
display "Updating application '#{name}'..."
app = client.app_info(name)
infra = app[:infra] ? app[:infra][:provider] : nil
upload_app_bits(name, @application, infra)
restart name if app[:state] == 'STARTED'
end
end
end
def push(appname=nil)
unless no_prompt || @options[:path]
proceed = ask(
'Would you like to deploy from the current directory?',
:default => true
)
unless proceed
@path = ask('Deployment path')
end
end
pushed = false
each_app(false) do |name|
display "Pushing application '#{name}'..." if name
do_push(name)
pushed = true
end
unless pushed
@application = @path
do_push(appname)
end
end
def environment(appname)
app = client.app_info(appname)
env = app[:env] || []
return display JSON.pretty_generate(env) if @options[:json]
return display "No Environment Variables" if env.empty?
etable = table do |t|
t.headings = 'Variable', 'Value'
env.each do |e|
k,v = e.split('=', 2)
t << [k, v]
end
end
display "\n"
display etable
end
def environment_add(appname, k, v=nil)
no_restart = @options[:norestart]
app = client.app_info(appname)
env = app[:env] || []
k,v = k.split('=', 2) unless v
env << "#{k}=#{v}"
display "Adding Environment Variable [#{k}=#{v}]: ", false
app[:env] = env
client.update_app(appname, app)
display 'OK'.green
restart appname if app[:state] == 'STARTED' unless no_restart
end
def environment_del(appname, variable)
app = client.app_info(appname)
env = app[:env] || []
deleted_env = nil
env.each do |e|
k,v = e.split('=')
if (k == variable)
deleted_env = e
break;
end
end
display "Deleting Environment Variable [#{variable}]: ", false
if deleted_env
env.delete(deleted_env)
app[:env] = env
client.update_app(appname, app)
display 'OK'.green
restart appname if app[:state] == 'STARTED' unless no_restart
else
display 'OK'.green
end
end
def rename(oldname, newname)
# Check if new app name is taken
if newname
err "Application '#{newname}' already exists" if app_exists?(newname)
else
raise VMC::Client::AuthError unless client.logged_in?
end
app = client.app_info(oldname)
app[:name] = newname
client.update_app(oldname, app)
display "Successfully updated app name to #{newname}".green
end
private
def app_exists?(appname)
app_info = client.app_info(appname)
app_info != nil
rescue VMC::Client::NotFound
false
end
def check_deploy_directory(path)
err 'Deployment path does not exist' unless File.exists? path
return if File.expand_path(Dir.tmpdir) != File.expand_path(path)
err "Can't deploy applications from staging directory: [#{Dir.tmpdir}]"
end
def upload_app_bits(appname, path, infra)
display 'Uploading Application:'
upload_file, file = "#{Dir.tmpdir}/#{appname}.zip", nil
FileUtils.rm_f(upload_file)
explode_dir = "#{Dir.tmpdir}/.vmc_#{appname}_files"
FileUtils.rm_rf(explode_dir) # Make sure we didn't have anything left over..
if path =~ /\.(war|zip)$/
#single file that needs unpacking
VMC::Cli::ZipUtil.unpack(path, explode_dir)
elsif !File.directory? path
#single file that doesn't need unpacking
FileUtils.mkdir(explode_dir)
FileUtils.cp(path,explode_dir)
else
Dir.chdir(path) do
# Stage the app appropriately and do the appropriate fingerprinting, etc.
if war_file = Dir.glob('*.war').first
VMC::Cli::ZipUtil.unpack(war_file, explode_dir)
elsif zip_file = Dir.glob('*.zip').first
VMC::Cli::ZipUtil.unpack(zip_file, explode_dir)
else
FileUtils.mkdir(explode_dir)
afi = VMC::Cli::FileHelper::AppFogIgnore.from_file("#{path}")
files = Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)
check_unreachable_links(path,afi.included_files(files))
copy_files( path, ignore_sockets( afi.included_files(files)), explode_dir )
end
end
end
# Send the resource list to the cloudcontroller, the response will tell us what it already has..
unless @options[:noresources]
display ' Checking for available resources: ', false
fingerprints = []
total_size = 0
resource_files = Dir.glob("#{explode_dir}/**/*", File::FNM_DOTMATCH)
resource_files.each do |filename|
next if (File.directory?(filename) || !File.exists?(filename))
fingerprints << {
:size => File.size(filename),
:sha1 => Digest::SHA1.file(filename).hexdigest,
:fn => filename
}
total_size += File.size(filename)
end
# Check to see if the resource check is worth the round trip
if (total_size > (64*1024)) # 64k for now
# Send resource fingerprints to the cloud controller
# FIXME where do I get infra?
appcloud_resources = client.check_resources(fingerprints,infra)
end
display 'OK'.green
if appcloud_resources
display ' Processing resources: ', false
# We can then delete what we do not need to send.
appcloud_resources.each do |resource|
FileUtils.rm_f resource[:fn]
# adjust filenames sans the explode_dir prefix
resource[:fn].sub!("#{explode_dir}/", '')
end
display 'OK'.green
end
end
# If no resource needs to be sent, add an empty file to ensure we have
# a multi-part request that is expected by nginx fronting the CC.
if VMC::Cli::ZipUtil.get_files_to_pack(explode_dir).empty?
Dir.chdir(explode_dir) do
File.new(".__empty__", "w")
end
end
# Perform Packing of the upload bits here.
display ' Packing application: ', false
VMC::Cli::ZipUtil.pack(explode_dir, upload_file)
display 'OK'.green
upload_size = File.size(upload_file);
if upload_size > 1024*1024
upload_size = (upload_size/(1024.0*1024.0)).round.to_s + 'M'
elsif upload_size > 0
upload_size = (upload_size/1024.0).round.to_s + 'K'
else
upload_size = '0K'
end
upload_str = " Uploading (#{upload_size}): "
display upload_str, false
FileWithPercentOutput.display_str = upload_str
FileWithPercentOutput.upload_size = File.size(upload_file);
file = FileWithPercentOutput.open(upload_file, 'rb')
client.upload_app(appname, file, appcloud_resources)
display 'OK'.green if VMC::Cli::ZipUtil.get_files_to_pack(explode_dir).empty?
display 'Push Status: ', false
display 'OK'.green
ensure
# Cleanup if we created an exploded directory.
FileUtils.rm_f(upload_file) if upload_file
FileUtils.rm_rf(explode_dir) if explode_dir
end
def check_app_limit
usage = client_info[:usage]
limits = client_info[:limits]
return unless usage and limits and limits[:apps]
if limits[:apps] == usage[:apps]
display "Not enough capacity for operation.".red
tapps = limits[:apps] || 0
apps = usage[:apps] || 0
err "Current Usage: (#{apps} of #{tapps} total apps already in use)"
end
end
def check_has_capacity_for(mem_wanted)
usage = client_info[:usage]
limits = client_info[:limits]
return unless usage and limits
available_for_use = limits[:memory].to_i - usage[:memory].to_i
if mem_wanted > available_for_use
tmem = pretty_size(limits[:memory]*1024*1024)
mem = pretty_size(usage[:memory]*1024*1024)
display "Not enough capacity for operation.".yellow
available = pretty_size(available_for_use * 1024 * 1024)
err "Current Usage: (#{mem} of #{tmem} total, #{available} available for use)"
end
end
def mem_choices
default = ['64M', '128M', '256M', '512M', '1G', '2G']
return default unless client_info
return default unless (usage = client_info[:usage] and limits = client_info[:limits])
available_for_use = limits[:memory].to_i - usage[:memory].to_i
check_has_capacity_for(64) if available_for_use < 64
return ['64M'] if available_for_use < 128
return ['64M', '128M'] if available_for_use < 256
return ['64M', '128M', '256M'] if available_for_use < 512
return ['64M', '128M', '256M', '512M'] if available_for_use < 1024
return ['64M', '128M', '256M', '512M', '1G'] if available_for_use < 2048
return ['64M', '128M', '256M', '512M', '1G', '2G']
end
def normalize_mem(mem)
return mem if /K|G|M/i =~ mem
"#{mem}M"
end
def mem_choice_to_quota(mem_choice)
(mem_choice =~ /(\d+)M/i) ? mem_quota = $1.to_i : mem_quota = mem_choice.to_i * 1024
mem_quota
end
def mem_quota_to_choice(mem)
if mem < 1024
mem_choice = "#{mem}M"
else
mem_choice = "#{(mem/1024).to_i}G"
end
mem_choice
end
def get_instances(appname)
instances_info_envelope = client.app_instances(appname)
# Empty array is returned if there are no instances running.
instances_info_envelope = {} if instances_info_envelope.is_a?(Array)
instances_info = instances_info_envelope[:instances] || []
instances_info = instances_info.sort {|a,b| a[:index] - b[:index]}
return display JSON.pretty_generate(instances_info) if @options[:json]
return display "No running instances for [#{appname}]".yellow if instances_info.empty?
instances_table = table do |t|
show_debug = instances_info.any? { |e| e[:debug_port] }
headings = ['Index', 'State', 'Start Time']
headings << 'Debug IP' if show_debug
headings << 'Debug Port' if show_debug
t.headings = headings
instances_info.each do |entry|
row = [entry[:index], entry[:state], Time.at(entry[:since]).strftime("%m/%d/%Y %I:%M%p")]
row << entry[:debug_ip] if show_debug
row << entry[:debug_port] if show_debug
t << row
end
end
display "\n"
display instances_table
end
def change_instances(appname, instances)
app = client.app_info(appname)
match = instances.match(/([+-])?\d+/)
err "Invalid number of instances '#{instances}'" unless match
instances = instances.to_i
current_instances = app[:instances]
new_instances = match.captures[0] ? current_instances + instances : instances
err "There must be at least 1 instance." if new_instances < 1
if current_instances == new_instances
display "Application [#{appname}] is already running #{new_instances} instance#{'s' if new_instances > 1}.".yellow
return
end
up_or_down = new_instances > current_instances ? 'up' : 'down'
display "Scaling Application instances #{up_or_down} to #{new_instances}: ", false
app[:instances] = new_instances
client.update_app(appname, app)
display 'OK'.green
end
def health(d)
return 'N/A' unless (d and d[:state])
return 'STOPPED' if d[:state] == 'STOPPED'
healthy_instances = d[:runningInstances]
expected_instance = d[:instances]
health = nil
if d[:state] == "STARTED" && expected_instance > 0 && healthy_instances
health = format("%.3f", healthy_instances.to_f / expected_instance).to_f
end
if health
if health == 1.0
return "RUNNING"
else
return "#{(health * 100).round}%"
end
elsif d[:state] == "STARTED"
return 'N/A' # unstarted instances
else
return d[:state]
end
end
def app_started_properly(appname, error_on_health)
app = client.app_info(appname)
case health(app)
when 'N/A'
# Health manager not running.
err "\nApplication '#{appname}'s state is undetermined, not enough information available." if error_on_health
return false
when 'RUNNING'
return true
else
if app[:meta][:debug] == "suspend"
display "\nApplication [#{appname}] has started in a mode that is waiting for you to trigger startup."
return true
else
return false
end
end
end
def display_logfile(path, content, instance='0', banner=nil)
banner ||= "====> #{path} <====\n\n"
unless content.empty?
display banner
prefix = "[#{instance}: #{path}] -".bold if @options[:prefixlogs]
unless prefix
display content
else
lines = content.split("\n")
lines.each { |line| display "#{prefix} #{line}"}
end
display ''
end
end
def grab_all_logs(appname)
instances_info_envelope = client.app_instances(appname)
return if instances_info_envelope.is_a?(Array)
instances_info = instances_info_envelope[:instances] || []
instances_info.each do |entry|
grab_logs(appname, entry[:index])
end
end
def grab_logs(appname, instance)
files_under(appname, instance, "/logs").each do |path|
begin
content = client.app_files(appname, path, instance)
display_logfile(path, content, instance)
rescue VMC::Client::NotFound, VMC::Client::TargetError
end
end
end
def files_under(appname, instance, path)
client.app_files(appname, path, instance).split("\n").collect do |l|
"#{path}/#{l.split[0]}"
end
rescue VMC::Client::NotFound, VMC::Client::TargetError
[]
end
def grab_crash_logs(appname, instance, was_staged=false)
# stage crash info
crashes(appname, false) unless was_staged
instance ||= '0'
map = VMC::Cli::Config.instances
instance = map[instance] if map[instance]
(files_under(appname, instance, "/logs") +
files_under(appname, instance, "/app/logs") +
files_under(appname, instance, "/app/log")).each do |path|
content = client.app_files(appname, path, instance)
display_logfile(path, content, instance)
end
end
def grab_startup_tail(appname, since = 0)
new_lines = 0
path = "logs/startup.log"
content = client.app_files(appname, path)
if content && !content.empty?
display "\n==== displaying startup log ====\n\n" if since == 0
response_lines = content.split("\n")
lines = response_lines.size
tail = response_lines[since, lines] || []
new_lines = tail.size
display tail.join("\n") if new_lines > 0
end
since + new_lines
rescue VMC::Client::NotFound, VMC::Client::TargetError
0
end
def provisioned_services_apps_hash
apps = client.apps
services_apps_hash = {}
apps.each {|app|
app[:services].each { |svc|
svc_apps = services_apps_hash[svc]
unless svc_apps
svc_apps = Set.new
services_apps_hash[svc] = svc_apps
end
svc_apps.add(app[:name])
} unless app[:services] == nil
}
services_apps_hash
end
def delete_app(appname, force)
app = client.app_info(appname)
services_to_delete = []
app_services = app[:services]
services_apps_hash = provisioned_services_apps_hash
app_services.each { |service|
del_service = force && no_prompt
unless no_prompt || force
del_service = ask(
"Provisioned service [#{service}] detected, would you like to delete it?",
:default => false
)
if del_service
apps_using_service = services_apps_hash[service].reject!{ |app| app == appname}
if apps_using_service.size > 0
del_service = ask(
"Provisioned service [#{service}] is also used by #{apps_using_service.size == 1 ? "app" : "apps"} #{apps_using_service.entries}, are you sure you want to delete it?",
:default => false
)
end
end
end
services_to_delete << service if del_service
}
display "Deleting application [#{appname}]: ", false
client.delete_app(appname)
display 'OK'.green
services_to_delete.each do |s|
delete_service_banner(s)
end
end
def do_start(appname, push=false)
app = client.app_info(appname)
return display "Application '#{appname}' could not be found".red if app.nil?
return display "Application '#{appname}' already started".yellow if app[:state] == 'STARTED'
if @options[:debug]
runtimes = client.runtimes_info
return display "Cannot get runtime information." unless runtimes
runtime = runtimes[app[:staging][:stack].to_sym]
return display "Unknown runtime." unless runtime
unless runtime[:debug_modes] and runtime[:debug_modes].include? @options[:debug]
modes = runtime[:debug_modes] || []
display "\nApplication '#{appname}' cannot start in '#{@options[:debug]}' mode"
if push
display "Try 'vmc start' with one of the following modes: #{modes.inspect}"
else
display "Available modes: #{modes.inspect}"
end
return
end
end
banner = "Staging Application '#{appname}': "
display banner, false
t = Thread.new do
count = 0
while count < TAIL_TICKS do
display '.', false
sleep SLEEP_TIME
count += 1
end
end
app[:state] = 'STARTED'
app[:debug] = @options[:debug]
app[:console] = VMC::Cli::Framework.lookup_by_framework(app[:staging][:model]).console
client.update_app(appname, app)
Thread.kill(t)
clear(LINE_LENGTH)
display "#{banner}#{'OK'.green}"
banner = "Starting Application '#{appname}': "
display banner, false
count = log_lines_displayed = 0
failed = false
start_time = Time.now.to_i
loop do
display '.', false unless count > TICKER_TICKS
sleep SLEEP_TIME
break if app_started_properly(appname, count > HEALTH_TICKS)
if !crashes(appname, false, start_time).empty?
# Check for the existance of crashes
display "\nError: Application [#{appname}] failed to start, logs information below.\n".red
grab_crash_logs(appname, '0', true)
if push and !no_prompt
display "\n"
delete_app(appname, false) if ask "Delete the application?", :default => true
end
failed = true
break
elsif count > TAIL_TICKS
log_lines_displayed = grab_startup_tail(appname, log_lines_displayed)
end
count += 1
if count > GIVEUP_TICKS # 2 minutes
display "\nApplication is taking too long to start, check your logs".yellow
break
end
end
exit(false) if failed
clear(LINE_LENGTH)
display "#{banner}#{'OK'.green}"
end
def do_stop(appname)
app = client.app_info(appname)
return display "Application '#{appname}' already stopped".yellow if app[:state] == 'STOPPED'
display "Stopping Application '#{appname}': ", false
app[:state] = 'STOPPED'
client.update_app(appname, app)
display 'OK'.green
end
def do_push(appname=nil)
unless @app_info || no_prompt
@manifest = { "applications" => { @path => { "name" => appname } } }
interact