Module: Rudy::Routines::DiskHelper

Extended by:
DiskHelper
Includes:
HelperBase
Included in:
DiskHelper
Defined in:
lib/rudy/routines/helpers/diskhelper.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HelperBase

#command_separator, #execute_rbox_command, #keep_going?

Methods included from Huxtable

change_environment, change_position, change_region, change_role, change_zone, #check_keys, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_size, #current_user, #current_user_keypairpath, debug?, #debug?, domain, domain_exists?, #group_metadata, #has_keypair?, #has_keys?, #has_pem_keys?, #has_root_keypair?, keypair_path_to_name, #known_machine_group?, #root_keypairname, #root_keypairpath, #switch_user, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath

Class Method Details

.rudy_mkfs(*args) ⇒ Object

We need to add mkfs since it’s not enabled by default. We add it only to this instance we’re using. We give it a funny name so we can delete it.



29
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 29

def @rbox.rudy_mkfs(*args); cmd('mkfs', args); end

Instance Method Details

#create(disks) ⇒ Object



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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 135

def create(disks)
  rdisk = Rudy::Disks.new
  
  disks.each_pair do |path, props|
    disk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
    olddisk = rdisk.get(disk.name)
    if olddisk && olddisk.exists?
      olddisk.update
      puts "Disk found: #{olddisk.name}"
      if olddisk.attached?
        puts "In use. Skipping...".color(:red)
        return
      else
        disk = olddisk
      end
    else
      puts "Creating #{disk.name} "
      disk.fstype = props[:fstype] || 'ext3'
    end
    
    unless disk.exists? # Checks the EBS volume
      msg = "Creating volume... "
      disk.create
      Rudy::Utils.waiter(2, 60, STDOUT, msg) { 
        disk.available?
      }
    end
    
    msg = "Attaching #{disk.awsid} to #{@machine.awsid}... "
    disk.attach(@machine.awsid)
    Rudy::Utils.waiter(2, 10, STDOUT, msg) { 
      disk.attached?
    }
    
    # The device needs some time. 
    # Otherwise mkfs returns:
    # "No such file or directory while trying to determine filesystem size"
    sleep 2 
    
    # TODO: Cleanup. See ScriptHelper
    begin
      if disk.raw == true
        print "Creating #{disk.fstype} filesystem for #{disk.device}... "
        @rbox.rudy_mkfs(:t, disk.fstype, :F, disk.device)
        disk.raw = false
        disk.save
        puts "done"
      end
      
      @rbox.mkdir(:p, disk.path)
      
      print "Mounting at #{disk.path}... "
  
      ret = @rbox.mount(:t, disk.fstype, disk.device, disk.path) 
      print_response ret
      if ret.exit_code > 0
        STDERR.puts "Error creating disk".color(:red)
        return
      else
        puts "done"
      end
      disk.mounted = true
      disk.save
      
    rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch => ex  
      STDERR.puts "Error creating disk".color(:red)
      STDERR.puts ex.message.color(:red)
     rescue Rye::CommandNotFound => ex
      puts "  CommandNotFound: #{ex.message}".color(:red)
      
    rescue
      STDERR.puts "Error creating disk" .color(:red)
      Rudy::Utils.bug
    end
 
  end
end

#destroy(disks) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 295

def destroy(disks)
  rdisk = Rudy::Disks.new
  
  disks.each_pair do |path, props|
    adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
    disk = rdisk.get(adisk.name)
    if disk == nil
      puts "Not found: #{adisk.name}".color(:red)
      return
    end
    
    puts "Destroying #{disk.name}"

    if disk.mounted?
      print "Unmounting #{disk.path}..."
      execute_rbox_command { @rbox.umount(disk.path) }
      puts " done"
      sleep 0.5
    end
    
    if disk.attached?
      msg = "Detaching #{disk.awsid}..."
      disk.detach 
      Rudy::Utils.waiter(2, 60, STDOUT, msg) { 
        disk.available? 
      }
      sleep 0.5
    end
    
    puts "Destroying volume and metadata... "
    disk.destroy
    
  end
end

#disks?(routine) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 8

def disks?(routine)
  (routine.is_a?(Caesars::Hash) && routine.disks && 
  routine.disks.is_a?(Caesars::Hash) && !routine.disks.empty?) ? true : false
end

#execute(routine, machine, rbox) ⇒ Object



18
19
20
21
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
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 18

def execute(routine, machine, rbox)
  return unless routine
  raise "Not a Rudy::Machine" unless machine.is_a?(Rudy::Machine)
  raise "Not a Rye::Box" unless rbox.is_a?(Rye::Box)
  
  @machine = machine
  @rbox = rbox
  
  # We need to add mkfs since it's not enabled by default. 
  # We add it only to this instance we're using. 
  # We give it a funny name so we can delete it. 
  def @rbox.rudy_mkfs(*args); cmd('mkfs', args); end
  
  unless disks?(routine)
    STDERR.puts "[nothing to do]"
    return
  end

  modified = []
  routine.disks.each_pair do |action, disks|
    unless DiskHelper.respond_to?(action)  
      STDERR.puts %Q(DiskHelper: unknown action "#{action}")
      next
    end
    send(action, disks) # create, copy, destroy, ...
    modified << disks
  end
  
  # TODO: remove rudy_mkfs method
  
end

#mount(disks) ⇒ Object



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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 213

def mount(disks)
  rdisk = Rudy::Disks.new
  disks.each_pair do |path, props|
    adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
    disk = rdisk.get(adisk.name)
    if disk == nil
      puts "Not found: #{adisk.name}".color(:red)
      return
    end
    
    msg = "Attaching #{disk.awsid} to #{@machine.awsid}... "
    disk.attach(@machine.awsid)
    Rudy::Utils.waiter(2, 10, STDOUT, msg) { 
      disk.attached?
    }
    
    sleep 2
    
    begin
      @rbox.mkdir(:p, disk.path)
      
      print "Mounting at #{disk.path}... "
  
      ret = @rbox.mount(:t, disk.fstype, disk.device, disk.path) 
      print_response ret
      if ret.exit_code > 0
        STDERR.puts "Error creating disk".color(:red)
        return
      else
        puts "done"
      end
      disk.mounted = true
      disk.save
      
    rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch => ex  
      STDERR.puts "Error creating disk".color(:red)
      STDERR.puts ex.message.color(:red)
     rescue Rye::CommandNotFound => ex
      puts "  CommandNotFound: #{ex.message}".color(:red)
      
    rescue
      STDERR.puts "Error creating disk" .color(:red)
      Rudy::Utils.bug
    end
    
  end
end

#paths(routine) ⇒ Object



13
14
15
16
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 13

def paths(routine)
  return nil unless disks?(routine)
  routine.disks.values.collect { |d| d.keys }.flatten
end

#restore(disks) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 66

def restore(disks)
  rdisk = Rudy::Disks.new
  rback = Rudy::Backups.new
  
  disks.each_pair do |path, props|
    disk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
    
    olddisk = rdisk.get(disk.name)
    back = nil
    if olddisk && olddisk.exists?
      olddisk.update
      puts "Disk found: #{olddisk.name}. Skipping...".color(:red)
      return
    else
      disk.fstype = props[:fstype] || 'ext3'
      back = (rback.list(nil, nil, props) || []).first
      raise "No backup found" unless back
      puts "Found backup #{back.name} "
    end
    
    
    
    unless disk.exists? # Checks the EBS volume
      msg = "Creating volume from snapshot (#{back.awsid})... "
      disk.create(back.size, @@global.zone, back.awsid)
      Rudy::Utils.waiter(2, 60, STDOUT, msg) { 
        disk.available?
      }
    end
    
    msg = "Attaching #{disk.awsid} to #{@machine.awsid}... "
    disk.attach(@machine.awsid)
    Rudy::Utils.waiter(2, 10, STDOUT, msg) { 
      disk.attached?
    }
    
    sleep 2
    
    begin
      @rbox.mkdir(:p, disk.path)
      
      print "Mounting at #{disk.path}... "
  
      ret = @rbox.mount(:t, disk.fstype, disk.device, disk.path) 
      print_response ret
      if ret.exit_code > 0
        STDERR.puts "Error creating disk".color(:red)
        return
      else
        puts "done"
      end
      disk.mounted = true
      disk.save
      
    rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch => ex  
      STDERR.puts "Error creating disk".color(:red)
      STDERR.puts ex.message.color(:red)
     rescue Rye::CommandNotFound => ex
      puts "  CommandNotFound: #{ex.message}".color(:red)
      
    rescue
      STDERR.puts "Error creating disk" .color(:red)
      Rudy::Utils.bug
    end
    
  end
end

#snapshot(disks) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 50

def snapshot(disks)
  rdisk = Rudy::Disks.new
  rback = Rudy::Backups.new
  
  disks.each_pair do |path, props|
    adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
    disk = rdisk.get(adisk.name)
    if disk == nil
      puts "Not found: #{adisk.name}".color(:red)
      return
    end
    back = disk.backup
    puts "Created backup: #{back.name}"
  end
end

#umount(disks) ⇒ Object Also known as: unmount



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rudy/routines/helpers/diskhelper.rb', line 262

def umount(disks)
  rdisk = Rudy::Disks.new
  disks.each_pair do |path, props|
    adisk = Rudy::MetaData::Disk.new(path, props[:size], props[:device], @machine.position)
    disk = rdisk.get(adisk.name)
    if disk == nil
      puts "Not found: #{adisk.name}".color(:red)
      return
    end
    
    if disk.mounted?
      print "Unmounting #{disk.path}..."
      execute_rbox_command { @rbox.umount(disk.path) }
      puts " done"
      sleep 0.5
    end
    
    sleep 2
    
    if disk.attached?
      msg = "Detaching #{disk.awsid}..."
      disk.detach 
      Rudy::Utils.waiter(2, 60, STDOUT, msg) { 
        disk.available? 
      }
      sleep 0.5
    end
    
    
  end
end