ebs_tagger 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/ebs_tagger +171 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ad08ec14733feab6a60b83530fe7f63c225b3530
|
4
|
+
data.tar.gz: 07dc2941799220c263dd5135898cab40998e8647
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c968621dc9c1f5a9cdab921d894286b994cc3f5fbbdb5a97fbdb9edbb75f336fef15be81a0a151ab33f7b775fce6d01102074d771c6b5f16dba6906793dcf312
|
7
|
+
data.tar.gz: 4b94ffc73e87e5199279727f574a3dc9c7b197e9a807a224ab10c78a4df77cc7591a2eea4a888206c03905f7d4791565e6c82c5e6d005b9f36ddb0fac7ebe41d
|
data/bin/ebs_tagger
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This script is designed to go through all AWS instances
|
4
|
+
# and add their tags to each associated ebs volume.
|
5
|
+
|
6
|
+
require 'aws-sdk'
|
7
|
+
require 'optparse'
|
8
|
+
|
9
|
+
options = {}
|
10
|
+
creds = {}
|
11
|
+
OptionParser.new do |parser|
|
12
|
+
parser.on("-i", "--id ACCESS_ID", "The AWS access ID") do |access_id|
|
13
|
+
creds['access_id'] = access_id
|
14
|
+
end
|
15
|
+
parser.on("-k", "--key SECRET_KEY", "The AWS access key") do |secret_key|
|
16
|
+
creds['secret_key'] = secret_key
|
17
|
+
end
|
18
|
+
parser.on("-r", "--regions REGIONS", "Comma-separated list of AWS regions to sync tags for") do |regions|
|
19
|
+
options[:regions] = regions.split(",")
|
20
|
+
end
|
21
|
+
parser.on("--add-id", "Tag each volume with the Instance ID of the instance it is attached to") do |add_id|
|
22
|
+
options[:add_id] = add_id
|
23
|
+
end
|
24
|
+
parser.on("--only ONLY", "Comma-separated list of specific tags to sync from instances to volumes") do |only_tags|
|
25
|
+
options[:only] = only_tags.split(",")
|
26
|
+
end
|
27
|
+
parser.on("--ignore IGNORE", "Comma-separated list of tags to NOT sync, overrides --only") do |ignored_tags|
|
28
|
+
options[:ignore] = ignored_tags.split(",")
|
29
|
+
end
|
30
|
+
end.parse!
|
31
|
+
|
32
|
+
if !creds['access_id']
|
33
|
+
raise Exception.new 'You must provide an AWS access ID (--id)'
|
34
|
+
elsif !creds['secret_key']
|
35
|
+
raise Exception.new 'You must provide an AWS access key (--key)'
|
36
|
+
elsif !options[:regions]
|
37
|
+
raise Exception.new 'You must provide an at least one AWS Region (--regions)'
|
38
|
+
end
|
39
|
+
|
40
|
+
@too_many_tags_errors = 0
|
41
|
+
|
42
|
+
def tag_volume volume, instance_tags, instance_id, volume_tags, ignored_tags
|
43
|
+
begin
|
44
|
+
if instance_id
|
45
|
+
instance_tags["Instance ID"] = instance_id
|
46
|
+
end
|
47
|
+
if instance_tags.empty?
|
48
|
+
puts "No tags to sync to this volume"
|
49
|
+
end
|
50
|
+
instance_tags.each do |tag_key, tag_value|
|
51
|
+
if ignored_tags && (ignored_tags.include? tag_key)
|
52
|
+
puts "Ignoring \"#{tag_key}\""
|
53
|
+
else
|
54
|
+
if tag_key[0 .. 3] == 'aws:'
|
55
|
+
puts "\"#{tag_key}\" is reserved for internal use"
|
56
|
+
else
|
57
|
+
if (volume_tags[tag_key] == tag_value)
|
58
|
+
puts "\"#{tag_key} => #{tag_value}\" unchanged"
|
59
|
+
else
|
60
|
+
volume.create_tags({tags: [{key: tag_key, value: tag_value}]})
|
61
|
+
if volume_tags[tag_key]
|
62
|
+
puts "\"#{tag_key} => #{tag_value}\" updated on volume"
|
63
|
+
else
|
64
|
+
puts "\"#{tag_key} => #{tag_value}\" added to volume"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
rescue Aws::EC2::Errors::TagLimitExceeded
|
71
|
+
puts "WARNING: TOO MANY TAGS"
|
72
|
+
@too_many_tags_errors += 1
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_tags thing
|
77
|
+
tags = {}
|
78
|
+
thing.tags.each do |tag|
|
79
|
+
tags[tag.key] = tag.value
|
80
|
+
end
|
81
|
+
tags
|
82
|
+
end
|
83
|
+
|
84
|
+
def create_resource credentials, region
|
85
|
+
Aws.config[:credentials] = credentials
|
86
|
+
Aws.config[:region] = region
|
87
|
+
begin
|
88
|
+
ec2_resource = Aws::EC2::Resource.new
|
89
|
+
|
90
|
+
# check if credentials passed
|
91
|
+
ec2_resource.security_groups.count
|
92
|
+
puts "Your credentials have passed"
|
93
|
+
rescue Aws::EC2::Errors::AuthFailure
|
94
|
+
puts "UNAUTHORIZED: Your credentials have failed"
|
95
|
+
exit 2
|
96
|
+
end
|
97
|
+
ec2_resource
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
begin
|
102
|
+
# Establish AWS EC2 credentials
|
103
|
+
aws_credentials = Aws::Credentials.new(creds['access_id'], creds['secret_key'])
|
104
|
+
|
105
|
+
options[:regions].each do |region|
|
106
|
+
ec2_resource = create_resource aws_credentials, region
|
107
|
+
puts "Syncing tags on #{region} instances..."
|
108
|
+
|
109
|
+
instance_tracker = 0
|
110
|
+
instance_count = ec2_resource.instances.count
|
111
|
+
|
112
|
+
ec2_resource.instances.each do |instance|
|
113
|
+
puts Time.now
|
114
|
+
instance_id = instance.id
|
115
|
+
instance_tags = get_tags instance
|
116
|
+
puts "#{instance_id} - #{instance_tags['Name']}"
|
117
|
+
puts "----------"
|
118
|
+
puts "#{instance.volumes.count} volume(s)"
|
119
|
+
|
120
|
+
|
121
|
+
if options[:only]
|
122
|
+
only_tags = {}
|
123
|
+
options[:only].each do |tag|
|
124
|
+
if instance_tags[tag]
|
125
|
+
only_tags[tag] = instance_tags[tag]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
instance_tags = only_tags
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
instance.volumes.each do |volume|
|
133
|
+
retries = 1
|
134
|
+
begin
|
135
|
+
puts volume.id
|
136
|
+
volume_tags = get_tags volume
|
137
|
+
instance_id = false if !options[:add_id]
|
138
|
+
tag_volume volume, instance_tags, instance_id, volume_tags, options[:ignore]
|
139
|
+
rescue Aws::EC2::Errors::RequestLimitExceeded
|
140
|
+
if retries <= 5
|
141
|
+
puts "AWS Request Limit Exceeded. Waiting #{5 * retries} seconds to retry..."
|
142
|
+
sleep (5 * retries)
|
143
|
+
retry
|
144
|
+
else
|
145
|
+
puts "Too many retries on this volume, exiting"
|
146
|
+
exit
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
instance_tracker += 1
|
151
|
+
puts "#{region}: Instance #{instance_tracker} of #{instance_count} done\n\n"
|
152
|
+
sleep 3
|
153
|
+
end
|
154
|
+
puts "#{region}: EBS Volume tagging complete"
|
155
|
+
if too_many_tags_errors > 0
|
156
|
+
puts "# of TOO MANY TAGS errors: #{too_many_tags_errors}"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
rescue Aws::EC2::Errors::RequestLimitExceeded
|
163
|
+
if retries <= 5
|
164
|
+
puts "AWS Request Limit Exceeded. Waiting #{10 * retries} seconds to retry..."
|
165
|
+
sleep (10 * retries)
|
166
|
+
retry
|
167
|
+
else
|
168
|
+
puts "Too many retries on this instance, exiting"
|
169
|
+
exit
|
170
|
+
end
|
171
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ebs_tagger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hunter Wong
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2'
|
27
|
+
description: Goes through all AWS instances in a region and tags their attached volumes
|
28
|
+
with their own tags
|
29
|
+
email: quishee@gmail.com
|
30
|
+
executables:
|
31
|
+
- ebs_tagger
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- bin/ebs_tagger
|
36
|
+
homepage: https://bitbucket.org/oryhara/ebs_tagger/src
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.5.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Tags EBS volumes
|
60
|
+
test_files: []
|
61
|
+
has_rdoc:
|