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



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

def deploy
  EY.ui.info "Loading application data from EY Cloud..."

  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[:force_ref]) ||
                    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[:force_ref]) ||
                    repo.current_branch ||
                    raise(DeployArgumentError)
                end

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

  deploy_options = {'extras' => {'deployed_by' => api.user.name}.merge(options[:extra_deploy_hook_options])}
  if options.has_key?('migrate') # thor set migrate => nil when --no-migrate
    deploy_options['migrate'] = options['migrate'].respond_to?(:to_str) ? options['migrate'] : !!options['migrate']
  end
  deploy_options['verbose'] = options['verbose'] if options.has_key?('verbose')


  if environment.deploy(app, deploy_ref, deploy_options)
    EY.ui.info "Deploy complete"
    EY.ui.info "Now you can run `ey launch' to open the application in a browser."
  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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/engineyard/cli.rb', line 118

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 EY Cloud:\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 + "\nUse #{self.class.send(:banner_base)} environments --all to see all environments.")
    end

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

#help(*cmds) ⇒ Object



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

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

#launchObject



349
350
351
352
# File 'lib/engineyard/cli.rb', line 349

def launch
  environment = fetch_environment(options[:environment], options[:account])
  environment.launch
end

#loginObject



361
362
363
# File 'lib/engineyard/cli.rb', line 361

def 
  whoami
end

#logoutObject



366
367
368
369
370
371
372
373
374
# File 'lib/engineyard/cli.rb', line 366

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

#logsObject



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

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



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

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

#rollbackObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/engineyard/cli.rb', line 179

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:



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/engineyard/cli.rb', line 218

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



100
101
102
103
104
105
106
107
108
# File 'lib/engineyard/cli.rb', line 100

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



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

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

#whoamiObject



355
356
357
358
# File 'lib/engineyard/cli.rb', line 355

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