lib/nexussw/lxd/driver/mixins/rest.rb in lxd-common-0.9.8 vs lib/nexussw/lxd/driver/mixins/rest.rb in lxd-common-0.9.9

- old
+ new

@@ -1,8 +1,8 @@ -require 'nexussw/lxd/rest_api' -require 'nexussw/lxd/driver/mixins/helpers/wait' -require 'nexussw/lxd/transport/rest' +require "nexussw/lxd/rest_api" +require "nexussw/lxd/driver/mixins/helpers/wait" +require "nexussw/lxd/transport/rest" module NexusSW module LXD class Driver module Mixins @@ -23,47 +23,53 @@ attr_reader :api, :rest_endpoint, :driver_options include Helpers::WaitMixin def server_info - @server_info ||= api.get('/1.0')[:metadata] + api.server_info end def transport_for(container_name) Transport::Rest.new container_name, info: server_info, connection: api, driver_options: driver_options, rest_endpoint: rest_endpoint end def create_container(container_name, container_options = {}) + autostart = (container_options.delete(:autostart) != false) if container_exists?(container_name) - start_container container_name # Start the container for Parity with the CLI + 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, container_options.merge(sync: false)) end - start_container container_name + start_container(container_name) if autostart container_name end + def update_container(container_name, container_options) + api.update_container(container_name, container_options) + container(container_name) + end + def start_container(container_id) - return if container_status(container_id) == 'running' + return if container_status(container_id) == "running" retry_forever do api.start_container(container_id, sync: false) end - wait_for_status container_id, 'running' + wait_for_status container_id, "running" end def stop_container(container_id, options = {}) - return if container_status(container_id) == 'stopped' + return if container_status(container_id) == "stopped" if options[:force] api.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' + 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] @@ -73,51 +79,39 @@ 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' + 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' + wait_for_status container_id, "stopped" end 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 - - # trial return to normal - # begin api.delete_container container_id - # rescue ::Faraday::ConnectionFailed, ::NexusSW::LXD::RestAPI::Error::BadRequest - # LXD.with_timeout_and_retries timeout: 120 do - # loop do - # return unless container_exists? container_id - # sleep 0.3 - # end - # end - # end end def container_status(container_id) STATUS_CODES[api.container(container_id)[:metadata][:status_code].to_i] end def container_state(container_id) - return nil unless container_status(container_id) == 'running' # Parity with CLI + return nil unless container_status(container_id) == "running" # Parity with CLI api.container_state(container_id)[:metadata] end def container(container_id) - api.container(container_id)[:metadata] + Driver.convert_bools api.container(container_id)[:metadata] end def container_exists?(container_id) - api.containers[:metadata].map { |url| url.split('/').last }.include? container_id + api.containers[:metadata].map { |url| url.split("/").last }.include? container_id end protected def wait_for_status(container_id, newstatus)