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



47
48
49
50
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
80
81
82
83
84
85
# File 'lib/engineyard/cli.rb', line 47

def deploy
  app         = fetch_app(options[:app])
  environment = fetch_environment(options[:environment], app)
  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 "Connecting to the server..."

  loudly_check_eydeploy(environment)

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

  # missing means do what the yaml file says
  # nil means don't do it
  # true (the lazy default) means do it with the custom command
  # a string means do it with this specific command

  deploy_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



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/engineyard/cli.rb', line 95

def environments
  if options[:all] && options[:simple]
    # just put each env
    api.environments.each do |env|
      puts env.name
    end
  else
    apps = get_apps(options[:all])
    EY.ui.warn(NoAppError.new(repo).message) unless apps.any? || options[:all]
    EY.ui.print_envs(apps, EY.config.default_environment, options[:simple])
  end
end

#help(*cmds) ⇒ Object



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

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)
    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

    EY::Thor.subcommands.each do |name, klass|
      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 = EY::Thor.subcommands[cmds.first]
    klass.new.help(*cmds[1..-1])
  else
    super
  end
end

#logsObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/engineyard/cli.rb', line 204

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

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

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

#rebuildObject



122
123
124
125
126
# File 'lib/engineyard/cli.rb', line 122

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

#rollbackObject



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/engineyard/cli.rb', line 140

def rollback
  app = fetch_app(options[:app])
  env = fetch_environment(options[:environment], app)

  loudly_check_eydeploy(env)

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

#ssh(cmd = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/engineyard/cli.rb', line 170

def ssh(cmd=nil)
  env = fetch_environment(options[:environment])

  if options[:all]
    raise NoCommandError.new unless cmd

    hosts = env.instances.map do |instance|
      instance.public_hostname
    end

    if hosts.empty?
      raise NoInstancesError.new(env.name)
    else
      hosts.each do |host|
        system "ssh #{env.username}@#{host} #{cmd}"
      end
    end
  else
    if env.app_master
      system "ssh #{env.username}@#{env.app_master.public_hostname} #{cmd}"
    else
      raise NoAppMasterError.new(env.name)
    end
  end
end

#versionObject



228
229
230
# File 'lib/engineyard/cli.rb', line 228

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