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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.startObject



14
15
16
17
18
# File 'lib/engineyard/cli.rb', line 14

def self.start(*)
  Thor::Base.shell = EY::CLI::UI
  EY.ui = EY::CLI::UI.new
  super
end

Instance Method Details

#deployObject



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

def deploy
  app, environment = fetch_app_and_environment(options[:app], options[:environment], options[:account])
  environment.ignore_bad_master = options[:ignore_bad_master]
  deploy_ref  = if options[:app]
                  environment.resolve_branch(options[:ref], options[:ignore_default_branch]) ||
                    raise(EY::Error, "When specifying the application, you must also specify the ref to deploy\nUsage: ey deploy --app <app name> --ref <branch|tag|ref>")
                else
                  environment.resolve_branch(options[:ref], options[:ignore_default_branch]) ||
                    repo.current_branch ||
                    raise(DeployArgumentError)
                end

  EY.ui.info "Beginning deploy for '#{app.name}' in '#{environment.name}' on server..."

  deploy_options = {'extras' => options[:extra_deploy_hook_options]}
  deploy_options['migrate'] = options['migrate'] if options.has_key?('migrate')
  deploy_options['verbose'] = options['verbose'] if options.has_key?('verbose')

  if environment.deploy(app, deploy_ref, deploy_options)
    EY.ui.info "Deploy complete"
  else
    raise EY::Error, "Deploy failed"
  end

rescue NoEnvironmentError => e
  # Give better feedback about why we couldn't find the environment.
  exists = api.environments.named(options[:environment])
  raise exists ? EnvironmentUnlinkedError.new(options[:environment]) : e
end

#environmentsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/engineyard/cli.rb', line 110

def environments
  if options[:all] && options[:simple]
    # just put each env
    puts api.environments.map {|env| env.name}
  elsif options[:all]
    EY.ui.print_envs(api.apps, EY.config.default_environment, options[:simple])
  else
    apps = api.apps_for_repo(repo)

    if apps.size > 1
      message = "This git repo matches multiple Applications in AppCloud:\n"
      apps.each { |app| message << "\t#{app.name}\n" }
      message << "The following environments contain those applications:\n\n"
      EY.ui.warn(message)
    elsif apps.empty?
      EY.ui.warn(NoAppError.new(repo).message)
    end

    EY.ui.print_envs(apps, EY.config.default_environment, options[:simple])
  end
end

#help(*cmds) ⇒ Object



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

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

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

    EY.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

    EY.ui.print_help(deploy_cmds)
    EY.ui.say

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

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

    self.class.send(:class_options_help, shell)
    EY.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

#logsObject



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/engineyard/cli.rb', line 260

def logs
  environment = fetch_environment(options[:environment], options[:account])
  environment.logs.each do |log|
    EY.ui.info log.instance_name

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

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

#rebuildObject



148
149
150
151
152
# File 'lib/engineyard/cli.rb', line 148

def rebuild
  environment = fetch_environment(options[:environment], options[:account])
  EY.ui.debug("Rebuilding #{environment.name}")
  environment.rebuild
end

#rollbackObject



171
172
173
174
175
176
177
178
179
180
# File 'lib/engineyard/cli.rb', line 171

def rollback
  app, environment = fetch_app_and_environment(options[:app], options[:environment], options[:account])

  EY.ui.info("Rolling back '#{app.name}' in '#{environment.name}'")
  if environment.rollback(app, options[:extra_deploy_hook_options], options[:verbose])
    EY.ui.info "Rollback complete"
  else
    raise EY::Error, "Rollback failed"
  end
end

#ssh(cmd = nil) ⇒ Object

Raises:



210
211
212
213
214
215
216
217
218
219
# File 'lib/engineyard/cli.rb', line 210

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

  hosts.each do |host|
    system Escape.shell_command(['ssh', "#{environment.username}@#{host}", cmd].compact)
  end
end

#statusObject



92
93
94
95
96
97
98
99
100
# File 'lib/engineyard/cli.rb', line 92

def status
  app, environment = fetch_app_and_environment(options[:app], options[:environment], options[:account])
  deployment = app.last_deployment_on(environment)
  if deployment
    EY.ui.show_deployment(deployment)
  else
    raise EY::Error, "Application #{app.name} hass not been deployed on #{environment.name}."
  end
end

#versionObject



284
285
286
# File 'lib/engineyard/cli.rb', line 284

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