Class: Rudy::Backup

Inherits:
Storable
  • Object
show all
Includes:
Gibbler::Complex, Metadata
Defined in:
lib/rudy/metadata/backup.rb

Constant Summary

Constants included from Metadata

Metadata::COMMON_FIELDS

Instance Method Summary collapse

Methods included from Metadata

#==, build_criteria, connect, create_domain, destroy, destroy_domain, domain, domain=, exists?, #exists?, get, get_rclass, included, put, #refresh!, #save, select

Methods included from Huxtable

config, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_bucket, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_os, #current_machine_size, #current_machine_user, #current_user_keypairname, #current_user_keypairpath, #defined_keypairpath, domain, domain_exists?, global, keypair_path_to_name, #known_machine_group?, ld, #ld, #le, le, li, #li, logger, reset_config, reset_global, #root_keypairname, #root_keypairpath, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath

Constructor Details

#initialize(position = nil, path = nil, opts = {}) ⇒ Backup

If one argument is supplied:

  • path is a an absolute filesystem path

  • opts is a hash of disk options.

If two arguments are supplied:

  • position

  • path is a an absolute filesystem path

  • opts is a hash of disk options.

Valid options are:

  • :path is a an absolute filesystem path (overridden by path arg)

  • :position (overridden by position arg)

  • :created is an instance of Time

  • :user is the name of the user which created the backup.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rudy/metadata/backup.rb', line 42

def initialize(position=nil, path=nil, opts={})
  # Swap arg values if only one is supplied. 
  path, position = position, nil if !position.nil? && path.nil?
  position ||= '01'
  
  opts = {
    :created => Time.now.utc,
    :user => Rudy.sysinfo.user
  }.merge opts
  
  super Rudy::Backups::RTYPE, opts  # Rudy::Metadata#initialize
  
  @position, @path = position, path
  
  postprocess

end

Instance Method Details

#any?Boolean

Are there any backups for the associated disk?

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/rudy/metadata/backup.rb', line 114

def any?
  backups = Rudy::Backups.list self.descriptors, [:year, :month, :day, :hour, :second]
  !backups.nil?
end

#createObject

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rudy/metadata/backup.rb', line 92

def create
  raise DuplicateRecord, self.name if exists?
  disk = self.disk
  ld "DISK: #{disk.name}"
  raise Rudy::Backups::NoDisk, disk.name unless disk.exists?
  @volid ||= disk.volid
  snap = Rudy::AWS::EC2::Snapshots.create(@volid) 
  #snap = Rudy::AWS::EC2::Snapshots.list.first   # debugging
  ld "SNAP: #{snap.inspect}"
  @snapid, @raw = snap.awsid, true
  @size, @fstype = disk.size, disk.fstype
  self.save :replace
  self
end

#dateObject



84
85
86
# File 'lib/rudy/metadata/backup.rb', line 84

def date
  "%s%s%s" % [@year, @month, @day]
end

#descriptorsObject



119
120
121
# File 'lib/rudy/metadata/backup.rb', line 119

def descriptors
  super :position, :path, :year, :month, :day, :hour, :second
end

#destroyObject



123
124
125
126
# File 'lib/rudy/metadata/backup.rb', line 123

def destroy 
  Rudy::AWS::EC2::Snapshots.destroy(@snapid) if snapshot_exists?
  super()
end

#diskObject



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rudy/metadata/backup.rb', line 132

def disk
  opts = {
    :region => @region,  :zone => @zone,
    :environment => @environment, :role => @role, 
    :size => @size, :fstype => @fstype
  }
  disk = Rudy::Disk.new @position, @path, opts
  disk.refresh! if disk.exists?
  disk.size = @size
  disk.fstype = @fstype
  disk
end

#disk_exists?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/rudy/metadata/backup.rb', line 145

def disk_exists?
  self.disk.exists?
end

#nameObject



74
75
76
77
78
79
80
81
82
# File 'lib/rudy/metadata/backup.rb', line 74

def name
  sep = File::SEPARATOR
  dirs = @path.split sep if @path && !@path.empty?
  unless @path == File::SEPARATOR
    dirs.shift while dirs && (dirs[0].nil? || dirs[0].empty?)
  end
  # Calls Rudy::Metadata#name with backup specific components
  super [dirs, date, time, second]
end

#postprocessObject



60
61
62
63
64
65
66
67
68
# File 'lib/rudy/metadata/backup.rb', line 60

def postprocess
  @position &&= @position.to_s.rjust(2, '0')
  @year = @created.year
  @month = @created.month.to_s.rjust(2, '0')
  @day = @created.mday.to_s.rjust(2, '0')
  @hour = @created.hour.to_s.rjust(2, '0')
  @minute = @created.min.to_s.rjust(2, '0')
  @second = @created.sec.to_s.rjust(2, '0')
end

#restoreObject

Raises:



107
108
109
110
111
# File 'lib/rudy/metadata/backup.rb', line 107

def restore
  raise UnknownObject, self.name unless exists?
  raise Rudy::Backups::NoBackup, self.name unless any?
  
end

#timeObject



88
89
90
# File 'lib/rudy/metadata/backup.rb', line 88

def time
  "%s%s" % [@hour, @minute]
end

#to_s(*args) ⇒ Object



70
71
72
# File 'lib/rudy/metadata/backup.rb', line 70

def to_s(*args)
  [self.name.bright, self.snapid, self.volid, self.size, self.fstype].join '; '
end

#valid?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/rudy/metadata/backup.rb', line 128

def valid?
  !@path.nil? && !@path.empty? && @created.is_a?(Time) && !@volid.nil?
end