Module: PoolParty::Remoter::InstanceMethods

Includes:
Callbacks, Scheduler
Defined in:
lib/poolparty/remoter.rb

Instance Attribute Summary

Attributes included from Scheduler

#tasker

Instance Method Summary collapse

Methods included from Scheduler

#_tasker, #add_task, #daemonize, #interval, #run_thread_list, #run_thread_loop, #run_threads

Methods included from Callbacks

included

Instance Method Details

#execute_tasks(opts = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/poolparty/remoter.rb', line 85

def execute_tasks(opts={})
  # reset!
  
  set_hosts(nil) unless opts[:dont_set_hosts]
  
  yield if block_given?
  
  run_array_of_tasks(scp_tasks)
  run_array_of_tasks(ssh_tasks)
end

#remote_command_tasks(commands) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/poolparty/remoter.rb', line 32

def remote_command_tasks commands
  reset!
  commands = commands.join ' && ' if commands.is_a? Array
  
  returning ssh_tasks do |tasks|
    target_hosts.each do |ip|
      ssh_tasks << "#{self.class.ssh_string} #{ip} '#{commands}'"
    end
  end
end

#reset!Object



80
81
82
83
# File 'lib/poolparty/remoter.rb', line 80

def reset!
  @ssh_tasks = @scp_tasks = nil
  @hosts = nil
end

#rsync_tasks(local, remote) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/poolparty/remoter.rb', line 23

def rsync_tasks local, remote
  reset!
  returning scp_tasks do |tasks|
    target_hosts.each do |ip|
      tasks << "#{self.class.rsync_string} #{local} #{ip}:#{remote}"
    end
  end
end

#run(command, on = self) ⇒ Object

Nearly Directly from vlad



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
# File 'lib/poolparty/remoter.rb', line 113

def run command, on=self
  cmd = [self.class.ssh_string, on.ip].compact
  result = []

  commander = cmd.join(" ") << " \"#{command.runnable}\""        
  
  pid, inn, out, err = Open4::popen4(commander)

  inn.sync   = true
  streams    = [out, err]
  out_stream = {
    out => $stdout,
    err => $stderr,
  }

  # Handle process termination ourselves
  status = nil
  Thread.start do
    status = Process.waitpid2(pid).last
  end

  until streams.empty? do
    # don't busy loop
    selected, = select streams, nil, nil, 0.1

    next if selected.nil? or selected.empty?

    selected.each do |stream|
      if stream.eof? then
        streams.delete stream if status # we've quit, so no more writing
        next
      end

      data = stream.readpartial(1024)
      out_stream[stream].write data

      if stream == err and data =~ /^Password:/ then
        inn.puts sudo_password
        data << "\n"
        $stderr.write "\n"
      end

      result << data
    end
  end

  PoolParty.message "execution failed with status #{status.exitstatus}: #{cmd.join ' '}" unless status.success?

  result.join
end

#run_array_of_tasks(task_list) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/poolparty/remoter.rb', line 100

def run_array_of_tasks(task_list)
  unless task_list.size == 0
    task_list.each do |task|
      add_task {Kernel.system("#{task}")}
    end
    run_thread_list
  end
end

#run_now(command) ⇒ Object



73
74
75
# File 'lib/poolparty/remoter.rb', line 73

def run_now command        
  run command unless command.empty?
end

#scp(local, remote, opts = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/poolparty/remoter.rb', line 43

def scp local, remote, opts={}          
  cmd = self.class.rsync_string
  arr = []
  
  target_hosts.each do |ip|
    arr << "#{cmd} #{local} #{ip}:#{remote}"
  end          
  
  run_array_of_tasks(arr)
end

#scp_tasksObject



78
# File 'lib/poolparty/remoter.rb', line 78

def scp_tasks;@scp_tasks ||= [];end

#set_hosts(c = nil) ⇒ Object



109
110
# File 'lib/poolparty/remoter.rb', line 109

def set_hosts(c=nil)
end

#single_scp(local, remote, opts = {}) ⇒ Object



54
55
56
# File 'lib/poolparty/remoter.rb', line 54

def single_scp local, remote, opts={}
  scp local, remote, opts.merge({:single => self.ip})
end

#ssh(command = "", opts = {}, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/poolparty/remoter.rb', line 58

def ssh command="", opts={}, &block
  cmd = self.class.ssh_string
  arr = []
  
  if command.empty?
    system("#{cmd} #{self.ip}") if self.class == RemoteInstance
  else
    target_hosts.each do |ip|
      arr << "#{cmd} #{ip} '#{command.runnable}'"
    end
  end
          
  run_array_of_tasks arr
end

#ssh_tasksObject



77
# File 'lib/poolparty/remoter.rb', line 77

def ssh_tasks;@ssh_tasks ||= [];end

#target_hostsObject



96
97
98
# File 'lib/poolparty/remoter.rb', line 96

def target_hosts
  @hosts ||= Master.collect_nodes {|a| a.ip }
end