Class: EY::CLI

Inherits:
Thor show all
Includes:
Thor::Actions
Defined in:
lib/engineyard/cli.rb,
lib/engineyard/cli/ui.rb,
lib/engineyard/cli/api.rb,
lib/engineyard/cli/web.rb,
lib/engineyard/cli/recipes.rb

Defined Under Namespace

Classes: API, Recipes, UI, Web

Constant Summary

Constants inherited from Thor

Thor::DynamicTask, Thor::HELP_MAPPINGS, Thor::HiddenTask, Thor::THOR_RESERVED_WORDS, Thor::UndefinedTaskError, Thor::VERSION

Instance Attribute Summary

Attributes included from Thor::Base

#args, #options, #parent_options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Thor

check_unknown_options!, check_unknown_options?, command_help, default_command, desc, help, long_desc, map, method_option, method_options, package_name, printable_commands, register, stop_on_unknown_option!, stop_on_unknown_option?, subcommand, subcommands

Methods included from Thor::Base

included, #initialize, register_klass_file, shell, shell=, subclass_files, subclasses

Class Method Details

.start(given_args = ARGV, config = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/engineyard/cli.rb', line 19

def self.start(given_args=ARGV, config={})
  Thor::Base.shell = EY::CLI::UI
  ui = EY::CLI::UI.new
  super(given_args, {:shell => ui}.merge(config))
rescue EY::Error, EY::CloudClient::Error => e
  ui.print_exception(e)
  raise
rescue Interrupt => e
  puts
  ui.print_exception(e)
  ui.say("Quitting...")
  raise
rescue SystemExit, Errno::EPIPE
  # don't print a message for safe exits
  raise
rescue Exception => e
  ui.print_exception(e)
  raise
end

Instance Method Details

#deployObject



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
# File 'lib/engineyard/cli.rb', line 80

def deploy
  app_env = fetch_app_environment(options[:app], options[:environment], options[:account])

  env_config    = config.environment_config(app_env.environment_name)
  deploy_config = EY::DeployConfig.new(options, env_config, repo, ui)

  deployment = app_env.new_deployment({
    :ref                => deploy_config.ref,
    :migrate            => deploy_config.migrate,
    :migrate_command    => deploy_config.migrate_command,
    :extra_config       => deploy_config.extra_config,
    :serverside_version => serverside_version,
  })

  runner = serverside_runner(app_env, deploy_config.verbose, deployment.serverside_version, options[:ignore_bad_master])

  out = EY::CLI::UI::Tee.new(ui.out, deployment.output)
  err = EY::CLI::UI::Tee.new(ui.err, deployment.output)

  ui.info  "Beginning deploy...", :green
  begin
    deployment.start
  rescue
    ui.error "Error encountered before deploy. Deploy not started."
    raise
  end

  begin
    ui.show_deployment(deployment)
    out << "Deploy initiated.\n"

    runner.deploy do |args|
      args.config  = deployment.config          if deployment.config
      if deployment.migrate
        args.migrate = deployment.migrate_command
      else
        args.migrate = false
      end
      args.ref     = deployment.resolved_ref
    end
    deployment.successful = runner.call(out, err)
  rescue Interrupt
    Signal.trap(:INT) { # The fingers you have used to dial are too fat...
      ui.info "\nRun `ey timeout-deploy` to mark an unfinished deployment as failed."
      exit 1
    }
    err << "Interrupted. Deployment halted.\n"
    ui.warn <<-WARN
Recording interruption of this unfinished deployment in Engine Yard Cloud...

WARNING: Interrupting again may prevent Engine Yard Cloud from recording this
     failed deployment. Unfinished deployments can block future deploys.
    WARN
    raise
  rescue StandardError => e
    deployment.err << "Error encountered during deploy.\n#{e.class} #{e}\n"
    ui.print_exception(e)
    raise
  ensure
    ui.info "Saving log... ", :green
    deployment.finished

    if deployment.successful?
      ui.info "Successful deployment recorded on Engine Yard Cloud.", :green
      ui.info "Run `ey launch` to open the application in a browser."
    else
      ui.info "Failed deployment recorded on Engine Yard Cloud", :green
      raise EY::Error, "Deploy failed"
    end
  end
end

#environmentsObject



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
# File 'lib/engineyard/cli.rb', line 233

def environments
  if options[:all] && options[:simple]
    ui.print_simple_envs api.environments
  elsif options[:all]
    ui.print_envs api.apps
  else
    remotes = nil
    if options[:app] == ''
      repo.fail_on_no_remotes!
      remotes = repo.remotes
    end

    resolver = api.resolve_app_environments({
      :account_name     => options[:account],
      :app_name         => options[:app],
      :environment_name => options[:environment],
      :remotes          => remotes,
    })

    resolver.no_matches do |errors|
      messages = errors
      messages << "Use #{self.class.send(:banner_base)} environments --all to see all environments."
      raise EY::NoMatchesError.new(messages.join("\n"))
    end

    apps = resolver.matches.map { |app_env| app_env.app }.uniq

    if options[:simple]
      if apps.size > 1
        message = "# This app matches multiple Applications in Engine Yard Cloud:\n"
        apps.each { |app| message << "#\t#{app.name}\n" }
        message << "# The following environments contain those applications:\n\n"
        ui.warn(message)
      end
      ui.print_simple_envs(apps.map{ |app| app.environments }.flatten)
    else
      ui.print_envs(apps, config.default_environment)
    end
  end
end

#help(*cmds) ⇒ Object



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
# File 'lib/engineyard/cli.rb', line 450

def help(*cmds)
  if cmds.empty?
    base = self.class.send(:banner_base)
    list = self.class.printable_tasks

    ui.say "Usage:"
    ui.say "  #{base} [--help] [--version] COMMAND [ARGS]"
    ui.say

    ui.say "Deploy commands:"
    deploy_cmds = %w(deploy environments logs rebuild rollback status)
    deploy_cmds.map! do |name|
      list.find{|task| task[0] =~ /^#{base} #{name}/ }
    end
    list -= deploy_cmds

    ui.print_help(deploy_cmds)
    ui.say

    self.class.subcommands.each do |name|
      klass = self.class.subcommand_class_for(name)
      list.reject!{|cmd| cmd[0] =~ /^#{base} #{name}/}
      ui.say "#{name.capitalize} commands:"
      tasks = klass.printable_tasks.reject{|t| t[0] =~ /help$/ }
      ui.print_help(tasks)
      ui.say
    end

    %w(help version).each{|n| list.reject!{|c| c[0] =~ /^#{base} #{n}/ } }
    if list.any?
      ui.say "Other commands:"
      ui.print_help(list)
      ui.say
    end

    self.class.send(:class_options_help, shell)
    ui.say "See '#{base} help COMMAND' for more information on a specific command."
  elsif klass = self.class.subcommand_class_for(cmds.first)
    klass.new.help(*cmds[1..-1])
  else
    super
  end
end

#launchObject



504
505
506
507
# File 'lib/engineyard/cli.rb', line 504

def launch
  app_env = fetch_app_environment(options[:app], options[:environment], options[:account])
  Launchy.open(app_env.uri)
end

#loginObject



528
529
530
# File 'lib/engineyard/cli.rb', line 528

def 
  whoami
end

#logoutObject



533
534
535
536
537
538
539
540
541
# File 'lib/engineyard/cli.rb', line 533

def logout
  eyrc = EYRC.load
  if eyrc.delete_api_token
    ui.info "API token removed: #{eyrc.path}"
    ui.info "Run any other command to login again."
  else
    ui.info "Already logged out. Run any other command to login again."
  end
end

#logsObject



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/engineyard/cli.rb', line 420

def logs
  environment = fetch_environment(options[:environment], options[:account])
  environment.logs.each do |log|
    ui.say "Instance: #{log.instance_name}"

    if log.main
      ui.say "Main logs for #{environment.name}:", :green
      ui.say  log.main
    end

    if log.custom
      ui.say "Custom logs for #{environment.name}:", :green
      ui.say  log.custom
    end
  end
end

#rebuildObject



292
293
294
295
296
# File 'lib/engineyard/cli.rb', line 292

def rebuild
  environment = fetch_environment(options[:environment], options[:account])
  ui.info "Updating instances on #{environment.hierarchy_name}"
  environment.rebuild
end

#rollbackObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/engineyard/cli.rb', line 318

def rollback
  app_env = fetch_app_environment(options[:app], options[:environment], options[:account])
  env_config    = config.environment_config(app_env.environment_name)
  deploy_config = EY::DeployConfig.new(options, env_config, repo, ui)

  ui.info "Rolling back #{app_env.hierarchy_name}"

  runner = serverside_runner(app_env, deploy_config.verbose)
  runner.rollback do |args|
    args.config = deploy_config.extra_config if deploy_config.extra_config
  end

  if runner.call(ui.out, ui.err)
    ui.info "Rollback complete"
  else
    raise EY::Error, "Rollback failed"
  end
end

#ssh(cmd = nil) ⇒ Object

Raises:



367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/engineyard/cli.rb', line 367

def ssh(cmd=nil)
  environment = fetch_environment(options[:environment], options[:account])
  hosts = ssh_hosts(options, environment)

  raise NoCommandError.new if cmd.nil? and hosts.size != 1

  exits = hosts.map do |host|
    system Escape.shell_command(['ssh', "#{environment.username}@#{host}", cmd].compact)
    $?.exitstatus
  end
  exit exits.detect {|status| !status.zero?} || 0
end

#statusObject



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

def status
  app_env = fetch_app_environment(options[:app], options[:environment], options[:account])
  deployment = app_env.last_deployment
  if deployment
    ui.deployment_status(deployment)
  else
    raise EY::Error, "Application #{app_env.app.name} has not been deployed on #{app_env.environment.name}."
  end
end

#timeout_deployObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/engineyard/cli.rb', line 173

def timeout_deploy
  app_env = fetch_app_environment(options[:app], options[:environment], options[:account])
  deployment = app_env.last_deployment
  if deployment && !deployment.finished?
    begin
      ui.info  "Marking last deployment failed...", :green
      deployment.timeout
      ui.deployment_status(deployment)
    rescue EY::CloudClient::RequestFailed => e
      ui.error "Error encountered attempting to timeout previous deployment."
      raise
    end
  else
    raise EY::Error, "No unfinished deployment was found for #{app_env.hierarchy_name}."
  end
end

#versionObject



444
445
446
# File 'lib/engineyard/cli.rb', line 444

def version
  ui.say %{engineyard version #{EY::VERSION}}
end

#whoamiObject



510
511
512
513
# File 'lib/engineyard/cli.rb', line 510

def whoami
  current_user = api.current_user
  ui.say "#{current_user.name} (#{current_user.email})"
end