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
20
# 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



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

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



72
73
74
75
76
77
78
79
# File 'lib/bolt/transport/jail/connection.rb', line 72

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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bolt/transport/jail/connection.rb', line 52

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



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

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

#jail_pathObject



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

def jail_path
  @jail_info['path']
end

#reset_cwd?Boolean

Returns:

  • (Boolean)


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

def reset_cwd?
  true
end

#shellObject



22
23
24
# File 'lib/bolt/transport/jail/connection.rb', line 22

def shell
  @shell ||= Bolt::Shell::Bash.new(target, self)
end

#upload_file(source, destination) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/bolt/transport/jail/connection.rb', line 64

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