diff --git a/src/vmm_mad/remotes/lib/lxd/client.rb b/src/vmm_mad/remotes/lib/lxd/client.rb index 750396755e..5785a29f8d 100644 --- a/src/vmm_mad/remotes/lib/lxd/client.rb +++ b/src/vmm_mad/remotes/lib/lxd/client.rb @@ -94,7 +94,7 @@ class LXDClient def wait(response, timeout) operation_id = response['operation'].split('/').last - timeout = "?timeout=#{timeout}" if timeout + timeout = "?timeout=#{timeout}" if [nil, ''].include?(timeout) response = get("operations/#{operation_id}/wait#{timeout}") @@ -142,13 +142,17 @@ end # Error used for raising LXDClient exception when response is error return value class LXDError < StandardError - attr_reader :body, :error, :error_code, :type + attr_reader :body, :error, :code, :type - def initialize(msg = 'LXD API error') - @body = msg + def initialize(response) + raise "Got wrong argument class: #{response.class}, expecting Hash" \ + unless response.class == Hash + + @body = response + @code = @body['error_code'] + @type = @body['type'] @error = @body['error'] - @error_code = @body['error_code'] - @type = @body['type'] + super end diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index 940d48f3c3..6c5edc8747 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -88,8 +88,6 @@ class Container one = OpenNebulaVM.new(one_xml) if one_xml Container.new(info, one, client) - rescue LXDError => exception - raise exception end # Creates container from a OpenNebula VM xml description @@ -112,12 +110,12 @@ class Container containers end - # Returns boolean indicating if the container exists(true) or not (false) + # Returns boolean indicating the container exists(true) or not (false) def exist?(name, client) client.get("#{CONTAINERS}/#{name}") true - rescue LXDError => exception - raise exception if exception.body['error_code'] != 404 + rescue LXDError => e + raise e if e.code != 404 false end @@ -234,7 +232,7 @@ class Container return nil unless status end - return 'no context' unless @one.has_context? + return true unless @one.has_context? csrc = @lxc['devices']['context']['source'].clone @@ -271,7 +269,7 @@ class Container # Removes the context section from the LXD configuration and unmap the # context device def detach_context - return 'no context' unless @one.has_context? + return true unless @one.has_context? csrc = @lxc['devices']['context']['source'].clone diff --git a/src/vmm_mad/remotes/lib/lxd/opennebula_vm.rb b/src/vmm_mad/remotes/lib/lxd/opennebula_vm.rb index c604126e30..20dacca3e4 100644 --- a/src/vmm_mad/remotes/lib/lxd/opennebula_vm.rb +++ b/src/vmm_mad/remotes/lib/lxd/opennebula_vm.rb @@ -357,7 +357,7 @@ class OpenNebulaVM begin LXDClient.new.get("profiles/#{profile}") rescue LXDError => e - raise e unless e.error_code == 404 + raise e unless e.code == 404 OpenNebula.log_error "Profile \"#{profile}\" not found\n#{e}" profile = 'default' diff --git a/src/vmm_mad/remotes/lxd/deploy b/src/vmm_mad/remotes/lxd/deploy index 04ef7ad911..3d67455b87 100755 --- a/src/vmm_mad/remotes/lxd/deploy +++ b/src/vmm_mad/remotes/lxd/deploy @@ -51,7 +51,7 @@ if Container.exist?(container.name, client) container.get_metadata err_msg = 'A container with the same ID is already running' - raise LXDError, err_msg if container.status == 'Running' + raise err_msg if container.status == 'Running' container.config = config @@ -74,7 +74,9 @@ else begin operation = container.start raise operation if container.status != 'Running' - rescue LXDError => exception + rescue StandardError => exception + # TODO: Improve wait condition + sleep 5 # Wait for LXD to revert non-rootfs disks mountpoint storage_deleted = container.setup_storage('unmap') if storage_deleted @@ -83,7 +85,7 @@ else OpenNebula.log_error 'failed to dismantle container storage' end - raise LXDError, exception + raise exception end end