Class: Bolt::Transport::Jail::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

#userObject (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

#connectObject



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.message}",
    '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.message, '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_idObject



29
30
31
# File 'lib/bolt/transport/jail/connection.rb', line 29

def jail_id
  @jail_info['jid'].to_s
end

#jail_pathObject



33
34
35
# File 'lib/bolt/transport/jail/connection.rb', line 33

def jail_path
  @jail_info['path']
end

#reset_cwd?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/bolt/transport/jail/connection.rb', line 25

def reset_cwd?
  true
end

#shellObject



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.message, 'WRITE_ERROR')
end