예제 #1
0
    def delete(self, application_name, cluster_name):
        """Deregisters a service instance.

        The ``write`` authority is required. See :ref:`application_auth` also.

        :param application_name: The name of application.
        :param cluster_name: The name of cluster.
        :form key: The name of instance.
        :<header Authorization: Huskar Token (See :ref:`token`)
        :status 404: The application is not found.
        :status 400: The request body is invalid.
        :status 200: The service instance is deregistered successfully.
        """
        check_application_auth(application_name, Authority.WRITE)
        check_cluster_name(cluster_name, application_name)

        key = request.values['key'].strip()
        validate_fields(instance_schema, {
            'application': application_name,
            'cluster': cluster_name,
            'key': key,
        })

        im = InstanceManagement(huskar_client, application_name,
                                SERVICE_SUBDOMAIN)
        instance, _ = im.get_instance(cluster_name, key, resolve=False)
        if instance.stat is None:
            abort(
                404, '%s %s/%s/%s does not exist' % (
                    SERVICE_SUBDOMAIN,
                    application_name,
                    cluster_name,
                    key,
                ))

        old_data = instance.data
        with audit_log(audit_log.types.DELETE_SERVICE,
                       application_name=application_name,
                       cluster_name=cluster_name,
                       key=key,
                       old_data=old_data):
            service_facade.delete(application_name,
                                  cluster_name,
                                  key,
                                  strict=True)

        # Writes container registry finally
        if is_container_id(key):
            cm = ContainerManagement(huskar_client, key)
            cm.deregister_from(application_name, cluster_name)

        return api_response()
예제 #2
0
    def delete(self, container_id):
        """Reregisters all instances bound to this container.

        The site admin authority is required. See :ref:`site_admin` also.

        :param container_id: The container id (a.k.a CID, Task ID).
        :<header Authorization: Huskar Token (See :ref:`token`)
        :status 409: The container is still using by another one, and who are
                     registering new service instance on it. It is recommended
                     to try again since the container is truly dead.
        :status 200: The request is successful.
        """
        g.auth.require_admin()

        cm = ContainerManagement(huskar_client, container_id)
        cm.set_barrier()

        for application_name, cluster_name in cm.lookup():
            # Deregister current instance
            old_data = service_facade.get_value(
                application_name, cluster_name, container_id)
            service_facade.delete(
                application_name, cluster_name, container_id, strict=False)
            audit_log.emit(
                audit_log.types.DELETE_SERVICE,
                application_name=application_name, cluster_name=cluster_name,
                key=container_id, old_data=old_data)
            # Release container record
            cm.deregister_from(application_name, cluster_name)

        try:
            cm.destroy()
        except NotEmptyError:
            message = 'Container {0} is still registering new instance'.format(
                container_id)
            abort(409, message)

        return api_response()