def get_by_name_for_release(cls, release, name, fail_if_not_found=False, lock_for_update=False): """Get sequence by name. :param release: the release object :param name: the name of sequence :param fail_if_not_found: True means raising of exception in case if object is not found :param lock_for_update: True means acquiring exclusive access for object :return: deployment sequence object """ q = db().query(cls.model).filter_by(release_id=release.id, name=name) if lock_for_update: q = q.order_by('id') q = q.with_lockmode('update') res = q.first() if not res and fail_if_not_found: raise errors.ObjectNotFound( "Sequence with name='{0}' is not found for release {1}".format( name, release.id)) return res
def get_by_uuid(cls, uuid, fail_if_not_found=False, lock_for_update=False): # maybe consider using uuid as pk? q = db().query(cls.model).filter_by(uuid=uuid, deleted_at=None) if lock_for_update: q = q.order_by('id') q = q.with_lockmode('update') res = q.first() if not res and fail_if_not_found: raise errors.ObjectNotFound( "Task with UUID={0} is not found in DB".format(uuid)) return res
def get_by_uid_excluding_deleted(cls, uid, fail_if_not_found=False, lock_for_update=False): q = db().query(cls.model).filter_by(id=uid, deleted_at=None) if lock_for_update: q = q.order_by('id') q = q.with_lockmode('update') res = q.first() if not res and fail_if_not_found: raise errors.ObjectNotFound( "Task with ID='{0}' is not found in DB".format(uid)) return res
def get_by_uid(cls, uid, fail_if_not_found=False, lock_for_update=False): """Get instance by it's uid (PK in case of SQLAlchemy) :param uid: uid of object :param fail_if_not_found: raise an exception if object is not found :param lock_for_update: lock returned object for update (DB mutex) :returns: instance of an object (model) """ q = db().query(cls.model) if lock_for_update: q = q.with_lockmode('update') res = q.get(uid) if not res and fail_if_not_found: raise errors.ObjectNotFound( "Object '{0}' with UID={1} is not found in DB".format( cls.__name__, uid)) return res
def get_one(cls, fail_if_not_found=False, lock_for_update=False): """Get one instance from table. :param fail_if_not_found: raise an exception if object is not found :param lock_for_update: lock returned object for update (DB mutex) :return: instance of an object (model) """ q = db().query(cls.model) if lock_for_update: q = q.with_lockmode('update') res = q.first() if not res and fail_if_not_found: raise errors.ObjectNotFound( "Object '{0}' is not found in DB".format(cls.__name__)) return res
def get_by_uid(cls, uid, fail_if_not_found=False, lock_for_update=False): """Get instance by it's uid (PK in case of SQLAlchemy) :param uid: uid of object :param fail_if_not_found: raise an exception if object is not found :param lock_for_update: lock returned object for update (DB mutex) :returns: instance of an object (model) """ q = db().query(cls.model) if lock_for_update: # todo(ikutukov): replace to the with_for_update # http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy. # orm.query.Query.with_for_update q = q.with_lockmode('update') res = q.get(uid) if not res and fail_if_not_found: raise errors.ObjectNotFound( "Object '{0}' with UID={1} is not found in DB".format( cls.__name__, uid)) return res
def validate_node(cls, node_id): node = adapters.NailgunNodeAdapter.get_by_uid(node_id) if not node: raise errors.ObjectNotFound( "Node with id {0} was not found.".format(node_id), log_message=True) # node can go to error state while upgrade process allowed_statuses = (consts.NODE_STATUSES.ready, consts.NODE_STATUSES.provisioned, consts.NODE_STATUSES.error) if node.status not in allowed_statuses: raise errors.InvalidData("Node should be in one of statuses: {0}." " Currently node has {1} status.".format( allowed_statuses, node.status), log_message=True) if node.status == consts.NODE_STATUSES.error and\ node.error_type != consts.NODE_ERRORS.deploy: raise errors.InvalidData("Node should be in error state only with" "deploy error type. Currently error type" " of node is {0}".format(node.error_type), log_message=True) return node