Class: Kitchen::Transport::Lxd::Connection

Inherits:
Transport::Base::Connection
  • Object
show all
Defined in:
lib/kitchen/transport/lxd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, options, state_filename) ⇒ Connection

Returns a new instance of Connection.



33
34
35
36
37
# File 'lib/kitchen/transport/lxd.rb', line 33

def initialize(transport, options, state_filename)
  super options
  @nx_transport = transport
  @state_filename = state_filename
end

Instance Attribute Details

#nx_transportObject (readonly)

Returns the value of attribute nx_transport.



39
40
41
# File 'lib/kitchen/transport/lxd.rb', line 39

def nx_transport
  @nx_transport
end

#state_filenameObject (readonly)

Returns the value of attribute state_filename.



39
40
41
# File 'lib/kitchen/transport/lxd.rb', line 39

def state_filename
  @state_filename
end

Instance Method Details

#download(remotes, local) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/kitchen/transport/lxd.rb', line 68

def download(remotes, local)
  FileUtils.mkdir_p local unless Dir.exist? local
  [remotes].flatten.each do |remote|
    nx_transport.download_folder remote.to_s, local, auto_detect: true
  end
rescue NexusSW::LXD::RestAPI::Error => ex
  raise TransportFailed, ex
end

#execute(command) ⇒ Object

Raises:

  • (TransportFailed)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kitchen/transport/lxd.rb', line 41

def execute(command)
  return unless command && !command.empty?

  # There are some bash-isms coming from chef_zero (in particular, multiple_converge)
  # so let's wrap it
  command = command.shelljoin if command.is_a? Array
  command = ["bash", "-c", command]
  res = nx_transport.execute(command, capture: true) do |stdout_chunk, stderr_chunk|
    logger << stdout_chunk if stdout_chunk
    logger << stderr_chunk if stderr_chunk
  end
  raise TransportFailed.new("The command #{res.command} failed with exit code: #{res.exitstatus}", res.exitstatus) if res.error?
end

#login_commandObject



77
78
79
80
# File 'lib/kitchen/transport/lxd.rb', line 77

def 
  args = [File.expand_path("../../../../bin/lxc-shell", __FILE__), state_filename]
  LoginCommand.new "ruby", args
end

#upload(locals, remote) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kitchen/transport/lxd.rb', line 55

def upload(locals, remote)
  nx_transport.execute("mkdir -p #{remote}").error!
  [locals].flatten.each do |local|
    nx_transport.upload_file local, File.join(remote, File.basename(local)) if File.file? local
    if File.directory? local
      debug "Transferring folder (#{local}) to remote: #{remote}"
      nx_transport.upload_folder local, remote
    end
  end
rescue NexusSW::LXD::RestAPI::Error => ex
  raise TransportFailed, ex
end