Class: Fum::Commands::Launch

Inherits:
Fum::Command show all
Includes:
DNS
Defined in:
lib/fum/commands/launch.rb

Instance Method Summary collapse

Methods included from DNS

#dns, #dns_names_equal, #ensure_trailing_dot, #find_records, #update_zone, #update_zones

Methods inherited from Fum::Command

#initialize, #stage

Methods included from Util

#die

Constructor Details

This class inherits a constructor from Fum::Command

Instance Method Details

#execute(options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'lib/fum/commands/launch.rb', line 21

def execute(options)
  app = @application.main_decl

  stage_name = options[:stage_name]
  stage_decl = stage(@application.main_decl, stage_name)

  if options[:verbose]
    puts "Verifying application '#{app.name}' exists in AWS."
  end

  beanstalk = Fog::AWS[:beanstalk]

  beanstalk_app = beanstalk.applications.get(app.name)

  if beanstalk_app.nil?
    if options[:create]
      beanstalk_app = beanstalk.applications.create(:name => app.name)
      beanstalk.versions.create({
          :application_name => app.name,
          :label => 'Sample'
                                })
    else
      die "Could not find app '#{app.name}' in AWS account."
    end

  end

  env_opt = {
      :application_name => app.name,
      :name => stage_decl.environment_name
  }
  env_opt[:template_name] = stage_decl.template_name unless stage_decl.template_name.nil?
  env_opt[:solution_stack_name] = stage_decl.solution_stack_name unless stage_decl.solution_stack_name.nil?
  env_opt[:description] = stage_decl.env_description unless stage_decl.env_description.nil?

  set_version(stage_decl, env_opt, options)

  if options[:verbose] || options[:noop]
    puts "Launching an environment named '#{stage_decl.environment_name}'"
  end

  begin
    new_env = beanstalk.environments.create(env_opt) unless options[:noop]
  rescue Exception => e
    die "Problem creating environment: #{e.to_s}"
  end

  puts "Launched environment '#{stage_decl.environment_name}'."

  puts "Waiting for environment to become ready..."

  new_env.wait_for { ready? } unless options[:noop]

  puts "New environment is ready."

  puts "Waiting for environment to become healthy..."

  new_env.wait_for { healthy? } unless options[:noop]

  puts "New environment is healthy."

  update_zones(stage_decl, new_env, options) unless options[:no_dns]

end

#parse_optionsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fum/commands/launch.rb', line 6

def parse_options
  options = Trollop::options do
    banner "usage launch [options] <environment-id>, where options are:"
    opt :version_label, "Launch the specified version", :long => "version", :type => :string
    opt :create, "Create application if it does not exist"
    opt :no_dns, "Do not change any DNS records", :default => false
    opt :no_wait, "Launch environment, but do not wait for ready", :default => false, :short => 'w'
  end
  if ARGV.empty?
    die "Stage not specified.  Please specify a stage to launch"
  end
  options[:stage_name] = ARGV.shift
  options
end

#set_version(stage_decl, env_opt, options) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fum/commands/launch.rb', line 86

def set_version(stage_decl, env_opt, options)
  version_label = stage_decl.version_label
  return nil if version_label.nil?

  if version_label.is_a?(String)
    env_opt[:version_label] = version_label
    puts "Using verison label #{version_label}"
  elsif version_label.is_a?(Hash) && version_label.has_key?(:from_stage)
    from_stage = version_label[:from_stage]
    from_stage_decl = stage(@application.main_decl, from_stage)
    analyzer = StageAnalyzer.new(from_stage_decl)
    analyzer.analyze(options)
    active = analyzer.active
    die "Cannot determine version to launch. No active environment for stage '#{from_stage}' specified." if active.nil?
    env_opt[:version_label] = active.version_label
    puts "Using version #{active.version_label} from stage #{from_stage}."
  else
    "Unknown version label #{version_label.inspect}"
  end


end