Class: Octosh::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/octosh/worker/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, user, pass) ⇒ Worker

Returns a new instance of Worker.



11
12
13
14
15
16
17
# File 'lib/octosh/worker/worker.rb', line 11

def initialize(host, user, pass)
  @host = host
  @user = user
  @password = pass
  
  @ssh = Net::SSH.start(@host, @user, :password => @password)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/octosh/worker/worker.rb', line 9

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/octosh/worker/worker.rb', line 9

def password
  @password
end

#sshObject (readonly)

Returns the value of attribute ssh.



9
10
11
# File 'lib/octosh/worker/worker.rb', line 9

def ssh
  @ssh
end

#userObject (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

#exec(command) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/octosh/worker/worker.rb', line 19

def exec(command)
  channel = @ssh.open_channel do |ch|
    ch.exec(command) do |ch, success|
      raise "Error executing #{command}" unless success
      
      ch.on_data do |c, data|
        puts "#{@host} -- #{data.to_s}"
      end
      
      ch.on_extended_data do |c, type, data|
        puts "#{@host} -- #{data}"
      end
      
      ch.on_close do
        #puts "Octosh execution complete!"
      end
    end
  end
  
  channel.wait
end

#exec_script(script_path) ⇒ Object



53
54
55
56
57
58
# File 'lib/octosh/worker/worker.rb', line 53

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}")
  return self.exec("/tmp/#{tmp_script_name}")
end

#get(remote_path, local_path) ⇒ Object



45
46
47
# File 'lib/octosh/worker/worker.rb', line 45

def get(remote_path, local_path)
  @ssh.scp.download!(remote_path, local_path)
end

#put(local_path, remote_path) ⇒ Object



41
42
43
# File 'lib/octosh/worker/worker.rb', line 41

def put(local_path, remote_path)
  @ssh.scp.upload!(local_path, remote_path)
end

#read(remote_path) ⇒ Object



49
50
51
# File 'lib/octosh/worker/worker.rb', line 49

def read(remote_path)
  return @ssh.scp.download!(remote_path)
end