Class: Stemcell::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/stemcell/launcher.rb

Constant Summary collapse

REQUIRED_OPTIONS =
[
  'region',
]
REQUIRED_LAUNCH_PARAMETERS =
[
  'chef_role',
  'chef_environment',
  'chef_data_bag_secret',
  'git_branch',
  'git_key',
  'git_origin',
  'key_name',
  'instance_type',
  'image_id',
  'availability_zone',
  'count'
]
LAUNCH_PARAMETERS =
[
  'chef_package_source',
  'chef_version',
  'chef_role',
  'chef_environment',
  'chef_data_bag_secret',
  'chef_data_bag_secret_path',
  'cpu_options',
  'git_branch',
  'git_key',
  'git_origin',
  'key_name',
  'instance_type',
  'instance_hostname',
  'instance_domain_name',
  'image_id',
  'availability_zone',
  'vpc_id',
  'subnet',
  'private_ip_address',
  'dedicated_tenancy',
  'associate_public_ip_address',
  'count',
  'min_count',
  'max_count',
  'security_groups',
  'security_group_ids',
  'tags',
  'iam_role',
  'ebs_optimized',
  'termination_protection',
  'block_device_mappings',
  'ephemeral_devices',
  'placement_group'
]
TEMPLATE_PATH =
'../templates/bootstrap.sh.erb'
LAST_BOOTSTRAP_LINE =
"Stemcell bootstrap finished successfully!"
MAX_RUNNING_STATE_WAIT_TIME =

seconds

300
RUNNING_STATE_WAIT_SLEEP_TIME =

seconds

5

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Launcher

Returns a new instance of Launcher.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/stemcell/launcher.rb', line 69

def initialize(opts={})
  @log = Logger.new(STDOUT)
  @log.level = Logger::INFO unless ENV['DEBUG']
  @log.debug "creating new stemcell object"
  @log.debug "opts are #{opts.inspect}"

  REQUIRED_OPTIONS.each do |opt|
    raise ArgumentError, "missing required option 'region'" unless opts[opt]
  end

  @region = opts['region']
  @vpc_id = opts['vpc_id']
  @ec2_endpoint = opts['ec2_endpoint']
  @aws_access_key = opts['aws_access_key']
  @aws_secret_key = opts['aws_secret_key']
  @aws_session_token = opts['aws_session_token']
  @max_attempts = opts['max_attempts'] || 3
  @bootstrap_template_path = opts['bootstrap_template_path']
  configure_aws_creds_and_region
end

Instance Method Details

#kill(instance_ids, opts = {}) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/stemcell/launcher.rb', line 245

def kill(instance_ids, opts={})
  return if !instance_ids || instance_ids.empty?

  @log.warn "Terminating instances #{instance_ids}"
  ec2.terminate_instances(instance_ids: instance_ids)
  nil # nil == success
rescue Aws::EC2::Errors::InvalidInstanceIDNotFound => e
  raise unless opts[:ignore_not_found]

  invalid_ids = e.message.scan(/i-[a-z0-9]+/)
  instance_ids -= invalid_ids
  retry unless instance_ids.empty? || invalid_ids.empty? # don't retry if we couldn't find any instance ids
end

#launch(opts = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
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
129
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
169
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/stemcell/launcher.rb', line 90

def launch(opts={})
  verify_required_options(opts, REQUIRED_LAUNCH_PARAMETERS)

  # attempt to accept keys as file paths
  opts['git_key'] = try_file(opts['git_key'])
  opts['chef_data_bag_secret'] = try_file(opts['chef_data_bag_secret'])

  # generate tags and merge in any that were specified as inputs
  tags = {
    'Name' => "#{opts['chef_role']}-#{opts['chef_environment']}",
    'Group' => "#{opts['chef_role']}-#{opts['chef_environment']}",
    'created_by' => opts.fetch('user', ENV['USER']),
    'stemcell' => VERSION,
  }
  # Short name if we're in production
  tags['Name'] = opts['chef_role'] if opts['chef_environment'] == 'production'
  tags.merge!(opts['tags']) if opts['tags']

  #  Min/max number of instances to launch
  min_count = opts['min_count'] || opts['count']
  max_count = opts['max_count'] || opts['count']

  # generate launch options
  launch_options = {
    :image_id => opts['image_id'],
    :instance_type => opts['instance_type'],
    :key_name => opts['key_name'],
    :min_count => min_count,
    :max_count => max_count,
  }

  # Associate Public IP can only bet set on network_interfaces, and if present
  # security groups and subnet should be set on the interface. VPC-only.
  # Primary network interface
  network_interface = {
    device_index: 0,
  }
  launch_options[:network_interfaces] = [network_interface]

  if opts['security_group_ids'] && !opts['security_group_ids'].empty?
    network_interface[:groups] = opts['security_group_ids']
  end

  if opts['security_groups'] && !opts['security_groups'].empty?
    # convert sg names to sg ids as VPC only accepts ids
    security_group_ids = get_vpc_security_group_ids(@vpc_id, opts['security_groups'])
    network_interface[:groups] ||= []
    network_interface[:groups].concat(security_group_ids)
  end

  launch_options[:placement] = placement = {}
  # specify availability zone (optional)
  if opts['availability_zone']
    placement[:availability_zone] = opts['availability_zone']
  end

  if opts['subnet']
    network_interface[:subnet_id] = opts['subnet']
  end

  if opts['private_ip_address']
    launch_options[:private_ip_address] = opts['private_ip_address']
  end

  if opts['dedicated_tenancy']
    placement[:tenancy] = 'dedicated'
  end

  if opts['associate_public_ip_address']
    network_interface[:associate_public_ip_address] = opts['associate_public_ip_address']
  end

  # specify IAM role (optional)
  if opts['iam_role']
    launch_options[:iam_instance_profile] = {
      name: opts['iam_role']
    }
  end

  # specify placement group (optional)
  if opts['placement_group']
    placement[:group_name] = opts['placement_group']
  end

  # specify an EBS-optimized instance (optional)
  launch_options[:ebs_optimized] = true if opts['ebs_optimized']

  # specify placement group (optional)
  if opts['instance_initiated_shutdown_behavior']
    launch_options[:instance_initiated_shutdown_behavior] =
      opts['instance_initiated_shutdown_behavior']
  end

  # specify raw block device mappings (optional)
  if opts['block_device_mappings']
    launch_options[:block_device_mappings] = opts['block_device_mappings']
  end

  # specify cpu options (optional)
  if opts['cpu_options']
    launch_options[:cpu_options] = opts['cpu_options']
  end

  # specify ephemeral block device mappings (optional)
  if opts['ephemeral_devices']
    launch_options[:block_device_mappings] ||= []
    opts['ephemeral_devices'].each_with_index do |device,i|
      launch_options[:block_device_mappings].push ({
        :device_name => device,
        :virtual_name => "ephemeral#{i}"
      })
    end
  end

  if opts['termination_protection']
    launch_options[:disable_api_termination] = true
  end

  # generate user data script to bootstrap instance, include in launch
  # options UNLESS we have manually set the user-data (ie. for ec2admin)
  launch_options[:user_data] = Base64.encode64(opts.fetch('user_data', render_template(opts)))

  # add tags to launch options so we don't need to make a separate CreateTags call
  launch_options[:tag_specifications] = [{
    resource_type: 'instance',
    tags: tags.map { |k, v| { key: k, value: v } }
  }]

  # launch instances
  instances = do_launch(launch_options)

  # everything from here on out must succeed, or we kill the instances we just launched
  begin
    # wait for aws to report instance stats
    if opts.fetch('wait', true)
      instance_ids = instances.map(&:instance_id)
      @log.info "Waiting up to #{MAX_RUNNING_STATE_WAIT_TIME} seconds for #{instances.count} " \
            "instance(s): (#{instance_ids})"
      instances = wait(instance_ids)
      print_run_info(instances)
      @log.info "launched instances successfully"
    end
  rescue => e
    @log.info "launch failed, killing all launched instances"
    begin
      kill(instances, :ignore_not_found => true)
    rescue => kill_error
      @log.warn "encountered an error during cleanup: #{kill_error.message}"
    end
    raise e
  end

  return instances
end

#render_template(opts = {}) ⇒ Object

this is made public for ec2admin usage



260
261
262
263
264
265
266
267
268
# File 'lib/stemcell/launcher.rb', line 260

def render_template(opts={})
  template_file_path = @bootstrap_template_path || File.expand_path(TEMPLATE_PATH, __FILE__)
  template_file = File.read(template_file_path)
  erb_template = ERB.new(template_file)
  last_bootstrap_line = LAST_BOOTSTRAP_LINE
  generated_template = erb_template.result(binding)
  @log.debug "generated template is #{generated_template}"
  return generated_template
end