Beispiel #1
0
    def create_host_object(self, provider, payload, env, created_host_metadata,
                           static_ip_id, **kw):
        nic = created_host_metadata.get("nic", '')
        vm = created_host_metadata.get("vm", '')

        configs = [conf for conf in nic["properties"]["ipConfigurations"]]
        primary_ip_address = [
            i["properties"]["privateIPAddress"] for i in configs
        ][0]
        identifier = vm["properties"]["vmId"]

        address = primary_ip_address
        host = Host(name=payload["name"],
                    group=payload["group"],
                    engine=payload["engine"],
                    environment=env,
                    cpu=payload["cpu"],
                    memory=payload["memory"],
                    provider=provider.credential.provider,
                    identifier=identifier,
                    address=address,
                    zone=provider.credential._zone)
        host.save()

        return host
Beispiel #2
0
 def create_host_object(self, provider, payload, env,
                        created_host_metadata, *args, **kw):
     host = Host(
         name=f"{created_host_metadata.metadata.name}-0",
         group=payload['group'],
         engine=payload['engine'], environment=env, cpu=payload['cpu'],
         memory=payload['memory'], provider=provider.credential.provider,
         identifier=created_host_metadata.metadata.name,
         address=created_host_metadata.metadata.name, zone=''
     )
     host.save()
     return host
Beispiel #3
0
 def create_host_object(self, provider, payload, env, created_host_metadata,
                        *args, **kw):
     address = created_host_metadata.private_ips[0]
     host = Host(name=payload['name'],
                 group=payload['group'],
                 engine=payload['engine'],
                 environment=env,
                 cpu=payload['cpu'],
                 memory=payload['memory'],
                 provider=provider.credential.provider,
                 identifier=created_host_metadata.id,
                 address=address,
                 zone=provider.credential._zone)
     host.save()
     return host
Beispiel #4
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)
Beispiel #5
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()
Beispiel #6
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()
Beispiel #7
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)
Beispiel #8
0
    def create_host_object(self, provider, payload, env, created_host_metadata,
                           static_ip_id, **kw):

        zone = provider.credential.zone
        instance_name = payload['name']

        instance = self.get_instance(instance_name, zone)
        address = instance['networkInterfaces'][0]['networkIP']

        host = Host(name=payload['name'],
                    group=payload['group'],
                    engine=payload['engine'],
                    environment=env,
                    cpu=payload['cpu'],
                    memory=payload['memory'],
                    provider=provider.credential.provider,
                    identifier=created_host_metadata['id'],
                    address=address,
                    zone=zone)
        host.save()
        return host
Beispiel #9
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)
Beispiel #10
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)
Beispiel #11
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()
Beispiel #12
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")
Beispiel #13
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()
Beispiel #14
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()
Beispiel #15
0
 def destroy(self, group, identifier):
     self._destroy(identifier)
     quantity = len(Host.filter(group=group))
     if quantity:
         self._all_node_destroyed(group)
Beispiel #16
0
 def setUp(self):
     self.host = Host()
     self.host._data = {'id': 11, 'name': 'fake_name'}