aws-watcher 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/aws_watcher.rb +22 -2
  3. data/lib/aws-watcher.rb +17 -0
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2bf8eeafa65fe3b5e1c3be1c34c3d6a5bb012379
4
- data.tar.gz: 4c71a451b443e2da69140601bc10f9aac7eb5208
3
+ metadata.gz: cafd8ceb3eba8271b24c78e5258d615683f29737
4
+ data.tar.gz: c8a16fcc14a07faff6367507f6fb6d51ec2ed729
5
5
  SHA512:
6
- metadata.gz: 4b0839c4435fa5cbb5012030fbc1bb99ec8c060ebfc0118fbdd6fe6aa5457de4a783a41b438da2f55cb235356e4f5163cdecb8f33155cf9c4970051093d05723
7
- data.tar.gz: b9cefb0f11336db142c711e6023f4229894318620adaf2d9d0f542d883f8788766e4aae051d7d0f062a3aa73307eeb12e991cf67f1f1a48fb2489c3754be04b1
6
+ metadata.gz: f5eb282580bc31b65af2cdad4ae7f501da13c53357fea8a9058d2f02325182f9499ac44add697c8e851fad02e032945072e63dca0ecf9747fe31f451524818be
7
+ data.tar.gz: 86cfbc76b19e301256e1b6beec42a1dbd721825607c2711194c0c2df227c9a0402aa105eef0c82423f6c510fa088002f686f2cbf7c4b25c7ddf948d037d267ee
@@ -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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-watcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Abrams