Module: NexusSW::LXD::Transport::Mixins::Rest
- Included in:
- Rest
- Defined in:
- lib/nexussw/lxd/transport/mixins/rest.rb
Defined Under Namespace
Classes: StdinStub, WSController
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#container_name ⇒ Object
readonly
Returns the value of attribute container_name.
-
#rest_endpoint ⇒ Object
readonly
Returns the value of attribute rest_endpoint.
Attributes included from Helpers::UsersMixin
#file_mode, #gid, #uid, #username
Instance Method Summary collapse
- #download_file(path, local_path) ⇒ Object
- #execute_chunked(command, options = {}, &block) ⇒ Object
- #initialize(container_name, config = {}) ⇒ Object
-
#read_file(path) ⇒ Object
empty ” instead of an exception is a chef-provisioning expectation - at this level we’ll let the exception propagate.
- #upload_file(local_path, path, options = {}) ⇒ Object
- #write_file(path, content, options = {}) ⇒ Object
Methods included from Helpers::UsersMixin
Methods included from Helpers::FolderTxfr
#download_files_individually, #download_folder, #download_using_tarball, #upload_files_individually, #upload_folder, #upload_using_tarball
Methods included from Helpers::ExecuteMixin
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
23 24 25 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 23 def api @api end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
23 24 25 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 23 def config @config end |
#container_name ⇒ Object (readonly)
Returns the value of attribute container_name.
23 24 25 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 23 def container_name @container_name end |
#rest_endpoint ⇒ Object (readonly)
Returns the value of attribute rest_endpoint.
23 24 25 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 23 def rest_endpoint @rest_endpoint end |
Instance Method Details
#download_file(path, local_path) ⇒ Object
133 134 135 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 133 def download_file(path, local_path) api.pull_file container_name, path, local_path end |
#execute_chunked(command, options = {}, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 64 def execute_chunked(command, = {}, &block) opid = nil backchannel = nil getlogs = false command = runas_command(command, ) if block_given? && ([:capture] || !config[:info][:api_extensions].include?("container_exec_recording")) apiopts = { 'wait-for-websocket': true, interactive: false, sync: false } apiopts[:interactive] = true if [:capture] == :interactive retval = api.execute_command(container_name, command, apiopts)[:metadata] opid = retval[:id] backchannel = [:capture] == :interactive ? ws_connect(opid, retval[:metadata][:fds]) : ws_connect(opid, retval[:metadata][:fds], &block) # patch for interactive session if [:capture] == :interactive return Helpers::ExecuteMixin::InteractiveResult.new(command, , StdinStub.pipe(backchannel.waitlist[:'0']), backchannel).tap do |active| backchannel.callback = proc do |stdout| active.send_output stdout end yield active backchannel.exit if backchannel.respond_to? :exit retval = api.wait_for_operation opid active.exitstatus = retval[:metadata][:return].to_i end end elsif block_given? && config[:info][:api_extensions].include?("container_exec_recording") getlogs = true retval = api.execute_command(container_name, command, 'record-output': true, interactive: false, sync: false) opid = retval[:metadata][:id] else opid = api.execute_command(container_name, command, sync: false)[:metadata][:id] end LXD.with_timeout_and_retries({ timeout: 0 }.merge()) do begin retval = api.wait_for_operation(opid)[:metadata] backchannel.waitlist[:'0'].close if backchannel.respond_to? :waitlist backchannel.waitlist[:control].close if backchannel.respond_to? :waitlist backchannel.join if backchannel.respond_to? :join if getlogs begin stdout_log = retval[:metadata][:output][:'1'].split("/").last stderr_log = retval[:metadata][:output][:'2'].split("/").last stdout = api.log container_name, stdout_log stderr = api.log container_name, stderr_log yield stdout, stderr api.delete_log container_name, stdout_log api.delete_log container_name, stderr_log end end return Helpers::ExecuteMixin::ExecuteResult.new command, , retval[:metadata][:return].to_i rescue Faraday::TimeoutError => e raise Timeout::Retry.new e # rubocop:disable Style/RaiseArgs end end end |
#initialize(container_name, config = {}) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 14 def initialize(container_name, config = {}) @container_name = container_name @config = config @rest_endpoint = config[:rest_endpoint] = config[:driver_options] @api = config[:connection] raise "The rest transport requires the following keys: { :connection, :driver_options, :rest_endpoint }" unless @rest_endpoint && @api && end |
#read_file(path) ⇒ Object
empty ” instead of an exception is a chef-provisioning expectation - at this level we’ll let the exception propagate
121 122 123 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 121 def read_file(path) api.read_file container_name, path end |
#upload_file(local_path, path, options = {}) ⇒ Object
137 138 139 140 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 137 def upload_file(local_path, path, = {}) # return api.push_file(local_path, container_name, path) write_file(path, IO.binread(local_path), ) end |
#write_file(path, content, options = {}) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 125 def write_file(path, content, = {}) = .merge content: content [:uid] ||= uid if uid [:gid] ||= gid if gid [:file_mode] ||= file_mode if file_mode api.write_file container_name, path, end |