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::HELP_MAPPINGS, Thor::THOR_RESERVED_WORDS, 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?, default_task, desc, help, long_desc, map, method_option, method_options, printable_tasks, register, subcommand, subcommands, task_help

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)
  exit 1
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
# 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
    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
    err << "Interrupted. Deployment halted.\n"
    ui.warn "Recording canceled deployment in Engine Yard Cloud..."
    ui.warn "WARNING: Interrupting again may result in a never-finished deployment in the deployment history on Engine Yard Cloud."
    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



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

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



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

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



452
453
454
455
# File 'lib/engineyard/cli.rb', line 452

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

#loginObject



476
477
478
# File 'lib/engineyard/cli.rb', line 476

def 
  whoami
end

#logoutObject



481
482
483
484
485
486
487
488
489
# File 'lib/engineyard/cli.rb', line 481

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



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/engineyard/cli.rb', line 368

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



240
241
242
243
244
# File 'lib/engineyard/cli.rb', line 240

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

#rollbackObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/engineyard/cli.rb', line 266

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:



315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/engineyard/cli.rb', line 315

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



152
153
154
155
156
157
158
159
160
# File 'lib/engineyard/cli.rb', line 152

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

#versionObject



392
393
394
# File 'lib/engineyard/cli.rb', line 392

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

#whoamiObject



458
459
460
461
# File 'lib/engineyard/cli.rb', line 458

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