2013-10-10 03:52:28 +00:00
|
|
|
import logging
|
|
|
|
import string
|
2013-10-18 20:36:11 +00:00
|
|
|
import shutil
|
|
|
|
import os
|
|
|
|
import hashlib
|
2013-10-10 03:52:28 +00:00
|
|
|
|
2013-10-18 22:28:37 +00:00
|
|
|
from datetime import datetime, timedelta
|
2013-10-25 19:47:34 +00:00
|
|
|
from flask import url_for
|
2013-10-10 03:52:28 +00:00
|
|
|
|
2013-10-18 20:36:11 +00:00
|
|
|
import storage
|
|
|
|
|
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__)
|
2013-10-18 20:36:11 +00:00
|
|
|
store = storage.load()
|
2013-10-10 03:52:28 +00:00
|
|
|
logging.basicConfig(**app.config['LOGGING_CONFIG'])
|
|
|
|
|
|
|
|
|
2013-10-19 00:18:23 +00:00
|
|
|
SAMPLE_DIFFS = ['test/data/sample/diffs/diffs%s.json' % i
|
|
|
|
for i in range(1, 10)]
|
2013-10-18 20:36:11 +00:00
|
|
|
|
2013-10-18 22:28:37 +00:00
|
|
|
REFERENCE_DATE = datetime(2013, 6, 23)
|
|
|
|
|
2013-10-18 20:36:11 +00:00
|
|
|
|
|
|
|
def __gen_checksum(image_id):
|
|
|
|
h = hashlib.md5(image_id)
|
2013-10-19 00:35:14 +00:00
|
|
|
return 'tarsum+sha256:' + h.hexdigest() + h.hexdigest()
|
2013-10-10 03:52:28 +00:00
|
|
|
|
|
|
|
|
2013-10-18 22:28:37 +00:00
|
|
|
def __gen_image_id(repo, image_num):
|
|
|
|
str_to_hash = "%s/%s/%s" % (repo.namespace, repo.name, image_num)
|
2013-10-18 20:36:11 +00:00
|
|
|
|
|
|
|
h = hashlib.md5(str_to_hash)
|
2013-10-19 00:35:14 +00:00
|
|
|
return h.hexdigest() + h.hexdigest()
|
2013-10-10 03:52:28 +00:00
|
|
|
|
|
|
|
|
2013-10-18 22:28:37 +00:00
|
|
|
global_image_num = [0]
|
2013-10-10 03:52:28 +00:00
|
|
|
def create_subtree(repo, structure, parent):
|
2013-10-10 04:40:07 +00:00
|
|
|
num_nodes, subtrees, last_node_tags = structure
|
2013-10-10 03:52:28 +00:00
|
|
|
|
|
|
|
# create the nodes
|
|
|
|
for i in range(num_nodes):
|
2013-10-18 22:28:37 +00:00
|
|
|
image_num = global_image_num[0]
|
|
|
|
global_image_num[0] += 1
|
|
|
|
docker_image_id = __gen_image_id(repo, image_num)
|
2013-10-18 20:36:11 +00:00
|
|
|
logger.debug('new docker id: %s' % docker_image_id)
|
|
|
|
checksum = __gen_checksum(docker_image_id)
|
2013-10-10 03:52:28 +00:00
|
|
|
|
|
|
|
new_image = model.create_image(docker_image_id, repo)
|
|
|
|
model.set_image_checksum(docker_image_id, repo, checksum)
|
|
|
|
|
2013-10-18 22:28:37 +00:00
|
|
|
creation_time = REFERENCE_DATE + timedelta(days=image_num)
|
2013-10-10 03:52:28 +00:00
|
|
|
new_image = model.set_image_metadata(docker_image_id, repo.namespace,
|
2013-10-18 22:28:37 +00:00
|
|
|
repo.name, str(creation_time),
|
2013-10-10 03:52:28 +00:00
|
|
|
'no comment', parent)
|
|
|
|
|
2013-10-18 20:36:11 +00:00
|
|
|
# Populate the diff file
|
|
|
|
diff_path = store.image_file_diffs_path(repo.namespace, repo.name,
|
2013-10-18 22:28:37 +00:00
|
|
|
docker_image_id)
|
|
|
|
source_diff = SAMPLE_DIFFS[image_num % len(SAMPLE_DIFFS)]
|
2013-10-18 20:36:11 +00:00
|
|
|
|
|
|
|
with open(source_diff, 'r') as source_file:
|
|
|
|
store.stream_write(diff_path, source_file)
|
|
|
|
|
2013-10-10 03:52:28 +00:00
|
|
|
parent = new_image
|
|
|
|
|
2013-10-10 04:40:07 +00:00
|
|
|
if last_node_tags:
|
|
|
|
if not isinstance(last_node_tags, list):
|
|
|
|
last_node_tags = [last_node_tags]
|
|
|
|
|
|
|
|
for tag_name in last_node_tags:
|
|
|
|
model.create_or_update_tag(repo.namespace, repo.name, tag_name,
|
|
|
|
new_image.docker_image_id)
|
2013-10-10 03:52:28 +00:00
|
|
|
|
|
|
|
for subtree in subtrees:
|
|
|
|
create_subtree(repo, subtree, new_image)
|
|
|
|
|
|
|
|
|
2013-10-17 02:37:29 +00:00
|
|
|
def __generate_repository(user, name, description, is_public, permissions,
|
|
|
|
structure):
|
2013-10-10 03:52:28 +00:00
|
|
|
repo = model.create_repository(user.username, name, user)
|
|
|
|
|
|
|
|
if is_public:
|
|
|
|
model.set_repository_visibility(repo, 'public')
|
|
|
|
|
2013-10-12 01:28:02 +00:00
|
|
|
if description:
|
|
|
|
repo.description = description
|
|
|
|
repo.save()
|
|
|
|
|
2013-10-10 03:52:28 +00:00
|
|
|
for delegate, role in permissions:
|
|
|
|
model.set_user_repo_permission(delegate.username, user.username, name,
|
|
|
|
role)
|
|
|
|
|
|
|
|
create_subtree(repo, structure, None)
|
|
|
|
|
2013-10-25 19:47:34 +00:00
|
|
|
return repo
|
|
|
|
|
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',
|
2013-10-10 17:44:07 +00:00
|
|
|
'jschorr@devtable.com')
|
2013-10-10 03:52:28 +00:00
|
|
|
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()
|
|
|
|
|
2013-11-05 22:09:50 +00:00
|
|
|
new_user_3 = model.create_user('freshuser', 'password', 'no@thanks.com')
|
|
|
|
new_user_3.verified = True
|
|
|
|
new_user_3.save()
|
|
|
|
|
2013-10-12 01:28:02 +00:00
|
|
|
__generate_repository(new_user_1, 'simple', 'Simple repository.', False,
|
|
|
|
[], (4, [], ['latest', 'prod']))
|
2013-10-10 03:52:28 +00:00
|
|
|
|
2013-10-12 01:28:02 +00:00
|
|
|
__generate_repository(new_user_1, 'complex',
|
|
|
|
'Complex repository with many branches and tags.',
|
|
|
|
False, [(new_user_2, 'read')],
|
2013-10-10 03:52:28 +00:00
|
|
|
(2, [(3, [], 'v2.0'),
|
2013-10-17 02:37:29 +00:00
|
|
|
(1, [(1, [(1, [], ['prod'])],
|
2013-10-10 04:40:07 +00:00
|
|
|
'staging'),
|
2013-10-10 03:52:28 +00:00
|
|
|
(1, [], None)], None)], None))
|
|
|
|
|
2013-10-12 17:31:05 +00:00
|
|
|
__generate_repository(new_user_1, 'gargantuan', None, False, [],
|
2013-10-11 01:01:25 +00:00
|
|
|
(2, [(3, [], 'v2.0'),
|
|
|
|
(1, [(1, [(1, [], ['latest', 'prod'])],
|
|
|
|
'staging'),
|
|
|
|
(1, [], None)], None),
|
|
|
|
(20, [], 'v3.0'),
|
|
|
|
(5, [], 'v4.0'),
|
|
|
|
(1, [(1, [], 'v5.0'), (1, [], 'v6.0')], None)],
|
|
|
|
None))
|
|
|
|
|
2013-10-12 01:28:02 +00:00
|
|
|
__generate_repository(new_user_2, 'publicrepo',
|
|
|
|
'Public repository pullable by the world.', True,
|
|
|
|
[], (10, [], 'latest'))
|
2013-10-10 03:52:28 +00:00
|
|
|
|
2013-10-12 01:28:02 +00:00
|
|
|
__generate_repository(new_user_1, 'shared',
|
|
|
|
'Shared repository, another user can write.', False,
|
2013-10-10 03:52:28 +00:00
|
|
|
[(new_user_2, 'write')], (5, [], 'latest'))
|
2013-10-17 02:37:29 +00:00
|
|
|
|
2013-10-25 19:47:34 +00:00
|
|
|
building = __generate_repository(new_user_1, 'building',
|
|
|
|
'Empty repository which is building.',
|
|
|
|
False, [], (0, [], None))
|
|
|
|
|
2013-11-01 23:34:17 +00:00
|
|
|
|
|
|
|
org = model.create_organization('devtableorg', 'quay@devtable.com',
|
|
|
|
new_user_1)
|
|
|
|
|
|
|
|
org_repo = __generate_repository(org, 'orgrepo',
|
|
|
|
'Repository owned by an org.', False,
|
|
|
|
[], (4, [], ['latest', 'prod']))
|
|
|
|
|
2013-11-04 21:57:20 +00:00
|
|
|
reader_team = model.create_team('readers', org, 'member',
|
|
|
|
'Readers of orgrepo.')
|
2013-11-01 23:34:17 +00:00
|
|
|
model.set_team_repo_permission(reader_team.name, org_repo.namespace,
|
|
|
|
org_repo.name, 'read')
|
|
|
|
model.add_user_to_team(new_user_2, reader_team)
|
|
|
|
|
2013-10-25 22:17:43 +00:00
|
|
|
token = model.create_access_token(building, 'write')
|
|
|
|
tag = 'ci.devtable.com:5000/%s/%s' % (building.namespace, building.name)
|
|
|
|
build = model.create_repository_build(building, token, '123-45-6789', tag)
|
|
|
|
|
2013-10-25 19:47:34 +00:00
|
|
|
build.build_node_id = 1
|
|
|
|
build.phase = 'building'
|
|
|
|
build.status_url = 'http://localhost:5000/test/build/status'
|
|
|
|
build.save()
|