2014-02-11 00:12:43 +00:00
|
|
|
import logging
|
2015-02-27 21:00:32 +00:00
|
|
|
import datetime
|
2014-02-11 00:12:43 +00:00
|
|
|
|
|
|
|
from random import SystemRandom
|
2014-07-31 20:46:02 +00:00
|
|
|
from functools import wraps, partial
|
2014-02-12 23:58:40 +00:00
|
|
|
from copy import deepcopy
|
2014-07-31 20:46:02 +00:00
|
|
|
from jinja2.utils import generate_lorem_ipsum
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-05-09 22:45:11 +00:00
|
|
|
from data.buildlogs import RedisBuildLogs
|
2014-02-11 00:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-02-12 23:58:40 +00:00
|
|
|
random = SystemRandom()
|
|
|
|
|
|
|
|
|
2014-07-31 20:46:02 +00:00
|
|
|
get_sentence = partial(generate_lorem_ipsum, html=False, n=1, min=5, max=10)
|
|
|
|
|
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
def maybe_advance_script(is_get_status=False):
|
|
|
|
def inner_advance(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
advance_units = random.randint(1, 500)
|
|
|
|
logger.debug('Advancing script %s units', advance_units)
|
|
|
|
while advance_units > 0 and self.remaining_script:
|
|
|
|
units = self.remaining_script[0][0]
|
|
|
|
|
|
|
|
if advance_units > units:
|
|
|
|
advance_units -= units
|
|
|
|
self.advance_script(is_get_status)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return inner_advance
|
2014-02-11 00:12:43 +00:00
|
|
|
|
|
|
|
|
2014-05-09 22:45:11 +00:00
|
|
|
class TestBuildLogs(RedisBuildLogs):
|
2014-11-24 21:07:38 +00:00
|
|
|
COMMAND_TYPES = ['FROM', 'MAINTAINER', 'RUN', 'CMD', 'EXPOSE', 'ENV', 'ADD',
|
2014-02-12 23:58:40 +00:00
|
|
|
'ENTRYPOINT', 'VOLUME', 'USER', 'WORKDIR']
|
|
|
|
STATUS_TEMPLATE = {
|
|
|
|
'total_commands': None,
|
|
|
|
'current_command': None,
|
|
|
|
'push_completion': 0.0,
|
2014-04-30 22:48:36 +00:00
|
|
|
'pull_completion': 0.0,
|
2014-02-12 23:58:40 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 18:16:42 +00:00
|
|
|
def __init__(self, redis_config, namespace, repository, test_build_id, allow_delegate=True):
|
|
|
|
super(TestBuildLogs, self).__init__(redis_config)
|
2014-02-11 17:09:59 +00:00
|
|
|
self.namespace = namespace
|
|
|
|
self.repository = repository
|
|
|
|
self.test_build_id = test_build_id
|
2014-07-08 22:19:13 +00:00
|
|
|
self.allow_delegate = allow_delegate
|
2014-02-12 23:58:40 +00:00
|
|
|
self.remaining_script = self._generate_script()
|
|
|
|
logger.debug('Total script size: %s', len(self.remaining_script))
|
|
|
|
self._logs = []
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
self._status = {}
|
|
|
|
self._last_status = {}
|
|
|
|
|
|
|
|
def advance_script(self, is_get_status):
|
|
|
|
(_, log, status_wrapper) = self.remaining_script.pop(0)
|
|
|
|
if log is not None:
|
|
|
|
self._logs.append(log)
|
|
|
|
|
|
|
|
if status_wrapper is not None:
|
|
|
|
(phase, status) = status_wrapper
|
|
|
|
|
|
|
|
if not is_get_status:
|
2015-05-08 02:25:23 +00:00
|
|
|
from data import model
|
2015-07-15 21:25:41 +00:00
|
|
|
build_obj = model.build.get_repository_build(self.test_build_id)
|
2015-05-08 02:25:23 +00:00
|
|
|
build_obj.phase = phase
|
|
|
|
build_obj.save()
|
|
|
|
|
|
|
|
self._status = status
|
2014-02-12 23:58:40 +00:00
|
|
|
self._last_status = status
|
|
|
|
|
|
|
|
def _generate_script(self):
|
|
|
|
script = []
|
|
|
|
|
|
|
|
# generate the init phase
|
|
|
|
script.append(self._generate_phase(400, 'initializing'))
|
|
|
|
script.extend(self._generate_logs(random.randint(1, 3)))
|
|
|
|
|
|
|
|
# move to the building phase
|
|
|
|
script.append(self._generate_phase(400, 'building'))
|
|
|
|
total_commands = random.randint(5, 20)
|
|
|
|
for command_num in range(1, total_commands + 1):
|
|
|
|
command_weight = random.randint(50, 100)
|
2014-10-01 18:23:15 +00:00
|
|
|
script.append(self._generate_command(command_num, total_commands, command_weight))
|
2014-02-12 23:58:40 +00:00
|
|
|
|
|
|
|
# we want 0 logs some percent of the time
|
|
|
|
num_logs = max(0, random.randint(-50, 400))
|
|
|
|
script.extend(self._generate_logs(num_logs))
|
|
|
|
|
|
|
|
# move to the pushing phase
|
|
|
|
script.append(self._generate_phase(400, 'pushing'))
|
|
|
|
script.extend(self._generate_push_statuses(total_commands))
|
|
|
|
|
|
|
|
# move to the error or complete phase
|
|
|
|
if random.randint(0, 1) == 0:
|
|
|
|
script.append(self._generate_phase(400, 'complete'))
|
|
|
|
else:
|
|
|
|
script.append(self._generate_phase(400, 'error'))
|
|
|
|
script.append((1, {'message': 'Something bad happened! Oh noes!',
|
|
|
|
'type': self.ERROR}, None))
|
2014-02-11 03:43:48 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
return script
|
2014-02-11 03:43:48 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
def _generate_phase(self, start_weight, phase_name):
|
2015-02-27 21:00:32 +00:00
|
|
|
message = {
|
|
|
|
'message': phase_name,
|
|
|
|
'type': self.PHASE,
|
|
|
|
'datetime': str(datetime.datetime.now())
|
|
|
|
}
|
|
|
|
|
|
|
|
return (start_weight, message,
|
2014-02-12 23:58:40 +00:00
|
|
|
(phase_name, deepcopy(self.STATUS_TEMPLATE)))
|
2014-02-11 03:43:48 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
def _generate_command(self, command_num, total_commands, command_weight):
|
2014-02-11 03:43:48 +00:00
|
|
|
sentence = get_sentence()
|
2014-02-12 23:58:40 +00:00
|
|
|
command = random.choice(self.COMMAND_TYPES)
|
2014-02-11 03:43:48 +00:00
|
|
|
if command == 'FROM':
|
2014-02-13 02:32:46 +00:00
|
|
|
sentence = random.choice(['ubuntu', 'lopter/raring-base',
|
|
|
|
'quay.io/devtable/simple',
|
2014-02-12 23:58:40 +00:00
|
|
|
'quay.io/buynlarge/orgrepo',
|
|
|
|
'stackbrew/ubuntu:precise'])
|
2014-02-11 03:43:48 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
msg = {
|
|
|
|
'message': 'Step %s: %s %s' % (command_num, command, sentence),
|
|
|
|
'type': self.COMMAND,
|
2015-02-27 21:00:32 +00:00
|
|
|
'datetime': str(datetime.datetime.now())
|
2014-02-11 00:12:43 +00:00
|
|
|
}
|
2014-02-12 23:58:40 +00:00
|
|
|
status = deepcopy(self.STATUS_TEMPLATE)
|
|
|
|
status['total_commands'] = total_commands
|
|
|
|
status['current_command'] = command_num
|
|
|
|
return (command_weight, msg, ('building', status))
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def _generate_logs(count):
|
2014-02-14 03:01:26 +00:00
|
|
|
others = []
|
|
|
|
if random.randint(0, 10) <= 8:
|
2015-02-27 21:00:32 +00:00
|
|
|
premessage = {
|
|
|
|
'message': '\x1b[91m' + get_sentence(),
|
2015-03-06 19:29:56 +00:00
|
|
|
'data': {'datetime': str(datetime.datetime.now())}
|
2015-02-27 21:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
postmessage = {
|
|
|
|
'message': '\x1b[0m',
|
2015-03-06 19:29:56 +00:00
|
|
|
'data': {'datetime': str(datetime.datetime.now())}
|
2015-02-27 21:00:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 03:01:26 +00:00
|
|
|
count = count - 2
|
2015-02-27 21:00:32 +00:00
|
|
|
others = [(1, premessage, None), (1, postmessage, None)]
|
|
|
|
|
|
|
|
def get_message():
|
|
|
|
return {
|
|
|
|
'message': get_sentence(),
|
2015-03-06 19:29:56 +00:00
|
|
|
'data': {'datetime': str(datetime.datetime.now())}
|
2015-02-27 21:00:32 +00:00
|
|
|
}
|
2014-02-14 03:01:26 +00:00
|
|
|
|
2015-02-27 21:00:32 +00:00
|
|
|
return others + [(1, get_message(), None) for _ in range(count)]
|
2014-02-11 00:12:43 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2014-02-12 23:58:40 +00:00
|
|
|
def _compute_total_completion(statuses, total_images):
|
|
|
|
percentage_with_sizes = float(len(statuses.values()))/total_images
|
|
|
|
sent_bytes = sum([status[u'current'] for status in statuses.values()])
|
|
|
|
total_bytes = sum([status[u'total'] for status in statuses.values()])
|
|
|
|
return float(sent_bytes)/total_bytes*percentage_with_sizes
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def _generate_push_statuses(total_commands):
|
|
|
|
push_status_template = deepcopy(TestBuildLogs.STATUS_TEMPLATE)
|
|
|
|
push_status_template['current_command'] = total_commands
|
|
|
|
push_status_template['total_commands'] = total_commands
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
push_statuses = []
|
2014-02-11 17:09:59 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
one_mb = 1 * 1024 * 1024
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
num_images = random.randint(2, 7)
|
|
|
|
sizes = [random.randint(one_mb, one_mb * 5) for _ in range(num_images)]
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-04-30 22:54:59 +00:00
|
|
|
image_completion = {}
|
2014-02-12 23:58:40 +00:00
|
|
|
for image_num, image_size in enumerate(sizes):
|
|
|
|
image_id = 'image_id_%s' % image_num
|
|
|
|
|
|
|
|
image_completion[image_id] = {
|
|
|
|
'current': 0,
|
|
|
|
'total': image_size,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in range(one_mb, image_size, one_mb):
|
|
|
|
image_completion[image_id]['current'] = i
|
|
|
|
new_status = deepcopy(push_status_template)
|
|
|
|
completion = TestBuildLogs._compute_total_completion(image_completion,
|
|
|
|
num_images)
|
|
|
|
new_status['push_completion'] = completion
|
|
|
|
push_statuses.append((250, None, ('pushing', new_status)))
|
|
|
|
|
|
|
|
return push_statuses
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
@maybe_advance_script()
|
|
|
|
def get_log_entries(self, build_id, start_index):
|
2014-02-11 17:09:59 +00:00
|
|
|
if build_id == self.test_build_id:
|
2014-02-12 23:58:40 +00:00
|
|
|
return (len(self._logs), self._logs[start_index:])
|
2014-07-11 16:05:33 +00:00
|
|
|
elif not self.allow_delegate:
|
|
|
|
return None
|
2014-02-11 00:12:43 +00:00
|
|
|
else:
|
2014-02-12 23:58:40 +00:00
|
|
|
return super(TestBuildLogs, self).get_log_entries(build_id, start_index)
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
@maybe_advance_script(True)
|
2014-02-11 00:12:43 +00:00
|
|
|
def get_status(self, build_id):
|
2014-02-11 17:09:59 +00:00
|
|
|
if build_id == self.test_build_id:
|
2014-02-12 23:58:40 +00:00
|
|
|
returnable_status = self._last_status
|
|
|
|
self._last_status = self._status
|
|
|
|
return returnable_status
|
2014-07-08 22:19:13 +00:00
|
|
|
elif not self.allow_delegate:
|
|
|
|
return None
|
2014-02-11 00:12:43 +00:00
|
|
|
else:
|
|
|
|
return super(TestBuildLogs, self).get_status(build_id)
|
2014-09-11 19:33:10 +00:00
|
|
|
|
2014-09-12 17:13:14 +00:00
|
|
|
def expire_log_entries(self, build_id):
|
2014-09-11 19:33:10 +00:00
|
|
|
if build_id == self.test_build_id:
|
|
|
|
return
|
|
|
|
if not self.allow_delegate:
|
|
|
|
return None
|
|
|
|
else:
|
2014-09-12 17:13:14 +00:00
|
|
|
return super(TestBuildLogs, self).expire_log_entries(build_id)
|