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', 'security_groups', 'availability_zone', 'count' ]
- LAUNCH_PARAMETERS =
[ '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', 'count', 'security_groups', '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(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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/stemcell/launcher.rb', line 58 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}) @ec2 = AWS::EC2.new(:ec2_endpoint => @ec2_url) end |
Instance Method Details
#find_instance(id) ⇒ Object
165 166 167 |
# File 'lib/stemcell/launcher.rb', line 165 def find_instance(id) return @ec2.instances[id] end |
#kill(instances, opts = {}) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/stemcell/launcher.rb', line 169 def kill(instances,opts={}) return if instances.nil? instances.each do |i| begin instance = find_instance(i) @log.warn "Terminating instance #{instance.instance_id}" instance.terminate rescue AWS::EC2::Errors::InvalidInstanceID::NotFound => e throw e unless opts[:ignore_not_found] end end end |
#launch(opts = {}) ⇒ Object
81 82 83 84 85 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 |
# File 'lib/stemcell/launcher.rb', line 81 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, } .merge!(opts['tags']) if opts['tags'] # generate launch options = { :image_id => opts['image_id'], :security_groups => opts['security_groups'], :instance_type => opts['instance_type'], :key_name => opts['key_name'], :count => opts['count'], } # specify availability zone (optional) if opts['availability_zone'] [:availability_zone] = opts['availability_zone'] 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 (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 return instances end |
#render_template(opts = {}) ⇒ Object
this is made public for ec2admin usage
183 184 185 186 187 188 189 190 191 |
# File 'lib/stemcell/launcher.rb', line 183 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 |