Class: VMC::Cli::Command::Apps

Inherits:
Base show all
Includes:
VMC::Cli::ConsoleHelper, FileHelper, ManifestHelper, ServicesHelper, TunnelHelper
Defined in:
lib/cli/commands/apps.rb

Constant Summary collapse

SLEEP_TIME =
1
LINE_LENGTH =
80
TICKER_TICKS =

Numerators are in secs

25/SLEEP_TIME
HEALTH_TICKS =
5/SLEEP_TIME
TAIL_TICKS =
45/SLEEP_TIME
GIVEUP_TICKS =
120/SLEEP_TIME

Constants included from TunnelHelper

TunnelHelper::HELPER_APP, TunnelHelper::HELPER_VERSION, TunnelHelper::PORT_RANGE

Constants included from ManifestHelper

ManifestHelper::DEFAULTS, ManifestHelper::MANIFEST, ManifestHelper::YES_SET

Constants inherited from Base

Base::MANIFEST

Instance Attribute Summary

Attributes inherited from Base

#no_prompt, #prompt_ok

Instance Method Summary collapse

Methods included from FileHelper

#check_unreachable_links, #copy_files, #ignore_sockets

Methods included from VMC::Cli::ConsoleHelper

#close_console, #console_connection_info, #console_credentials, #console_login, #console_tab_completion_data, #send_console_command, #start_local_console

Methods included from TunnelHelper

#display_tunnel_connection_info, #grab_ephemeral_port, #invalidate_tunnel_app_info, #pick_tunnel_port, #push_caldecott, #resolve_symbols, #start_caldecott, #start_local_prog, #start_tunnel, #stop_caldecott, #tunnel_app_info, #tunnel_appname, #tunnel_auth, #tunnel_bound?, #tunnel_connection_info, #tunnel_healthy?, #tunnel_pushed?, #tunnel_uniquename, #tunnel_url, #wait_for_tunnel_end, #wait_for_tunnel_start

Methods included from ManifestHelper

#bind_services, #configure_app, #configure_service, #create_services, #detect_framework, #detect_runtime, #each_app, #interact, #save_manifest, #set, #target_manifest

Methods included from ServicesHelper

#bind_service_banner, #check_app_for_restart, #create_service_banner, #delete_service_banner, #display_provisioned_services, #display_provisioned_services_table, #display_system_services, #generate_cloned_service_name, #random_service_name, #unbind_service_banner

Methods inherited from Base

#auth_token, #client, #client_info, #default_infra, #find_in_hash, #find_symbol, #frameworks_info, #initialize, #load_manifest, #load_manifest_structure, #manifest, #manifest_file, #merge_manifest, #merge_parent, #resolve_in, #resolve_lexically, #resolve_manifest, #resolve_symbol, #runtimes_info, #target_base, #target_url

Constructor Details

This class inherits a constructor from VMC::Cli::Command::Base

Instance Method Details

#clone(src_appname, dest_appname, dest_infra = nil) ⇒ Object



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
# File 'lib/cli/commands/apps.rb', line 230

def clone(src_appname, dest_appname, dest_infra=nil)

  if (@options[:label])
    label = @options[:label]
  else
    label = ''
  end

  # 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, label)

    # 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

#console(appname, interactive = true) ⇒ Object



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
# File 'lib/cli/commands/apps.rb', line 59

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

#crashes(appname, print_results = true, since = 0) ⇒ Object



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
# File 'lib/cli/commands/apps.rb', line 316

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

#crashlogs(appname) ⇒ Object



351
352
353
354
# File 'lib/cli/commands/apps.rb', line 351

def crashlogs(appname)
  instance = @options[:instance] || '0'
  grab_crash_logs(appname, instance)
end

#delete(appname = nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/cli/commands/apps.rb', line 192

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

#diff(appname) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/cli/commands/apps.rb', line 480

def diff(appname)
  if app_exists?(appname)
    diff = client.app_diff(appname)[0]
    return display "No diff available" if diff.nil? or diff.empty?
    hash = hash_app_bits(@path)

    comp = (hash == diff[:update_hash])
    comparison = comp ? "Deployed app '" + appname + "' matches app in current directory" : "Deployed app '" + appname + "' does not match app in current directory"
    display comparison
  else
    display "No application named '" + appname + "' found"
  end
end

#download(appname, path = nil) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/cli/commands/apps.rb', line 214

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

#environment(appname) ⇒ Object



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/cli/commands/apps.rb', line 494

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

#environment_add(appname, k, v = nil) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
# File 'lib/cli/commands/apps.rb', line 510

def environment_add(appname, k, v=nil)
  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'
end

#environment_del(appname, variable) ⇒ Object



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/cli/commands/apps.rb', line 522

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'
  else
    display 'OK'.green
  end
end

#files(appname, path = '/') ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/cli/commands/apps.rb', line 205

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

#hash(path = nil) ⇒ Object



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
# File 'lib/cli/commands/apps.rb', line 454

def hash(path=nil)
  if (@options[:full])
    full = true
  else
    full = false
  end

  current_dir = false
  if not path
    current_dir = true
    path = @path
  end

  hash = hash_app_bits(File.expand_path(path))

  if full
    display hash.to_s
  else
    if current_dir
      display "The hash for the current directory is: " + hash.to_s[0..9]
    else
      display "The hash for " + path + " is: " + hash.to_s[0..9]
    end
  end
end

#history(appname) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/cli/commands/apps.rb', line 434

def history(appname)
  if app_exists?(appname)
    history = client.app_history(appname)

    # return display JSON.pretty_generate(history) if @options[:json]
    return display "No history available" if history.empty?
    history_table = table do |t|
      t.headings = 'Label', 'Release ', 'By User', 'Release Date', 'Hash', 'Changed'
      history.each do |app|
        a = [app[:label], "v" << app[:release].to_s, app[:updated_by], Time.parse(app[:updated_at]).to_time, app[:update_hash][0..9], app[:is_changed]==true ? "Yes" : "No"]
        t << a
      end
    end
    display "\n"
    display history_table
  else
    display "No application named '" + appname + "' found"
  end
end

#info(what, default = nil) ⇒ Object



55
56
57
# File 'lib/cli/commands/apps.rb', line 55

def info(what, default=nil)
  @options[what] || (@app_info && @app_info[what.to_s]) || default
end

#instances(appname, num = nil) ⇒ Object



356
357
358
359
360
361
362
# File 'lib/cli/commands/apps.rb', line 356

def instances(appname, num=nil)
  if num
    change_instances(appname, num)
  else
    get_instances(appname)
  end
end

#listObject Also known as: apps



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cli/commands/apps.rb', line 20

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

#logs(appname) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/cli/commands/apps.rb', line 308

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

#map(appname, url) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/cli/commands/apps.rb', line 172

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

#mem(appname, memsize = nil) ⇒ Object



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
# File 'lib/cli/commands/apps.rb', line 140

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

#pull(appname, path = nil) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/cli/commands/apps.rb', line 222

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

#push(appname = nil) ⇒ Object



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
# File 'lib/cli/commands/apps.rb', line 403

def push(appname=nil)
  if (@options[:label])
    label = @options[:label]
  else
    label = ''
  end

  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(label, name)
    pushed = true
  end

  unless pushed
    @application = @path
    do_push(label, appname)
  end
end

#rename(oldname, newname) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/cli/commands/apps.rb', line 545

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

#restart(appname = nil) ⇒ Object



135
136
137
138
# File 'lib/cli/commands/apps.rb', line 135

def restart(appname=nil)
  stop(appname)
  start(appname)
end

#start(appname = nil, push = false) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/cli/commands/apps.rb', line 110

def start(appname=nil, push=false)
  if appname
    do_start(appname, push)
  else
    each_app do |name|
      do_start(name, push)
    end
  end
end

#stats(appname = nil) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
# File 'lib/cli/commands/apps.rb', line 364

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

#stop(appname = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cli/commands/apps.rb', line 120

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

#unmap(appname, url) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/cli/commands/apps.rb', line 181

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

#update(appname = nil) ⇒ Object



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
# File 'lib/cli/commands/apps.rb', line 376

def update(appname=nil)
  if (@options[:label])
    label = @options[:label]
  else
    label = ''
  end

  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, label)
    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, label)
      restart name if app[:state] == 'STARTED'
    end
  end
end