def on_get(self, req, resp, address):
        """
        Handles retrieval of an existing Host.

        :param req: Request instance that will be passed through.
        :type req: falcon.Request
        :param resp: Response instance that will be passed through.
        :type resp: falcon.Response
        :param address: The address of the Host being requested.
        :type address: str
        """
        # If the host is still bootstrapping, the store handler
        # won't find it yet.  So respond with a fake host status.
        if cherrypy.engine.publish('investigator-is-pending', address)[0]:
            host = Host.new(address=address, status='investigating')
            resp.status = falcon.HTTP_200
            req.context['model'] = host
            return

        # TODO: Verify input
        try:
            store_manager = cherrypy.engine.publish('get-store-manager')[0]
            # TODO: use some kind of global default for Hosts
            host = store_manager.get(Host.new(address=address))
            resp.status = falcon.HTTP_200
            req.context['model'] = host
        except:
            resp.status = falcon.HTTP_404
            return
Exemple #2
0
    def on_get(self, req, resp, address):
        """
        Handles retrieval of existing Host credentials.

        :param req: Request instance that will be passed through.
        :type req: falcon.Request
        :param resp: Response instance that will be passed through.
        :type resp: falcon.Response
        :param address: The address of the Host being requested.
        :type address: str
        """
        # TODO: Verify input
        # TODO: Decide if this should be a model or if it makes sense to
        #       stay a subset off of Host and bypass the req.context
        #       middleware system.
        try:
            store_manager = cherrypy.engine.publish('get-store-manager')[0]
            host = store_manager.get(Host.new(address=address))
            resp.status = falcon.HTTP_200
            body = {
                'ssh_priv_key': host.ssh_priv_key,
                'remote_user': host.remote_user or 'root',
            }
            resp.body = json.dumps(body)
        except:
            resp.status = falcon.HTTP_404
            return
    def on_get(self, req, resp, address):
        """
        Handles retrieval of existing Host credentials.

        :param req: Request instance that will be passed through.
        :type req: falcon.Request
        :param resp: Response instance that will be passed through.
        :type resp: falcon.Response
        :param address: The address of the Host being requested.
        :type address: str
        """
        # TODO: Verify input
        # TODO: Decide if this should be a model or if it makes sense to
        #       stay a subset off of Host and bypass the req.context
        #       middleware system.
        try:
            store_manager = cherrypy.engine.publish('get-store-manager')[0]
            host = store_manager.get(Host.new(address=address))
            resp.status = falcon.HTTP_200
            body = {
                'ssh_priv_key': host.ssh_priv_key,
                'remote_user': host.remote_user or 'root',
            }
            resp.body = json.dumps(body)
        except:
            resp.status = falcon.HTTP_404
            return
 def test__format_key_with_primary_key(self):
     """
     Verify etcd keys are generated correctly whith a primary key.
     """
     self.assertEquals(
         '/commissaire/hosts/10.0.0.1',
         self.instance._format_key(
             Host.new(
                 address='10.0.0.1', status='', os='', cpus=2,
                 memory=1024, space=1000, last_check='',
                 ssh_priv_key='', remote_user='')))
    def test__dispatch(self):
        """
        Verify dispatching of operations works properly.
        """
        # Test namespace
        self.instance._save_on_namespace = mock.MagicMock()
        self.instance._dispatch('save', Cluster.new(name='test'))
        self.instance._save_on_namespace.assert_called_once()

        self.instance._get_on_namespace = mock.MagicMock()
        self.instance._dispatch('get', Cluster.new(name='test'))
        self.instance._get_on_namespace.assert_called_once()

        self.instance._delete_on_namespace = mock.MagicMock()
        self.instance._dispatch('delete', Cluster.new(name='test'))
        self.instance._delete_on_namespace.assert_called_once()

        self.instance._list_on_namespace = mock.MagicMock()
        self.instance._dispatch('list', Cluster.new(name='test'))
        self.instance._list_on_namespace.assert_called_once()

        # Test host
        self.instance._save_host = mock.MagicMock()
        self.instance._dispatch('save', Host.new(name='test'))
        self.instance._save_host.assert_called_once()

        self.instance._get_host = mock.MagicMock()
        self.instance._dispatch('get', Host.new(name='test'))
        self.instance._get_host.assert_called_once()

        self.instance._delete_host = mock.MagicMock()
        self.instance._dispatch('delete', Host.new(name='test'))
        self.instance._delete_host.assert_called_once()

        self.instance._list_host = mock.MagicMock()
        self.instance._dispatch('list', Host.new(name='test'))
        self.instance._list_host.assert_called_once()
Exemple #6
0
    def test__dispatch(self):
        """
        Verify dispatching of operations works properly.
        """
        # Test namespace
        self.instance._save_on_namespace = mock.MagicMock()
        self.instance._dispatch('save', Cluster.new(name='test'))
        self.instance._save_on_namespace.assert_called_once()

        self.instance._get_on_namespace = mock.MagicMock()
        self.instance._dispatch('get', Cluster.new(name='test'))
        self.instance._get_on_namespace.assert_called_once()

        self.instance._delete_on_namespace = mock.MagicMock()
        self.instance._dispatch('delete', Cluster.new(name='test'))
        self.instance._delete_on_namespace.assert_called_once()

        self.instance._list_on_namespace = mock.MagicMock()
        self.instance._dispatch('list', Cluster.new(name='test'))
        self.instance._list_on_namespace.assert_called_once()

        # Test host
        self.instance._save_host = mock.MagicMock()
        self.instance._dispatch('save', Host.new(name='test'))
        self.instance._save_host.assert_called_once()

        self.instance._get_host = mock.MagicMock()
        self.instance._dispatch('get', Host.new(name='test'))
        self.instance._get_host.assert_called_once()

        self.instance._delete_host = mock.MagicMock()
        self.instance._dispatch('delete', Host.new(name='test'))
        self.instance._delete_host.assert_called_once()

        self.instance._list_host = mock.MagicMock()
        self.instance._dispatch('list', Host.new(name='test'))
        self.instance._list_host.assert_called_once()
    def on_delete(self, req, resp, address):
        """
        Handles the Deletion of a Host.

        :param req: Request instance that will be passed through.
        :type req: falcon.Request
        :param resp: Response instance that will be passed through.
        :type resp: falcon.Response
        :param address: The address of the Host being requested.
        :type address: str
        """
        resp.body = '{}'
        store_manager = cherrypy.engine.publish('get-store-manager')[0]
        try:
            host = Host.new(address=address)
            WATCHER_QUEUE.dequeue(host)
            store_manager.delete(host)
            self.logger.debug(
                'Deleted host {0} and dequeued it from the watcher.'.format(
                    host.address))
            resp.status = falcon.HTTP_200
        except:
            resp.status = falcon.HTTP_404

        # Also remove the host from all clusters.
        # Note: We've done all we need to for the host deletion,
        #       so if an error occurs from here just log it and
        #       return.
        try:
            clusters = store_manager.list(Clusters(clusters=[]))
        except:
            self.logger.warn('Store does not have any clusters')
            return
        for cluster in clusters.clusters:
            try:
                self.logger.debug('Checking cluster {0}'.format(cluster.name))
                if address in cluster.hostset:
                    self.logger.info('Removing {0} from cluster {1}'.format(
                        address, cluster.name))
                    cluster.hostset.remove(address)
                    store_manager.save(cluster)
                    self.logger.info(
                        '{0} has been removed from cluster {1}'.format(
                            address, cluster.name))
            except:
                self.logger.warn(
                    'Failed to remove {0} from cluster {1}'.format(
                        address, cluster.name))
 def test__format_key_with_primary_key(self):
     """
     Verify etcd keys are generated correctly whith a primary key.
     """
     self.assertEquals(
         '/commissaire/hosts/10.0.0.1',
         self.instance._format_key(
             Host.new(address='10.0.0.1',
                      status='',
                      os='',
                      cpus=2,
                      memory=1024,
                      space=1000,
                      last_check='',
                      ssh_priv_key='',
                      remote_user='')))
Exemple #9
0
    def on_delete(self, req, resp, address):
        """
        Handles the Deletion of a Host.

        :param req: Request instance that will be passed through.
        :type req: falcon.Request
        :param resp: Response instance that will be passed through.
        :type resp: falcon.Response
        :param address: The address of the Host being requested.
        :type address: str
        """
        resp.body = '{}'
        store_manager = cherrypy.engine.publish('get-store-manager')[0]
        try:
            # TODO: use some kind of global default for Hosts
            store_manager.delete(Host.new(address=address))
            resp.status = falcon.HTTP_200
        except:
            resp.status = falcon.HTTP_404

        # Also remove the host from all clusters.
        # Note: We've done all we need to for the host deletion,
        #       so if an error occurs from here just log it and
        #       return.
        try:
            clusters = store_manager.list(Clusters(clusters=[]))
        except:
            self.logger.warn('Store does not have any clusters')
            return
        for cluster in clusters.clusters:
            try:
                self.logger.debug(
                    'Checking cluster {0}'.format(cluster.name))
                if address in cluster.hostset:
                    self.logger.info(
                        'Removing {0} from cluster {1}'.format(
                            address, cluster.name))
                    cluster.hostset.remove(address)
                    store_manager.save(cluster)
                    self.logger.info(
                        '{0} has been removed from cluster {1}'.format(
                            address, cluster.name))
            except:
                self.logger.warn(
                    'Failed to remove {0} from cluster {1}'.format(
                        address, cluster.name))
    def _list_host(self, model_instance):
        """
        Lists data at a location in a store and returns back model instances.

        :param model_instance: Model instance to search for and list
        :type model_instance: commissaire.model.Model
        :returns: A list of models
        :rtype: list
        """
        hosts = []
        path = _model_mapper[model_instance.__class__.__name__]
        items = self._store.get(self._endpoint + path).json()
        for item in items.get('items'):
            try:
                hosts.append(self._format_model(item, Host.new(), True))
            except (TypeError, KeyError):
                # TODO: Add logging
                pass

        return Hosts.new(hosts=hosts)
    def _list_host(self, model_instance):
        """
        Lists data at a location in a store and returns back model instances.

        :param model_instance: Model instance to search for and list
        :type model_instance: commissaire.model.Model
        :returns: A list of models
        :rtype: list
        """
        hosts = []
        path = _model_mapper[model_instance.__class__.__name__]
        items = self._store.get(self._endpoint + path).json()
        for item in items.get('items'):
            try:
                hosts.append(self._format_model(item, Host.new(), True))
            except (TypeError, KeyError):
                # TODO: Add logging
                pass

        return Hosts.new(hosts=hosts)
Exemple #12
0
    def on_get(self, req, resp, address):
        """
        Handles retrieval of an existing Host.

        :param req: Request instance that will be passed through.
        :type req: falcon.Request
        :param resp: Response instance that will be passed through.
        :type resp: falcon.Response
        :param address: The address of the Host being requested.
        :type address: str
        """
        # TODO: Verify input
        try:
            store_manager = cherrypy.engine.publish('get-store-manager')[0]
            # TODO: use some kind of global default for Hosts
            host = store_manager.get(Host.new(address=address))
            resp.status = falcon.HTTP_200
            req.context['model'] = host
        except:
            resp.status = falcon.HTTP_404
            return
    def on_get(self, req, resp, address):
        """
        Handles retrieval of existing Host status.

        :param req: Request instance that will be passed through.
        :type req: falcon.Request
        :param resp: Response instance that will be passed through.
        :type resp: falcon.Response
        :param address: The address of the Host being requested.
        :type address: str
        """
        try:
            store_manager = cherrypy.engine.publish('get-store-manager')[0]
            host = store_manager.get(Host.new(address=address))
            self.logger.debug('StatusHost found host {0}'.format(host.address))
            status = HostStatus.new(host={
                'last_check': host.last_check,
                'status': host.status,
            })

            try:
                resp.status = falcon.HTTP_200
                cluster = util.cluster_for_host(host.address, store_manager)
                status.type = cluster.type
                self.logger.debug('Cluster type for {0} is {1}'.format(
                    host.address, status.type))

                if status.type != C.CLUSTER_TYPE_HOST:
                    try:
                        container_mgr = store_manager.list_container_managers(
                            cluster.type)[0]
                    except Exception as error:
                        self.logger.error(
                            'StatusHost for host {0} did not find a '
                            'container_mgr: {1}: {2}'.format(
                                host.address, type(error), error))
                        raise error
                    self.logger.debug(
                        'StatusHost for host {0} got container_mgr '
                        'instance {1}'.format(host.address,
                                              type(container_mgr)))

                    is_raw = req.get_param_as_bool('raw') or False
                    self.logger.debug(
                        'StatusHost raw={0} found host {0} will'.format(
                            is_raw, host.address))

                    status_code, result = container_mgr.get_host_status(
                        host.address, is_raw)

                    # If we have a raw request ..
                    if is_raw:
                        # .. forward the http status as well or fall back to
                        # service unavailable
                        resp.status = getattr(
                            falcon.status_codes,
                            'HTTP_{0}'.format(status_code),
                            falcon.status_codes.HTTP_SERVICE_UNAVAILABLE)
                    status.container_manager = result
                else:
                    # Raise to be caught in host only type
                    raise KeyError
            except KeyError:
                # The host is not in a cluster.
                self.logger.info(
                    'Host {0} is not in a cluster. Defaulting to {1}'.format(
                        host.address, C.CLUSTER_TYPE_HOST))
                status.type = C.CLUSTER_TYPE_HOST

            self.logger.debug(
                'StatusHost end status code: {0} json={1}'.format(
                    resp.status, status.to_json()))

        except Exception as ex:
            self.logger.debug(
                'Host Status exception caught for {0}: {1}:{2}'.format(
                    host.address, type(ex), ex))
            resp.status = falcon.HTTP_404
            return

        self.logger.debug('Status for {0}: {1}'.format(host.address,
                                                       status.to_json()))

        req.context['model'] = status
    Returns a new deepcopy of an instance.
    """
    return copy.deepcopy(instance)


#: Response JSON for a single host
HOST_JSON = (
    '{"address": "10.2.0.2",'
    ' "status": "available", "os": "atomic",'
    ' "cpus": 2, "memory": 11989228, "space": 487652,'
    ' "last_check": "2015-12-17T15:48:18.710454"}')
#: Credential JSON for tests
HOST_CREDS_JSON = '{"remote_user": "******", "ssh_priv_key": "dGVzdAo="}'
#: Host model for most tests
HOST = Host.new(
    ssh_priv_key='dGVzdAo=',
    remote_user='******',
    **json.loads(HOST_JSON))
#: Hosts model for most tests
HOSTS = Hosts.new(
    hosts=[HOST]
)
#: Cluster model for most tests
CLUSTER = Cluster.new(
    name='cluster',
    status='ok',
    hostset=[],
)
#: Cluster model with HOST for most tests
CLUSTER_WITH_HOST = Cluster.new(
    name='cluster',
    status='ok',
Exemple #15
0
def etcd_host_create(address, ssh_priv_key, remote_user, cluster_name=None):
    """
    Creates a new host record in etcd and optionally adds the host to
    the specified cluster.  Returns a (status, host) tuple where status
    is the Falcon HTTP status and host is a Host model instance, which
    may be None if an error occurred.

    This function is idempotent so long as the host parameters agree
    with an existing host record and cluster membership.

    :param address: Host address.
    :type address: str
    :param ssh_priv_key: Host's SSH key, base64-encoded.
    :type ssh_priv_key: str
    :param remote_user: The user to use with SSH.
    :type remote_user: str
    :param cluster_name: Name of the cluster to join, or None
    :type cluster_name: str or None
    :return: (status, host)
    :rtype: tuple
    """
    store_manager = cherrypy.engine.publish('get-store-manager')[0]

    try:
        # Check if the request conflicts with the existing host.
        existing_host = store_manager.get(Host.new(address=address))
        if existing_host.ssh_priv_key != ssh_priv_key:
            return (falcon.HTTP_409, None)
        if cluster_name:
            try:
                assert etcd_cluster_has_host(cluster_name, address)
            except (AssertionError, KeyError):
                return (falcon.HTTP_409, None)

        # Request is compatible with the existing host, so
        # we're done.  (Not using HTTP_201 since we didn't
        # actually create anything.)
        return (falcon.HTTP_200, existing_host)
    except:
        pass

    host_creation = Host.new(
        address=address,
        ssh_priv_key=ssh_priv_key,
        status='investigating',
        remote_user=remote_user
    ).__dict__

    # Verify the cluster exists, if given.  Do it now
    # so we can fail before writing anything to etcd.
    if cluster_name:
        if not etcd_cluster_exists(cluster_name):
            return (falcon.HTTP_409, None)

    host = Host(**host_creation)

    new_host = store_manager.save(host)

    # Add host to the requested cluster.
    if cluster_name:
        etcd_cluster_add_host(cluster_name, address)

    manager_clone = store_manager.clone()
    job_request = (manager_clone, host_creation, ssh_priv_key, remote_user)
    INVESTIGATE_QUEUE.put(job_request)

    return (falcon.HTTP_201, new_host)
Exemple #16
0
def etcd_host_create(address, ssh_priv_key, remote_user, cluster_name=None):
    """
    Creates a new host record in etcd and optionally adds the host to
    the specified cluster.  Returns a (status, host) tuple where status
    is the Falcon HTTP status and host is a Host model instance, which
    may be None if an error occurred.

    This function is idempotent so long as the host parameters agree
    with an existing host record and cluster membership.

    :param address: Host address.
    :type address: str
    :param ssh_priv_key: Host's SSH key, base64-encoded.
    :type ssh_priv_key: str
    :param remote_user: The user to use with SSH.
    :type remote_user: str
    :param cluster_name: Name of the cluster to join, or None
    :type cluster_name: str or None
    :return: (status, host)
    :rtype: tuple
    """
    store_manager = cherrypy.engine.publish('get-store-manager')[0]

    try:
        # Check if the request conflicts with the existing host.
        existing_host = store_manager.get(Host.new(address=address))
        if existing_host.ssh_priv_key != ssh_priv_key:
            return (falcon.HTTP_409, None)
        if cluster_name:
            try:
                assert etcd_cluster_has_host(cluster_name, address)
            except (AssertionError, KeyError):
                return (falcon.HTTP_409, None)

        # Request is compatible with the existing host, so
        # we're done.  (Not using HTTP_201 since we didn't
        # actually create anything.)
        return (falcon.HTTP_200, existing_host)
    except:
        pass

    # Verify the cluster exists, if given.  Do it now
    # so we can fail before writing anything to etcd.
    if cluster_name:
        cluster = get_cluster_model(cluster_name)
        if cluster is None:
            return (falcon.HTTP_409, None)
    else:
        cluster = None

    host = Host.new(
        address=address,
        ssh_priv_key=ssh_priv_key,
        status='investigating',
        remote_user=remote_user)

    def callback(store_manager, host, exception):
        if exception is None:
            store_manager.save(host)

            # Add host to the requested cluster.
            if cluster_name:
                etcd_cluster_add_host(cluster_name, host.address)

    cherrypy.engine.publish(
        'investigator-submit', store_manager, host, cluster, callback)

    return (falcon.HTTP_201, host)
                     ' "cpus": 0, "memory": 0, "space": 0,'
                     ' "last_check": ""}')
#: Response JSON for a newly created implicit host (no address given)
INITIAL_IMPLICIT_HOST_JSON = ('{"address": "127.0.0.1",'
                              ' "status": "investigating", "os": "",'
                              ' "cpus": 0, "memory": 0, "space": 0,'
                              ' "last_check": ""}')
#: Credential JSON for tests
HOST_CREDS_JSON = '{"remote_user": "******", "ssh_priv_key": "dGVzdAo="}'
#: HostStatus JSON for tests
HOST_STATUS_JSON = (
    '{"type": "host_only", "container_manager": {}, "commissaire": '
    '{"status": "available", "last_check": "2016-07-29T20:39:50.529454"}}')
#: Host model for most tests
HOST = Host.new(ssh_priv_key='dGVzdAo=',
                remote_user='******',
                **json.loads(HOST_JSON))
#: HostStatus model for most tests
HOST_STATUS = HostStatus.new(**json.loads(HOST_STATUS_JSON))
#: Hosts model for most tests
HOSTS = Hosts.new(hosts=[HOST])
#: Cluster model for most tests
CLUSTER = Cluster.new(
    name='cluster',
    status='ok',
    hostset=[],
)
#: Cluster model with HOST for most tests
CLUSTER_WITH_HOST = Cluster.new(
    name='cluster',
    status='ok',