Class: Rudy::CLI::Machines

Inherits:
CommandBase
  • Object
show all
Defined in:
lib/rudy/cli/machines.rb

Instance Attribute Summary

Attributes inherited from CommandBase

#config

Instance Method Summary collapse

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

#machinesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rudy/cli/machines.rb', line 8

def machines
  # Rudy::Machines.list takes two optional args for adding or 
  # removing metadata attributes to modify the select query. 
  # When all is specified we want to find machines in every
  # environment and role to we remove these attributes from
  # the select. 
  more, less = nil, nil
  less = [:environment, :role] if @option.all
  
  rmach = Rudy::Machines.new
  mlist = rmach.list(more, less) || []
  if mlist.empty?
    if @option.all
      puts "No machines running"
    else
      puts "No machines running in #{current_machine_group}" 
      puts "Try: rudy machines --all"
    end
  end
  mlist.each do |m|
    puts @@global.verbose > 0 ? m.inspect : m.dump(@@global.format)
  end
end

#machines_washObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rudy/cli/machines.rb', line 32

def machines_wash
  rmach = Rudy::Machines.new
  dirt = (rmach.list || []).select { |m| !m.running? }
  if dirt.empty?
    puts "Nothing to wash in #{rmach.current_machine_group}"
    return
  end
  
  puts "The following machine metadata will be deleted:".bright
  puts dirt.collect {|m| m.name.bright }
  execute_check(:medium)
  
  dirt.each do |m|
    m.destroy
  end
  
end

#sshObject



51
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rudy/cli/machines.rb', line 51

def ssh
  # TODO: Give this methos a good look over
  pkey = user_keypairpath(@@global.user)
  unless pkey
    puts "No private key configured for #{@@global.user} in #{current_machine_group}"
  end
  
  # Options to be sent to Net::SSH
  ssh_opts = { :user => @@global.user || Rudy.sysinfo.user, :debug => nil }
  if pkey 
    raise "Cannot find file #{pkey}" unless File.exists?(pkey)
    raise InsecureKeyPermissions, @pkey unless File.stat(pkey).mode == 33152
    ssh_opts[:keys] = pkey 
  end


  # The user specified a command to run. We won't create an interactive
  # session so we need to prepare the command and its arguments
  if @argv.first
    command, command_args = @argv.shift, @argv || []
    puts "#{command} #{command_args.join(' ')}" if @@global.verbose > 1

  # otherwise, we'll open an ssh session or print command
  else
    command, command_args = :interactive_ssh, @option.print.nil?
  end


  checked = false
  rudy = Rudy::Machines.new
  lt = rudy.list 
  unless lt
    puts "No machines running in #{rudy.current_machine_group}"
    exit
  end
  lt.each do |machine|
    machine.update  # make sure we have the latest DNS info
    
    # Print header
    if @@global.quiet
      print "You are #{ssh_opts[:user].to_s.bright}. " if !checked # only the 1st
    else
      puts machine_separator(machine.name, machine.awsid)
      puts "Connecting #{ssh_opts[:user].to_s.bright}@#{machine.dns_public} "
      puts
    end

    # Make sure we want to run this command on all instances
    if !checked && command != :interactive_ssh 
      execute_check(:low) if ssh_opts[:user] == "root"
      checked = true
    end
    
    # Open the connection and run the command
    rbox = Rye::Box.new(machine.dns_public, ssh_opts)
    ret = rbox.send(command, command_args)
    puts ret unless command == :interactive_ssh
  end
end