Class: Sortinghat::AWS

Inherits:
Object
  • Object
show all
Defined in:
lib/sortinghat/aws.rb

Instance Method Summary collapse

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

#discoverObject

Method to discover all the alive auto-scaling instances 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
# 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 grab all autoscaling instances
  resp = autoscale.describe_auto_scaling_instances()
  @log.info("Grabbed all AutoScaling instances via aws-sdk")

  # Grab their instanceId(s)
  resp.auto_scaling_instances.each do |instance|
  ids << idtoname(instance.instance_id)
  end
  
  # Return the ids
  ids
end

#privateipObject



105
106
107
# File 'lib/sortinghat/aws.rb', line 105

def privateip()
  return grabinstanceprivateip()
end

#removetag!Object

Method to remove the Name tag, and set a temporary one Returns nothing



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sortinghat/aws.rb', line 61

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()).create_tags({
    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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sortinghat/aws.rb', line 79

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sortinghat/aws.rb', line 43

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()).create_tags({
    tags: [
      { 
        key: 'Name',
        value: hostname,
      },
  ]
  })
  @log.info("Set Name tag to #{hostname} via aws-sdk")
end