sortinghat 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf767b88b4dc1e13e2c2ba670b706efc8ce09d33
4
- data.tar.gz: 42cda4780b90f711e220d44f7f46baa96d93d402
3
+ metadata.gz: 59892d22cedf8e98be4abe64b1eaad223777806a
4
+ data.tar.gz: c66c95ed0098a384a0e5797e6a914354e7431c0c
5
5
  SHA512:
6
- metadata.gz: cde94d2d42010301ed9131a059086021067279fdd046069468f3ee9b3ec622ecdf43619a1a69360e36323963d15a4d8ded25fbaf1c19437f502b93c66ce8058d
7
- data.tar.gz: 2845e1f467a07980e370e465e16f38fffc385cc02dd6eddd966465e0ed359f497dd9d9f7f700c0d3efb4213eabb99e14b291fff5302d97f51bba17c200fc50d2
6
+ metadata.gz: 0bef5473f37c0d14c36ab6290340c9d1764ac70c72b3c39bbcea12455da3ae871a88ad5288aa700cec18eddf96c961b93b1d2989090362d0d8bc2017ba3d2e2d
7
+ data.tar.gz: 8ae5477f8b4b6fdec97b50193147601353f546fbf2543c44bed0cc0c326bc63cd3504a56e6f742e9f1d4d1cd4484b2d3408f9ec3ab505858dff589a92e570ca4
data/README.md CHANGED
@@ -14,7 +14,7 @@ It follows a specific pattern for hostnames/fqdn:
14
14
  For example:
15
15
 
16
16
  ```
17
- nike-prod-nginx09.prod-internal.nike.com
17
+ nike-prod-nginx09.prod-nike.com
18
18
  ```
19
19
 
20
20
  ## Installation:
@@ -32,7 +32,8 @@ It requires the following gems:
32
32
  * pure_json
33
33
 
34
34
  During actually usage, the gem requires that the instance have the following IAM actions allowed via policy:
35
- * autoscaling:Describe*
35
+ * autoscaling:DescribeAutoScalingInstances
36
+ * autoscaling:DescribeAutoScalingGroups
36
37
  * ec2:DescribeInstances
37
38
  * ec2:CreateTags
38
39
  * route53:ListHostedZones
@@ -73,7 +74,6 @@ Run:
73
74
 
74
75
  Bug reports and pull requests are welcome on GitHub at https://github.com/praymann/sortinghat.
75
76
 
76
-
77
77
  ## License
78
78
 
79
79
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -16,7 +16,7 @@ module Sortinghat
16
16
  @log = Syslog::Logger.new 'sortinghat'
17
17
  end
18
18
 
19
- # Method to discover all the alive auto-scaling instances
19
+ # Method to discover what auto-scaling group the current instance is in, then the instances in that group
20
20
  # Returns array of the Name tag values of the instances
21
21
  def discover()
22
22
  # Temporay array for use
@@ -25,14 +25,25 @@ module Sortinghat
25
25
  # Start a new client
26
26
  autoscale = Aws::AutoScaling::Client.new( region: @region )
27
27
 
28
- # Use the client to grab all autoscaling instances
29
- resp = autoscale.describe_auto_scaling_instances()
30
- @log.info("Grabbed all AutoScaling instances via aws-sdk")
28
+ # Use the client to describe this instance
29
+ current = autoscale.describe_auto_scaling_instances({
30
+ instance_ids: [grabinstanceid()],
31
+ max_records: 1
32
+ })
33
+ @log.info("Grabbed current AutoScaling instance via aws-sdk")
34
+
35
+ # Use the client to grab all instances in this auto-scaling group
36
+ all = autoscale.describe_auto_scaling_groups({
37
+ auto_scaling_group_names: [current.auto_scaling_instances[0].auto_scaling_group_name],
38
+ max_records: 1
39
+ })
40
+ @log.info("Grabbed the instances of our AutoScaling group via aws-sdk")
31
41
 
32
42
  # Grab their instanceId(s)
33
- resp.auto_scaling_instances.each do |instance|
34
- ids << idtoname(instance.instance_id)
43
+ all.auto_scaling_groups[0].instances.each do |instance|
44
+ ids << idtoname(instance.instance_id)
35
45
  end
46
+ @log.info("Returning instances '#{ids.join("','")}'")
36
47
 
37
48
  # Return the ids
38
49
  ids
@@ -51,7 +62,7 @@ module Sortinghat
51
62
  key: 'Name',
52
63
  value: hostname,
53
64
  },
54
- ]
65
+ ]
55
66
  })
56
67
  @log.info("Set Name tag to #{hostname} via aws-sdk")
57
68
  end
@@ -110,11 +121,8 @@ module Sortinghat
110
121
 
111
122
  def idtoname(instance_id)
112
123
  resource = Aws::EC2::Resource.new(client: @client)
113
- resource.instance(instance_id).tags.each do |tag|
114
- if tag.key == 'Name'
115
- return tag.value
116
- end
117
- end
124
+ name = resource.instance(instance_id).tags.find { |tag| tag.key == 'Name' }
125
+ return name.value unless name.nil?
118
126
  end
119
127
 
120
128
  def zonetoid(hostedzone)
@@ -29,17 +29,19 @@ module Sortinghat
29
29
  # Check for sentinel file
30
30
  if File.exists?('/etc/.sorted')
31
31
  # We found it, log error and exit successfully
32
- @log.error('Found /etc/.sorted, refusing to sort.')
33
- abort('Found /etc/.sorted, refusing to sort.')
32
+ @log.info('Found /etc/.sorted, refusing to sort.')
33
+ exit 0
34
34
  end
35
35
  end
36
36
 
37
37
  # Main method of Sortinghat
38
38
  def start!
39
39
  # Find out who is who, instances alive
40
+ # If discover() returns a nil Array, alive will be nil
40
41
  alive = cleanup(@aws.discover())
41
42
 
42
43
  # Given the alive instances, find our prefix
44
+ # If alive is nil, selection will return the number '1'
43
45
  @prefix = ensurezero(selection(alive))
44
46
 
45
47
  # Put together hostname/fqdn
@@ -48,6 +50,7 @@ module Sortinghat
48
50
  @aws.settag!(@hostname)
49
51
 
50
52
  # Find out who is who, instances alive
53
+ # If discover() returns a nil Array, alive will be nil
51
54
  alive = cleanup(@aws.discover())
52
55
 
53
56
  unless alive.uniq.length == alive.length
@@ -90,10 +93,14 @@ module Sortinghat
90
93
  end
91
94
 
92
95
  def cleanup(array)
96
+ return [] if array.any? { |item| item.nil? }
93
97
  array.select! { |name| name.include?(@options[:env]) and name.include?(@options[:client]) and name.include?(@options[:type]) }
94
98
  end
95
99
 
96
100
  def selection(array)
101
+ # If array is nil, just return 01
102
+ return 1 if array.nil?
103
+
97
104
  # Array to store the numbers already taken
98
105
  taken = Array.new
99
106
 
@@ -1,3 +1,3 @@
1
1
  module Sortinghat
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortinghat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - D. Pramann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-17 00:00:00.000000000 Z
11
+ date: 2015-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler