def get_cluster_deploy(message, bus): """ Gets a specific deployment. :param message: jsonrpc message structure. :type message: dict :param bus: Bus instance. :type bus: commissaire_http.bus.Bus :returns: A jsonrpc structure. :rtype: dict """ try: name = message['params']['name'] cluster_deploy = bus.storage.get(models.ClusterDeploy.new(name=name)) cluster_deploy._validate() return create_jsonrpc_response(message['id'], cluster_deploy.to_dict_safe()) except models.ValidationError as error: LOGGER.info('Invalid data retrieved. "%s"', error) LOGGER.debug('Data="%s"', message['params']) return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INVALID_REQUEST']) except _bus.StorageLookupError as error: return create_jsonrpc_error(message, error, JSONRPC_ERRORS['NOT_FOUND']) except Exception as error: return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INTERNAL_ERROR'])
def get_cluster_operation(model_cls, message, bus): """ Gets a specific operation based on the model_cls. :param model_cls: The model class to use. :type model_cls: class :param message: jsonrpc message structure. :type message: dict :param bus: Bus instance. :type bus: commissaire_http.bus.Bus :returns: A jsonrpc structure. :rtype: dict """ try: name = message['params']['name'] model = bus.storage.get(model_cls.new(name=name)) model._validate() return create_jsonrpc_response(message['id'], model.to_dict_safe()) except models.ValidationError as error: LOGGER.info('Invalid data retrieved. "%s"', error) LOGGER.debug('Data="%s"', message['params']) return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INVALID_REQUEST']) except _bus.StorageLookupError as error: return create_jsonrpc_error(message, error, JSONRPC_ERRORS['NOT_FOUND']) except Exception as error: LOGGER.debug('Error getting %s: %s: %s', model_cls.__name__, type(error), error) return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INTERNAL_ERROR'])
def create_cluster_deploy(message, bus): """ Creates a new cluster deployment. :param message: jsonrpc message structure. :type message: dict :param bus: Bus instance. :type bus: commissaire_http.bus.Bus :returns: A jsonrpc structure. :rtype: dict """ try: cluster_deploy = models.ClusterDeploy.new( name=message['params'].get('name'), version=message['params'].get('version')) cluster_deploy._validate() result = bus.request( 'jobs.clusterexec.deploy', params=[cluster_deploy.name, cluster_deploy.version]) return create_jsonrpc_response(message['id'], result['result']) except models.ValidationError as error: LOGGER.info('Invalid data provided. "%s"', error) LOGGER.debug('Data="%s"', message['params']) return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INVALID_REQUEST']) except Exception as error: LOGGER.debug('Error creating ClusterDeploy: %s: %s', type(error), error) return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INTERNAL_ERROR'])
def delete_host(message, bus): """ Deletes an existing host. :param message: jsonrpc message structure. :type message: dict :param bus: Bus instance. :type bus: commissaire_http.bus.Bus :returns: A jsonrpc structure. :rtype: dict """ try: address = message['params']['address'] LOGGER.debug('Attempting to delete host "{}"'.format(address)) bus.request('storage.delete', params=['Host', {'address': address}]) # TODO: kick off service job to remove the host? # Remove from a cluster for cluster in bus.request('storage.list', params=['Clusters', True])['result']: if address in cluster['hostset']: LOGGER.info('Removing host "{}" from cluster "{}"'.format( address, cluster['name'])) cluster['hostset'].pop(cluster['hostset'].index(address)) bus.request('storage.save', params=['Cluster', cluster]) # A host can only be part of one cluster so break the loop break return create_response(message['id'], []) except _bus.RemoteProcedureCallError as error: LOGGER.debug('Error deleting host: {}: {}'.format(type(error), error)) return return_error(message, error, JSONRPC_ERRORS['NOT_FOUND']) except Exception as error: LOGGER.debug('Error deleting host: {}: {}'.format(type(error), error)) return return_error(message, error, JSONRPC_ERRORS['INTERNAL_ERROR'])
def create_network(message, bus): """ Creates a new network. :param message: jsonrpc message structure. :type message: dict :param bus: Bus instance. :type bus: commissaire_http.bus.Bus :returns: A jsonrpc structure. :rtype: dict """ try: LOGGER.debug('create_network params: {}'.format(message['params'])) # Check to see if we already have a network with that name network = bus.request( 'storage.get', params=['Network', { 'name': message['params']['name'] }]) LOGGER.debug( 'Creation of already exisiting network "{0}" requested.'.format( message['params']['name'])) # If they are the same thing then go ahead and return success if models.Network.new( **network['result']).to_dict() == models.Network.new( **message['params']).to_dict(): return create_response(message['id'], network['result']) # Otherwise error with a CONFLICT return return_error(message, 'A network with that name already exists.', JSONRPC_ERRORS['CONFLICT']) except _bus.RemoteProcedureCallError as error: LOGGER.debug('Error getting network: {}: {}'.format( type(error), error)) LOGGER.info('Attempting to create new network: "{}"'.format( message['params'])) # Create the new network try: network = models.Network.new(**message['params']) network._validate() response = bus.request('storage.save', params=['Network', network.to_dict()]) return create_response(message['id'], response['result']) except models.ValidationError as error: return return_error(message, error, JSONRPC_ERRORS['INVALID_REQUEST'])
def create_cluster_operation(model_cls, message, bus, routing_key): """ Creates a new operation based on the model_cls. :param model_cls: The model class to use. :type model_cls: class :param message: jsonrpc message structure. :type message: dict :param bus: Bus instance. :type bus: commissaire_http.bus.Bus :returns: A jsonrpc structure. :param routing_key: Routing key for the cluster operation request. :type routing_key: str :rtype: dict """ # Verify cluster exists first cluster_name = message['params']['name'] try: bus.storage.get_cluster(cluster_name) LOGGER.debug('Found cluster "%s"', cluster_name) except: error_msg = 'Cluster "{}" does not exist.'.format(cluster_name) LOGGER.debug(error_msg) return create_jsonrpc_error(message, error_msg, JSONRPC_ERRORS['NOT_FOUND']) try: model = model_cls.new( name=cluster_name, started_at=datetime.datetime.utcnow().isoformat()) model._validate() # XXX Assumes the only method argument is cluster_name. result = bus.request(routing_key, params=[cluster_name]) return create_jsonrpc_response(message['id'], result['result']) except models.ValidationError as error: LOGGER.info('Invalid data provided. "%s"', error) LOGGER.debug('Data="%s"', message['params']) return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INVALID_REQUEST']) except Exception as error: LOGGER.debug('Error creating %s: %s: %s', model_cls.__name__, type(error), error) return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INTERNAL_ERROR'])
def delete_host(message, bus): """ Deletes an existing host. :param message: jsonrpc message structure. :type message: dict :param bus: Bus instance. :type bus: commissaire_http.bus.Bus :returns: A jsonrpc structure. :rtype: dict """ try: address = message['params']['address'] LOGGER.debug('Attempting to delete host "{}"'.format(address)) bus.storage.delete(models.Host.new(address=address)) # TODO: kick off service job to remove the host? try: # Remove from a cluster container = bus.storage.list(models.Clusters) for cluster in container.clusters: if address in cluster.hostset: LOGGER.info('Removing host "{}" from cluster "{}"'.format( address, cluster.name)) cluster.hostset.pop(cluster.hostset.index(address)) bus.storage.save(cluster) # Remove from container manager (if applicable) if cluster.container_manager: params = [cluster.container_manager, address] bus.request('container.remove_node', params=params) # A host can only be part of one cluster so break the loop break except _bus.RemoteProcedureCallError as error: LOGGER.info('{} not part of a cluster.'.format(address)) return create_jsonrpc_response(message['id'], []) except _bus.RemoteProcedureCallError as error: return create_jsonrpc_error(message, error, JSONRPC_ERRORS['NOT_FOUND']) except Exception as error: LOGGER.debug('Error deleting host: {}: {}'.format(type(error), error)) return create_jsonrpc_error(message, error, JSONRPC_ERRORS['INTERNAL_ERROR'])
def create_network(message, bus): """ Creates a new network. :param message: jsonrpc message structure. :type message: dict :param bus: Bus instance. :type bus: commissaire_http.bus.Bus :returns: A jsonrpc structure. :rtype: dict """ try: name = message['params']['name'] LOGGER.debug('create_network params: {}'.format(message['params'])) # Check to see if we already have a network with that name input_network = models.Network.new(**message['params']) saved_network = bus.storage.get(input_network) LOGGER.debug( 'Creation of already exisiting network "{0}" requested.'.format( name)) # If they are the same thing then go ahead and return success if saved_network.to_dict() == input_network.to_dict(): return create_jsonrpc_response( message['id'], saved_network.to_dict_safe()) # Otherwise error with a CONFLICT return create_jsonrpc_error( message, 'A network with that name already exists.', JSONRPC_ERRORS['CONFLICT']) except _bus.StorageLookupError as error: LOGGER.info('Attempting to create new network: "{}"'.format( message['params'])) # Create the new network try: input_network = models.Network.new(**message['params']) saved_network = bus.storage.save(input_network) return create_jsonrpc_response( message['id'], saved_network.to_dict_safe()) except models.ValidationError as error: return create_jsonrpc_error( message, error, JSONRPC_ERRORS['INVALID_REQUEST'])