Module: NexusSW::LXD::Driver::Mixins::Rest

Includes:
Helpers::WaitMixin
Included in:
Rest
Defined in:
lib/nexussw/lxd/driver/mixins/rest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::WaitMixin

#check_for_ip, #wait_for

Instance Attribute Details

#driver_optionsObject (readonly)

Returns the value of attribute driver_options.



24
25
26
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 24

def driver_options
  @driver_options
end

#hkObject (readonly)

Returns the value of attribute hk.



24
25
26
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 24

def hk
  @hk
end

#rest_endpointObject (readonly)

Returns the value of attribute rest_endpoint.



24
25
26
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 24

def rest_endpoint
  @rest_endpoint
end

Instance Method Details

#container(container_id) ⇒ Object



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

def container(container_id)
  @hk.container container_id
end

#container_exists?(container_id) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 117

def container_exists?(container_id)
  hk.containers.include? container_id
end

#container_state(container_id) ⇒ Object



108
109
110
111
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 108

def container_state(container_id)
  return nil unless container_status(container_id) == 'running' # Parity with CLI
  @hk.container_state(container_id)
end

#container_status(container_id) ⇒ Object



104
105
106
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 104

def container_status(container_id)
  STATUS_CODES[container(container_id)[:status_code].to_i]
end

#create_container(container_name, container_options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 36

def create_container(container_name, container_options = {})
  if container_exists?(container_name)
    start_container container_name # Start the container for Parity with the CLI
    return container_name
  end
  # parity note: CLI will run indefinitely rather than timeout hence the 0 timeout
  retry_forever do
    @hk.create_container(container_name, container_options.merge(sync: false))
  end
  start_container container_name
  container_name
end

#delete_container(container_id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 87

def delete_container(container_id)
  return unless container_exists? container_id
  stop_container container_id, force: true

  # ISSUE 17: something upstream is causing a double-tap on the REST endpoint
  begin
    @hk.delete_container container_id
  rescue ::Faraday::ConnectionFailed, ::Hyperkit::BadRequest
    LXD.with_timeout_and_retries timeout: 120 do
      loop do
        return unless container_exists? container_id
        sleep 0.3
      end
    end
  end
end

#initialize(rest_endpoint, driver_options = {}, inner_driver = nil) ⇒ Object

PARITY note: CLI functions are on an indefinite timeout by default, yet we have a 2 minute socket read timeout Leaving it alone, for now, on calls that are quick in nature Adapting on known long running calls such as create, stop, execute REQUEST_TIMEOUT = 120 # upstream default: 120



14
15
16
17
18
19
20
21
22
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 14

def initialize(rest_endpoint, driver_options = {}, inner_driver = nil)
  @rest_endpoint = rest_endpoint
  @driver_options = driver_options
  hkoptions = (driver_options || {}).merge(
    api_endpoint: rest_endpoint,
    auto_sync: true
  )
  @hk = inner_driver || Hyperkit::Client.new(hkoptions)
end

#server_infoObject



28
29
30
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 28

def server_info
  @server_info ||= hk.get('/1.0')[:metadata]
end

#start_container(container_id) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 49

def start_container(container_id)
  return if container_status(container_id) == 'running'
  retry_forever do
    @hk.start_container(container_id, sync: false)
  end
  wait_for_status container_id, 'running'
end

#stop_container(container_id, options = {}) ⇒ Object



57
58
59
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
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 57

def stop_container(container_id, options = {})
  return if container_status(container_id) == 'stopped'
  if options[:force]
    @hk.stop_container(container_id, force: true)
  else
    last_id = nil
    use_last = false
    LXD.with_timeout_and_retries({ timeout: 0 }.merge(options)) do # timeout: 0 to enable retry functionality
      return if container_status(container_id) == 'stopped'
      begin
        unless use_last
          # Keep resubmitting until the server complains (Stops will be ignored/hang if init is not yet listening for SIGPWR i.e. recently started)
          begin
            last_id = @hk.stop_container(container_id, sync: false)[:id]
          rescue Hyperkit::BadRequest # Happens if a stop command has previously been accepted as well as other reasons.  handle that on next line
            # if we have a last_id then a prior stop command has successfully initiated so we'll just wait on that one
            raise unless last_id # rubocop:disable Metrics/BlockNesting
            use_last = true
          end
        end
        @hk.wait_for_operation last_id # , options[:retry_interval]
      rescue Faraday::TimeoutError => e
        return if container_status(container_id) == 'stopped'
        raise Timeout::Retry.new e # if options[:retry_interval] # rubocop:disable Style/RaiseArgs
      end
    end
  end
  wait_for_status container_id, 'stopped'
end

#transport_for(container_name) ⇒ Object



32
33
34
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 32

def transport_for(container_name)
  Transport::Rest.new container_name, info: server_info, connection: hk, driver_options: driver_options, rest_endpoint: rest_endpoint
end