Class: Bolt::Transport::LXD::Connection
- Inherits:
-
Object
- Object
- Bolt::Transport::LXD::Connection
- Defined in:
- lib/bolt/transport/lxd/connection.rb
Instance Attribute Summary collapse
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #add_env_vars(env_vars) ⇒ Object
- #connect ⇒ Object
- #container_id ⇒ Object
- #download_file(source, destination, _download) ⇒ Object
- #execute(command) ⇒ Object
-
#initialize(target, options) ⇒ Connection
constructor
A new instance of Connection.
- #reset_cwd? ⇒ Boolean
- #shell ⇒ Object
- #upload_file(source, destination) ⇒ Object
Constructor Details
permalink #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, ) raise Bolt::ValidationError, "Target #{target.safe_name} does not have a host" unless target.host @target = target @user = ENV['USER'] || Etc.getlogin @options = @logger = Bolt::Logger.logger(target.safe_name) @logger.trace("Initializing LXD connection to #{target.safe_name}") end |
Instance Attribute Details
permalink #target ⇒ Object (readonly)
Returns the value of attribute target.
10 11 12 |
# File 'lib/bolt/transport/lxd/connection.rb', line 10 def target @target end |
permalink #user ⇒ Object (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
permalink #add_env_vars(env_vars) ⇒ Object
[View source]
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 |
permalink #connect ⇒ Object
[View source]
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.}", 'CONNECT_ERROR' ) end |
permalink #container_id ⇒ Object
[View source]
30 31 32 |
# File 'lib/bolt/transport/lxd/connection.rb', line 30 def container_id "#{@target.transport_config['remote']}:#{@target.host}" end |
permalink #download_file(source, destination, _download) ⇒ Object
[View source]
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/bolt/transport/lxd/connection.rb', line 90 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., 'WRITE_ERROR') end |
permalink #execute(command) ⇒ Object
[View source]
59 60 61 62 63 64 65 66 |
# File 'lib/bolt/transport/lxd/connection.rb', line 59 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 |
permalink #reset_cwd? ⇒ Boolean
26 27 28 |
# File 'lib/bolt/transport/lxd/connection.rb', line 26 def reset_cwd? true end |
permalink #shell ⇒ Object
[View source]
22 23 24 |
# File 'lib/bolt/transport/lxd/connection.rb', line 22 def shell Bolt::Shell::Bash.new(target, self) end |
permalink #upload_file(source, destination) ⇒ Object
[View source]
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bolt/transport/lxd/connection.rb', line 72 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., 'WRITE_ERROR') end |