Class: Bolt::Transport::LXD::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/transport/lxd/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, options) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
18
19
20
# File 'lib/bolt/transport/lxd/connection.rb', line 12

def initialize(target, options)
  raise Bolt::ValidationError, "Target #{target.safe_name} does not have a host" unless target.host

  @target = target
  @user = ENV['USER'] || Etc.getlogin
  @options = options
  @logger = Bolt::Logger.logger(target.safe_name)
  @logger.trace("Initializing LXD connection to #{target.safe_name}")
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



10
11
12
# File 'lib/bolt/transport/lxd/connection.rb', line 10

def target
  @target
end

#userObject (readonly)

Returns the value of attribute user.



10
11
12
# File 'lib/bolt/transport/lxd/connection.rb', line 10

def user
  @user
end

Instance Method Details

#add_env_vars(env_vars) ⇒ Object



48
49
50
51
52
53
# File 'lib/bolt/transport/lxd/connection.rb', line 48

def add_env_vars(env_vars)
  @env_vars = env_vars.each_with_object([]) do |env_var, acc|
    acc << "--env"
    acc << "#{env_var[0]}=#{Shellwords.shellescape(env_var[1])}"
  end
end

#connectObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bolt/transport/lxd/connection.rb', line 30

def connect
  out, err, status = execute_local_command(%W[list #{container_id} --format json])
  unless status.exitstatus.zero?
    raise "Error listing available containers: #{err}"
  end
  containers = JSON.parse(out)
  if containers.empty?
    raise "Could not find a container with name or ID matching '#{container_id}'"
  end
  @logger.trace("Opened session")
  true
rescue StandardError => e
  raise Bolt::Node::ConnectError.new(
    "Failed to connect to #{container_id}: #{e.message}",
    'CONNECT_ERROR'
  )
end

#container_idObject



26
27
28
# File 'lib/bolt/transport/lxd/connection.rb', line 26

def container_id
  "#{@target.transport_config['remote']}:#{@target.host}"
end

#download_file(source, destination, _download) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/bolt/transport/lxd/connection.rb', line 86

def download_file(source, destination, _download)
  @logger.trace { "Downloading #{source} to #{destination}" }
  FileUtils.mkdir_p(destination)
  _out, err, stat = execute_local_command(%W[file pull --recursive #{container_id}#{source} #{destination}])
  unless stat.exitstatus.zero?
    raise "Error downloading content from container #{container_id}: #{err}"
  end
rescue StandardError => e
  raise Bolt::Node::FileError.new(e.message, 'WRITE_ERROR')
end

#execute(command) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/bolt/transport/lxd/connection.rb', line 55

def execute(command)
  lxc_command = %w[lxc exec]
  lxc_command += @env_vars if @env_vars
  lxc_command += %W[#{container_id} -- sh -c #{Shellwords.shellescape(command)}]

  @logger.trace { "Executing: #{lxc_command.join(' ')}" }
  Open3.popen3(lxc_command.join(' '))
end

#shellObject



22
23
24
# File 'lib/bolt/transport/lxd/connection.rb', line 22

def shell
  Bolt::Shell::Bash.new(target, self)
end

#upload_file(source, destination) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bolt/transport/lxd/connection.rb', line 68

def upload_file(source, destination)
  @logger.trace { "Uploading #{source} to #{destination}" }
  args = %w[--create-dirs]
  if File.directory?(source)
    args << '--recursive'
    # If we don't do this, LXD will upload to
    # /tmp/d2020-11/d2020-11/dir instead of /tmp/d2020-11/dir
    destination = Pathname.new(destination).dirname.to_s
  end
  cmd = %w[file push] + args + %W[#{source} #{container_id}#{destination}]
  _out, err, stat = execute_local_command(cmd)
  unless stat.exitstatus.zero?
    raise "Error writing to #{container_id}: #{err}"
  end
rescue StandardError => e
  raise Bolt::Node::FileError.new(e.message, 'WRITE_ERROR')
end