Module: OpsWorks

Defined in:
lib/opsworks.rb

Instance Method Summary collapse

Instance Method Details

#create_and_launch_stack(stack_description) ⇒ Object



27
28
29
30
31
32
# File 'lib/opsworks.rb', line 27

def create_and_launch_stack(stack_description)
  stack_id = create_stack(stack_description)
  launch_stack(stack_id)
  deploy_apps(stack_id)
  stack_id
end

#create_and_launch_stack_in_order(stack_description, layers_by_order) ⇒ Object



34
35
36
37
38
39
# File 'lib/opsworks.rb', line 34

def create_and_launch_stack_in_order(stack_description, layers_by_order)
  stack_id = create_stack(stack_description)
  launch_stack_in_order(stack_id, layers_by_order)
  deploy_apps(stack_id)
  stack_id
end

#create_stack(stack_description) ⇒ Object



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
85
86
87
88
89
90
# File 'lib/opsworks.rb', line 41

def create_stack(stack_description)
  opsworks_client = AWS::OpsWorks::Client::V20130218.new

  response = opsworks_client.create_stack(stack_description.stack_description)
  stack_id = response[:stack_id]

  wait_on_opsworks_sg_creation(stack_description.stack_description[:vpc_id]) if stack_description.stack_description[:vpc_id]

  layer_descriptions = stack_description.layer_descriptions
  layer_descriptions.each do |layer_description|
    layer_description[:layer][:stack_id] = stack_id

    response = opsworks_client.create_layer(layer_description[:layer])
    layer_id = response[:layer_id]
    layer_description[:layer][:layer_id] = layer_id

    layer_description[:instances].each do |instance_description|
      instance_description[:stack_id] = stack_id
      instance_description[:layer_ids] = [layer_id]

      save_off = instance_description[:extra_layers]
      instance_description.delete :extra_layers
      response = opsworks_client.create_instance(instance_description)
      instance_description[:instance_id] = response[:instance_id]
      instance_description[:extra_layers] = save_off
    end
  end

  layer_descriptions.each do |layer_description|
    layer_description[:instances].each do |instance_description|
      if instance_description[:extra_layers]
        instance_description[:extra_layers].each do |extra_layer_name|
          found_extra_layer = layer_descriptions.find { |desc| desc[:layer][:name] == extra_layer_name }

          raise "missing extra layer: #{extra_layer_name}" unless found_extra_layer
          instance_description[:layer_ids] << found_extra_layer[:layer][:layer_id]
          response = opsworks_client.update_instance(:instance_id => instance_description[:instance_id],
                                                     :layer_ids => instance_description[:layer_ids])
        end
      end
    end
  end

  stack_description.app_descriptions.each do |app_description|
    app_description[:stack_id] = stack_id
    response = opsworks_client.create_app(app_description)
  end

  stack_id
end

#deploy_apps(stack_id) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/opsworks.rb', line 130

def deploy_apps(stack_id)
  deployment_ids = []
  opsworks_client = AWS::OpsWorks::Client::V20130218.new
  response = opsworks_client.describe_apps(:stack_id => stack_id)
  response[:apps].each do |app|

    puts "DEPLOYING THE APP: #{app}"
    response = opsworks_client.create_deployment(:stack_id => stack_id,
                                                 :app_id => app[:app_id],
                                                 :command => {
                                                   :name => 'deploy',
                                                   :args => deployment_args_factory(app)
                                                 })
    deployment_ids << response[:deployment_id]
  end

  max_attempts = 250
  num_attempts = 0

  deployments_complete = false
  until deployments_complete
    response = opsworks_client.describe_deployments(:deployment_ids => deployment_ids)
    deployments_complete = response[:deployments].inject(true) do |status, deployment|
      puts "Deployment status: #{deployment[:status]}"

      if deployment[:status] == 'failed'
        raise 'deployment failed'
      end

      status and (deployment[:status] == 'successful')
    end
    num_attempts += 1
    if num_attempts >= max_attempts
      raise 'stuck waiting on configure command max attempts'
    end
    sleep 10
  end
  deployment_ids
end

#discover_private_ips(stack_id, layer_name) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/opsworks.rb', line 170

def discover_private_ips(stack_id, layer_name)
  opsworks_client = AWS::OpsWorks::Client::V20130218.new
  response = opsworks_client.describe_layers( :stack_id => stack_id)
  layer = response[:layers].find { |layer| layer[:name] == layer_name }
  raise "layer #{layer_name} not found in stack: #{stack_id}" if layer.nil?

  response = opsworks_client.describe_instances( :layer_id => layer[:layer_id] )
  response[:instances].collect { |instance| instance[:private_ip] }
end

#discover_public_ips(stack_id, layer_name) ⇒ Object



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

def discover_public_ips(stack_id, layer_name)
  opsworks_client = AWS::OpsWorks::Client::V20130218.new
  response = opsworks_client.describe_layers( :stack_id => stack_id)
  layer = response[:layers].find { |layer| layer[:name] == layer_name }
  raise "layer #{layer_name} not found in stack: #{stack_id}" if layer.nil?

  response = opsworks_client.describe_instances( :layer_id => layer[:layer_id] )
  response[:instances].collect { |instance| instance[:public_ip] }
end

#launch_stack(stack_id) ⇒ Object



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

def launch_stack(stack_id)
  opsworks_client = AWS::OpsWorks::Client::V20130218.new

  opsworks_client.start_stack(:stack_id => stack_id)

  wait_on_setup(stack_id)

  wait_on_all_configures(stack_id)
end

#launch_stack_in_order(stack_id, layers_by_order) ⇒ Object



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
# File 'lib/opsworks.rb', line 103

def launch_stack_in_order(stack_id, layers_by_order)
  opsworks_client = AWS::OpsWorks::Client::V20130218.new

  layers_by_order.each do |layer_names|
    layer_names.each do |layer_name|

      response = opsworks_client.describe_layers( :stack_id => stack_id)
      layer = response[:layers].find { |layer| layer[:shortname] == layer_name }
      raise "layer #{layer_name} not found in stack: #{stack_id}" if layer.nil?

      response = opsworks_client.describe_instances( :layer_id => layer[:layer_id])
      response[:instances].each do |instance|
        opsworks_client.start_instance( :instance_id => instance[:instance_id])
      end
    end

    layer_names.each do |layer_name|
      response = opsworks_client.describe_layers( :stack_id => stack_id)
      layer = response[:layers].find { |layer| layer[:shortname] == layer_name }
      raise "layer #{layer_name} not found in stack: #{stack_id}" if layer.nil?

      wait_on_layer_setup(layer[:layer_id])
      wait_on_layer_configures(layer[:layer_id])
    end
  end
end