2013-09-20 15:55:44 +00:00
|
|
|
import string
|
2013-10-02 16:43:45 +00:00
|
|
|
import logging
|
2014-02-04 00:08:37 +00:00
|
|
|
import uuid
|
2015-02-12 19:11:56 +00:00
|
|
|
import time
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
from random import SystemRandom
|
|
|
|
from datetime import datetime
|
2013-09-20 15:55:44 +00:00
|
|
|
from peewee import *
|
2014-07-02 23:10:24 +00:00
|
|
|
from data.read_slave import ReadSlaveModel
|
2014-04-10 22:30:09 +00:00
|
|
|
from sqlalchemy.engine.url import make_url
|
2015-02-12 19:11:56 +00:00
|
|
|
|
|
|
|
from data.read_slave import ReadSlaveModel
|
2014-09-11 19:45:41 +00:00
|
|
|
from util.names import urn_generator
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2014-10-14 17:58:08 +00:00
|
|
|
|
2013-10-02 16:43:45 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-04-07 20:59:22 +00:00
|
|
|
|
2014-04-09 23:11:33 +00:00
|
|
|
|
|
|
|
SCHEME_DRIVERS = {
|
2014-05-19 21:14:23 +00:00
|
|
|
'mysql': MySQLDatabase,
|
2014-05-22 16:13:41 +00:00
|
|
|
'mysql+pymysql': MySQLDatabase,
|
2014-04-09 23:11:33 +00:00
|
|
|
'sqlite': SqliteDatabase,
|
2014-08-21 23:21:20 +00:00
|
|
|
'postgresql': PostgresqlDatabase,
|
|
|
|
'postgresql+psycopg2': PostgresqlDatabase,
|
2014-04-07 20:59:22 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 19:58:56 +00:00
|
|
|
SCHEME_RANDOM_FUNCTION = {
|
|
|
|
'mysql': fn.Rand,
|
|
|
|
'mysql+pymysql': fn.Rand,
|
|
|
|
'sqlite': fn.Random,
|
|
|
|
'postgresql': fn.Random,
|
2014-11-11 22:22:37 +00:00
|
|
|
'postgresql+psycopg2': fn.Random,
|
2014-09-15 19:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:32:13 +00:00
|
|
|
def real_for_update(query):
|
|
|
|
return query.for_update()
|
|
|
|
|
|
|
|
def null_for_update(query):
|
|
|
|
return query
|
|
|
|
|
|
|
|
SCHEME_SPECIALIZED_FOR_UPDATE = {
|
|
|
|
'sqlite': null_for_update,
|
|
|
|
}
|
|
|
|
|
2014-09-15 19:58:56 +00:00
|
|
|
class CallableProxy(Proxy):
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
if self.obj is None:
|
|
|
|
raise AttributeError('Cannot use uninitialized Proxy.')
|
|
|
|
return self.obj(*args, **kwargs)
|
|
|
|
|
2014-11-06 23:00:52 +00:00
|
|
|
|
|
|
|
class CloseForLongOperation(object):
|
|
|
|
""" Helper object which disconnects the database then reconnects after the nested operation
|
|
|
|
completes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, config_object):
|
|
|
|
self.config_object = config_object
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
close_db_filter(None)
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
2014-11-10 18:44:36 +00:00
|
|
|
# Note: Nothing to do. The next SQL call will reconnect automatically.
|
|
|
|
pass
|
2014-11-06 23:00:52 +00:00
|
|
|
|
|
|
|
|
2014-11-06 22:50:48 +00:00
|
|
|
class UseThenDisconnect(object):
|
|
|
|
""" Helper object for conducting work with a database and then tearing it down. """
|
|
|
|
|
|
|
|
def __init__(self, config_object):
|
|
|
|
self.config_object = config_object
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
configure(self.config_object)
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
close_db_filter(None)
|
|
|
|
|
|
|
|
|
2014-05-13 16:17:26 +00:00
|
|
|
db = Proxy()
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slave = Proxy()
|
2014-09-15 19:58:56 +00:00
|
|
|
db_random_func = CallableProxy()
|
2015-01-30 21:32:13 +00:00
|
|
|
db_for_update = CallableProxy()
|
2014-09-15 19:58:56 +00:00
|
|
|
|
2014-04-09 23:11:33 +00:00
|
|
|
|
2015-01-20 19:46:22 +00:00
|
|
|
def validate_database_url(url, connect_timeout=5):
|
|
|
|
driver = _db_from_url(url, {
|
|
|
|
'connect_timeout': connect_timeout
|
|
|
|
})
|
|
|
|
driver.connect()
|
|
|
|
driver.close()
|
2014-09-15 19:58:56 +00:00
|
|
|
|
2014-04-09 23:11:33 +00:00
|
|
|
|
2014-07-02 23:10:24 +00:00
|
|
|
def _db_from_url(url, db_kwargs):
|
|
|
|
parsed_url = make_url(url)
|
2014-04-10 22:30:09 +00:00
|
|
|
|
|
|
|
if parsed_url.host:
|
|
|
|
db_kwargs['host'] = parsed_url.host
|
|
|
|
if parsed_url.port:
|
|
|
|
db_kwargs['port'] = parsed_url.port
|
|
|
|
if parsed_url.username:
|
|
|
|
db_kwargs['user'] = parsed_url.username
|
|
|
|
if parsed_url.password:
|
2014-08-21 23:21:20 +00:00
|
|
|
db_kwargs['password'] = parsed_url.password
|
2014-04-10 22:30:09 +00:00
|
|
|
|
2015-01-20 19:46:22 +00:00
|
|
|
# Note: sqlite does not support connect_timeout.
|
|
|
|
if parsed_url.drivername == 'sqlite' and 'connect_timeout' in db_kwargs:
|
|
|
|
del db_kwargs['connect_timeout']
|
|
|
|
|
2014-07-02 23:10:24 +00:00
|
|
|
return SCHEME_DRIVERS[parsed_url.drivername](parsed_url.database, **db_kwargs)
|
|
|
|
|
2014-09-15 19:58:56 +00:00
|
|
|
|
2014-07-02 23:10:24 +00:00
|
|
|
def configure(config_object):
|
2014-11-06 23:00:52 +00:00
|
|
|
logger.debug('Configuring database')
|
2014-07-02 23:10:24 +00:00
|
|
|
db_kwargs = dict(config_object['DB_CONNECTION_ARGS'])
|
|
|
|
write_db_uri = config_object['DB_URI']
|
|
|
|
db.initialize(_db_from_url(write_db_uri, db_kwargs))
|
|
|
|
|
2014-09-15 19:58:56 +00:00
|
|
|
parsed_write_uri = make_url(write_db_uri)
|
|
|
|
db_random_func.initialize(SCHEME_RANDOM_FUNCTION[parsed_write_uri.drivername])
|
2015-01-30 21:32:13 +00:00
|
|
|
db_for_update.initialize(SCHEME_SPECIALIZED_FOR_UPDATE.get(parsed_write_uri.drivername,
|
|
|
|
real_for_update))
|
2014-09-15 19:58:56 +00:00
|
|
|
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slave_uri = config_object.get('DB_READ_SLAVE_URI', None)
|
|
|
|
if read_slave_uri is not None:
|
|
|
|
read_slave.initialize(_db_from_url(read_slave_uri, db_kwargs))
|
2014-04-09 23:11:33 +00:00
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-11-20 21:13:03 +00:00
|
|
|
def random_string_generator(length=16):
|
|
|
|
def random_string():
|
|
|
|
random = SystemRandom()
|
|
|
|
return ''.join([random.choice(string.ascii_uppercase + string.digits)
|
|
|
|
for _ in range(length)])
|
|
|
|
return random_string
|
|
|
|
|
|
|
|
|
2014-02-04 00:08:37 +00:00
|
|
|
def uuid_generator():
|
|
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
2015-03-21 03:21:20 +00:00
|
|
|
get_epoch_timestamp = lambda: int(time.time())
|
2015-02-12 19:11:56 +00:00
|
|
|
|
|
|
|
|
2014-10-14 17:58:08 +00:00
|
|
|
def close_db_filter(_):
|
|
|
|
if not db.is_closed():
|
|
|
|
logger.debug('Disconnecting from database.')
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
if read_slave.obj is not None and not read_slave.is_closed():
|
|
|
|
logger.debug('Disconnecting from read slave.')
|
|
|
|
read_slave.close()
|
|
|
|
|
|
|
|
|
2014-11-07 17:05:21 +00:00
|
|
|
class QuayUserField(ForeignKeyField):
|
2015-01-14 17:56:06 +00:00
|
|
|
def __init__(self, allows_robots=False, robot_null_delete=False, *args, **kwargs):
|
2014-11-07 17:05:21 +00:00
|
|
|
self.allows_robots = allows_robots
|
2015-01-14 17:56:06 +00:00
|
|
|
self.robot_null_delete = robot_null_delete
|
2014-11-07 17:05:21 +00:00
|
|
|
if not 'rel_model' in kwargs:
|
|
|
|
kwargs['rel_model'] = User
|
|
|
|
|
|
|
|
super(QuayUserField, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2014-07-02 23:10:24 +00:00
|
|
|
class BaseModel(ReadSlaveModel):
|
2013-09-20 15:55:44 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2015-04-10 19:27:37 +00:00
|
|
|
def __getattribute__(self, name):
|
|
|
|
""" Adds _id accessors so that foreign key field IDs can be looked up without making
|
|
|
|
a database roundtrip.
|
|
|
|
"""
|
|
|
|
if name.endswith('_id'):
|
|
|
|
field_name = name[0:len(name) - 3]
|
|
|
|
if field_name in self._meta.fields:
|
|
|
|
return self._data.get(field_name)
|
|
|
|
|
|
|
|
return super(BaseModel, self).__getattribute__(name)
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
class User(BaseModel):
|
2014-11-19 20:32:30 +00:00
|
|
|
uuid = CharField(default=uuid_generator, max_length=36, null=True)
|
2013-09-28 03:33:59 +00:00
|
|
|
username = CharField(unique=True, index=True)
|
2013-10-10 03:00:34 +00:00
|
|
|
password_hash = CharField(null=True)
|
2013-11-20 21:13:03 +00:00
|
|
|
email = CharField(unique=True, index=True,
|
|
|
|
default=random_string_generator(length=64))
|
2013-09-27 23:55:04 +00:00
|
|
|
verified = BooleanField(default=False)
|
2013-10-02 04:48:03 +00:00
|
|
|
stripe_id = CharField(index=True, null=True)
|
2013-10-31 20:46:04 +00:00
|
|
|
organization = BooleanField(default=False, index=True)
|
2013-11-20 21:13:03 +00:00
|
|
|
robot = BooleanField(default=False, index=True)
|
2013-11-15 19:42:31 +00:00
|
|
|
invoice_email = BooleanField(default=False)
|
2014-09-02 19:27:05 +00:00
|
|
|
invalid_login_attempts = IntegerField(default=0)
|
|
|
|
last_invalid_login = DateTimeField(default=datetime.utcnow)
|
2015-02-12 19:11:56 +00:00
|
|
|
removed_tag_expiration_s = IntegerField(default=1209600) # Two weeks
|
2013-10-31 20:46:04 +00:00
|
|
|
|
2014-11-10 18:18:17 +00:00
|
|
|
def delete_instance(self, recursive=False, delete_nullable=False):
|
|
|
|
# If we are deleting a robot account, only execute the subset of queries necessary.
|
|
|
|
if self.robot:
|
|
|
|
# For all the model dependencies, only delete those that allow robots.
|
|
|
|
for query, fk in self.dependencies(search_nullable=True):
|
|
|
|
if isinstance(fk, QuayUserField) and fk.allows_robots:
|
|
|
|
model = fk.model_class
|
2015-01-14 17:56:06 +00:00
|
|
|
|
|
|
|
if fk.robot_null_delete:
|
|
|
|
model.update(**{fk.name: None}).where(query).execute()
|
|
|
|
else:
|
|
|
|
model.delete().where(query).execute()
|
2014-11-10 18:18:17 +00:00
|
|
|
|
|
|
|
# Delete the instance itself.
|
|
|
|
super(User, self).delete_instance(recursive=False, delete_nullable=False)
|
|
|
|
else:
|
|
|
|
super(User, self).delete_instance(recursive=recursive, delete_nullable=delete_nullable)
|
2013-10-31 20:46:04 +00:00
|
|
|
|
2013-11-04 20:42:08 +00:00
|
|
|
class TeamRole(BaseModel):
|
|
|
|
name = CharField(index=True)
|
|
|
|
|
|
|
|
|
2013-10-31 20:46:04 +00:00
|
|
|
class Team(BaseModel):
|
2013-11-01 23:34:17 +00:00
|
|
|
name = CharField(index=True)
|
2014-11-07 17:05:21 +00:00
|
|
|
organization = QuayUserField(index=True)
|
2013-11-04 20:42:08 +00:00
|
|
|
role = ForeignKeyField(TeamRole)
|
2013-11-04 21:57:20 +00:00
|
|
|
description = TextField(default='')
|
2013-10-31 20:46:04 +00:00
|
|
|
|
2013-11-01 23:34:17 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2013-11-01 23:34:17 +00:00
|
|
|
indexes = (
|
|
|
|
# A team name must be unique within an organization
|
|
|
|
(('name', 'organization'), True),
|
|
|
|
)
|
|
|
|
|
2013-10-31 20:46:04 +00:00
|
|
|
|
|
|
|
class TeamMember(BaseModel):
|
2014-11-07 17:05:21 +00:00
|
|
|
user = QuayUserField(allows_robots=True, index=True)
|
2013-10-31 20:46:04 +00:00
|
|
|
team = ForeignKeyField(Team, index=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2013-10-31 20:46:04 +00:00
|
|
|
indexes = (
|
|
|
|
# A user may belong to a team only once
|
|
|
|
(('user', 'team'), True),
|
|
|
|
)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
2014-08-15 21:47:43 +00:00
|
|
|
class TeamMemberInvite(BaseModel):
|
|
|
|
# Note: Either user OR email will be filled in, but not both.
|
2014-11-07 17:05:21 +00:00
|
|
|
user = QuayUserField(index=True, null=True)
|
2014-08-15 21:47:43 +00:00
|
|
|
email = CharField(null=True)
|
|
|
|
team = ForeignKeyField(Team, index=True)
|
2014-08-18 21:24:00 +00:00
|
|
|
inviter = ForeignKeyField(User, related_name='inviter')
|
2014-09-11 19:45:41 +00:00
|
|
|
invite_token = CharField(default=urn_generator(['teaminvite']))
|
2014-08-15 21:47:43 +00:00
|
|
|
|
|
|
|
|
2013-10-10 03:00:34 +00:00
|
|
|
class LoginService(BaseModel):
|
|
|
|
name = CharField(unique=True, index=True)
|
|
|
|
|
|
|
|
|
|
|
|
class FederatedLogin(BaseModel):
|
2014-11-07 17:05:21 +00:00
|
|
|
user = QuayUserField(allows_robots=True, index=True)
|
2013-10-10 03:00:34 +00:00
|
|
|
service = ForeignKeyField(LoginService, index=True)
|
|
|
|
service_ident = CharField()
|
2014-11-11 22:22:37 +00:00
|
|
|
metadata_json = TextField(default='{}')
|
2013-10-10 03:00:34 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2013-10-10 03:00:34 +00:00
|
|
|
indexes = (
|
|
|
|
# create a unique index on service and the local service id
|
|
|
|
(('service', 'service_ident'), True),
|
|
|
|
|
|
|
|
# a user may only have one federated login per service
|
|
|
|
(('service', 'user'), True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
class Visibility(BaseModel):
|
2014-05-29 15:24:10 +00:00
|
|
|
name = CharField(index=True, unique=True)
|
2013-09-20 22:38:17 +00:00
|
|
|
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
class Repository(BaseModel):
|
2014-11-07 17:05:21 +00:00
|
|
|
namespace_user = QuayUserField(null=True)
|
2013-09-20 15:55:44 +00:00
|
|
|
name = CharField()
|
2013-09-20 22:38:17 +00:00
|
|
|
visibility = ForeignKeyField(Visibility)
|
2013-10-01 03:22:52 +00:00
|
|
|
description = TextField(null=True)
|
2014-02-28 21:23:36 +00:00
|
|
|
badge_token = CharField(default=uuid_generator)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2013-09-20 15:55:44 +00:00
|
|
|
indexes = (
|
|
|
|
# create a unique index on namespace and name
|
2014-09-24 22:01:35 +00:00
|
|
|
(('namespace_user', 'name'), True),
|
2013-09-20 15:55:44 +00:00
|
|
|
)
|
|
|
|
|
2014-11-11 04:05:20 +00:00
|
|
|
def delete_instance(self, recursive=False, delete_nullable=False):
|
|
|
|
# Note: peewee generates extra nested deletion statements here that are slow and unnecessary.
|
|
|
|
# Therefore, we define our own deletion order here and use the dependency system to verify it.
|
|
|
|
ordered_dependencies = [RepositoryAuthorizedEmail, RepositoryTag, Image, LogEntry,
|
|
|
|
RepositoryBuild, RepositoryBuildTrigger, RepositoryNotification,
|
2015-04-13 17:31:07 +00:00
|
|
|
RepositoryPermission, AccessToken, Star, RepositoryActionCount]
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
for query, fk in self.dependencies(search_nullable=True):
|
|
|
|
model = fk.model_class
|
|
|
|
if not model in ordered_dependencies:
|
|
|
|
raise Exception('Missing repository deletion dependency: %s', model)
|
|
|
|
|
|
|
|
for model in ordered_dependencies:
|
|
|
|
model.delete().where(model.repository == self).execute()
|
|
|
|
|
|
|
|
# Delete the repository itself.
|
|
|
|
super(Repository, self).delete_instance(recursive=False, delete_nullable=False)
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2014-11-19 19:50:56 +00:00
|
|
|
class Star(BaseModel):
|
2015-02-24 22:50:54 +00:00
|
|
|
user = ForeignKeyField(User, index=True)
|
|
|
|
repository = ForeignKeyField(Repository, index=True)
|
2014-11-19 19:50:56 +00:00
|
|
|
created = DateTimeField(default=datetime.now)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
read_slaves = (read_slave,)
|
|
|
|
indexes = (
|
|
|
|
# create a unique index on user and repository
|
|
|
|
(('user', 'repository'), True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
class Role(BaseModel):
|
2014-05-29 15:24:10 +00:00
|
|
|
name = CharField(index=True, unique=True)
|
2013-09-20 22:38:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RepositoryPermission(BaseModel):
|
2013-11-01 23:34:17 +00:00
|
|
|
team = ForeignKeyField(Team, index=True, null=True)
|
2014-11-07 17:05:21 +00:00
|
|
|
user = QuayUserField(allows_robots=True, index=True, null=True)
|
2013-09-28 03:33:59 +00:00
|
|
|
repository = ForeignKeyField(Repository, index=True)
|
2013-09-20 22:38:17 +00:00
|
|
|
role = ForeignKeyField(Role)
|
|
|
|
|
2013-09-28 03:33:59 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2013-09-28 03:33:59 +00:00
|
|
|
indexes = (
|
2013-11-01 23:34:17 +00:00
|
|
|
(('team', 'repository'), True),
|
2013-09-28 03:33:59 +00:00
|
|
|
(('user', 'repository'), True),
|
|
|
|
)
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
|
2014-01-17 22:28:21 +00:00
|
|
|
class PermissionPrototype(BaseModel):
|
2014-11-07 17:05:21 +00:00
|
|
|
org = QuayUserField(index=True, related_name='orgpermissionproto')
|
2014-02-04 00:08:37 +00:00
|
|
|
uuid = CharField(default=uuid_generator)
|
2014-11-07 17:05:21 +00:00
|
|
|
activating_user = QuayUserField(allows_robots=True, index=True, null=True,
|
|
|
|
related_name='userpermissionproto')
|
|
|
|
delegate_user = QuayUserField(allows_robots=True,related_name='receivingpermission',
|
|
|
|
null=True)
|
2014-01-21 00:05:26 +00:00
|
|
|
delegate_team = ForeignKeyField(Team, related_name='receivingpermission',
|
|
|
|
null=True)
|
2014-01-17 22:28:21 +00:00
|
|
|
role = ForeignKeyField(Role)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2014-01-17 22:28:21 +00:00
|
|
|
indexes = (
|
2014-01-21 00:05:26 +00:00
|
|
|
(('org', 'activating_user'), False),
|
2014-01-17 22:28:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-02-17 17:35:16 +00:00
|
|
|
class AccessTokenKind(BaseModel):
|
|
|
|
name = CharField(unique=True, index=True)
|
|
|
|
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
class AccessToken(BaseModel):
|
2013-10-16 18:24:10 +00:00
|
|
|
friendly_name = CharField(null=True)
|
|
|
|
code = CharField(default=random_string_generator(length=64), unique=True,
|
|
|
|
index=True)
|
2013-09-20 15:55:44 +00:00
|
|
|
repository = ForeignKeyField(Repository)
|
|
|
|
created = DateTimeField(default=datetime.now)
|
2013-10-16 18:24:10 +00:00
|
|
|
role = ForeignKeyField(Role)
|
|
|
|
temporary = BooleanField(default=True)
|
2015-02-17 17:35:16 +00:00
|
|
|
kind = ForeignKeyField(AccessTokenKind, null=True)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
2014-02-21 22:09:56 +00:00
|
|
|
class BuildTriggerService(BaseModel):
|
2014-05-29 15:24:10 +00:00
|
|
|
name = CharField(index=True, unique=True)
|
2014-02-21 22:09:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RepositoryBuildTrigger(BaseModel):
|
|
|
|
uuid = CharField(default=uuid_generator)
|
|
|
|
service = ForeignKeyField(BuildTriggerService, index=True)
|
|
|
|
repository = ForeignKeyField(Repository, index=True)
|
2014-11-07 17:05:21 +00:00
|
|
|
connected_user = QuayUserField()
|
2015-03-27 15:20:30 +00:00
|
|
|
auth_token = CharField(null=True)
|
2015-03-19 18:30:25 +00:00
|
|
|
private_key = TextField(null=True)
|
2014-02-21 22:09:56 +00:00
|
|
|
config = TextField(default='{}')
|
|
|
|
write_token = ForeignKeyField(AccessToken, null=True)
|
2014-11-07 17:05:21 +00:00
|
|
|
pull_robot = QuayUserField(allows_robots=True, null=True, related_name='triggerpullrobot')
|
2014-02-21 22:09:56 +00:00
|
|
|
|
|
|
|
|
2013-09-27 23:29:01 +00:00
|
|
|
class EmailConfirmation(BaseModel):
|
2013-09-28 03:33:59 +00:00
|
|
|
code = CharField(default=random_string_generator(), unique=True, index=True)
|
2014-11-07 17:05:21 +00:00
|
|
|
user = QuayUserField()
|
2013-09-27 23:29:01 +00:00
|
|
|
pw_reset = BooleanField(default=False)
|
2014-01-17 22:09:31 +00:00
|
|
|
new_email = CharField(null=True)
|
2013-09-27 23:29:01 +00:00
|
|
|
email_confirm = BooleanField(default=False)
|
|
|
|
created = DateTimeField(default=datetime.now)
|
|
|
|
|
|
|
|
|
2014-02-16 22:38:47 +00:00
|
|
|
class ImageStorage(BaseModel):
|
2014-10-07 20:03:57 +00:00
|
|
|
uuid = CharField(default=uuid_generator, index=True, unique=True)
|
2014-02-16 22:38:47 +00:00
|
|
|
checksum = CharField(null=True)
|
|
|
|
created = DateTimeField(null=True)
|
|
|
|
comment = TextField(null=True)
|
|
|
|
command = TextField(null=True)
|
2014-05-02 20:59:46 +00:00
|
|
|
image_size = BigIntegerField(null=True)
|
2014-09-16 04:18:57 +00:00
|
|
|
uncompressed_size = BigIntegerField(null=True)
|
2015-03-10 05:03:39 +00:00
|
|
|
aggregate_size = BigIntegerField(null=True)
|
2014-05-02 20:59:46 +00:00
|
|
|
uploading = BooleanField(default=True, null=True)
|
2014-02-16 22:38:47 +00:00
|
|
|
|
|
|
|
|
2014-09-18 21:26:40 +00:00
|
|
|
class ImageStorageTransformation(BaseModel):
|
|
|
|
name = CharField(index=True, unique=True)
|
|
|
|
|
|
|
|
|
2015-02-04 20:29:24 +00:00
|
|
|
class ImageStorageSignatureKind(BaseModel):
|
|
|
|
name = CharField(index=True, unique=True)
|
|
|
|
|
|
|
|
|
|
|
|
class ImageStorageSignature(BaseModel):
|
|
|
|
storage = ForeignKeyField(ImageStorage, index=True)
|
|
|
|
kind = ForeignKeyField(ImageStorageSignatureKind)
|
|
|
|
signature = TextField(null=True)
|
|
|
|
uploading = BooleanField(default=True, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
read_slaves = (read_slave,)
|
|
|
|
indexes = (
|
|
|
|
(('kind', 'storage'), True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-18 21:26:40 +00:00
|
|
|
class DerivedImageStorage(BaseModel):
|
|
|
|
source = ForeignKeyField(ImageStorage, null=True, related_name='source')
|
|
|
|
derivative = ForeignKeyField(ImageStorage, related_name='derivative')
|
|
|
|
transformation = ForeignKeyField(ImageStorageTransformation)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
read_slaves = (read_slave,)
|
|
|
|
indexes = (
|
|
|
|
(('source', 'transformation'), True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-06-17 20:03:43 +00:00
|
|
|
class ImageStorageLocation(BaseModel):
|
|
|
|
name = CharField(unique=True, index=True)
|
|
|
|
|
|
|
|
|
|
|
|
class ImageStoragePlacement(BaseModel):
|
|
|
|
storage = ForeignKeyField(ImageStorage)
|
|
|
|
location = ForeignKeyField(ImageStorageLocation)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2014-06-17 20:03:43 +00:00
|
|
|
indexes = (
|
|
|
|
# An image can only be placed in the same place once
|
|
|
|
(('storage', 'location'), True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
class Image(BaseModel):
|
2013-09-26 19:58:11 +00:00
|
|
|
# This class is intentionally denormalized. Even though images are supposed
|
|
|
|
# to be globally unique we can't treat them as such for permissions and
|
|
|
|
# security reasons. So rather than Repository <-> Image being many to many
|
|
|
|
# each image now belongs to exactly one repository.
|
2014-11-13 17:51:50 +00:00
|
|
|
docker_image_id = CharField(index=True)
|
2013-09-25 20:46:28 +00:00
|
|
|
repository = ForeignKeyField(Repository)
|
|
|
|
|
2013-09-30 19:30:00 +00:00
|
|
|
# '/' separated list of ancestory ids, e.g. /1/2/6/7/10/
|
2014-02-14 21:06:30 +00:00
|
|
|
ancestors = CharField(index=True, default='/', max_length=64535, null=True)
|
|
|
|
|
|
|
|
storage = ForeignKeyField(ImageStorage, index=True, null=True)
|
2013-09-30 19:30:00 +00:00
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2013-09-20 15:55:44 +00:00
|
|
|
indexes = (
|
|
|
|
# we don't really want duplicates
|
2014-06-11 19:37:45 +00:00
|
|
|
(('repository', 'docker_image_id'), True),
|
2013-09-20 15:55:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-09-26 19:58:11 +00:00
|
|
|
class RepositoryTag(BaseModel):
|
|
|
|
name = CharField()
|
|
|
|
image = ForeignKeyField(Image)
|
|
|
|
repository = ForeignKeyField(Repository)
|
2015-03-21 03:21:20 +00:00
|
|
|
lifetime_start_ts = IntegerField(default=get_epoch_timestamp)
|
2015-02-12 19:11:56 +00:00
|
|
|
lifetime_end_ts = IntegerField(null=True, index=True)
|
2015-02-18 21:37:38 +00:00
|
|
|
hidden = BooleanField(default=False)
|
2015-04-16 21:18:00 +00:00
|
|
|
reversion = BooleanField(default=False)
|
2013-09-26 19:58:11 +00:00
|
|
|
|
2013-09-28 03:33:59 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
2014-07-02 23:10:24 +00:00
|
|
|
read_slaves = (read_slave,)
|
2013-09-28 03:33:59 +00:00
|
|
|
indexes = (
|
2015-02-11 16:54:30 +00:00
|
|
|
(('repository', 'name'), False),
|
2015-03-21 03:21:20 +00:00
|
|
|
|
|
|
|
# This unique index prevents deadlocks when concurrently moving and deleting tags
|
|
|
|
(('repository', 'name', 'lifetime_end_ts'), True),
|
2013-09-28 03:33:59 +00:00
|
|
|
)
|
|
|
|
|
2013-09-26 19:58:11 +00:00
|
|
|
|
2014-09-08 20:43:17 +00:00
|
|
|
class BUILD_PHASE(object):
|
|
|
|
""" Build phases enum """
|
|
|
|
ERROR = 'error'
|
2014-11-21 19:27:06 +00:00
|
|
|
INTERNAL_ERROR = 'internalerror'
|
2015-05-14 19:37:16 +00:00
|
|
|
BUILD_SCHEDULED = 'build-scheduled'
|
2014-09-08 20:43:17 +00:00
|
|
|
UNPACKING = 'unpacking'
|
|
|
|
PULLING = 'pulling'
|
|
|
|
BUILDING = 'building'
|
|
|
|
PUSHING = 'pushing'
|
2015-02-13 20:54:01 +00:00
|
|
|
WAITING = 'waiting'
|
2014-09-08 20:43:17 +00:00
|
|
|
COMPLETE = 'complete'
|
|
|
|
|
|
|
|
|
2015-02-12 21:19:44 +00:00
|
|
|
class QueueItem(BaseModel):
|
|
|
|
queue_name = CharField(index=True, max_length=1024)
|
|
|
|
body = TextField()
|
|
|
|
available_after = DateTimeField(default=datetime.utcnow, index=True)
|
|
|
|
available = BooleanField(default=True, index=True)
|
|
|
|
processing_expires = DateTimeField(null=True, index=True)
|
|
|
|
retries_remaining = IntegerField(default=5)
|
|
|
|
|
|
|
|
|
2013-10-24 20:37:03 +00:00
|
|
|
class RepositoryBuild(BaseModel):
|
2014-02-04 00:08:37 +00:00
|
|
|
uuid = CharField(default=uuid_generator, index=True)
|
|
|
|
repository = ForeignKeyField(Repository, index=True)
|
2013-10-25 22:17:43 +00:00
|
|
|
access_token = ForeignKeyField(AccessToken)
|
2015-03-23 19:40:43 +00:00
|
|
|
resource_key = CharField(index=True, null=True)
|
2014-02-24 21:11:23 +00:00
|
|
|
job_config = TextField()
|
2015-02-13 20:54:01 +00:00
|
|
|
phase = CharField(default=BUILD_PHASE.WAITING)
|
2014-02-11 17:25:38 +00:00
|
|
|
started = DateTimeField(default=datetime.now)
|
2014-02-12 18:52:12 +00:00
|
|
|
display_name = CharField()
|
2014-02-19 21:08:33 +00:00
|
|
|
trigger = ForeignKeyField(RepositoryBuildTrigger, null=True, index=True)
|
2014-11-07 17:05:21 +00:00
|
|
|
pull_robot = QuayUserField(null=True, related_name='buildpullrobot')
|
2014-09-08 20:43:17 +00:00
|
|
|
logs_archived = BooleanField(default=False)
|
2015-02-23 18:38:01 +00:00
|
|
|
queue_id = CharField(null=True, index=True)
|
2013-10-17 22:25:19 +00:00
|
|
|
|
|
|
|
|
2013-11-27 07:29:31 +00:00
|
|
|
class LogEntryKind(BaseModel):
|
2014-05-29 15:24:10 +00:00
|
|
|
name = CharField(index=True, unique=True)
|
2013-11-27 07:29:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LogEntry(BaseModel):
|
|
|
|
kind = ForeignKeyField(LogEntryKind, index=True)
|
2014-11-07 17:05:21 +00:00
|
|
|
account = QuayUserField(index=True, related_name='account')
|
|
|
|
performer = QuayUserField(allows_robots=True, index=True, null=True,
|
2015-01-14 17:56:06 +00:00
|
|
|
related_name='performer', robot_null_delete=True)
|
2013-11-27 07:29:31 +00:00
|
|
|
repository = ForeignKeyField(Repository, index=True, null=True)
|
|
|
|
datetime = DateTimeField(default=datetime.now, index=True)
|
|
|
|
ip = CharField(null=True)
|
|
|
|
metadata_json = TextField(default='{}')
|
|
|
|
|
2015-04-30 19:56:40 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
read_slaves = (read_slave,)
|
|
|
|
indexes = (
|
|
|
|
# create an index on repository and date
|
|
|
|
(('repository', 'datetime'), False),
|
|
|
|
)
|
|
|
|
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2015-04-13 17:31:07 +00:00
|
|
|
class RepositoryActionCount(BaseModel):
|
|
|
|
repository = ForeignKeyField(Repository, index=True)
|
|
|
|
count = IntegerField()
|
|
|
|
date = DateField(index=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
read_slaves = (read_slave,)
|
|
|
|
indexes = (
|
|
|
|
# create a unique index on repository and date
|
|
|
|
(('repository', 'date'), True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-03-12 16:37:06 +00:00
|
|
|
class OAuthApplication(BaseModel):
|
|
|
|
client_id = CharField(index=True, default=random_string_generator(length=20))
|
|
|
|
client_secret = CharField(default=random_string_generator(length=40))
|
|
|
|
redirect_uri = CharField()
|
2014-03-14 22:57:28 +00:00
|
|
|
application_uri = CharField()
|
2014-11-07 17:05:21 +00:00
|
|
|
organization = QuayUserField()
|
2014-03-20 19:46:13 +00:00
|
|
|
|
2014-03-14 22:57:28 +00:00
|
|
|
name = CharField()
|
|
|
|
description = TextField(default='')
|
2014-11-25 00:25:13 +00:00
|
|
|
avatar_email = CharField(null=True, db_column='gravatar_email')
|
2014-03-12 16:37:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OAuthAuthorizationCode(BaseModel):
|
|
|
|
application = ForeignKeyField(OAuthApplication)
|
|
|
|
code = CharField(index=True)
|
|
|
|
scope = CharField()
|
2014-03-25 20:06:34 +00:00
|
|
|
data = TextField() # Context for the code, such as the user
|
2014-03-12 16:37:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OAuthAccessToken(BaseModel):
|
2014-03-25 00:57:02 +00:00
|
|
|
uuid = CharField(default=uuid_generator, index=True)
|
2014-03-12 16:37:06 +00:00
|
|
|
application = ForeignKeyField(OAuthApplication)
|
2014-11-07 17:05:21 +00:00
|
|
|
authorized_user = QuayUserField()
|
2014-03-12 16:37:06 +00:00
|
|
|
scope = CharField()
|
|
|
|
access_token = CharField(index=True)
|
|
|
|
token_type = CharField(default='Bearer')
|
|
|
|
expires_at = DateTimeField()
|
|
|
|
refresh_token = CharField(index=True, null=True)
|
2014-03-25 16:42:40 +00:00
|
|
|
data = TextField() # This is context for which this token was generated, such as the user
|
2014-03-12 16:37:06 +00:00
|
|
|
|
|
|
|
|
2014-03-12 04:49:03 +00:00
|
|
|
class NotificationKind(BaseModel):
|
2014-05-29 15:24:10 +00:00
|
|
|
name = CharField(index=True, unique=True)
|
2014-03-12 04:49:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Notification(BaseModel):
|
|
|
|
uuid = CharField(default=uuid_generator, index=True)
|
|
|
|
kind = ForeignKeyField(NotificationKind, index=True)
|
2014-11-07 17:05:21 +00:00
|
|
|
target = QuayUserField(index=True)
|
2014-03-12 04:49:03 +00:00
|
|
|
metadata_json = TextField(default='{}')
|
|
|
|
created = DateTimeField(default=datetime.now, index=True)
|
2014-07-28 22:23:46 +00:00
|
|
|
dismissed = BooleanField(default=False)
|
2014-03-12 04:49:03 +00:00
|
|
|
|
2014-07-16 20:30:47 +00:00
|
|
|
|
|
|
|
class ExternalNotificationEvent(BaseModel):
|
|
|
|
name = CharField(index=True, unique=True)
|
|
|
|
|
|
|
|
|
|
|
|
class ExternalNotificationMethod(BaseModel):
|
|
|
|
name = CharField(index=True, unique=True)
|
|
|
|
|
|
|
|
|
|
|
|
class RepositoryNotification(BaseModel):
|
|
|
|
uuid = CharField(default=uuid_generator, index=True)
|
|
|
|
repository = ForeignKeyField(Repository, index=True)
|
|
|
|
event = ForeignKeyField(ExternalNotificationEvent)
|
|
|
|
method = ForeignKeyField(ExternalNotificationMethod)
|
|
|
|
config_json = TextField()
|
|
|
|
|
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
class RepositoryAuthorizedEmail(BaseModel):
|
|
|
|
repository = ForeignKeyField(Repository, index=True)
|
|
|
|
email = CharField()
|
|
|
|
code = CharField(default=random_string_generator(), unique=True, index=True)
|
|
|
|
confirmed = BooleanField(default=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
read_slaves = (read_slave,)
|
|
|
|
indexes = (
|
|
|
|
# create a unique index on email and repository
|
|
|
|
(('email', 'repository'), True),
|
|
|
|
)
|
2014-11-11 22:22:37 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
|
|
|
|
|
2014-03-12 16:37:06 +00:00
|
|
|
all_models = [User, Repository, Image, AccessToken, Role, RepositoryPermission, Visibility,
|
|
|
|
RepositoryTag, EmailConfirmation, FederatedLogin, LoginService, QueueItem,
|
2014-07-16 20:30:47 +00:00
|
|
|
RepositoryBuild, Team, TeamMember, TeamRole, LogEntryKind, LogEntry,
|
2014-03-12 16:37:06 +00:00
|
|
|
PermissionPrototype, ImageStorage, BuildTriggerService, RepositoryBuildTrigger,
|
2014-03-19 19:39:44 +00:00
|
|
|
OAuthApplication, OAuthAuthorizationCode, OAuthAccessToken, NotificationKind,
|
2014-07-16 20:30:47 +00:00
|
|
|
Notification, ImageStorageLocation, ImageStoragePlacement,
|
2014-07-28 18:58:12 +00:00
|
|
|
ExternalNotificationEvent, ExternalNotificationMethod, RepositoryNotification,
|
2014-10-07 18:03:17 +00:00
|
|
|
RepositoryAuthorizedEmail, ImageStorageTransformation, DerivedImageStorage,
|
2015-02-17 17:35:16 +00:00
|
|
|
TeamMemberInvite, ImageStorageSignature, ImageStorageSignatureKind,
|
2015-04-13 17:31:07 +00:00
|
|
|
AccessTokenKind, Star, RepositoryActionCount]
|