Class: LxcSsh::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lxc_ssh/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Initializes the connection



9
10
11
12
13
14
# File 'lib/lxc_ssh/connection.rb', line 9

def initialize
  @session = nil
  @manager = nil
  @lxc_version = nil
  @lxc_path = nil
end

Instance Attribute Details

#managerObject (readonly)

Returns the value of attribute manager.



6
7
8
# File 'lib/lxc_ssh/connection.rb', line 6

def manager
  @manager
end

Instance Method Details

#connect(hostname, username, password, lxc_path = '/usr') ⇒ Object

Connects to the host system via ssh and authenticates by either public key or password authentication

Parameters:

  • hostname (String)

    SSH Hostname

  • username (String)

    SSH Username

  • password (String)

    SSH Password

  • lxc_path (String) (defaults to: '/usr')

    LXC installation path (default: /usr)



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lxc_ssh/connection.rb', line 23

def connect(hostname, username, password, lxc_path = '/usr')
  if @password.nil?
    # pubkey
    @session = Net::SSH.start(hostname, username)
  else
    @session = Net::SSH.start(hostname, username, password)
  end

  @lxc_path = lxc_path
  @lxc_version = obtain_version
  @manager = self.init_manager
end

#init_managerObject

Initializes the lxc manager object



46
47
48
49
50
51
52
# File 'lib/lxc_ssh/connection.rb', line 46

def init_manager
  if @lxc_version.nil?
    raise 'no lxc version set, please detect and set the lxc version using detect_lxc_version'
  end

  @manager = LxcSsh::Manager.new(@session, @lxc_version, @lxc_path)
end

#obtain_versionObject

Obtains the version number of the lxc installation

Returns:

  • string



39
40
41
42
43
# File 'lib/lxc_ssh/connection.rb', line 39

def obtain_version
  output = @session.exec! @lxc_path + "/bin/lxc-start --version"

  output.strip
end