Class: Rudy::Machines

Inherits:
Object
  • Object
show all
Includes:
MetaData
Defined in:
lib/rudy/machines.rb

Defined Under Namespace

Classes: Offline

Instance Method Summary collapse

Methods included from MetaData

#format_timestamp, #init, #initialize

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

Instance Method Details

#create(&each_mach) ⇒ Object



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
212
213
214
215
# File 'lib/rudy/machines.rb', line 176

def create(&each_mach)
  raise MachineGroupAlreadyRunning, current_machine_group if running?
  raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
  
  unless (1..MAX_INSTANCES).member?(current_machine_count)
    raise "Instance count must be more than 0, less than #{MAX_INSTANCES}"
  end
  
  unless @rgrp.exists?(current_group_name)
    puts "Creating group: #{current_group_name}"
    @rgrp.create(current_group_name)
  end
  
  unless @rkey.exists?(root_keypairname)
    kp_file = File.join(Rudy::CONFIG_DIR, root_keypairname)
    raise PrivateKeyFileExists, kp_file if File.exists?(kp_file)
    puts "Creating keypair: #{root_keypairname}"
    kp = @rkey.create(root_keypairname)
    puts "Saving #{kp_file}"
    Rudy::Utils.write_to_file(kp_file, kp.private_key, 'w', 0600)
  else
    kp_file = root_keypairpath
    # This means no keypair file can be found
    raise PrivateKeyNotFound, root_keypairname if kp_file.nil?
    # This means we found a keypair in the config but we cannot find the private key file. 
    raise PrivateKeyNotFound, kp_file if !File.exists?(kp_file)
  end
  
  machines = []
  current_machine_count.times do  |i|
    machine = Rudy::Machine.new
    
    #puts "Starting %s" % machine.name
    
    machine.start
    machines << machine
  end
  machines.each { |m| each_mach.call(m) } if each_mach
  machines
end

#destroy(&each_mach) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/rudy/machines.rb', line 218

def destroy(&each_mach)
  raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
  raise MachineGroupNotRunning, current_machine_group unless running?
  list.each { |m| each_mach.call(m); } if each_mach
  list do |mach|
    #puts "Destroying #{mach.name}"
    mach.destroy
  end
end

#get(rname = nil) ⇒ Object



259
260
261
# File 'lib/rudy/machines.rb', line 259

def get(rname=nil)
  Rudy::Machine.from_hash(@sdb.get(Rudy::DOMAIN, rname)) # Returns nil if empty
end

#list(more = [], less = [], &each_mach) ⇒ Object



241
242
243
244
245
# File 'lib/rudy/machines.rb', line 241

def list(more=[], less=[], &each_mach)
  machines = list_as_hash(more, less, &each_mach)
  machines &&= machines.values
  machines
end

#list_as_hash(more = [], less = [], &each_mach) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rudy/machines.rb', line 247

def list_as_hash(more=[], less=[], &each_mach)
  query = to_select([:rtype, 'm'], less)
  list = @sdb.select(query) || {}
  machines = {}
  list.each_pair do |n,m|
    machines[n] = Rudy::Machine.from_hash(m)
  end
  machines.each_pair { |n,mach| each_mach.call(mach) } if each_mach
  machines = nil if machines.empty?
  machines
end

#restart(&each_mach) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rudy/machines.rb', line 229

def restart(&each_mach)
  raise MachineGroupNotDefined, current_machine_group unless known_machine_group?
  raise MachineGroupNotRunning, current_machine_group unless running?
  machines = list
  machines.each do |mach|
    each_mach.call(mach) if each_mach
    puts "Restarting #{mach.name}"
    mach.restart
  end
  machines
end

#running?Boolean

Returns:

  • (Boolean)


264
265
266
267
# File 'lib/rudy/machines.rb', line 264

def running?
  !list.nil?
  # TODO: add logic that checks whether the instances are running.
end