예제 #1
0
    def _destroy(self, identifier):
        attempt = 0
        host = Host.get(identifier=identifier)

        get_inst = self.get_instance(host.name,
                                     host.zone,
                                     execute_request=False)

        while attempt < self.WAIT_ATTEMPTS:
            try:
                inst = get_inst.execute()
            except Exception as ex:
                if ex.resp.status == 404:
                    return True
                raise ex
            else:
                if (inst.get('status') in ['STOPPING']):
                    sleep(self.WAIT_TIME)
                else:
                    attempt = self.WAIT_ATTEMPTS

            attempt += 1

        destroy = self.client.instances().delete(
            project=self.credential.project,
            zone=host.zone,
            instance=host.name).execute()

        return self.wait_operation(operation=destroy.get('name'),
                                   zone=host.zone)
예제 #2
0
def reinstall_host(provider_name, env):
    data = request.get_json()
    host_id = data.get("host_id", None)
    engine = data.get("engine", None)
    service_account = data.get("service_account", None)
    extra_params = {
        'team_name': data.get("team_name", None),
        'database_name': data.get("database_name", None),
        'group': data.get("group", None),
        'service_account': service_account
    }

    # TODO improve validation and response
    if not host_id:
        return response_invalid_request(
            "host_id are required. Payload: {}".format(data)
        )

    try:
        host = Host.get(id=host_id)
    except Host.DoesNotExist:
        return response_not_found(host_id)

    try:
        provider = build_provider(provider_name, env, host.engine)
        provider.restore(host, engine, **extra_params)
        if engine and engine != host.engine:
            host.engine = engine
            host.save()
    except Exception as e:  # TODO What can get wrong here?
        print_exc()  # TODO Improve log
        return response_invalid_request(str(e))

    return response_ok()
예제 #3
0
def resize_host(provider_name, env):
    data = request.get_json()
    host_id = data.get("host_id", None)
    cpus = data.get("cpus", None)
    memory = data.get("memory", None)

    # TODO improve validation and response
    if not cpus or not host_id or not memory:
        return response_invalid_request(
            "cpus, host_id and memory are required. Payload: {}".format(data)
        )

    try:
        host = Host.get(id=host_id)
    except Host.DoesNotExist:
        return response_not_found(host_id)

    try:
        provider = build_provider(provider_name, env, host.engine)
        if int(cpus) == host.cpu and int(memory) == host.memory:
            logging.error(
                "Notting to resize for host {}, offering already done".format(
                    host.id
                )
            )
            return response_ok()
        provider.resize(host, cpus, memory)
    except Exception as e:  # TODO What can get wrong here?
        print_exc()  # TODO Improve log
        return response_invalid_request(str(e))

    host.cpu = int(cpus)
    host.memory = int(memory)
    host.save()
    return response_ok()
예제 #4
0
def status_host(provider_name, env, host_id):
    if not host_id:
        return response_invalid_request("Missing parameter host_id")

    try:
        host = Host.get(id=host_id, environment=env)
        provider = build_provider(provider_name, env, host.engine)
        host_status, version_id = provider.get_status(host)
        return response_ok(host_status=host_status, version_id=version_id)
    except Host.DoesNotExist:
        return response_not_found(host_id)
예제 #5
0
    def stop(self, identifier):
        host = Host.get(identifier=identifier)

        project = self.credential.project
        zone = host.zone
        instance_name = host.name

        stop = self.client.instances().stop(project=project,
                                            zone=zone,
                                            instance=instance_name).execute()

        return self.wait_operation(operation=stop.get('name'), zone=zone)
예제 #6
0
def _host_info(provider_name, env, host_id, refresh=False):
    if not host_id:
        return response_invalid_request("Missing parameter host_id")

    try:
        host = Host.get(id=host_id, environment=env)
        provider = build_provider(provider_name, env, host.engine)
        if refresh:
            provider.refresh_metadata(host)
        database_host_metadata = host.to_dict
        if hasattr(provider, 'fqdn'):
            database_host_metadata.update({'fqdn': provider.fqdn(host)})
        return response_ok(**database_host_metadata)
    except Host.DoesNotExist:
        return response_not_found(host_id)
예제 #7
0
    def stop(self, identifier):
        host = None
        try:
            host = Host.get(identifier=identifier)
        except Host.DoesNotExist:
            raise NodeFoundError("Could not find host")

        action = (
            self.connCls.paths_connection_restapi.get("action_stopvm").format(
                self.credential.subscription_id,
                self.credential.resource_group, host.name))
        header = {}
        self.get_azure_connection()
        base_url = self.azClient.endpoint
        self.azClient.connect(base_url=base_url)
        self.azClient.add_default_headers(header)
        self.azClient.connection.request("POST", action, headers=header)
        response = self.azClient.connection.getresponse()
예제 #8
0
    def _destroy(self, identifier):
        host = None
        try:
            host = Host.get(identifier=identifier)
        except Host.DoesNotExist:
            raise NodeFoundError("Could not find host")

        action = (self.connCls.paths_connection_restapi.get(
            "action_destroyvm").format(self.credential.subscription_id,
                                       self.credential.resource_group,
                                       host.name))
        header = {}
        self.get_azure_connection()
        base_url = self.azClient.endpoint
        self.azClient.connect(base_url=base_url)
        self.azClient.add_default_headers(header)
        self.azClient.connection.request("DELETE", action, headers=header)
        response = self.azClient.connection.getresponse()
        if response.status_code != 202 and response.status_code != 204:
            raise OperationNotAllowed("Operation not allowed")
예제 #9
0
def start_host(provider_name, env):
    data = request.get_json()
    host_id = data.get("host_id", None)

    # TODO improve validation and response
    if not host_id:
        return response_invalid_request("invalid data {}".format(data))

    try:
        host = Host.get(id=host_id)
    except Host.DoesNotExist:
        return response_not_found(host_id)

    try:
        provider = build_provider(provider_name, env, host.engine)
        provider.start(host)
    except Exception as e:  # TODO What can get wrong here?
        print_exc()  # TODO Improve log
        return response_invalid_request(str(e))

    return response_ok()
예제 #10
0
def destroy_host(provider_name, env, host_id):
    # TODO improve validation and response
    if not host_id:
        return response_invalid_request("invalid data")

    try:
        host = Host.get(id=host_id)
    except Host.DoesNotExist:
        return response_not_found(host_id)

    try:
        provider = build_provider(provider_name, env, host.engine)
        provider.destroy(
            group=host.group,
            identifier=host.identifier
        )
    except Exception as e:  # TODO What can get wrong here?
        print_exc()  # TODO Improve log
        return response_invalid_request(str(e))

    host.delete_instance()

    return response_ok()