Class: Vos::Drivers::Ssh

Inherits:
Object
  • Object
show all
Includes:
SshVfsStorage
Defined in:
lib/vos/drivers/ssh.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  config: true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SshVfsStorage

#_create_root_dir, #_delete_root_dir, #attributes, #create_dir, #delete_dir, #delete_file, #each_entry, #local?, #read_file, #set_attributes, #tmp, #write_file

Constructor Details

#initialize(options = {}) ⇒ Ssh

Vos::Drivers::Ssh.new name: ‘xxx’, user: ‘xxx’, password: ‘xxx’



17
18
19
20
21
22
23
24
25
26
# File 'lib/vos/drivers/ssh.rb', line 17

def initialize options = {}
  options = options.clone
  raise ":host not provided!" unless options[:host]
  @root = options.delete(:root) || ''
  @options = DEFAULT_OPTIONS.merge options

  # config_options = Net::SSH.configuration_for(options[:host])
  # options = DEFAULT_OPTIONS.merge(config_options).merge options
  # raise ":user not provided (provide explicitly or in .ssh/config)!" unless options[:user]
end

Instance Attribute Details

#boxObject

Returns the value of attribute box.



8
9
10
# File 'lib/vos/drivers/ssh.rb', line 8

def box
  @box
end

Instance Method Details

#bash(command) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/vos/drivers/ssh.rb', line 83

def bash command
  # somehow net-ssh doesn't executes ~/.profile, so we need to execute it manually
  # command = ". ~/.profile && #{command}"
  stdout_and_stderr, stderr, code, signal = hacked_exec! ssh, command, true

  return code, stdout_and_stderr
end

#closeObject



56
57
58
59
60
61
62
# File 'lib/vos/drivers/ssh.rb', line 56

def close
  if @ssh
    @ssh.close
    # @sftp.close not needed
    @ssh, @sftp = nil
  end
end

#exec(command) ⇒ Object

Shell



74
75
76
77
78
79
80
81
# File 'lib/vos/drivers/ssh.rb', line 74

def exec command
  # somehow net-ssh doesn't executes ~/.profile, so we need to execute it manually
  # command = ". ~/.profile && #{command}"

  stdout, stderr, code, signal = hacked_exec! ssh, command

  return code, stdout, stderr
end

#hostObject



96
# File 'lib/vos/drivers/ssh.rb', line 96

def host; options[:host] end

#open(&block) ⇒ Object

Establishing SSH channel



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vos/drivers/ssh.rb', line 32

def open &block
  if block
    if @ssh
      block.call self
    else
      begin
        open
        block.call self
      ensure
        close
      end
    end
  else
    unless @ssh
      opt = self.options.clone
      host = opt.delete :host #] || raise('host not provided!')
      # user = options.delete(:user) || raise('user not provied!')

      @ssh = Net::SSH.start(host, nil, opt)
      @sftp = @ssh.sftp.connect
    end
  end
end

#to_sObject

Miscellaneous



95
# File 'lib/vos/drivers/ssh.rb', line 95

def to_s; options[:host] end