Class: Larrow::Runner::Service::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/larrow/runner/service/executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, user, port, password) ⇒ Executor

Returns a new instance of Executor.



9
10
11
12
13
14
15
16
# File 'lib/larrow/runner/service/executor.rb', line 9

def initialize ip, user, port, password
  self.ip       = ip
  self.user     = user
  self.port     = port
  self.password = password
  @canceling = nil
  @dlogger = RunLogger #::Logger.new "#{ip}_cmd.log"
end

Instance Attribute Details

#ipObject

Returns the value of attribute ip.



8
9
10
# File 'lib/larrow/runner/service/executor.rb', line 8

def ip
  @ip
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/larrow/runner/service/executor.rb', line 8

def password
  @password
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/larrow/runner/service/executor.rb', line 8

def port
  @port
end

#userObject

Returns the value of attribute user.



8
9
10
# File 'lib/larrow/runner/service/executor.rb', line 8

def user
  @user
end

Instance Method Details

#connectionObject



50
51
52
# File 'lib/larrow/runner/service/executor.rb', line 50

def connection
  @connection ||= Net::SSH.start(ip,user)
end

#execute(cmd, base_dir: nil, verbose: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/larrow/runner/service/executor.rb', line 18

def execute cmd, base_dir:nil,verbose:nil
  connection.open_channel do |ch|
    RunLogger.level(1).detail "# #{cmd}"
    cmd = "cd #{base_dir}; #{cmd}" unless base_dir.nil?
    errmsg = ''
    ch.exec cmd do |ch,success|
      if verbose
        ch.on_data{ |c, data| yield data }
        ch.on_extended_data{ |c, type, data| yield data }
      else
        ch.on_extended_data{ |c, type, data| errmsg << data }
      end
      ch.on_request('exit-status') do |c,data|
        status = data.read_long
        if status != 0
          fail ExecutionError,{cmd:cmd,
                               errmsg: errmsg, 
                               status: status}
        end
      end
    end
  end
  trap("INT") { @canceling = true }
  connection.loop(0.1) do
    not (@canceling || connection.channels.empty?)
  end
end

#scp(local_file_path, remote_file_path) ⇒ Object



46
47
48
# File 'lib/larrow/runner/service/executor.rb', line 46

def scp local_file_path, remote_file_path
  raise 'not completed.'
end