def edit(self, token, args): """ Add a distribution. @param token: A security token. @type token: string @param args: A dictionary of distribution properties. @type args: dict - id - kernel (optional) - initrd (optional) - kickstart (optional) - name (optional) - architecture (optional) - kernel_options (optional) - kickstart_metadata (optional) @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ('id',) optional = ('kernel', 'initrd', 'name', 'architecture', 'kickstart', 'kernel_options', 'kickstart_metadata') filter = ('id', 'options') self.validate(args, required) session = db.open_session() try: distribution = db.Distribution.get(session, args['id']) distribution.update(args, filter) session.save(distribution) session.flush() self.cobbler_sync(distribution.get_hash()) return success() finally: session.close()
def get_by_hostname(self, token, args): """ Get a machines by hostname. @param token: A security token. @type token: string @param args: A dictionary of machine attributes. - hostname - offset (optional) - limit (optional) @type args: dict @return A list of all machines. @rtype: [dict,] @raise SQLException: On database error """ required = ("hostname",) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] hostname = args["hostname"] offset, limit = self.offset_and_limit(args) query = session.query(db.Machine).offset(offset).limit(limit) for machine in query.select_by(hostname=hostname): result.append(self.expand(machine)) return codes.success(result) finally: session.close()
def get_by_name(self, token, args): """ Get all distributions. @param token: A security token. @type token: string @param args: A dictionary of distribution attributes. @type args: dict - name @return: A distribution by name. @rtype: dict - id - kernel - initrd - options (optional) - kickstart (optional) - name - architecture - kernel_options (optional) - kickstart_metadata (optional) @raise SQLException: On database error """ required = ('name',) FieldValidator(args).verify_required(required) session = db.open_session() try: name = args['name'] distribution = session.query(db.Distribution).selectfirst_by(name = name) if distribution is None: raise NoSuchObjectException(comment="objectid") return success(distribution.get_hash()) finally: session.close()
def add(self, token, args): """ Add a distribution. @param token: A security token. @type token: string @param args: A dictionary of distribution properties. @type args: dict - kernel - initrd - options (optional) - kickstart (optional) - name - architecture - kernel_options (optional) - kickstart_metadata (optional) @raise SQLException: On database error """ required = ('kernel', 'initrd', 'name', 'architecture') optional = ('options', 'kickstart', 'kernel_options', 'kickstart_metadata') self.validate(args, required) session = db.open_session() try: distribution = db.Distribution() distribution.update(args) session.save(distribution) session.flush() self.cobbler_sync(distribution.get_hash()) finally: session.close()
def list(self, token, args): """ Get all distributions. @param token: A security token. @type token: string @param args: A dictionary of distribution attributes. @type args: dict - offset (optional) - limit (optional) @return: A list of distributions. @rtype: [dict,] - id - kernel - initrd - options (optional) - kickstart (optional) - name - architecture - kernel_options (optional) - kickstart_metadata (optional) @raise SQLException: On database error """ session = db.open_session() try: result = [] offset, limit = self.offset_and_limit(args) for distribution in db.Distribution.list(session, offset, limit): result.append(distribution.get_hash()) return success(result) finally: session.close()
def get(self, token, args): """ Get all distributions. @param token: A security token. @type token: string @param args: A dictionary of distribution attributes. @type args: dict - id @return: A distribution by id. @rtype: dict - id - kernel - initrd - options (optional) - kickstart (optional) - name - architecture - kernel_options (optional) - kickstart_metadata (optional) @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ('id',) FieldValidator(args).verify_required(required) session = db.open_session() try: distribution = db.Distribution.get(session, args['id']) return success(distribution.get_hash()) finally: session.close()
def add(self, token, args): """ Create a user. @param token: A security token. @type token: string @param args: A dictionary of user attributes. @type args: dict - username - password - first - middle (optional) - last - description - email @raise SQLException: On database error """ required = ('username','password', 'first', 'last', 'email') optional = ('middle', 'description') FieldValidator(args).verify_required(required) session = db.open_session() self.__lock.acquire() try: user = db.User() user.update(args) session.save(user) session.flush() return success(user.id) finally: self.__lock.release() session.close()
def edit(self, token, args): """ Edit a user. @param token: A security token. @type token: string @param args: A dictionary of user attributes. @type args: dict. - id - username - password - first - middle (optional) - last - description - email @raise SQLException: On database error @raise NoSuchObjectException: On object not found. # TODO: password should be stored encrypted. """ required = ('id',) optional = ('username','password', 'first', 'middle', 'last', 'email', 'description') FieldValidator(args).verify_required(required) session = db.open_session() self.__lock.acquire() try: user = db.User.get(session, args['id']) user.update(args) session.save(user) session.flush() return success() finally: self.__lock.release() session.close()
def list(self, token, args): """ Get a list of all users. @param token: A security token. @type token: string @param args: A dictionary of user attributes. @type args: dict - offset (optional) - limit (optional) @return: A list of users. @rtype: [dict,] - id - username - first - middle (optional) - last - description (optional) - email @raise SQLException: On database error """ session = db.open_session() try: result = [] offset, limit = self.offset_and_limit(args) for user in db.User.list(session, offset, limit): result.append(user.get_hash()) return success(result) finally: session.close()
def add(self, token, args): """ Create a token. @param token: A security token. @type token: string @param args: A dictionary of token attributes. - token - profile_id (optional) - uses_remaining @type args: dict @raise SQLException: On database error """ required = [] optional = ['profile_id', 'uses_remaining', 'token'] validator = FieldValidator(args) validator.verify_required(required) validator.verify_int(['uses_remaining']) session = db.open_session() try: regtoken = db.RegToken() regtoken.update(args) session.save(regtoken) session.flush() return success(regtoken.id) finally: session.close()
def get(self, token, args): """ Get a regtoken by id. @param token: A security token. @type token: string @param args: A dictionary of token attributes. - id @type args: dict @return: A regtoken. @rtype: dict - id - token - profile_id (optional) - uses_remaining (optional) @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ('id',) FieldValidator(args).verify_required(required) session = db.open_session() try: rt = db.RegToken.get(session, args['id']) return success(self.expand(rt)) finally: session.close()
def get_by_regtoken(self, token, args): # FIXME: this code is currently non-operational in VF 0.0.3 and later # this code can be pruned if regtoken functional is needed and # reinstated. """ Get a machines by registration token. @param token: A security token. @type token: string @param args: A dictionary of machine attributes. - registration_token - offset (optional) - limit (optional) @type args: dict @return A list of machines. @rtype: [dict,] @raise SQLException: On database error """ required = ("registration_token",) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] regtoken = args["registration_token"] offset, limit = self.offset_and_limit(args) query = session.query(db.Machine).offset(offset).limit(limit) for machine in query.select_by(registration_token=regtoken): result.append(self.expand(machine)) return codes.success(result) finally: session.close()
def add(self, token, args): """ Create a task. @param token: A security token. @type token: string @param args: A dictionary of task attributes. - user_id - action_type - machine_id - deployment_id - state @type args: dict @raise SQLException: On database error """ optional = () required = ('user_id', 'action_type', 'machine_id', 'deployment_id', 'state') validator = FieldValidator(args) validator.verify_required(required) validator.verify_enum('state', VALID_TASK_STATES) validator.verify_enum('action_type', VALID_TASK_OPERATIONS) session = db.open_session() try: task = db.Task() task.update(args) session.save(task) session.flush() return success(task.id) finally: session.close()
def get_by_token(self, token, args): """ Get all regtokens by token. @param token: A security token. @type token: string @param args: A dictionary of token attributes. - token - offset (optional) - limit (optional) @type args: dict @return: A list of regtokens. @rtype: [dict,] - id - token - profile_id (optional) - uses_remaining (optional) @raise SQLException: On database error """ required = ('token',) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] offset, limit = self.offset_and_limit(args) query = session.query(db.RegToken).offset(offset).limit(limit) tknstr = args['token'] for rt in query.select_by(token=tknstr): result.append(self.expand(rt)) return success(result) finally: session.close()
def associate(self, token, deployment_id, machine_id, hostname, ip_addr, mac_addr, profile_id=None, architecture=None, processor_speed=None, processor_count=None, memory=None): """ Associate a machine with an ip/host/mac address """ self.logger.info("associating...") # THIS IS ALL OBSOLETE... # determine the profile from the token. # FIXME: inefficient. ideally we'd have a retoken.get_by_value() or equivalent #regtoken_obj = regtoken.RegToken() #if token is None: # self.logger.info("token is None???") #results = regtoken_obj.get_by_token(None, { "token" : token }) #self.logger.info("get_by_token") #self.logger.info("results: %s" % results) #if results.error_code != 0: # raise InvalidArgumentsException("bad token") # FIXME: check that at least some results are returned. session = db.open_session() deployment = db.Deployment().get(session, deployment_id) deployment.machine_id = machine_id deployment.hostname = hostname deployment.ip_address = ip_addr deployment.mac_address = mac_addr deployment.profile_id = profile_id session.save(deployment) session.flush() return success()
def edit(self, token, args): """ Edit a task. @param token: A security token. @type token: string @param args: A dictionary of task attributes. - id - state (optional) @type args: dict @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ('id',) optional = ('state',) filter_fields = ('id', 'user_id', 'action_type', 'machine_id', 'deployment_id') validator = FieldValidator(args) validator.verify_required(required) validator.verify_enum('state', VALID_TASK_STATES) validator.verify_enum('action_type', VALID_TASK_STATES) session = db.open_session() try: task = db.Task.get(session, args['id']) task.update(args, filter_fields) session.save(task) session.flush() return success() finally: session.close()
def get_by_regtoken(self, token, args): # FIXME: registration tokens are currently non-operational # for virt-factory 0.0.3 and later. This code can be pruned # if regtoken usage is not reinstated. """ Get deployments by registration token. @param token: A security token. @type token: string @param args: A dictionary of query attributes. - registration_token - offset (optional) - limit (optional) @type args: dict @return: A list of deployments. @rtype: [dict,] @raise SQLException: On database error """ required = ('registration_token',) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] regtoken = args['registration_token'] offset, limit = self.offset_and_limit(args) query = session.query(db.Deployment).offset(offset).limit(limit) for deployment in query.select_by(registration_token=regtoken): result.append(self.expand(deployment)) return success(result) finally: session.close()
def get(self, token, args): """ Get a deployment by id. @param token: A security token. @type token: string @param args: A dictionary of query attributes. - id @type args: dict @return: A deployment. @rtype: dict @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ('id',) FieldValidator(args).verify_required(required) session = db.open_session() try: deployment = db.Deployment.get(session, args['id']) results = self.expand(deployment) self.logger.info("your deployment is: %s" % results) # we want the state "now" so we must contact the node # to update it! self.refresh(token, results) # now re-get the updated record which will have an # accurate state. deployment = db.Deployment.get(session, args['id']) return success(self.expand(deployment)) finally: session.close()
def set_state(self, token, args): """ FIXME: authentication details TBA. requires: mac_address, state """ required = ('mac_address','state') FieldValidator(args).verify_required(required) which = self.get_by_mac_address(token,args) self.logger.debug("set_state on %s to %s" % (args["mac_address"],args["state"])) if which.error_code != 0: raise InvalidArgumentsException(comment="missing item") if len(which.data) == 0: raise InvalidArgumentsException(comment="missing item (no %s)" % args["mac_address"]) id = which.data[0]["id"] session = db.open_session() # BOOKMARK deployment = db.Deployment.get(session, id) results = self.expand(deployment) results["state"] = args["state"] # misnomer: this is the time of the last status update results["last_heartbeat"] = int(time.time()) self.logger.debug("setting deployment status: %s" % results) self.edit(token, results) return success(results)
def list(self, token, args): """ Get all regtokens. @param token: A security token. @type token: string @param args: A dictionary of query attributes. @type args: dict - offset (optional) - limit (optional) @return: A list of regtokens. @rtype: [dict,] - id - token - profile_id (optional) - uses_remaining (optional) @raise SQLException: On database error """ session = db.open_session() try: result = [] offset, limit = self.offset_and_limit(args) for rt in db.RegToken.list(session, offset, limit): result.append(self.expand(rt)) return success(result) finally: session.close()
def get(self, token, args): """ Get a task by id. @param token: A security token. @type token: string @param args: A dictionary of task attributes. - id @type args: dict - id - user_id - action_type - machine_id - deployment_id - state @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ('id',) FieldValidator(args).verify_required(required) session = db.open_session() try: task = db.Task.get(session, args['id']) return success(self.expand(task)) finally: session.close()
def list(self, token, args): """ Get a list of all deployments. @param token: A security token. @type token: string @param args: A dictionary of query arguments. @type args: dict - offset (optional) - limit (optional) @return: A list of deployments. @rtype: [dict,] @raise SQLException: On database error """ # it is going to be expensive to query all of the list results # for an update. so right now, we're doing it only for 1-item # gets, with the thought that nodes should be sending async # updates, or that we are periodically polling them for status session = db.open_session() try: result = [] offset, limit = self.offset_and_limit(args) for deployment in db.Deployment.list(session, offset, limit): result.append(self.expand(deployment)) return success(result) finally: session.close()
def add(self, token, args): """ Create a tag @param token: A security token. @type token: string @param args: A dictionary of tag attributes. @type args: dict - id - name - machine_ids - deployment_ids @raise SQLException: On database error """ required = ('name',) optional = ('machine_ids', 'deployment_ids') FieldValidator(args).verify_required(required) session = db.open_session() try: tag = db.Tag() tag.update(args) session.save(tag) if args.has_key('machine_ids'): setattr(tag, "machines", self.objects_from_ids(session, db.Machine, args['machine_ids'])) if args.has_key('deployment_ids'): setattr(tag, "deployments", self.objects_from_ids(session, db.Deployment, args['deployment_ids'])) session.flush() return success() finally: session.close()
def list(self, token, args, where_args=None): """ Get all tasks. @param token: A security token. @type token: string @param args: A dictionary of task attributes. @type args: dict - offset (optional) - limit (optional) - machine_id (optional) @return: A list of tasks. @rtype: [dict,] - id - user_id - action_type - machine_id - deployment_id - state @raise SQLException: On database error """ session = db.open_session() try: result = [] offset, limit = self.offset_and_limit(args) for task in db.Task.list(session, offset, limit, where_args=where_args): result.append(self.expand(task)) return success(result) finally: session.close()
def get_by_hostname(self, token, args): """ Get deployments by hostname. @param token: A security token. @type token: string @param args: A dictionary of query attributes. - hostname - offset (optional) - limit (optional) @type args: dict @return: A list of deployments. @rtype: [dict,] @raise SQLException: On database error """ required = ('hostname',) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] hostname = args['hostname'] offset, limit = self.offset_and_limit(args) query = session.query(db.Deployment).offset(offset).limit(limit) for deployment in query.select_by(hostname=hostname): result.append(self.expand(deployment)) return success(result) finally: session.close()
def delete(self, token, args): dargs = self.get(token, { "id" : args["id" ]}).data self.__set_locked(token, dargs, DEPLOYMENT_STATE_PENDING, True) self.__queue_operation(token, dargs, TASK_OPERATION_DELETE_VIRT) # FIXME/NOTE: there may be situations where the above operation fails in # which case it is the job of taskatron to ensure the virtual machine # perishes. It will be removed from the WUI immediately here. This # behavior can probably be improved somewhat but seems better than # having a non-existant undeletable entry stick around in the WUI. session = db.open_session() db.Deployment.delete(session, args['id']) return success() # FIXME: always?
def cleanup_old_sessions(self): """ Delete expired sessions. """ session = db.open_session() self.__lock.acquire() try: # cleanup expired sessions. mark = self.expired_mark() query = session.query(db.Session) for expired in query.select(db.Session.c.session_timestamp < mark): session.delete(expired) session.flush() finally: self.__lock.release() session.close()
def get_by_mac_address(self, token, args): """ """ required = ("mac_address",) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] mac_address = args["mac_address"] offset, limit = self.offset_and_limit(args) query = session.query(db.Machine).limit(limit).offset(offset) for machine in query.select_by(mac_address=mac_address): result.append(self.expand(machine)) return codes.success(result) finally: session.close()
def __set_locked(self, token, args, dep_state=DEPLOYMENT_STATE_PENDING, lock=True): session = db.open_session() try: deployment = db.Deployment.get(session, args['id']) deployment.state = dep_state if lock: deployment.is_locked = 1 else: deployment.is_locked = 0 session.save(deployment) session.flush() self.cobbler_sync(deployment.get_hash()) return success() finally: session.close() self.edit(token, args)
def get_by_deployment(self, token, args): """ Returns a list of tasks for a given guest """ required = ('deployment_id',) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] deployment_id = args['deployment_id'] offset, limit = self.offset_and_limit(args) query = session.query(db.Task).limit(limit).offset(offset) for task in query.select_by(deployment_id = deployment_id): result.append(self.expand(task)) return success(result) finally: session.close()
def test_should_use_correct_parameters_for_create_database_session( self, session_local, ): db = next(open_session()) db.close.assert_called_once()