Class: Stemcell::Launcher
- Inherits:
-
Object
- Object
- Stemcell::Launcher
- 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', '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', 'security_groups', 'security_group_ids', 'tags', 'classic_link', '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
-
#initialize(opts = {}) ⇒ Launcher
constructor
A new instance of Launcher.
- #kill(instances, opts = {}) ⇒ Object
- #launch(opts = {}) ⇒ Object
-
#render_template(opts = {}) ⇒ Object
this is made public for ec2admin usage.
Constructor Details
#initialize(opts = {}) ⇒ Launcher
Returns a new instance of Launcher.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/stemcell/launcher.rb', line 66 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 configure_aws_creds_and_region end |
Instance Method Details
#kill(instances, opts = {}) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/stemcell/launcher.rb', line 230 def kill(instances, opts={}) return if !instances || instances.empty? errors = run_batch_operation(instances) do |instance| begin @log.warn "Terminating instance #{instance.id}" instance.terminate nil # nil == success rescue AWS::EC2::Errors::InvalidInstanceID::NotFound => e opts[:ignore_not_found] ? nil : e end end check_errors(:kill, instances.map(&:id), errors) end |
#launch(opts = {}) ⇒ Object
86 87 88 89 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 |
# File 'lib/stemcell/launcher.rb', line 86 def launch(opts={}) (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 specefied as inputs = { '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 ['Name'] = opts['chef_role'] if opts['chef_environment'] == 'production' .merge!(opts['tags']) if opts['tags'] # generate launch options = { :image_id => opts['image_id'], :instance_type => opts['instance_type'], :key_name => opts['key_name'], :count => opts['count'], } if opts['security_group_ids'] && !opts['security_group_ids'].empty? [:security_group_ids] = opts['security_group_ids'] end if opts['security_groups'] && !opts['security_groups'].empty? if @vpc_id # convert sg names to sg ids as VPC only accepts ids security_group_ids = get_vpc_security_group_ids(@vpc_id, opts['security_groups']) [:security_group_ids] ||= [] [:security_group_ids].concat(security_group_ids) else [:security_groups] = opts['security_groups'] end end # specify availability zone (optional) if opts['availability_zone'] [:availability_zone] = opts['availability_zone'] end if opts['subnet'] [:subnet] = opts['subnet'] end if opts['private_ip_address'] [:private_ip_address] = opts['private_ip_address'] end if opts['dedicated_tenancy'] [:dedicated_tenancy] = opts['dedicated_tenancy'] end if opts['associate_public_ip_address'] [:associate_public_ip_address] = opts['associate_public_ip_address'] end # specify IAM role (optional) if opts['iam_role'] [:iam_instance_profile] = 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) [:ebs_optimized] = true if opts['ebs_optimized'] # specify placement group (optional) if opts['instance_initiated_shutdown_behavior'] [:instance_initiated_shutdown_behavior] = opts['instance_initiated_shutdown_behavior'] end # specify raw block device mappings (optional) if opts['block_device_mappings'] [:block_device_mappings] = opts['block_device_mappings'] end # specify ephemeral block device mappings (optional) if opts['ephemeral_devices'] [:block_device_mappings] ||= [] opts['ephemeral_devices'].each_with_index do |device,i| [:block_device_mappings].push ({ :device_name => device, :virtual_name => "ephemeral#{i}" }) end end # generate user data script to bootstrap instance, include in launch # options UNLESS we have manually set the user-data (ie. for ec2admin) [:user_data] = opts.fetch('user_data', render_template(opts)) # launch instances instances = do_launch() # everything from here on out must succeed, or we kill the instances we just launched begin # set tags on all instances launched (instances, ) @log.info "sent ec2 api tag requests successfully" # link to classiclink unless @vpc_id set_classic_link(instances, opts['classic_link']) @log.info "successfully applied classic link settings (if any)" end # turn on termination protection # we do this now to make sure all other settings worked if opts['termination_protection'] enable_termination_protection(instances) @log.info "successfully enabled termination protection" end # wait for aws to report instance stats if opts.fetch('wait', true) wait(instances) 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.}" end raise e end return instances end |
#render_template(opts = {}) ⇒ Object
this is made public for ec2admin usage
246 247 248 249 250 251 252 253 254 |
# File 'lib/stemcell/launcher.rb', line 246 def render_template(opts={}) template_file_path = File.(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 "genereated template is #{generated_template}" return generated_template end |