Class: Bolt::Transport::Docker

Inherits:
Base
  • Object
show all
Defined in:
lib/bolt/transport/docker.rb,
lib/bolt/transport/docker/connection.rb

Defined Under Namespace

Classes: Connection

Instance Attribute Summary

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#assert_batch_size_one, #batch_command, #batch_connected?, #batch_download, #batch_script, #batch_task, #batch_task_with, #batch_upload, #batches, #default_input_method, #envify_params, #initialize, #select_implementation, #select_interpreter, #unwrap_sensitive_args, #with_events

Constructor Details

This class inherits a constructor from Bolt::Transport::Base

Instance Method Details

#connected?(target) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
# File 'lib/bolt/transport/docker.rb', line 138

def connected?(target)
  with_connection(target) { true }
rescue Bolt::Node::ConnectError
  false
end

#download(target, source, destination, _options = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/bolt/transport/docker.rb', line 41

def download(target, source, destination, _options = {})
  with_connection(target) do |conn|
    download = File.join(destination, Bolt::Util.unix_basename(source))
    conn.download_remote_content(source, destination)
    Bolt::Result.for_download(target, source, destination, download)
  end
end

#provided_featuresObject



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

def provided_features
  ['shell']
end

#run_command(target, command, options = {}, position = []) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bolt/transport/docker.rb', line 49

def run_command(target, command, options = {}, position = [])
  execute_options = {}
  execute_options[:tty] = target.options['tty']
  execute_options[:environment] = options[:env_vars]

  if target.options['shell-command'] && !target.options['shell-command'].empty?
    # escape any double quotes in command
    command = command.gsub('"', '\"')
    command = "#{target.options['shell-command']} \" #{command}\""
  end
  with_connection(target) do |conn|
    stdout, stderr, exitcode = conn.execute(*Shellwords.split(command), execute_options)
    Bolt::Result.for_command(target,
                             stdout,
                             stderr,
                             exitcode,
                             'command',
                             command,
                             position)
  end
end

#run_script(target, script, arguments, options = {}, position = []) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bolt/transport/docker.rb', line 71

def run_script(target, script, arguments, options = {}, position = [])
  # unpack any Sensitive data
  arguments = unwrap_sensitive_args(arguments)
  execute_options = {}
  execute_options[:environment] = options[:env_vars]

  with_connection(target) do |conn|
    conn.with_remote_tmpdir do |dir|
      remote_path = conn.write_remote_executable(dir, script)
      stdout, stderr, exitcode = conn.execute(remote_path, *arguments, execute_options)
      Bolt::Result.for_command(target,
                               stdout,
                               stderr,
                               exitcode,
                               'script',
                               script,
                               position)
    end
  end
end

#run_task(target, task, arguments, _options = {}, position = []) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bolt/transport/docker.rb', line 92

def run_task(target, task, arguments, _options = {}, position = [])
  implementation = task.select_implementation(target, provided_features)
  executable = implementation['path']
  input_method = implementation['input_method']
  extra_files = implementation['files']
  input_method ||= 'both'

  # unpack any Sensitive data
  arguments = unwrap_sensitive_args(arguments)
  with_connection(target) do |conn|
    execute_options = {}
    execute_options[:interpreter] = select_interpreter(executable, target.options['interpreters'])
    conn.with_remote_tmpdir do |dir|
      if extra_files.empty?
        task_dir = dir
      else
        # TODO: optimize upload of directories
        arguments['_installdir'] = dir
        task_dir = File.join(dir, task.tasks_dir)
        conn.mkdirs([task_dir] + extra_files.map { |file| File.join(dir, File.dirname(file['name'])) })
        extra_files.each do |file|
          conn.write_remote_file(file['path'], File.join(dir, file['name']))
        end
      end

      remote_task_path = conn.write_remote_executable(task_dir, executable)

      if Bolt::Task::STDIN_METHODS.include?(input_method)
        execute_options[:stdin] = StringIO.new(JSON.dump(arguments))
      end

      if Bolt::Task::ENVIRONMENT_METHODS.include?(input_method)
        execute_options[:environment] = envify_params(arguments)
      end

      stdout, stderr, exitcode = conn.execute(remote_task_path, execute_options)
      Bolt::Result.for_task(target,
                            stdout,
                            stderr,
                            exitcode,
                            task.name,
                            position)
    end
  end
end

#upload(target, source, destination, _options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bolt/transport/docker.rb', line 20

def upload(target, source, destination, _options = {})
  with_connection(target) do |conn|
    conn.with_remote_tmpdir do |dir|
      basename = File.basename(source)
      tmpfile = "#{dir}/#{basename}"
      if File.directory?(source)
        conn.write_remote_directory(source, tmpfile)
      else
        conn.write_remote_file(source, tmpfile)
      end

      _, stderr, exitcode = conn.execute('mv', tmpfile, destination, {})
      if exitcode != 0
        message = "Could not move temporary file '#{tmpfile}' to #{destination}: #{stderr}"
        raise Bolt::Node::FileError.new(message, 'MV_ERROR')
      end
    end
    Bolt::Result.for_upload(target, source, destination)
  end
end

#with_connection(target) {|conn| ... } ⇒ Object

Yields:

  • (conn)


14
15
16
17
18
# File 'lib/bolt/transport/docker.rb', line 14

def with_connection(target)
  conn = Connection.new(target)
  conn.connect
  yield conn
end