Class: Octosh::Worker
- Inherits:
-
Object
- Object
- Octosh::Worker
- Defined in:
- lib/octosh/worker/worker.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#ssh ⇒ Object
readonly
Returns the value of attribute ssh.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #connect ⇒ Object
- #connect_if_not_connected ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #exec(command) ⇒ Object
- #exec_script(script_path) ⇒ Object
- #get(remote_path, local_path) ⇒ Object
-
#initialize(host, user, pass, options = {}) ⇒ Worker
constructor
A new instance of Worker.
- #put(local_path, remote_path) ⇒ Object
- #read(remote_path) ⇒ Object
Constructor Details
#initialize(host, user, pass, options = {}) ⇒ Worker
Returns a new instance of Worker.
13 14 15 16 17 18 |
# File 'lib/octosh/worker/worker.rb', line 13 def initialize(host, user, pass, = {}) @host = host @user = user @password = pass @options = end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/octosh/worker/worker.rb', line 9 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/octosh/worker/worker.rb', line 9 def @options end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
9 10 11 |
# File 'lib/octosh/worker/worker.rb', line 9 def password @password end |
#ssh ⇒ Object (readonly)
Returns the value of attribute ssh.
9 10 11 |
# File 'lib/octosh/worker/worker.rb', line 9 def ssh @ssh end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
9 10 11 |
# File 'lib/octosh/worker/worker.rb', line 9 def user @user end |
Instance Method Details
#connect ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/octosh/worker/worker.rb', line 30 def connect if not connected? forward_agent = @options[:forward_agent] || false @ssh = Net::SSH.start(@host, @user, :password => @password, :forward_agent => forward_agent) @connected = true return true end return false end |
#connect_if_not_connected ⇒ Object
24 25 26 27 28 |
# File 'lib/octosh/worker/worker.rb', line 24 def connect_if_not_connected if not connected? connect end end |
#connected? ⇒ Boolean
20 21 22 |
# File 'lib/octosh/worker/worker.rb', line 20 def connected? return @connected end |
#disconnect ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/octosh/worker/worker.rb', line 41 def disconnect if connected? @ssh.close @ssh = nil @connected = false return true end return false end |
#exec(command) ⇒ Object
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 |
# File 'lib/octosh/worker/worker.rb', line 52 def exec(command) connect_if_not_connected channel = @ssh.open_channel do |ch| ch.exec(command) do |ch, success| raise "Error executing #{command}" unless success ch.on_data do |c, data| if block_given? yield data.to_s else puts data.to_s end end ch.on_extended_data do |c, type, data| return data.to_s end ch.on_close do # For now do nothing end end end channel.wait end |
#exec_script(script_path) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/octosh/worker/worker.rb', line 94 def exec_script(script_path) tmp_script_name = "octosh-#{Pathname.new(script_path).basename.to_s}-#{UUID.new.generate}" self.put(script_path, "/tmp/#{tmp_script_name}") self.exec("chmod +x /tmp/#{tmp_script_name}") self.exec("/tmp/#{tmp_script_name}") do |output| if block_given? yield output else puts output end end end |
#get(remote_path, local_path) ⇒ Object
84 85 86 87 |
# File 'lib/octosh/worker/worker.rb', line 84 def get(remote_path, local_path) connect_if_not_connected @ssh.scp.download!(remote_path, local_path) end |
#put(local_path, remote_path) ⇒ Object
79 80 81 82 |
# File 'lib/octosh/worker/worker.rb', line 79 def put(local_path, remote_path) connect_if_not_connected @ssh.scp.upload!(local_path, remote_path) end |
#read(remote_path) ⇒ Object
89 90 91 92 |
# File 'lib/octosh/worker/worker.rb', line 89 def read(remote_path) connect_if_not_connected return @ssh.scp.download!(remote_path) end |