Class: Aebus::Core

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/aebus.rb

Constant Summary collapse

AWS_NAME_TAG =
'Name'
AEBUS_TAG =
'Aebus'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

log_to_file, logger, #logger

Class Method Details

.name_and_desc(volume_id, tags, utc_time) ⇒ Array

calculates the name and the description to be set to a snapshot

Parameters:

  • volume_id (String)

    the id of the volume whose snapshot we are creating

  • tags (Array)

    the tags currently associated with the Volume

  • utc_time (Time)

    the UTC time at which the backup process started (used to generate the correct name)

Returns:

  • (Array)

    an array in the form of [name, description]]



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/aebus.rb', line 249

def self.name_and_desc(volume_id, tags, utc_time)

  name = "backup_#{utc_time.strftime('%Y%m%d')}_#{volume_id}"
  volume_name = volume_id
  tags.each do |tag|
    if tag['key'].eql?(AWS_NAME_TAG)
      volume_name = tag['value']
      break
    end
  end

  description = "Backup for volume #{volume_name} taken at #{utc_time.strftime('%Y-%m-%d %H:%M:%S')}"

  return [name, description]

end

Instance Method Details

#backup(args, options) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/aebus.rb', line 93

def backup(args, options)


  backed_up = 0
  max_delay = 0
  purged = 0
  to_purge = 0
  to_backup = 0
  @current_time_utc = Time.now.utc
  @config = Config::Config.new(File.join(File.dirname("."), options.config), @current_time_utc)

  init_logger options
  logger.info("backup started at #{@current_time_utc}")

  @ec2 = Aws::EC2::Client.new(
      region: @config.defaults['region'],
      credentials: Aws::Credentials.new(@config.defaults['access_key_id'], @config.defaults['secret_access_key'])
  )

  target_volumes = target_volumes(args)

  abort('Configuration contains invalid volumes') unless validate_target_volumes(target_volumes)

  if options.manual

    target_volumes.each do |volume|
      to_backup += 1
      break unless backup_volume(volume, [EC2::AEBUS_MANUAL_TAG], @config.get_value_for_volume(volume.id, 'custom_tags'))
      backed_up += 1

    end

  else

    snap_map = get_snapshots_map

    target_volumes.each do |target|

      volume = @config.volumes[target]
      to_be_run = volume.backups_to_be_run(snap_map[target], @current_time_utc)
      max_delay = [max_delay, to_be_run[0]].max
      tags = to_be_run[1]
      if tags.count > 0
        tags << EC2::AEBUS_AUTO_TAG
        logger.info("Creating backup for volume #{target} with tags #{tags.join(',')}, max delay #{max_delay}")
        to_backup +=1
        break unless backup_volume(target, tags, @config.get_value_for_volume(volume.id, 'custom_tags'))
        backed_up += 1
      else
        logger.info("Volume #{target} does not need to be backed up")
      end

    end

    snap_map = get_snapshots_map # we reload the map since we may have created more snapshots
    if options.purge
      target_volumes.each do |target|
        volume = @config.volumes[target]
        purgeable_snapshot_ids = volume.purgeable_snapshot_ids(snap_map[target])
        purgeable_snapshot_ids.each do |snapshot_id|
          to_purge += 1
          purged += 1 if purge_snapshot(snapshot_id)

        end
      end
    else
      logger.info('Skipping purging phase')
    end

  end

  message = "Backup Completed at #{Time.now}. Checked #{target_volumes.count} volume(s), #{to_backup} to be backed up, #{backed_up} actually backed up, max delay detected #{max_delay}s,  #{to_purge} purgeable snapshot(s), #{purged} purged"
  logger.info(message)
  puts(message)
  if to_backup > 0 or to_purge > 0
    send_report message
  end
end

#backup_volume(volume_id, aebus_tags, custom_tags = {}) ⇒ boolean

backs up a given volume using the given time as part of the name and setting the given tags to the snapshot

Parameters:

  • volume_id (String)

    the id of the volume to be backed up

  • aebus_tags (Array)

    an array of String to be used as tags for the snapshot

  • custom_tags (Hash) (defaults to: {})

    a Hash containing custom tags to be added to the snapshot

Returns:

  • (boolean)

    true if the backup was successful, false otherwise



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/aebus.rb', line 192

def backup_volume(volume_id, aebus_tags, custom_tags = {})
  begin
    volume_info = @ec2.describe_volumes(volume_ids: [volume_id])

  rescue Aws::EC2::Errors::ServiceError => e
    logger.error("Volume Id #{volume_id} not found. Underlying message #{e.message}")
    return false
  end

  begin

    volume_tags = volume_info.volumes[0].tags

    name_and_desc = Core.name_and_desc(volume_id, volume_tags, @current_time_utc)
    create_response = @ec2.create_snapshot({volume_id: volume_id, description: name_and_desc[1]})

  rescue Aws::EC2::Errors::ServiceError => e
    logger.error("Volume Id #{volume_id} could not be backed up. Underlying message #{e.message}")
    return false
  end

  begin

    tags = [
        {
            key: AWS_NAME_TAG,
            value: name_and_desc[0]
        },
        {
            key: AEBUS_TAG,
            value: aebus_tags.join(',')
        }
    ]

    if custom_tags
      custom_tags.each_pair { |k, v| tags << {key: k, value: v} }
    end

    puts custom_tags

    @ec2.create_tags(resources: [create_response.snapshot_id], tags: tags)
  rescue Aws::EC2::Errors::ServiceError => e
    logger.error("[WARNING] Could not set tags to snapshot #{create_response.snapshot_id}. Underlying message #{e.message}")
    return false
  end

  logger.info("Created snapshot #{create_response.snapshot_id} for volume #{volume_id}")

  true

end

#check_status(target_volumes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aebus.rb', line 52

def check_status(target_volumes)
  result = {}
  result[:timestamp] = @current_time_utc
  snap_map = get_snapshots_map
  result[:volumes] = Array.new
  to_backup = 0
  to_purge = 0
  target_volumes.each do |target|
    vs = VolumeStatus.new(target)
    volume = @config.volumes[target]
    vs.last_backup = volume.last_backup
    vs.next_backup = volume.next_backup
    to_be_run = volume.backups_to_be_run(snap_map[target], @current_time_utc)

    vs.delay = to_be_run[0]
    vs.tags = to_be_run[1]

    if vs.needs_backup?
      logger.info("Volume #{target} needs to be backed up. Tags: #{vs.tags.join(',')}, max delay #{vs.delay}")
      to_backup += 1
    else
      logger.info("Volume #{target} does not need to be backed up")
    end

    vs.purgeable_snapshot_ids = volume.purgeable_snapshot_ids(snap_map[target])
    to_purge += vs.purgeable_snapshot_ids.count  if vs.purgeable_snapshot_ids
    logger.info("Volume #{target} has #{vs.purgeable_snapshot_ids.count} purgeable snapshot(s): #{vs.purgeable_snapshot_ids.join(',')}")

    result[:volumes] << vs

  end
  result[:to_backup] = to_backup
  result[:to_purge] = to_purge
  result[:delay] = result[:volumes].inject([0]) {|acc, vs| acc << vs.delay}.max
  result[:total] = result[:volumes].count
  result

end

#get_snapshots_mapObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/aebus.rb', line 266

def get_snapshots_map

  response = @ec2.describe_snapshots(owner_ids: ['self'])
  return Hash.new unless response.snapshots
  snap_array = response.snapshots
  result = Hash.new
  snap_array.each do |snap|
    snapshot = EC2::Snapshot.new(snap)
    if result.include?(snapshot.volume_id)
      vol_array = result[snapshot.volume_id]
      index = vol_array.index{ |s| snapshot.start_time > s.start_time}
      index ||= vol_array.count
      vol_array.insert(index, snapshot)
    else
      vol_array = Array.new
      vol_array << snapshot
      result.store(snapshot.volume_id, vol_array)
    end
  end
  result

end

#init_logger(options) ⇒ Object



183
184
185
# File 'lib/aebus.rb', line 183

def init_logger(options)
  Logging.log_to_file(options.logfile) unless options.logfile.nil?
end

#purge_snapshot(snapshot_id) ⇒ Object



289
290
291
292
293
294
295
296
297
298
# File 'lib/aebus.rb', line 289

def purge_snapshot(snapshot_id)
  begin
    @ec2.delete_snapshot(:snapshot_id => snapshot_id)
    logger.info("Purged snapshot #{snapshot_id}")
  rescue Aws::EC2::Errors::ServiceError => e
    logger.warn("Could not purge snapshot #{snapshot_id}; underlying message #{e.message}")
    false
  end

end

#send_report(message) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/aebus.rb', line 313

def send_report(message)
  if message.nil?
    logger.warn('Tried to send a message, but no message was specified')
    return
  end
  to_address = @config.defaults['to_address']
  if to_address.instance_of? String
    to_address = [to_address]
  end
  from_address = @config.defaults['from_address']
  if to_address.nil? or from_address.nil?
    logger.warn('Tried to send a message but either to or from address where missing from configuration')
    return
  end
  ses = Aws::SES::Client.new(
    region: @config.defaults['region'],
    credentials: Aws::Credentials.new(@config.defaults['access_key_id'], @config.defaults['secret_access_key'])
  )
  logger.info("Sending report to #{to_address} from account #{from_address}")
  ses.send_email({
    destination: {
      to_addresses: to_address
    },
    source: from_address,
    message: {
      subject: {
        charset: 'UTF-8',
        data: 'Aebus Report'
      },
      body: {
        text: {
            charset: 'UTF-8',
            data: message
        }
      }
    }
  })


end

#status(args, options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aebus.rb', line 22

def status(args, options)
  @current_time_utc = Time.now.utc
  init_logger options
  logger.info("status check started at #{@current_time_utc}")

  @config = Config::Config.new(File.join(File.dirname("."), options.config), @current_time_utc)

  @ec2 = Aws::EC2::Client.new(
     region: @config.defaults['region'],
     credentials: {
       access_key_id: @config.defaults['access_key_id'],
       secret_access_key: @config.defaults['secret_access_key']
     })



  target_volumes = target_volumes(args)

  abort('Configuration contains invalid volumes') unless validate_target_volumes(target_volumes)

  status = check_status(target_volumes)

  message = "status check completed - #{status[:total]} volume(s) checked,  #{status[:to_backup]} to be backed up, max delay detected #{status[:delay]}s, #{status[:to_purge]} snapshots to be purged"
  logger.info message
  puts message



end

#target_volumes(args) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/aebus.rb', line 172

def target_volumes(args)

  result = @config.volume_ids
  if args && (args.count > 0)
    result &= args
  end

  result

end

#validate_target_volumes(target_volumes) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/aebus.rb', line 300

def validate_target_volumes(target_volumes)
  logger.info(target_volumes)
  begin
    @ec2.describe_volumes({volume_ids: target_volumes})
    logger.info('Target volumes validated')
    true
  rescue Aws::EC2::Errors::ServiceError => e
    logger.error("Target validation failed with message '#{e.message}' Check your configuration")
    false
  end

end