Class: Stemcell::Launcher
- Inherits:
-
Object
- Object
- Stemcell::Launcher
- Defined in:
- lib/stemcell/launcher.rb
Constant Summary collapse
- REQUIRED_OPTIONS =
[ 'aws_access_key', 'aws_secret_key', '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', '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', 'iam_role', 'ebs_optimized', 'block_device_mappings', 'ephemeral_devices', 'placement_group' ]
- TEMPLATE_PATH =
'../templates/bootstrap.sh.erb'- LAST_BOOTSTRAP_LINE =
"Stemcell bootstrap finished successfully!"
Instance Method Summary collapse
- #find_instance(id) ⇒ Object
-
#initialize(opts = {}) ⇒ Launcher
constructor
A new instance of Launcher.
- #kill(instance_ids, 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 85 86 87 88 89 90 91 |
# 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 |req| raise ArgumentError, "missing required param #{req}" unless opts[req] instance_variable_set("@#{req}",opts[req]) end @ec2_url = "ec2.#{@region}.amazonaws.com" @timeout = 300 @start_time = Time.new AWS.config({ :access_key_id => @aws_access_key, :secret_access_key => @aws_secret_key}) if opts['vpc_id'] puts 'using vpc tho' @ec2 = AWS::VPC.new(opts['vpc_id'], :ec2_endpoint => @ec2_url) else @ec2 = AWS::EC2.new(:ec2_endpoint => @ec2_url) end end |
Instance Method Details
#find_instance(id) ⇒ Object
211 212 213 |
# File 'lib/stemcell/launcher.rb', line 211 def find_instance(id) return @ec2.instances[id] end |
#kill(instance_ids, opts = {}) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/stemcell/launcher.rb', line 215 def kill(instance_ids, opts={}) return if instance_ids.nil? errors = run_batch_operation(instance_ids) do |id| begin instance = find_instance(id) @log.warn "Terminating instance #{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, instance_ids, errors) end |
#launch(opts = {}) ⇒ Object
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 |
# File 'lib/stemcell/launcher.rb', line 94 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_groups'] && !opts['security_groups'].empty? [:security_groups] = opts['security_groups'] end if opts['security_group_ids'] && !opts['security_group_ids'].empty? [:security_group_ids] = opts['security_group_ids'] 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 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() # set tags on all instances launched begin (instances, ) @log.info "sent ec2 api requests successfully" # 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.message}" end raise e end return instances end |
#render_template(opts = {}) ⇒ Object
this is made public for ec2admin usage
232 233 234 235 236 237 238 239 240 |
# File 'lib/stemcell/launcher.rb', line 232 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 |