Example #1
0
class TestGetErrorMessage(MAASTestCase):

    scenarios = [
        ('auth', dict(
            exception=PowerAuthError('auth'),
            message="Could not authenticate to node's BMC: auth",
            )),
        ('conn', dict(
            exception=PowerConnError('conn'),
            message="Could not contact node's BMC: conn",
            )),
        ('setting', dict(
            exception=PowerSettingError('setting'),
            message="Missing or invalid power setting: setting",
            )),
        ('tool', dict(
            exception=PowerToolError('tool'),
            message="Missing power tool: tool",
            )),
        ('action', dict(
            exception=PowerActionError('action'),
            message="Failed to complete power action: action",
            )),
        ('unknown', dict(
            exception=PowerError('unknown error'),
            message="Failed talking to node's BMC: unknown error",
            )),
    ]

    def test_return_msg(self):
        self.assertEqual(self.message, get_error_message(self.exception))
Example #2
0
    def power_control_nova(self,
                           power_change,
                           nova_id=None,
                           os_tenantname=None,
                           os_username=None,
                           os_password=None,
                           os_authurl=None,
                           **extra):
        """Control power of nova instances."""
        if not self.try_novaapi_import():
            raise PowerToolError("Missing the python3-novaclient package.")
        if user_domain_name != "" and project_domain_name != "":
            nova = self.nova_api.Client(
                2,
                username=os_username,
                password=os_password,
                project_name=os_tenantname,
                auth_url=os_authurl,
                user_domain_name=user_domain_name,
                project_domain_name=project_domain_name)
        else:
            nova = self.nova_api.Client(2, os_username, os_password,
                                        os_tenantname, os_authurl)

        try:
            urllib.request.urlopen(os_authurl)
        except urllib.error.URLError:
            raise PowerError('%s: URL error' % os_authurl)
        try:
            nova.authenticate()
        except self.nova_api.exceptions.Unauthorized:
            raise PowerAuthError('Failed to authenticate with OpenStack')
        try:
            pwr_stateStr = "OS-EXT-STS:power_state"
            tsk_stateStr = "OS-EXT-STS:task_state"
            vm_stateStr = "OS-EXT-STS:vm_state"
            power_state = getattr(nova.servers.get(nova_id), pwr_stateStr)
            task_state = getattr(nova.servers.get(nova_id), tsk_stateStr)
            vm_state = getattr(nova.servers.get(nova_id), vm_stateStr)
        except self.nova_api.exceptions.NotFound:
            raise PowerError('%s: Instance id not found' % nova_id)

        if power_state == NovaPowerState.NOSTATE:
            raise PowerFatalError('%s: Failed to get power state' % nova_id)
        if power_state == NovaPowerState.RUNNING:
            if (power_change == 'off' and task_state != 'powering-off'
                    and vm_state != 'stopped'):
                nova.servers.get(nova_id).stop()
            elif power_change == 'query':
                return 'on'
        if power_state == NovaPowerState.SHUTDOWN:
            if (power_change == 'on' and task_state != 'powering-on'
                    and vm_state != 'active'):
                nova.servers.get(nova_id).start()
            elif power_change == 'query':
                return 'off'
Example #3
0
    def power_control_nova(
        self,
        power_change,
        nova_id=None,
        os_tenantname=None,
        os_username=None,
        os_password=None,
        os_authurl=None,
        **extra
    ):
        """Control power of nova instances."""
        if not self.try_novaapi_import():
            raise PowerToolError("Missing the python3-novaclient package.")
        nova = self.nova_api.Client(
            2, os_username, os_password, os_tenantname, os_authurl
        )

        try:
            urllib.request.urlopen(os_authurl)
        except urllib.error.URLError:
            raise PowerError("%s: URL error" % os_authurl)
        try:
            nova.authenticate()
        except self.nova_api.exceptions.Unauthorized:
            raise PowerAuthError("Failed to authenticate with OpenStack")
        try:
            pwr_stateStr = "OS-EXT-STS:power_state"
            tsk_stateStr = "OS-EXT-STS:task_state"
            vm_stateStr = "OS-EXT-STS:vm_state"
            power_state = getattr(nova.servers.get(nova_id), pwr_stateStr)
            task_state = getattr(nova.servers.get(nova_id), tsk_stateStr)
            vm_state = getattr(nova.servers.get(nova_id), vm_stateStr)
        except self.nova_api.exceptions.NotFound:
            raise PowerError("%s: Instance id not found" % nova_id)

        if power_state == NovaPowerState.NOSTATE:
            raise PowerFatalError("%s: Failed to get power state" % nova_id)
        if power_state == NovaPowerState.RUNNING:
            if (
                power_change == "off"
                and task_state != "powering-off"
                and vm_state != "stopped"
            ):
                nova.servers.get(nova_id).stop()
            elif power_change == "query":
                return "on"
        if power_state == NovaPowerState.SHUTDOWN:
            if (
                power_change == "on"
                and task_state != "powering-on"
                and vm_state != "active"
            ):
                nova.servers.get(nova_id).start()
            elif power_change == "query":
                return "off"