Module: NexusSW::LXD::Driver::Mixins::Rest
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#driver_options ⇒ Object
readonly
Returns the value of attribute driver_options.
-
#rest_endpoint ⇒ Object
readonly
Returns the value of attribute rest_endpoint.
Instance Method Summary collapse
- #container(container_id) ⇒ Object
- #container_exists?(container_id) ⇒ Boolean
- #container_state(container_id) ⇒ Object
- #container_status(container_id) ⇒ Object
- #create_container(container_name, container_options = {}) ⇒ Object
- #delete_container(container_id) ⇒ Object
-
#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.
- #server_info ⇒ Object
- #start_container(container_id) ⇒ Object
- #stop_container(container_id, options = {}) ⇒ Object
- #transport_for(container_name) ⇒ Object
- #update_container(container_name, container_options) ⇒ Object
Methods included from Helpers::WaitMixin
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
23 24 25 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 23 def api @api end |
#driver_options ⇒ Object (readonly)
Returns the value of attribute driver_options.
23 24 25 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 23 def @driver_options end |
#rest_endpoint ⇒ Object (readonly)
Returns the value of attribute rest_endpoint.
23 24 25 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 23 def rest_endpoint @rest_endpoint end |
Instance Method Details
#container(container_id) ⇒ Object
108 109 110 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 108 def container(container_id) Driver.convert_bools api.container(container_id)[:metadata] end |
#container_exists?(container_id) ⇒ Boolean
112 113 114 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 112 def container_exists?(container_id) api.containers[:metadata].map { |url| url.split("/").last }.include? container_id end |
#container_state(container_id) ⇒ Object
103 104 105 106 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 103 def container_state(container_id) return nil unless container_status(container_id) == "running" # Parity with CLI api.container_state(container_id)[:metadata] end |
#container_status(container_id) ⇒ Object
99 100 101 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 99 def container_status(container_id) STATUS_CODES[api.container(container_id)[:metadata][:status_code].to_i] end |
#create_container(container_name, container_options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 35 def create_container(container_name, = {}) autostart = (.delete(:autostart) != false) if container_exists?(container_name) start_container(container_name) if autostart return container_name end # parity note: CLI will run indefinitely rather than timeout hence the 0 timeout retry_forever do api.create_container(container_name, .merge(sync: false)) end start_container(container_name) if autostart container_name end |
#delete_container(container_id) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 92 def delete_container(container_id) return unless container_exists? container_id stop_container container_id, force: true api.delete_container container_id 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
13 14 15 16 17 18 19 20 21 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 13 def initialize(rest_endpoint, = {}, inner_driver = nil) @rest_endpoint = rest_endpoint @driver_options = = ( || {}).merge( api_endpoint: rest_endpoint, auto_sync: true ) @api = inner_driver || RestAPI.new() end |
#server_info ⇒ Object
27 28 29 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 27 def server_info api.server_info end |
#start_container(container_id) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 54 def start_container(container_id) return if container_status(container_id) == "running" retry_forever do api.start_container(container_id, sync: false) end wait_for_status container_id, "running" end |
#stop_container(container_id, options = {}) ⇒ Object
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 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 62 def stop_container(container_id, = {}) return if container_status(container_id) == "stopped" if [:force] api.stop_container(container_id, force: true) else last_id = nil use_last = false LXD.with_timeout_and_retries({ timeout: 0 }.merge()) 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 = api.stop_container(container_id, sync: false)[:metadata][:id] rescue NexusSW::LXD::RestAPI::Error::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 api.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
31 32 33 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 31 def transport_for(container_name) Transport::Rest.new container_name, info: server_info, connection: api, driver_options: , rest_endpoint: rest_endpoint end |
#update_container(container_name, container_options) ⇒ Object
49 50 51 52 |
# File 'lib/nexussw/lxd/driver/mixins/rest.rb', line 49 def update_container(container_name, ) api.update_container(container_name, ) container(container_name) end |