2013-09-20 15:55:44 +00:00
|
|
|
import string
|
2013-10-02 16:43:45 +00:00
|
|
|
import logging
|
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 *
|
|
|
|
from peewee import create_model_tables
|
|
|
|
|
2013-09-30 23:10:27 +00:00
|
|
|
from app import app
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-10-02 16:43:45 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2013-09-30 23:10:27 +00:00
|
|
|
db = app.config['DB_DRIVER'](app.config['DB_NAME'],
|
|
|
|
**app.config['DB_CONNECTION_ARGS'])
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
2013-10-02 16:43:45 +00:00
|
|
|
def close_db(exc):
|
|
|
|
if not db.is_closed():
|
|
|
|
logger.debug('Disconnecting from database.')
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
app.teardown_request(close_db)
|
|
|
|
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
class BaseModel(Model):
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
|
|
|
|
|
|
|
|
class User(BaseModel):
|
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-09-28 03:33:59 +00:00
|
|
|
email = CharField(unique=True, index=True)
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
class Team(BaseModel):
|
2013-11-01 23:34:17 +00:00
|
|
|
name = CharField(index=True)
|
2013-10-31 20:46:04 +00:00
|
|
|
organization = ForeignKeyField(User, index=True)
|
|
|
|
|
2013-11-01 23:34:17 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
indexes = (
|
|
|
|
# A team name must be unique within an organization
|
|
|
|
(('name', 'organization'), True),
|
|
|
|
)
|
|
|
|
|
2013-10-31 20:46:04 +00:00
|
|
|
|
|
|
|
class TeamMember(BaseModel):
|
|
|
|
user = ForeignKeyField(User, index=True)
|
|
|
|
team = ForeignKeyField(Team, index=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
indexes = (
|
|
|
|
# A user may belong to a team only once
|
|
|
|
(('user', 'team'), True),
|
|
|
|
)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
2013-10-10 03:00:34 +00:00
|
|
|
class LoginService(BaseModel):
|
|
|
|
name = CharField(unique=True, index=True)
|
|
|
|
|
|
|
|
|
|
|
|
class FederatedLogin(BaseModel):
|
|
|
|
user = ForeignKeyField(User, index=True)
|
|
|
|
service = ForeignKeyField(LoginService, index=True)
|
|
|
|
service_ident = CharField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
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):
|
2013-09-28 03:33:59 +00:00
|
|
|
name = CharField(index=True)
|
2013-09-20 22:38:17 +00:00
|
|
|
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
class Repository(BaseModel):
|
|
|
|
namespace = CharField()
|
|
|
|
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)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
indexes = (
|
|
|
|
# create a unique index on namespace and name
|
|
|
|
(('namespace', 'name'), True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
class Role(BaseModel):
|
2013-09-28 03:33:59 +00:00
|
|
|
name = CharField(index=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)
|
|
|
|
user = ForeignKeyField(User, 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
|
|
|
|
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
|
|
|
|
2013-10-31 20:46:04 +00:00
|
|
|
class TeamPermission(BaseModel):
|
|
|
|
team = ForeignKeyField(Team, index=True)
|
|
|
|
organization = ForeignKeyField(User, index=True)
|
|
|
|
role = ForeignKeyField(Role)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
indexes = (
|
|
|
|
(('team', 'organization'), True),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
def random_string_generator(length=16):
|
|
|
|
def random_string():
|
|
|
|
random = SystemRandom()
|
|
|
|
return ''.join([random.choice(string.ascii_uppercase + string.digits)
|
|
|
|
for x in range(length)])
|
|
|
|
return random_string
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2013-09-20 15:55:44 +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)
|
2013-09-27 23:29:01 +00:00
|
|
|
user = ForeignKeyField(User)
|
|
|
|
pw_reset = BooleanField(default=False)
|
|
|
|
email_confirm = BooleanField(default=False)
|
|
|
|
created = DateTimeField(default=datetime.now)
|
|
|
|
|
|
|
|
|
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.
|
2013-10-01 18:14:39 +00:00
|
|
|
docker_image_id = CharField()
|
2013-09-20 15:55:44 +00:00
|
|
|
checksum = CharField(null=True)
|
2013-09-26 19:58:11 +00:00
|
|
|
created = DateTimeField(null=True)
|
2013-10-01 03:22:52 +00:00
|
|
|
comment = TextField(null=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/
|
2013-09-30 23:10:27 +00:00
|
|
|
ancestors = CharField(index=True, default='/', max_length=64535)
|
2013-09-30 19:30:00 +00:00
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
indexes = (
|
|
|
|
# we don't really want duplicates
|
2013-10-01 18:14:39 +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)
|
|
|
|
|
2013-09-28 03:33:59 +00:00
|
|
|
class Meta:
|
|
|
|
database = db
|
|
|
|
indexes = (
|
|
|
|
(('repository', 'name'), True),
|
|
|
|
)
|
|
|
|
|
2013-09-26 19:58:11 +00:00
|
|
|
|
2013-10-24 20:37:03 +00:00
|
|
|
class RepositoryBuild(BaseModel):
|
2013-10-25 05:14:38 +00:00
|
|
|
repository = ForeignKeyField(Repository)
|
2013-10-25 22:17:43 +00:00
|
|
|
access_token = ForeignKeyField(AccessToken)
|
2013-10-25 05:14:38 +00:00
|
|
|
resource_key = CharField()
|
2013-10-25 22:17:43 +00:00
|
|
|
tag = CharField()
|
2013-10-25 19:13:11 +00:00
|
|
|
build_node_id = IntegerField(null=True)
|
2013-10-24 20:37:03 +00:00
|
|
|
phase = CharField(default='waiting')
|
|
|
|
status_url = CharField(null=True)
|
|
|
|
|
|
|
|
|
2013-10-18 18:30:49 +00:00
|
|
|
class QueueItem(BaseModel):
|
2013-10-17 22:25:19 +00:00
|
|
|
queue_name = CharField(index=True)
|
|
|
|
body = TextField()
|
|
|
|
available_after = DateTimeField(default=datetime.now, index=True)
|
|
|
|
available = BooleanField(default=True, index=True)
|
|
|
|
processing_expires = DateTimeField(null=True, index=True)
|
|
|
|
|
|
|
|
|
2013-09-20 23:35:35 +00:00
|
|
|
def initialize_db():
|
2013-09-26 19:58:11 +00:00
|
|
|
create_model_tables([User, Repository, Image, AccessToken, Role,
|
2013-09-27 23:55:04 +00:00
|
|
|
RepositoryPermission, Visibility, RepositoryTag,
|
2013-10-17 22:25:19 +00:00
|
|
|
EmailConfirmation, FederatedLogin, LoginService,
|
2013-10-31 20:46:04 +00:00
|
|
|
QueueItem, RepositoryBuild, Team, TeamMember,
|
|
|
|
TeamPermission])
|
2013-09-20 22:38:17 +00:00
|
|
|
Role.create(name='admin')
|
|
|
|
Role.create(name='write')
|
|
|
|
Role.create(name='read')
|
|
|
|
Visibility.create(name='public')
|
|
|
|
Visibility.create(name='private')
|
2013-10-10 03:00:34 +00:00
|
|
|
LoginService.create(name='github')
|