Class: Bolt::Transport::Jail::Connection
- Inherits:
-
Object
- Object
- Bolt::Transport::Jail::Connection
- Defined in:
- lib/bolt/transport/jail/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
- #connect ⇒ Object
- #download_file(source, destination, _download) ⇒ Object
- #execute(command) ⇒ Object
-
#initialize(target) ⇒ Connection
constructor
A new instance of Connection.
- #jail_id ⇒ Object
- #jail_path ⇒ Object
- #reset_cwd? ⇒ Boolean
- #shell ⇒ Object
- #upload_file(source, destination) ⇒ Object
Constructor Details
#initialize(target) ⇒ Connection
Returns a new instance of Connection.
12 13 14 15 16 17 18 19 |
# File 'lib/bolt/transport/jail/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 = @target.user || ENV['USER'] || Etc.getlogin @logger = Bolt::Logger.logger(target.safe_name) @jail_info = {} @logger.trace("Initializing jail 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/jail/connection.rb', line 10 def target @target end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
10 11 12 |
# File 'lib/bolt/transport/jail/connection.rb', line 10 def user @user end |
Instance Method Details
#connect ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bolt/transport/jail/connection.rb', line 37 def connect output = JSON.parse(`jls --libxo=json`) @jail_info = output['jail-information']['jail'].select { |jail| jail['hostname'] == target.host }.first raise "Could not find a jail with name matching #{target.host}" if @jail_info.nil? @logger.trace { "Opened session" } true rescue StandardError => e raise Bolt::Node::ConnectError.new( "Failed to connect to #{target.safe_name}: #{e.}", 'CONNECT_ERROR' ) end |
#download_file(source, destination, _download) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/bolt/transport/jail/connection.rb', line 70 def download_file(source, destination, _download) @logger.trace { "Downloading #{source} to #{destination}" } jail_source = File.join(jail_path, source) FileUtils.mkdir_p(destination) FileUtils.cp(jail_source, destination) rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |
#execute(command) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bolt/transport/jail/connection.rb', line 50 def execute(command) args = ['-lU', @user] jail_command = %w[jexec] + args + [jail_id] + Shellwords.split(command) @logger.trace { "Executing #{jail_command.join(' ')}" } Open3.popen3({}, *jail_command) rescue StandardError @logger.trace { "Command aborted" } raise end |
#jail_id ⇒ Object
29 30 31 |
# File 'lib/bolt/transport/jail/connection.rb', line 29 def jail_id @jail_info['jid'].to_s end |
#jail_path ⇒ Object
33 34 35 |
# File 'lib/bolt/transport/jail/connection.rb', line 33 def jail_path @jail_info['path'] end |
#reset_cwd? ⇒ Boolean
25 26 27 |
# File 'lib/bolt/transport/jail/connection.rb', line 25 def reset_cwd? true end |
#shell ⇒ Object
21 22 23 |
# File 'lib/bolt/transport/jail/connection.rb', line 21 def shell @shell ||= Bolt::Shell::Bash.new(target, self) end |
#upload_file(source, destination) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/bolt/transport/jail/connection.rb', line 62 def upload_file(source, destination) @logger.trace { "Uploading #{source} to #{destination}" } jail_destination = File.join(jail_path, destination) FileUtils.cp(source, jail_destination) rescue StandardError => e raise Bolt::Node::FileError.new(e., 'WRITE_ERROR') end |