Module: NexusSW::LXD::Transport::Mixins::Rest

Includes:
Helpers::ExecuteMixin, Helpers::UploadFolder
Included in:
Rest
Defined in:
lib/nexussw/lxd/transport/mixins/rest.rb

Defined Under Namespace

Classes: StdinStub, WSController

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::UploadFolder

#upload_files_individually, #upload_folder, #upload_using_tarball

Methods included from Helpers::ExecuteMixin

#execute

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 20

def config
  @config
end

#container_nameObject (readonly)

Returns the value of attribute container_name.



20
21
22
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 20

def container_name
  @container_name
end

#hkObject (readonly)

Returns the value of attribute hk.



20
21
22
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 20

def hk
  @hk
end

#rest_endpointObject (readonly)

Returns the value of attribute rest_endpoint.



20
21
22
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 20

def rest_endpoint
  @rest_endpoint
end

Instance Method Details

#download_file(path, local_path) ⇒ Object



122
123
124
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 122

def download_file(path, local_path)
  hk.pull_file container_name, path, local_path
end

#execute_chunked(command, options = {}, &block) ⇒ Object



60
61
62
63
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
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 60

def execute_chunked(command, options = {}, &block)
  opid = nil
  backchannel = nil
  getlogs = false
  if block_given? && (options[:capture] || !config[:info][:api_extensions].include?('container_exec_recording'))
    hkopts = { wait_for_websocket: true, interactive: false, sync: false }
    hkopts[:interactive] = true if options[:capture] == :interactive
    retval = hk.execute_command(container_name, command, hkopts)
    opid = retval[:id]
    backchannel = options[:capture] == :interactive ? ws_connect(opid, retval[:metadata][:fds]) : ws_connect(opid, retval[:metadata][:fds], &block)

    # patch for interactive session
    return Helpers::ExecuteMixin::InteractiveResult.new(command, options, -1, 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 = hk.wait_for_operation opid
      active.exitstatus = retval[:metadata][:return].to_i
    end if options[:capture] == :interactive
  elsif block_given? && config[:info][:api_extensions].include?('container_exec_recording')
    getlogs = true
    retval = hk.execute_command(container_name, command, record_output: true, interactive: false, sync: false)
    opid = retval[:id]
  else
    opid = hk.execute_command(container_name, command, sync: false)[:id]
  end
  LXD.with_timeout_and_retries({ timeout: 0 }.merge(options)) do
    begin
      retval = hk.wait_for_operation opid
      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 = hk.log container_name, stdout_log
          stderr = hk.log container_name, stderr_log
          yield stdout, stderr
        ensure
          hk.delete_log container_name, stdout_log
          hk.delete_log container_name, stderr_log
        end
      end
      return Helpers::ExecuteMixin::ExecuteResult.new command, options, 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



11
12
13
14
15
16
17
18
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 11

def initialize(container_name, config = {})
  @container_name = container_name
  @config = config
  @rest_endpoint = config[:rest_endpoint]
  @driver_options = config[:driver_options]
  @hk = config[:connection]
  raise 'The rest transport requires the following keys: { :connection, :driver_options, :rest_endpoint }' unless @rest_endpoint && @hk && @driver_options
end

#read_file(path) ⇒ Object

” instead of Hyperkit::NotFound is a chef-provisioning expectation - at this level we’ll let the exception propagate



112
113
114
115
116
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 112

def read_file(path)
  hk.read_file container_name, path
  # rescue ::Hyperkit::NotFound
  #   return ''
end

#upload_file(local_path, path) ⇒ Object



126
127
128
129
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 126

def upload_file(local_path, path)
  # return hk.push_file(local_path, container_name, path)
  write_file(path, IO.binread(local_path))
end

#write_file(path, content) ⇒ Object



118
119
120
# File 'lib/nexussw/lxd/transport/mixins/rest.rb', line 118

def write_file(path, content)
  hk.write_file container_name, path, content: content
end