Module: Leeroy::Helpers::AWS
- Includes:
- Leeroy::Helpers
- Included in:
- Inventory, Task::Fixture, Task::Image, Task::Instantiate, Task::Terminate
- Defined in:
- lib/leeroy/helpers/aws.rb
Instance Attribute Summary collapse
-
#ec2 ⇒ Object
readonly
Returns the value of attribute ec2.
-
#rds ⇒ Object
readonly
Returns the value of attribute rds.
-
#s3 ⇒ Object
readonly
Returns the value of attribute s3.
Instance Method Summary collapse
- #awsRequest(service, method, params = {}, global_options = self.global_options) ⇒ Object
- #buildS3ObjectName(key, type, prefixes = Leeroy::Env::S3_PREFIXES) ⇒ Object
- #checkSemaphore(semaphore) ⇒ Object
- #clearSemaphore(semaphore) ⇒ Object
- #createTags(tags = {}, resourceids = [], state = self.state, env = self.env, options = self.options) ⇒ Object
- #destroyInstance(state = self.state, env = self.env, ec2 = self.ec2, options = self.options) ⇒ Object
-
#ec2Request(method, params = {}, global_options = self.global_options) ⇒ Object
EC2.
- #filterImages(selector, collector = lambda { |x| x }, state = self.state, env = self.env, ec2 = self.ec2, options = self.options) ⇒ Object
- #genSemaphore(object, payload = '', bucket = checkEnv('LEEROY_S3_BUCKET')) ⇒ Object
- #getApplicationImageIndex(env_build_target = 'LEEROY_BUILD_TARGET', env_app_name = 'LEEROY_APP_NAME') ⇒ Object
- #getApplicationInstanceName(index = nil, env_app = 'LEEROY_APP_NAME', env_name = 'LEEROY_BUILD_TARGET') ⇒ Object
- #getGoldMasterImageIndex(env_prefix = 'LEEROY_GOLD_MASTER_IMAGE_PREFIX') ⇒ Object
- #getGoldMasterInstanceName(env_name = 'LEEROY_GOLD_MASTER_NAME') ⇒ Object
- #getImageByName(image_name) ⇒ Object
- #getMaxImageIndex(name_prefix) ⇒ Object
- #getRDSInstanceEndpoint(instancename) ⇒ Object
- #getSemaphore(semaphore) ⇒ Object
- #getSgId(sgname, vpcname, vpcid) ⇒ Object
- #getSubnetId(subnetname, vpcid, ec2 = self.ec2) ⇒ Object
- #getVpcId(vpcname) ⇒ Object
- #initialize(*args, &block) ⇒ Object
-
#rdsRequest(method, params = {}, global_options = self.global_options) ⇒ Object
RDS.
-
#s3Request(method, params = {}, global_options = self.global_options) ⇒ Object
S3.
- #setSemaphore(semaphore) ⇒ Object
Instance Attribute Details
#ec2 ⇒ Object (readonly)
Returns the value of attribute ec2.
16 17 18 |
# File 'lib/leeroy/helpers/aws.rb', line 16 def ec2 @ec2 end |
#rds ⇒ Object (readonly)
Returns the value of attribute rds.
16 17 18 |
# File 'lib/leeroy/helpers/aws.rb', line 16 def rds @rds end |
#s3 ⇒ Object (readonly)
Returns the value of attribute s3.
16 17 18 |
# File 'lib/leeroy/helpers/aws.rb', line 16 def s3 @s3 end |
Instance Method Details
#awsRequest(service, method, params = {}, global_options = self.global_options) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/leeroy/helpers/aws.rb', line 30 def awsRequest(service, method, params = {}, = self.) begin logger.debug "constructing AWS request for '#{service}: #{method}'" client = self.send(service.to_sym) params_mash = Leeroy::Types::Mash.new(params) params = params_mash # dry_run is an ec2 thing case service.to_sym when :ec2 dry_run = [:op] ? false : true params.dry_run = dry_run end resp = client.send(method.to_sym, params) logger.debug "resp: #{resp.inspect}" resp rescue StandardError => e raise e end end |
#buildS3ObjectName(key, type, prefixes = Leeroy::Env::S3_PREFIXES) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/leeroy/helpers/aws.rb', line 339 def buildS3ObjectName(key, type, prefixes = Leeroy::Env::S3_PREFIXES) begin logger.debug "building S3 prefix (key: #{key}, type: #{type})" pfx = Leeroy::Types::Mash.new(prefixes) root = pfx.jenkins prefix = pfx.fetch(type,type) # FIXME i should do this with URI [root, prefix, key].join('/') rescue StandardError => e raise e end end |
#checkSemaphore(semaphore) ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/leeroy/helpers/aws.rb', line 423 def checkSemaphore(semaphore) begin unless semaphore.kind_of?(Leeroy::Types::Semaphore) semaphore = Leeroy::Types::Semaphore.new(semaphore) end run_params = Leeroy::Types::Mash.new run_params.bucket = semaphore.bucket run_params.key = semaphore.object # is the object present in S3? logger.debug "checking for presence of #{semaphore}" resp = s3Request(:head_object, run_params) if resp.delete_marker.nil? resp else logger.debug "#{semaphore} already deleted" nil end rescue Aws::S3::Errors::NotFound => e logger.debug "#{semaphore} not found" nil rescue StandardError => e raise e end end |
#clearSemaphore(semaphore) ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/leeroy/helpers/aws.rb', line 391 def clearSemaphore(semaphore) begin logger.debug "semaphore.class: #{semaphore.class}" if semaphore.kind_of?(Leeroy::Types::Semaphore) logger.debug "received a semaphore, continuing" else logger.debug "did not receive a semaphore, initializing" semaphore = Leeroy::Types::Semaphore.new(semaphore) end run_params = Leeroy::Types::Mash.new run_params.bucket = semaphore.bucket run_params.key = semaphore.object # is the object present in S3? resp = checkSemaphore(semaphore) if checkSemaphore(semaphore) logger.debug "#{semaphore} present, deleting" resp = s3Request(:delete_object, run_params) else logger.debug "#{semaphore} not present, continuing" end semaphore rescue StandardError => e raise e end end |
#createTags(tags = {}, resourceids = [], state = self.state, env = self.env, options = self.options) ⇒ Object
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 |
# File 'lib/leeroy/helpers/aws.rb', line 185 def createTags( = {}, resourceids = [], state = self.state, env = self.env, = self.) begin if resourceids.length == 0 if state.instanceid? logger.debug "no resourceids provided for tagging, defaulting to instanceid #{state.instanceid} from state" resourceids.push(state.instanceid.to_s) end end run_params = Leeroy::Types::Mash.new logger.debug "resourceids: #{resourceids}" run_params.resources = resourceids tag_array = .collect {|key,value| {'key' => key, 'value' => value}} logger.debug "tags: #{}" logger.debug "tag_array: #{tag_array}" run_params. = tag_array resp = ec2Request(:create_tags, run_params) rescue Aws::EC2::Errors::DryRunOperation => e logger.info e. "DRYRUN_DUMMY_VALUE: #{self.class}" rescue StandardError => e raise e end end |
#destroyInstance(state = self.state, env = self.env, ec2 = self.ec2, options = self.options) ⇒ Object
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 |
# File 'lib/leeroy/helpers/aws.rb', line 158 def destroyInstance(state = self.state, env = self.env, ec2 = self.ec2, = self.) begin # did we get instance ID(s)? instanceids = .fetch(:instance, nil) if instanceids.nil? instanceids = Array(state.instanceid) end logger.debug "instanceids: #{instanceids}" run_params = Leeroy::Types::Mash.new run_params.instance_ids = instanceids resp = ec2Request(:terminate_instances, run_params) resp.terminating_instances.collect { |i| i.instance_id }.sort rescue Aws::EC2::Errors::DryRunOperation => e logger.info e. "DRYRUN_DUMMY_VALUE: #{self.class}" rescue StandardError => e raise e end end |
#ec2Request(method, params = {}, global_options = self.global_options) ⇒ Object
EC2
60 61 62 63 64 65 66 67 68 |
# File 'lib/leeroy/helpers/aws.rb', line 60 def ec2Request(method, params = {}, = self.) begin awsRequest(:ec2, method, params, ) rescue Aws::EC2::Errors::DryRunOperation => e logger.info e. "DRYRUN_DUMMY_VALUE: #{self.class.to_s}" end end |
#filterImages(selector, collector = lambda { |x| x }, state = self.state, env = self.env, ec2 = self.ec2, options = self.options) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/leeroy/helpers/aws.rb', line 216 def filterImages(selector, collector = lambda { |x| x }, state = self.state, env = self.env, ec2 = self.ec2, = self.) begin run_params = Leeroy::Types::Mash.new run_params.owners = ['self'] resp = ec2Request(:describe_images, run_params) # now filter based on callbacks resp.images.select {|x| selector.call(x)}.collect {|x| collector.call(x)} rescue Aws::EC2::Errors::DryRunOperation => e logger.info e. "DRYRUN_DUMMY_VALUE: #{self.class}" rescue StandardError => e raise e end end |
#genSemaphore(object, payload = '', bucket = checkEnv('LEEROY_S3_BUCKET')) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/leeroy/helpers/aws.rb', line 354 def genSemaphore(object, payload = '', bucket = checkEnv('LEEROY_S3_BUCKET')) begin logger.debug "creating a semaphore" semaphore = Leeroy::Types::Semaphore.new(bucket: bucket, object: object, payload: payload) logger.debug "semaphore: #{semaphore}" semaphore rescue StandardError => e raise e end end |
#getApplicationImageIndex(env_build_target = 'LEEROY_BUILD_TARGET', env_app_name = 'LEEROY_APP_NAME') ⇒ Object
280 281 282 283 284 285 |
# File 'lib/leeroy/helpers/aws.rb', line 280 def getApplicationImageIndex(env_build_target = 'LEEROY_BUILD_TARGET', env_app_name = 'LEEROY_APP_NAME') app_name = checkEnv(env_app_name) build_target = checkEnv(env_build_target) name_prefix = [app_name, build_target].join('-') getMaxImageIndex(name_prefix) end |
#getApplicationInstanceName(index = nil, env_app = 'LEEROY_APP_NAME', env_name = 'LEEROY_BUILD_TARGET') ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/leeroy/helpers/aws.rb', line 260 def getApplicationInstanceName(index = nil, env_app = 'LEEROY_APP_NAME', env_name = 'LEEROY_BUILD_TARGET') name_prefix = [checkEnv(env_app), checkEnv(env_name)].join('-') logger.debug "name_prefix: #{name_prefix}" if index.nil? index = getApplicationImageIndex end instance_name = [name_prefix, index].join('-') logger.debug "instance_name: #{instance_name}" instance_name end |
#getGoldMasterImageIndex(env_prefix = 'LEEROY_GOLD_MASTER_IMAGE_PREFIX') ⇒ Object
275 276 277 278 |
# File 'lib/leeroy/helpers/aws.rb', line 275 def getGoldMasterImageIndex(env_prefix = 'LEEROY_GOLD_MASTER_IMAGE_PREFIX') name_prefix = checkEnv(env_prefix) getMaxImageIndex(name_prefix) end |
#getGoldMasterInstanceName(env_name = 'LEEROY_GOLD_MASTER_NAME') ⇒ Object
256 257 258 |
# File 'lib/leeroy/helpers/aws.rb', line 256 def getGoldMasterInstanceName(env_name = 'LEEROY_GOLD_MASTER_NAME') checkEnv(env_name) end |
#getImageByName(image_name) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/leeroy/helpers/aws.rb', line 236 def getImageByName(image_name) begin raise "image_name parameter cannot be nil" if image_name.nil? selector = lambda {|image| image.name == image_name} collector = lambda {|image| image.image_id} image_ids = filterImages(selector, collector) logger.debug image_ids.inspect logger.debug "image_name: #{image_name}" image_ids[0] rescue StandardError => e raise e end end |
#getMaxImageIndex(name_prefix) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/leeroy/helpers/aws.rb', line 287 def getMaxImageIndex(name_prefix) # determine the index by looking at existing images selector = lambda {|image| image.name =~ /^#{name_prefix}/} # and extract the names collector = lambda {|image| image.name} image_names = filterImages(selector, collector) image_numbers = image_names.collect do |name| if name =~ /(\d+)$/ # extract numeric suffixes of names, convert to Integers $1.to_i end end latest_image = image_numbers.sort.compact.uniq.pop || 1 logger.debug "latest_image: #{latest_image}" latest_image end |
#getRDSInstanceEndpoint(instancename) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/leeroy/helpers/aws.rb', line 314 def getRDSInstanceEndpoint(instancename) begin logger.debug "getting DB Instance Endpoint for '#{instancename}'" resp = rdsRequest(:describe_db_instances, {:db_instance_identifier => instancename}) db_instances = resp.db_instances logger.debug "db_instances: #{db_instances.inspect}" db_instances[0].endpoint.address rescue Aws::EC2::Errors::DryRunOperation => e logger.info e. "DRYRUN_DUMMY_VALUE: #{self.class.to_s}" rescue StandardError => e raise e end end |
#getSemaphore(semaphore) ⇒ Object
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/leeroy/helpers/aws.rb', line 453 def getSemaphore(semaphore) begin unless semaphore.kind_of?(Leeroy::Types::Semaphore) semaphore = Leeroy::Types::Semaphore.new(semaphore) end run_params = Leeroy::Types::Mash.new run_params.bucket = semaphore.bucket run_params.key = semaphore.object logger.debug "downloading #{semaphore}" resp = s3Request(:get_object, run_params) resp.body.string rescue Aws::S3::Errors::NotFound => e logger.debug "#{semaphore} not found" nil rescue StandardError => e raise e end end |
#getSgId(sgname, vpcname, vpcid) ⇒ Object
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 |
# File 'lib/leeroy/helpers/aws.rb', line 98 def getSgId(sgname, vpcname, vpcid) begin logger.debug "getting SG ID for '#{sgname}'" resp = ec2Request(:describe_security_groups, {:filters => [{name: 'vpc-id', values: [vpcid]}]}) security_groups = resp.security_groups # now filter by sgname sgmatcher = %r{#{vpcname}-#{sgname}-.*} security_group = security_groups.select { |sg| sg.group_name =~ sgmatcher} logger.debug "security_group: #{security_group.inspect}" if security_group.length < 1 raise "No SG found with the name '#{sgname}'." elsif security_group.length > 1 raise "Multiple SGs found with the name '#{sgname}'." else sgid = security_group[0].group_id end logger.debug "sgid: #{sgid}" sgid rescue Aws::EC2::Errors::DryRunOperation => e logger.info e. "DRYRUN_DUMMY_VALUE: #{self.class.to_s}" rescue StandardError => e raise e end end |
#getSubnetId(subnetname, vpcid, ec2 = self.ec2) ⇒ 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 |
# File 'lib/leeroy/helpers/aws.rb', line 130 def getSubnetId(subnetname, vpcid, ec2 = self.ec2) begin logger.debug "getting Subnet ID for '#{subnetname}'" resp = ec2Request(:describe_subnets, {:filters => [{name: 'vpc-id', values: [vpcid]}, {name: 'tag:Name', values: [subnetname]}]}) subnets = resp.subnets logger.debug "subnets: #{subnets.inspect}" if subnets.length < 1 raise "No Subnet found with the name '#{subnetname}'." elsif subnets.length > 1 raise "Multiple Subnets found with the name '#{subnetname}'." else subnetid = subnets[0].subnet_id end logger.debug "subnetid: #{subnetid}" subnetid rescue Aws::EC2::Errors::DryRunOperation => e logger.info e. "DRYRUN_DUMMY_VALUE: #{self.class}" rescue StandardError => e raise e end end |
#getVpcId(vpcname) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/leeroy/helpers/aws.rb', line 70 def getVpcId(vpcname) begin logger.debug "getting VPC ID for '#{vpcname}'" resp = ec2Request(:describe_vpcs, {:filters => [{name: 'tag:Name', values: [vpcname]}]}) vpcs = resp.vpcs logger.debug "vpcs: #{vpcs.inspect}" if vpcs.length < 1 raise "No VPC found with the name '#{vpcname}'." elsif vpcs.length > 1 raise "Multiple VPCs found with the name '#{vpcname}'." else vpcid = vpcs[0].vpc_id end logger.debug "vpcid: #{vpcid}" vpcid rescue Aws::EC2::Errors::DryRunOperation => e logger.info e. "DRYRUN_DUMMY_VALUE: #{self.class.to_s}" rescue StandardError => e raise e end end |
#initialize(*args, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/leeroy/helpers/aws.rb', line 18 def initialize(*args, &block) super(*args, &block) logger.debug "initializing AWS helpers" @ec2 = Aws::EC2::Client.new @rds = Aws::RDS::Client.new @s3 = Aws::S3::Client.new logger.debug "AWS helpers initialized" end |
#rdsRequest(method, params = {}, global_options = self.global_options) ⇒ Object
RDS
310 311 312 |
# File 'lib/leeroy/helpers/aws.rb', line 310 def rdsRequest(method, params = {}, = self.) awsRequest(:rds, method, params, ) end |
#s3Request(method, params = {}, global_options = self.global_options) ⇒ Object
S3
335 336 337 |
# File 'lib/leeroy/helpers/aws.rb', line 335 def s3Request(method, params = {}, = self.) awsRequest(:s3, method, params, ) end |
#setSemaphore(semaphore) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/leeroy/helpers/aws.rb', line 368 def setSemaphore(semaphore) begin unless semaphore.kind_of?(Leeroy::Types::Semaphore) semaphore = Leeroy::Types::Semaphore.new(semaphore) end logger.debug "setting a semaphore" run_params = Leeroy::Types::Mash.new run_params.body = semaphore.payload run_params.bucket = semaphore.bucket run_params.key = semaphore.object resp = s3Request(:put_object, run_params) semaphore rescue StandardError => e raise e end end |