Class: Awt::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/awt/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: "localhost", port: 22, user: nil, key: "~/.ssh/id_rsa") ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
# File 'lib/awt/server.rb', line 5

def initialize(host: "localhost", port: 22, user: nil, key: "~/.ssh/id_rsa")
  @host = host
  @user = user.nil? ? ENV["USER"] : user
  @options = {port: port, keys: File.expand_path(key)}
  @printer = Printer.new(@host)
end

Instance Attribute Details

#envsObject

Returns the value of attribute envs.



3
4
5
# File 'lib/awt/server.rb', line 3

def envs
  @envs
end

#hostObject

Returns the value of attribute host.



3
4
5
# File 'lib/awt/server.rb', line 3

def host
  @host
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/awt/server.rb', line 3

def options
  @options
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/awt/server.rb', line 3

def user
  @user
end

Instance Method Details

#get(remote, local) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/awt/server.rb', line 50

def get(remote, local)
  reset_printer_host
  Net::SSH.start(@host, @user, @options) do |ssh|
    @printer.print_download remote
    ssh.scp.download! remote, local
  end
end

#put(local, remote) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/awt/server.rb', line 42

def put(local, remote)
  reset_printer_host
  Net::SSH.start(@host, @user, @options) do |ssh|
    @printer.print_upload local
    ssh.scp.upload! local, remote
  end
end

#run(cmd) ⇒ Object



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

def run(cmd)
  reset_printer_host
  cmd = "#{@envs};#{cmd}" if @envs
  result = OpenStruct.new

  Net::SSH.start(@host, @user, @options) do |ssh|
    @printer.print_run cmd
    ssh.open_channel do |channel|
      channel.exec cmd do |ch, success|
        channel.on_data {|ch,data| result.data = data}
        channel.on_extended_data {|ch,type,data| result.extended_data = data}
        channel.on_request("exit-status") {|ch,data| result.status = data.read_long}
      end
    end
    ssh.loop
  end

  @printer.print_out result.data if result.data
  @printer.print_ext result.extended_data if result.extended_data
  result
end

#with_env(envs, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/awt/server.rb', line 12

def with_env(envs, &block)
  env = []
  envs.each {|k,v| env << "export #{k.to_s.upcase}=\"#{v}\""}
  @envs = env.join(";") unless env.empty?
  self.instance_eval &block
  @envs = nil
end