2013-10-10 03:52:28 +00:00
|
|
|
import logging
|
|
|
|
import string
|
|
|
|
|
|
|
|
from random import SystemRandom
|
|
|
|
from datetime import datetime
|
|
|
|
|
2013-10-01 18:14:39 +00:00
|
|
|
from data.database import initialize_db
|
2013-10-10 03:52:28 +00:00
|
|
|
from data import model
|
|
|
|
from app import app
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logging.basicConfig(**app.config['LOGGING_CONFIG'])
|
|
|
|
|
|
|
|
|
|
|
|
def __gen_hex_id(length=64):
|
|
|
|
random = SystemRandom()
|
|
|
|
return ''.join([random.choice('abcdef' + string.digits)
|
|
|
|
for x in range(length)])
|
|
|
|
|
|
|
|
|
|
|
|
def __gen_checksum():
|
|
|
|
return 'tarsum+sha256:' + __gen_hex_id(64)
|
|
|
|
|
|
|
|
|
|
|
|
def create_subtree(repo, structure, parent):
|
|
|
|
num_nodes, subtrees, last_node_tag = structure
|
|
|
|
|
|
|
|
# create the nodes
|
|
|
|
for i in range(num_nodes):
|
|
|
|
docker_image_id = __gen_hex_id()
|
|
|
|
checksum = __gen_checksum()
|
|
|
|
|
|
|
|
new_image = model.create_image(docker_image_id, repo)
|
|
|
|
model.set_image_checksum(docker_image_id, repo, checksum)
|
|
|
|
|
|
|
|
new_image = model.set_image_metadata(docker_image_id, repo.namespace,
|
|
|
|
repo.name, str(datetime.now()),
|
|
|
|
'no comment', parent)
|
|
|
|
|
|
|
|
parent = new_image
|
|
|
|
|
|
|
|
if last_node_tag:
|
|
|
|
model.create_or_update_tag(repo.namespace, repo.name, last_node_tag,
|
|
|
|
new_image.docker_image_id)
|
|
|
|
|
|
|
|
for subtree in subtrees:
|
|
|
|
create_subtree(repo, subtree, new_image)
|
|
|
|
|
|
|
|
|
|
|
|
def __generate_repository(user, name, is_public, permissions, structure):
|
|
|
|
repo = model.create_repository(user.username, name, user)
|
|
|
|
|
|
|
|
if is_public:
|
|
|
|
model.set_repository_visibility(repo, 'public')
|
|
|
|
|
|
|
|
for delegate, role in permissions:
|
|
|
|
model.set_user_repo_permission(delegate.username, user.username, name,
|
|
|
|
role)
|
|
|
|
|
|
|
|
create_subtree(repo, structure, None)
|
|
|
|
|
2013-10-01 18:14:39 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-10-08 15:29:42 +00:00
|
|
|
initialize_db()
|
2013-10-10 03:52:28 +00:00
|
|
|
|
|
|
|
if app.config.get('POPULATE_DB_TEST_DATA', False):
|
|
|
|
logger.debug('Populating the DB with test data.')
|
|
|
|
|
|
|
|
new_user_1 = model.create_user('devtable', 'password',
|
|
|
|
'jake@devtable.com')
|
|
|
|
new_user_1.verified = True
|
|
|
|
new_user_1.save()
|
|
|
|
|
|
|
|
new_user_2 = model.create_user('public', 'password',
|
|
|
|
'jacob.moshenko@gmail.com')
|
|
|
|
new_user_2.verified = True
|
|
|
|
new_user_2.save()
|
|
|
|
|
|
|
|
__generate_repository(new_user_1, 'simple', False, [], (4, [], 'latest'))
|
|
|
|
|
|
|
|
__generate_repository(new_user_1, 'complex', False, [],
|
|
|
|
(2, [(3, [], 'v2.0'),
|
|
|
|
(1, [(1, [(1, [], 'latest')], 'staging'),
|
|
|
|
(1, [], None)], None)], None))
|
|
|
|
|
|
|
|
__generate_repository(new_user_2, 'publicrepo', True, [],
|
|
|
|
(10, [], 'latest'))
|
|
|
|
|
|
|
|
__generate_repository(new_user_1, 'shared', False,
|
|
|
|
[(new_user_2, 'write')], (5, [], 'latest'))
|