Class: VGH::EC2::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/vgh/ec2/snapshot.rb

Overview

Creates a snapshot of the specified volume.

Usage

Snapshot.new(volume_id, description, tags)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionString (readonly)

Returns The description of the snapshot.

Returns:

  • (String)

    The description of the snapshot



30
31
32
# File 'lib/vgh/ec2/snapshot.rb', line 30

def description
  @description
end

#snapshotObject (readonly)

Returns The Snapshot object.

Returns:

  • (Object)

    The Snapshot object



36
37
38
# File 'lib/vgh/ec2/snapshot.rb', line 36

def snapshot
  @snapshot
end

#tagsHash (readonly)

Returns The tags hash.

Returns:

  • (Hash)

    The tags hash



33
34
35
# File 'lib/vgh/ec2/snapshot.rb', line 33

def tags
  @tags
end

#volume_idString (readonly)

Returns The Volume ID.

Returns:

  • (String)

    The Volume ID



27
28
29
# File 'lib/vgh/ec2/snapshot.rb', line 27

def volume_id
  @volume_id
end

Instance Method Details

#all_backupsObject

Returns a list of snapshots that are named the same with the current FQDN.



121
122
123
124
125
# File 'lib/vgh/ec2/snapshot.rb', line 121

def all_backups
  @all ||= ec2.snapshots.
    with_owner('self').
    tagged('Name').tagged_values(fqdn)
end

#backup_expirationObject

Check for a an expiration period in the configuration file



99
100
101
102
103
104
105
106
107
# File 'lib/vgh/ec2/snapshot.rb', line 99

def backup_expiration
  expiration = config[:expiration]
  if expiration.nil?
    @backup_expiration = 7
  else
    @backup_expiration = expiration
  end
  return @backup_expiration.to_i
end

#checkpoints_to_keepObject

How many checkpoints should be kept



110
111
112
113
114
115
116
117
118
# File 'lib/vgh/ec2/snapshot.rb', line 110

def checkpoints_to_keep
  keep = config[:checkpoints]
  if keep.nil?
    @checkpoints_to_keep = 5
  else
    @checkpoints_to_keep = keep
  end
  return @checkpoints_to_keep.to_i
end

#create_snapshotObject

Creates a snapshot for the specified volume

Returns:

  • (Object)

    The newly created snapshot object



40
41
42
43
44
45
46
# File 'lib/vgh/ec2/snapshot.rb', line 40

def create_snapshot
  @snapshot = ec2.volumes[volume_id].
    create_snapshot("#{description}")
  message.info "Created snapshot \"#{snapshot.id}\""
  tag_snapshot
  return @snapshot
end

#expired_backupsArray

Creates a list of expired snapshots according to the expiration time specified in the app’s configuration file

Returns:

  • (Array)

    An array of expired snapshot objects



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vgh/ec2/snapshot.rb', line 76

def expired_backups
  @expired_backups = []
  all_backups.tagged('BACKUP').
    tagged('Name').tagged_values(fqdn).
    each {|snap|
      if snap.start_time < (Time.now - backup_expiration*24*60*60)
        @expired_backups.push snap
      end
    }
  return @expired_backups
end

#expired_checkpointsArray

Creates a list of checkpoints that should be purged

Returns:

  • (Array)

    An array of snapshot objects



90
91
92
93
94
95
96
# File 'lib/vgh/ec2/snapshot.rb', line 90

def expired_checkpoints
  @expired_checkpoints = all_backups.
    tagged('CHECKPOINT').
    tagged('Name').tagged_values(fqdn).
    sort_by(&:start_time).reverse.
    drop(checkpoints_to_keep)
end

#purge_backupsObject

Purges expired snapshots



58
59
60
61
62
63
# File 'lib/vgh/ec2/snapshot.rb', line 58

def purge_backups
  expired_backups.each do |snap|
    message.info "Deleting expired snapshot (#{snap.id})"
    snap.delete
  end
end

#purge_checkpointsObject

Purges expired snapshots



66
67
68
69
70
71
# File 'lib/vgh/ec2/snapshot.rb', line 66

def purge_checkpoints
  expired_checkpoints.each do |snap|
    message.info "Deleting checkpoint (#{snap.id})"
    snap.delete
  end
end

#snap_and_tag(volume_id, description, tags) ⇒ Snapshot

Create and tag snapshot, and also purge expired ones

Parameters:

  • volume_id (String)

    The ID of the volume to snapshot

  • description (String)

    The description for the new snapshot

  • tags (Hash)

    A Hash containing the names and values of the tags

Returns:



18
19
20
21
22
23
24
# File 'lib/vgh/ec2/snapshot.rb', line 18

def snap_and_tag(volume_id, description, tags)
  @volume_id   = volume_id
  @description = description
  @tags        = tags
  create_snapshot
  return snapshot
end

#tag_snapshotObject

Tags a Snapshot



49
50
51
52
53
54
55
# File 'lib/vgh/ec2/snapshot.rb', line 49

def tag_snapshot
  snap = snapshot
  message.info "Tagging snapshot \"#{snap.id}\"..."
  tags.map {|key, value|
    ec2.tags.create(snap, key, {:value => value})
  }
end