aws-watcher 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/aws_watcher.rb +22 -2
- data/lib/aws-watcher.rb +17 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cafd8ceb3eba8271b24c78e5258d615683f29737
|
4
|
+
data.tar.gz: c8a16fcc14a07faff6367507f6fb6d51ec2ed729
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5eb282580bc31b65af2cdad4ae7f501da13c53357fea8a9058d2f02325182f9499ac44add697c8e851fad02e032945072e63dca0ecf9747fe31f451524818be
|
7
|
+
data.tar.gz: 86cfbc76b19e301256e1b6beec42a1dbd721825607c2711194c0c2df227c9a0402aa105eef0c82423f6c510fa088002f686f2cbf7c4b25c7ddf948d037d267ee
|
data/bin/aws_watcher.rb
CHANGED
@@ -37,6 +37,7 @@ end
|
|
37
37
|
# get options
|
38
38
|
opts = Trollop.options do
|
39
39
|
opt :config, 'Path to config file', type: :string, default: 'config.yml'
|
40
|
+
opt :tag, 'tagName:tagValue1,tagValue2', type: String, required: true
|
40
41
|
end
|
41
42
|
|
42
43
|
@config = config(opts[:config])
|
@@ -45,6 +46,20 @@ end
|
|
45
46
|
@sqs_client = AwsCleaner::SQS.client(@config)
|
46
47
|
@chef_client = AwsCleaner::Chef.client(@config)
|
47
48
|
|
49
|
+
# get tags
|
50
|
+
begin
|
51
|
+
@tag_name = opts[:tag].split(':')[0].strip
|
52
|
+
@tag_values = opts[:tag].split(':')[1].split(',').strip
|
53
|
+
p "#{@tag_name}, #{@tag_values}"
|
54
|
+
rescue
|
55
|
+
msg = "You passed be a tag of #{ops[:tag]} which is not valid "
|
56
|
+
msg += 'tags must look like this: -t tag:value you can'
|
57
|
+
msg += 'optionally pass multiple values for the same tag like '
|
58
|
+
msg += 'this -t tag:value1,value2'
|
59
|
+
p msg
|
60
|
+
exit 1
|
61
|
+
end
|
62
|
+
|
48
63
|
# start program in infinate loop
|
49
64
|
loop do
|
50
65
|
messages = @sqs_client.receive_message(
|
@@ -53,9 +68,9 @@ loop do
|
|
53
68
|
visibility_timeout: 3
|
54
69
|
).messages
|
55
70
|
|
56
|
-
puts "Got #{messages.size} message(s)"
|
71
|
+
puts "Got #{messages.size} message(s)" # unless messages.size.zero?
|
57
72
|
messages.each_with_index do |message, index|
|
58
|
-
puts "Looking at message number #{index}" unless messages.size.zero?
|
73
|
+
puts "Looking at message number #{index}" # unless messages.size.zero?
|
59
74
|
body = AwsCleaner.new.parse(message.body)
|
60
75
|
id = message.receipt_handle
|
61
76
|
now = Time.now.utc
|
@@ -80,6 +95,11 @@ loop do
|
|
80
95
|
elsif AwsWatcher::Chef.registered?(@instance_id, @config)
|
81
96
|
p "instance: #{@instance_id} has bootstrapped, removing message from queue"
|
82
97
|
AwsCleaner.new.delete_message(id, @config)
|
98
|
+
next
|
99
|
+
elsif !AwsWatcher::EC2.tag_matches?(@instance_id, @tag_name, @tag_values, @config)
|
100
|
+
p "instance: #{@instance_id} has not bootstrap and was deleted because it did not have a tag of: #{@tag_name} matching one of these values: #{@tag_values}"
|
101
|
+
AwsCleaner.new.delete_message(id, @config)
|
102
|
+
next
|
83
103
|
else
|
84
104
|
p "instance: #{@instance_id}, launched: #{launch_time}, elapsed: #{elapsed_time} seconds"
|
85
105
|
end
|
data/lib/aws-watcher.rb
CHANGED
@@ -36,6 +36,23 @@ class AwsWatcher
|
|
36
36
|
end
|
37
37
|
false
|
38
38
|
end
|
39
|
+
|
40
|
+
def self.tag_matches?(id, tag, values, config)
|
41
|
+
instances = AwsWatcher::EC2.client(config).describe_instances(
|
42
|
+
instance_ids: [id],
|
43
|
+
filters: [
|
44
|
+
{
|
45
|
+
name: "tag:#{tag}",
|
46
|
+
values: values.to_a
|
47
|
+
}
|
48
|
+
]
|
49
|
+
)
|
50
|
+
if instances.reservations.empty?
|
51
|
+
false
|
52
|
+
else
|
53
|
+
true
|
54
|
+
end
|
55
|
+
end
|
39
56
|
end
|
40
57
|
|
41
58
|
module Notify
|