Class: Sortinghat::AWS
- Inherits:
-
Object
- Object
- Sortinghat::AWS
- Defined in:
- lib/sortinghat/aws.rb
Instance Method Summary collapse
-
#discover ⇒ Object
Method to discover what auto-scaling group the current instance is in, then the instances in that group Returns array of the Name tag values of the instances.
-
#initialize(region = 'us-east-1') ⇒ AWS
constructor
A new instance of AWS.
- #privateip ⇒ Object
-
#removetag! ⇒ Object
Method to remove the Name tag, and set a temporary one Returns nothing.
-
#setroute53(zone, fqdn) ⇒ Object
Method to set the A record in Route53 Returns nothing.
-
#settag!(hostname) ⇒ Object
Method to set the Name tag on the current instance Returns nothing.
Constructor Details
#initialize(region = 'us-east-1') ⇒ AWS
Returns a new instance of AWS.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sortinghat/aws.rb', line 8 def initialize( region = 'us-east-1' ) # Be using this lots, make it an instance variable @region = region # Set a generic client for use @client = Aws::EC2::Client.new(region: @region) # Create a syslog for us to use as an instance variable @log = Syslog::Logger.new 'sortinghat' end |
Instance Method Details
#discover ⇒ Object
Method to discover what auto-scaling group the current instance is in, then the instances in that group Returns array of the Name tag values of the instances
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sortinghat/aws.rb', line 21 def discover() # Temporay array for use ids = Array.new # Start a new client autoscale = Aws::AutoScaling::Client.new( region: @region ) # Use the client to describe this instance current = autoscale.describe_auto_scaling_instances({ instance_ids: [grabinstanceid()], max_records: 1 }) @log.info("Grabbed current AutoScaling instance via aws-sdk") # Use the client to grab all instances in this auto-scaling group all = autoscale.describe_auto_scaling_groups({ auto_scaling_group_names: [current.auto_scaling_instances[0].auto_scaling_group_name], max_records: 1 }) @log.info("Grabbed the instances of our AutoScaling group via aws-sdk") # Grab their instanceId(s) all.auto_scaling_groups[0].instances.each do |instance| ids << idtoname(instance.instance_id) end @log.info("Returning instances '#{ids.join("','")}'") # Return the ids ids end |
#privateip ⇒ Object
116 117 118 |
# File 'lib/sortinghat/aws.rb', line 116 def privateip() return grabinstanceprivateip() end |
#removetag! ⇒ Object
Method to remove the Name tag, and set a temporary one Returns nothing
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sortinghat/aws.rb', line 72 def removetag!() # Use the instance varible client to create a new Resource resource = Aws::EC2::Resource.new(client: @client) # Use the resource, to find current instance, and set the Name tag resource.instance(grabinstanceid()).({ tags: [ { key: 'Name', value: "sortinghat-#{rand(100)}", }, ] }) @log.info("Set Name tag to temporary #{hostname} via aws-sdk") end |
#setroute53(zone, fqdn) ⇒ Object
Method to set the A record in Route53 Returns nothing
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 |
# File 'lib/sortinghat/aws.rb', line 90 def setroute53(zone, fqdn) # Create a new client, and use it to update/insert our A record Aws::Route53::Client.new(region: @region).change_resource_record_sets({ hosted_zone_id: zonetoid(zone), change_batch: { comment: "Sorting Hat #{Date.today.to_s}", changes: [ { action: 'UPSERT', resource_record_set: { name: fqdn, type: 'A', ttl: '30', resource_records: [ { value: grabinstanceprivateip() }, ], }, }, ], }, }) @log.info("Issued UPSERT to Route53 for #{fqdn}") end |
#settag!(hostname) ⇒ Object
Method to set the Name tag on the current instance Returns nothing
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sortinghat/aws.rb', line 54 def settag!(hostname) # Use the instance varible client to create a new Resource resource = Aws::EC2::Resource.new(client: @client) # Use the resource, to find current instance, and set the Name tag resource.instance(grabinstanceid()).({ tags: [ { key: 'Name', value: hostname, }, ] }) @log.info("Set Name tag to #{hostname} via aws-sdk") end |