cucloud 0.5.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.rubocop.yml +3 -0
- data/lib/cucloud/ec2_utils.rb +91 -2
- data/lib/cucloud/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OTI1MTYwMTY3YzBhZDI0YjA5YjBjM2M0NDc3MGMzYjA1YzAwZDZjZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NTI0MjVhN2M1MGE0MDE2Mjc1NmNlZTIzZWYyZmVhNDJjZDRhYTM0Mg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Mzk2YjU0MTNlZDJmMWRiMmJlOGYxNzUxZGI1MGVjMzRhNDFjZGJiNDY1NjI3
|
10
|
+
MTg3YjhkM2I2MzhmMjI0YjZhMDBmN2FkZGMzMDFlYzBlNWQ4Yjc1ZTgzMTcy
|
11
|
+
MjQ2OWQ3NzcwYWRmOWVmODg5ZTEwMzc5NTM0YzBiOTBhYzYzMmY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MWVlMDNkYTY5NzgxZDQ1YmE3ODk2MThjMGQ2ZWMzN2JiNzMxYmQ1MTA3MDU5
|
14
|
+
ZGI3ODZiYjE3MGY2ZTQ1MTZmZWJhYTUyMDM2MzNlYmZiMjMyYTkwYTAzYTQy
|
15
|
+
OWNiODUxOTAwYjRkOGRlYzc4YjdjOGI4Mjk0NWVlM2RhODNmNmI=
|
data/.rubocop.yml
CHANGED
data/lib/cucloud/ec2_utils.rb
CHANGED
@@ -3,16 +3,33 @@ module Cucloud
|
|
3
3
|
class Ec2Utils
|
4
4
|
# This is the command sent to ubuntu for patching
|
5
5
|
UBUNTU_PATCH_COMMAND = 'apt-get update; apt-get -y upgrade; reboot'.freeze
|
6
|
-
#
|
6
|
+
# This is the command sent to amazon linux machines for patching
|
7
7
|
AMAZON_PATCH_COMMAND = 'yum update -y; reboot & disown '.freeze
|
8
|
+
# Used in time calculations
|
9
|
+
SECONDS_IN_A_DAY = 86_400
|
10
|
+
# Max attemps for a waiter to try
|
11
|
+
WAITER_MAX_ATTEMPS = 240
|
12
|
+
# Delay between calls used by waiter to check status
|
13
|
+
WAITER_DELAY = 15
|
8
14
|
|
9
15
|
def initialize(ec2_client = Aws::EC2::Client.new, ssm_utils = Cucloud::SSMUtils.new)
|
10
16
|
@ec2 = ec2_client
|
11
17
|
@ssm_utils = ssm_utils
|
12
18
|
end
|
13
19
|
|
20
|
+
# Get instnace object
|
21
|
+
# @param instance_id [String] instance id in the format of i-121231231231
|
22
|
+
# @return [Aws::EC2::Instance] Object represneting the intance see
|
23
|
+
# http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Instance.html
|
24
|
+
def get_instance(instance_id)
|
25
|
+
Aws::EC2::Instance.new(id: instance_id, client: @ec2)
|
26
|
+
end
|
27
|
+
|
14
28
|
# Get instance information for a specific instance
|
15
|
-
|
29
|
+
# @param instance [String] instance id in the format of i-121231231231
|
30
|
+
# @return [array] aws reservations see
|
31
|
+
# http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#describe_instances-instance_method
|
32
|
+
def get_instance_information(instance)
|
16
33
|
@ec2.describe_instances(instance_ids: [instance])
|
17
34
|
end
|
18
35
|
|
@@ -125,5 +142,77 @@ module Cucloud
|
|
125
142
|
|
126
143
|
all_instances
|
127
144
|
end
|
145
|
+
|
146
|
+
# Get the nice name of the ec2 intsance from the 'Name" tag'
|
147
|
+
# @param instance_id [String] instance id in the format of i-121231231231
|
148
|
+
# @return [String] Name of instacnce if found or nil if not found
|
149
|
+
def get_instance_name(instance_id)
|
150
|
+
instance = get_instance(instance_id)
|
151
|
+
tag_name = instance.tags.find { |tag| tag.key.eql?('Name') }
|
152
|
+
tag_name ? tag_name.value : nil
|
153
|
+
end
|
154
|
+
|
155
|
+
# Create a snapshot of an EBS volume and apply supplied tags
|
156
|
+
# will wait 20 minutes for the process to completed
|
157
|
+
# @param volume_id [String] volume id in the formate of vol-121231231231
|
158
|
+
# @param snapshot_desc [String] Description of the snapshot
|
159
|
+
# @param tags [Array] Array of key value pairs to be applied as tags to the snapshot
|
160
|
+
# @return snapshot information see
|
161
|
+
# http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#create_tags-instance_method
|
162
|
+
def create_ebs_snapshot(volume_id, snapshot_desc, tags = [])
|
163
|
+
snapshot_info = @ec2.create_snapshot(
|
164
|
+
volume_id: volume_id,
|
165
|
+
description: snapshot_desc
|
166
|
+
)
|
167
|
+
|
168
|
+
@ec2.wait_until(:snapshot_completed, snapshot_ids: [snapshot_info.snapshot_id]) do |w|
|
169
|
+
w.max_attempts = WAITER_MAX_ATTEMPS
|
170
|
+
w.delay = WAITER_DELAY
|
171
|
+
end
|
172
|
+
|
173
|
+
@ec2.create_tags(resources: [snapshot_info.snapshot_id], tags: tags) unless tags.empty?
|
174
|
+
|
175
|
+
snapshot_info
|
176
|
+
end
|
177
|
+
|
178
|
+
# Preforms a backup on volumes that do not have a recent snapshot_info
|
179
|
+
# @param days [Integer] defaults to 5
|
180
|
+
# @return [Array<Hash>] An array of hashes containing snapshot_id, instance_name and volume
|
181
|
+
def backup_volumes_unless_recent_backup(days = 5)
|
182
|
+
volumes_backed_up_recently = volumes_with_snapshot_within_last_days(days)
|
183
|
+
snapshots_created = []
|
184
|
+
|
185
|
+
volumes = @ec2.describe_volumes(filters: [{ name: 'attachment.status', values: ['attached'] }])
|
186
|
+
volumes.volumes.each do |volume|
|
187
|
+
next if volumes_backed_up_recently[volume.volume_id.to_s]
|
188
|
+
instance_name = get_instance_name(volume.attachments[0].instance_id)
|
189
|
+
|
190
|
+
tags = instance_name ? [{ key: 'Instance Name', value: instance_name }] : []
|
191
|
+
snapshot_info = create_ebs_snapshot(volume.volume_id,
|
192
|
+
'auto-ebs-snap-' + Time.now.strftime('%Y-%m-%d-%H:%M:%S'),
|
193
|
+
tags)
|
194
|
+
|
195
|
+
snapshots_created.push(snapshot_id: snapshot_info.snapshot_id,
|
196
|
+
instance_name: instance_name,
|
197
|
+
volume: volume.volume_id)
|
198
|
+
end
|
199
|
+
|
200
|
+
snapshots_created
|
201
|
+
end
|
202
|
+
|
203
|
+
# Find volumes that have a recent snapshot
|
204
|
+
# @param days [Integer] defaults to 5
|
205
|
+
# @return [Array] list of volume ids that have recent snapshots
|
206
|
+
def volumes_with_snapshot_within_last_days(days = 5)
|
207
|
+
volumes_backed_up_recently = {}
|
208
|
+
|
209
|
+
snapshots = @ec2.describe_snapshots(owner_ids: ['self'], filters: [{ name: 'status', values: ['completed'] }])
|
210
|
+
snapshots.snapshots.each do |snapshot|
|
211
|
+
if snapshot.start_time > Time.now - (SECONDS_IN_A_DAY * days)
|
212
|
+
volumes_backed_up_recently[snapshot.volume_id.to_s] = true
|
213
|
+
end
|
214
|
+
end
|
215
|
+
volumes_backed_up_recently
|
216
|
+
end
|
128
217
|
end
|
129
218
|
end
|
data/lib/cucloud/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cucloud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sbower
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-08-
|
13
|
+
date: 2016-08-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: aws-sdk
|