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
#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
#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 |
#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
#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 |
#connect ⇒ Object
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 |
#container_id ⇒ Object
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., '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.['tty'].to_s.empty? ? 'non-interactive' : 'interactive'] lxc_command += @env_vars if @env_vars lxc_command << '--' if target.['shell-command'].to_s.empty? lxc_command += Shellwords.split(command) else lxc_command += Shellwords.split(target.['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
26 27 28 |
# File 'lib/bolt/transport/lxd/connection.rb', line 26 def reset_cwd? true end |
#shell ⇒ Object
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., 'WRITE_ERROR') end |