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



52
53
54
55
56
57
# File 'lib/bolt/transport/lxd/connection.rb', line 52

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bolt/transport/lxd/connection.rb', line 34

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



30
31
32
# File 'lib/bolt/transport/lxd/connection.rb', line 30

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

#download_file(source, destination, _download) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/bolt/transport/lxd/connection.rb', line 102

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bolt/transport/lxd/connection.rb', line 59

def execute(command)
  lxc_command = %W[lxc exec #{container_id}]
  lxc_command += ['--mode', target.options['tty'].to_s.empty? ? 'non-interactive' : 'interactive']
  lxc_command += @env_vars if @env_vars
  lxc_command << '--'

  if target.options['shell-command'].to_s.empty?
    lxc_command += Shellwords.split(command)
  else
    lxc_command += Shellwords.split(target.options['shell-command'])
    lxc_command << command
  end

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

  Open3.popen3(*lxc_command)
rescue StandardError
  @logger.trace { "Command aborted" }
  raise
end

#reset_cwd?Boolean

Returns:

  • (Boolean)


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

def reset_cwd?
  true
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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bolt/transport/lxd/connection.rb', line 84

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