Module: PoolParty::Remote

Includes:
Pinger
Included in:
PoolParty, Cloud::Cloud, Installers::BaseInstaller, Pool::Pool, Provision::BootStrapper, Provision::DrConfigure, Ec2RemoteInstance, RemoterBase
Defined in:
lib/poolparty/net/remoter_base.rb,
lib/poolparty/net/remote_instance.rb,
lib/poolparty/net/remoter/connections.rb,
lib/poolparty/net/remoter/interactive.rb,
lib/poolparty/net/remoter/cloud_control.rb,
lib/poolparty/net/remoter_bases/ec2/ec2.rb,
lib/poolparty/net/remoter_bases/vmrun/vmrun.rb,
lib/poolparty/net/remoter_bases/libvirt/libvirt.rb,
lib/poolparty/net/remoter_bases/metavirt/metavirt.rb,
lib/poolparty/net/remoter_bases/vmrun/vmrun_instance.rb,
lib/poolparty/net/remoter_bases/ec2/ec2_remote_instance.rb,
lib/poolparty/net/remoter_bases/libvirt/libvirt_instance.rb,
lib/poolparty/net/remoter_bases/metavirt/metavirt_instance.rb

Defined Under Namespace

Classes: Ec2, Ec2RemoteInstance, Libvirt, LibvirtInstance, Metavirt, MetavirtInstance, RemoteInstance, RemoterBase, Vmrun, VmwareInstance

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pinger

included

Class Method Details

.availableObject



23
24
25
# File 'lib/poolparty/net/remoter_base.rb', line 23

def self.available
  @available ||= []
end

Instance Method Details

#are_any_nodes_exceeding_minimum_runtime?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/poolparty/net/remoter/cloud_control.rb', line 19

def are_any_nodes_exceeding_minimum_runtime?
  !list_of_nodes_exceeding_minimum_runtime.blank?
end

#are_too_few_instances_running?Boolean

# Are too few instances running?

Returns:

  • (Boolean)


7
8
9
# File 'lib/poolparty/net/remoter/cloud_control.rb', line 7

def are_too_few_instances_running?
  nodes(:status => "running").size < minimum_instances.to_i
end

#are_too_many_instances_running?Boolean

# Are there more instances than allowed?

Returns:

  • (Boolean)


11
12
13
# File 'lib/poolparty/net/remoter/cloud_control.rb', line 11

def are_too_many_instances_running?
  nodes(:status => "running").size > maximum_instances.to_i
end

#commandsObject



52
53
54
# File 'lib/poolparty/net/remoter/connections.rb', line 52

def commands
  @commands ||= Array.new
end

#execute!(cmds = commands) ⇒ Object

TODO: make extendable multithreaded version



57
58
59
60
61
62
# File 'lib/poolparty/net/remoter/connections.rb', line 57

def execute!(cmds=commands)
  netssh(
    [cmds.compact.join(' && ')],
    :host=>target_host, :user=>'root')
  # commands.each {|c| run_remote(c, target_host) }
end

#list_of_instances(with_neighborhood_default = true) ⇒ Object

Select the list of instances, either based on the neighborhoods loaded from /etc/poolparty/neighborhood.json or by the remote_base on keypair



30
31
32
33
34
35
# File 'lib/poolparty/net/remoter/interactive.rb', line 30

def list_of_instances(with_neighborhood_default=true)
  return @list_of_instances if @list_of_instances
  @containing_cloud = self
  n = false ? Neighborhoods.load_default : nil
  @list_of_instances = ((n.nil? || n.instances.empty?) ? _list_of_instances : n.instances)
end

#list_of_nodes_exceeding_minimum_runtimeObject



15
16
17
# File 'lib/poolparty/net/remoter/cloud_control.rb', line 15

def list_of_nodes_exceeding_minimum_runtime
  nodes(:status => "running").reject{|i| i.elapsed_runtime < minimum_runtime}
end

#netssh(cmds = [], opts = {}) ⇒ Object



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
# File 'lib/poolparty/net/remoter/connections.rb', line 64

def netssh(cmds=[], opts={})
  user = opts.delete(:user) || 'root' #user rescue 'root'
  host = opts.delete(:host) || target_host
  ssh_options_hash = {:keys         => [keypair.full_filepath],
                      :auth_methods => 'publickey',
                      :paranoid     => false,
                      :timeout      => 3.minutes,
                      :user         => user
                       }.merge(opts)
  # ssh_options_hash[:verbose]=:debug if debugging?
  puts "connecting to ssh with options = #{ssh_options_hash.inspect}"

  Net::SSH.start(host, user, ssh_options_hash) do |ssh|
    cmds.each do |command|
      $stderr.print command if debugging
      ssh.exec!(command) do |ch, stream, data|
        if stream == :stdout
         print data
        else
          $stderr.print "#{host} stderr => #{data}"
        end
      end
    end
  end
end

#nodes(hsh = {}, with_neighborhood_default = false) ⇒ Object

Select a list of instances based on their status



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/poolparty/net/remoter/interactive.rb', line 5

def nodes(hsh={}, with_neighborhood_default=false)
  unordered = begin
    if with_neighborhood_default
      list_of_instances(with_neighborhood_default).select_with_hash(hsh)
    else
      kname = (hsh[:keypair_name] ||self.keypair_name || self.keypair_name || keypair.basename)
      # Added keypair to filter on either keypair_name or keypair response
      key_condition = {:keypair_name => kname, :keypair => kname, :key_name => kname}
      # if hsh.delete(:uncached)
      #   @nodes = describe_instances.select_with_hash(conditions)
      # else
      #   @nodes ||= describe_instances.select_with_hash(conditions)
      # end
      # 
      results = describe_instances.select_with_hash(key_condition)
      results.select_with_hash(hsh)
    end
  end
  
  unordered.sort_by(&:launch_time) # provide consistent sorting for nodes 
end

#remote_rsync_commandObject



133
134
135
# File 'lib/poolparty/net/remoter/connections.rb', line 133

def remote_rsync_command
  "rsync -azP --exclude cache -e '#{remote_ssh_string}'"
end

#remote_ssh_arrayObject



127
128
129
# File 'lib/poolparty/net/remoter/connections.rb', line 127

def remote_ssh_array
  ["-o StrictHostKeyChecking=no", "-l '#{Default.user}'", '-i "'+remote_keypair_path+'"']
end

#remote_ssh_stringObject



130
131
132
# File 'lib/poolparty/net/remoter/connections.rb', line 130

def remote_ssh_string
  (["ssh"] << remote_ssh_array).join(" ")
end

#rsync(source_path, destination_path, rsync_opts = ['-v -a']) ⇒ Object



39
40
41
42
43
# File 'lib/poolparty/net/remoter/connections.rb', line 39

def rsync( source_path, destination_path, rsync_opts=['-v -a'] )
  dputs "rsync -e 'ssh #{ssh_options}' #{rsync_opts.join(' ')} #{source_path}  root@#{target_host}:#{destination_path}"
  out = %x{ rsync -e 'ssh #{ssh_options}' #{rsync_opts.join(' ')} #{source_path}  root@#{target_host}:#{destination_path} }
  puts out if debugging?
end

#rsync_commandObject



124
125
126
# File 'lib/poolparty/net/remoter/connections.rb', line 124

def rsync_command
  "rsync -azP --exclude cache -e '#{ssh_string} -l #{Default.user}'"
end

#rsync_storage_files_to(instance = nil) ⇒ Object

Rsync command to the instance



150
151
152
# File 'lib/poolparty/net/remoter/connections.rb', line 150

def rsync_storage_files_to(instance=nil)
  hide_output {Kernel.system "#{rsync_storage_files_to_command(instance)}" if instance}
end

#rsync_storage_files_to_command(remote_instance) ⇒ Object

TODO: Delete deprecated commands below here



94
95
96
97
# File 'lib/poolparty/net/remoter/connections.rb', line 94

def rsync_storage_files_to_command(remote_instance)
  #TODO: rsync_to_command("#{Default.storage_directory}/", Default.remote_storage_path, remote_storage_path) if remote_instance
  "#{rsync_command} #{Default.storage_directory}/ #{remote_instance.ip}:#{Default.remote_storage_path}" if remote_instance
end

#rsync_to(source, target = source, num = 0) ⇒ Object

Rsync a file or directory to a node. Rsync to master by default



143
144
145
146
147
# File 'lib/poolparty/net/remoter/connections.rb', line 143

def rsync_to(source, target=source, num=0)
  str = "#{rsync_to_command(source, target, get_instance_by_number( num ))}"
  dputs "Running: #{str}"
  verbose ?  Kernel.system(str) : hide_output {Kernel.system str}
end

#rsync_to_command(source, target = source, remote_instance = master) ⇒ Object

rsync a file to a node. By default to the master node.



99
100
101
# File 'lib/poolparty/net/remoter/connections.rb', line 99

def rsync_to_command(source, target=source, remote_instance=master)
  "#{rsync_command} #{source} #{remote_instance.ip}:#{target}"
end

#run_command_on(cmd, instance = nil) ⇒ Object

Take the rsync command and execute it on the system if there is an instance given



155
156
157
# File 'lib/poolparty/net/remoter/connections.rb', line 155

def run_command_on(cmd, instance=nil)        
  Kernel.system "#{run_command_on_command(cmd, instance)}"
end

#run_command_on_command(cmd = "ls -l", remote_instance = nil) ⇒ Object



102
103
104
105
# File 'lib/poolparty/net/remoter/connections.rb', line 102

def run_command_on_command(cmd="ls -l", remote_instance=nil)
  vputs "Running #{cmd} on #{remote_instance.name == %x[hostname].chomp ? "self (master)" : "#{remote_instance.name}"}"
  (remote_instance.nil? || remote_instance.name == %x[hostname].chomp) ? %x[#{cmd}] : "#{ssh_command(remote_instance)} '#{cmd}'"
end

#run_command_on_instance_number(cmd = "ls -l", num = 0) ⇒ Object

Run command on the instance by the number



172
173
174
# File 'lib/poolparty/net/remoter/connections.rb', line 172

def run_command_on_instance_number(cmd="ls -l", num=0)
  run_command_on(cmd, get_instance_by_number( num ) )
end

#run_local(commands) ⇒ Object

Run commands on the local machine, i.e. your laptop, not the server



46
47
48
49
50
# File 'lib/poolparty/net/remoter/connections.rb', line 46

def run_local(commands)
  commands.each do |cmd|
    puts `#{cmd}`
  end
end

#run_remote(hst, cmds) ⇒ Object



21
22
23
# File 'lib/poolparty/net/remoter/connections.rb', line 21

def run_remote( hst, cmds )
  netssh hst, cmds
end

#scp_arrayObject



121
122
123
# File 'lib/poolparty/net/remoter/connections.rb', line 121

def scp_array
  ["-o StrictHostKeyChecking=no", '-i "'+full_keypair_path+'"']
end

#scp_to_command(source, dest = target, remote_instance = master) ⇒ Object



137
138
139
140
# File 'lib/poolparty/net/remoter/connections.rb', line 137

def scp_to_command(source, dest=target, remote_instance=master)
  #TODO: check if source is Directory and add -r if it is
  "scp #{source} #{remote_instance.ip}:#{dest} #{scp_array.join(' ')}"
end

#simplest_run_remote(host = target_host, command = [], extra_ssh_ops = {}) ⇒ Object

Simply shell out and call ssh, simple, reliable and fewest dependencies, but slow



14
15
16
17
18
19
# File 'lib/poolparty/net/remoter/connections.rb', line 14

def simplest_run_remote(host=target_host, command=[], extra_ssh_ops={})
  command = command.compact.join(' && ') if command.is_a? Array
  cmd = "ssh #{host} #{ssh_options(extra_ssh_ops)} '#{command}'"
  puts "\n--------------------------------\nrunning_remote:\n #{cmd}\n"
  puts %x{#{cmd}}
end

#ssh_arrayObject

Array of ssh options Includes StrictHostKeyChecking to no Ssh with the user in Base And including the keypair_path “-l ‘#Default.user’”,



118
119
120
# File 'lib/poolparty/net/remoter/connections.rb', line 118

def ssh_array
  ["-o StrictHostKeyChecking=no", "-l #{Default.user}", '-i "'+full_keypair_path+'"']
end

#ssh_command(remote_instance) ⇒ Object



106
107
108
# File 'lib/poolparty/net/remoter/connections.rb', line 106

def ssh_command(remote_instance)
  "#{ssh_string} #{remote_instance.ip}"
end

#ssh_into(inst, extra_ssh_ops = {}) ⇒ Object



25
26
27
28
29
# File 'lib/poolparty/net/remoter/connections.rb', line 25

def ssh_into(inst, extra_ssh_ops={})
  ip =  ((inst.respond_to?(:has_key) && inst.has_key?(:ip)) || inst.respond_to?(:ip)) ? inst.ip : inst
  cmd = extra_ssh_ops.delete(:cmd) if extra_ssh_ops.has_key?(:cmd)
  Kernel.system("ssh #{ssh_options(extra_ssh_ops)} #{ip}%s" % [cmd ? " #{cmd}" : ""])
end

#ssh_into_instance_number(num = 0) ⇒ Object

Ssh into the instance given def ssh_into(instance)

cmd = "#{ssh_command(instance)}"
vputs "Running #{cmd}"
Kernel.system cmd if instance

end Find the instance by the number given and then ssh into the instance



167
168
169
# File 'lib/poolparty/net/remoter/connections.rb', line 167

def ssh_into_instance_number(num=0)
  ssh_into( get_instance_by_number( num ) )
end

#ssh_options(opts = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/poolparty/net/remoter/connections.rb', line 31

def ssh_options(opts={})
  o = {"-i" => keypair.full_filepath,
       "-l" => user,
       "-o" =>"StrictHostKeyChecking=no"
       }.merge(opts)
  o.collect{ |k,v| "#{k} #{v}"}.join(' ')
end

#ssh_stringObject

Generic commandable strings



110
111
112
# File 'lib/poolparty/net/remoter/connections.rb', line 110

def ssh_string
  (["ssh"] << ssh_array).join(" ")
end

#target_host(dns_or_ip = nil) ⇒ Object



9
10
11
# File 'lib/poolparty/net/remoter/connections.rb', line 9

def target_host(dns_or_ip=nil)
  dns_or_ip ? @target_host=dns_or_ip : @target_host
end