2014-11-11 23:23:15 +00:00
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import trollius
|
2014-11-14 00:41:17 +00:00
|
|
|
import re
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-12 19:03:07 +00:00
|
|
|
from autobahn.wamp.exception import ApplicationError
|
2014-11-11 23:23:15 +00:00
|
|
|
from trollius.coroutines import From
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
from buildman.basecomponent import BaseComponent
|
2014-11-12 19:03:07 +00:00
|
|
|
from buildman.buildpack import BuildPackage, BuildPackageException
|
2014-11-14 00:41:17 +00:00
|
|
|
from buildman.buildstatus import StatusHandler
|
|
|
|
from buildman.server import BUILD_JOB_RESULT
|
|
|
|
from buildman.workererror import WorkerError
|
|
|
|
|
|
|
|
from data.database import BUILD_PHASE
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
HEARTBEAT_DELTA = datetime.timedelta(seconds=15)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
class COMPONENT_STATUS(object):
|
|
|
|
JOINING = 'joining'
|
|
|
|
WAITING = 'waiting'
|
|
|
|
RUNNING = 'running'
|
|
|
|
BUILDING = 'building'
|
|
|
|
TIMED_OUT = 'timeout'
|
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
class BuildComponent(BaseComponent):
|
|
|
|
""" An application session component which conducts one (or more) builds. """
|
|
|
|
|
|
|
|
server_hostname = None
|
|
|
|
expected_token = None
|
|
|
|
builder_realm = None
|
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
_component_status = COMPONENT_STATUS.JOINING
|
2014-11-14 00:41:17 +00:00
|
|
|
_last_heartbeat = None
|
|
|
|
_current_job = None
|
|
|
|
_build_status = None
|
|
|
|
_image_info = None
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
def __init__(self, config, realm=None, token=None, **kwargs):
|
|
|
|
self.expected_token = token
|
|
|
|
self.builder_realm = realm
|
|
|
|
|
|
|
|
BaseComponent.__init__(self, config, **kwargs)
|
|
|
|
|
|
|
|
def onConnect(self):
|
|
|
|
self.join(self.builder_realm)
|
|
|
|
|
|
|
|
def onJoin(self, details):
|
|
|
|
logger.debug('Registering methods and listeners for component %s' % self.builder_realm)
|
|
|
|
yield From(self.register(self._on_ready, u'io.quay.buildworker.ready'))
|
2014-11-14 19:53:35 +00:00
|
|
|
yield From(self.register(self._ping, u'io.quay.buildworker.ping'))
|
2014-11-11 23:23:15 +00:00
|
|
|
yield From(self.subscribe(self._on_heartbeat, 'io.quay.builder.heartbeat'))
|
|
|
|
yield From(self.subscribe(self._on_log_message, 'io.quay.builder.logmessage'))
|
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
self._set_status(COMPONENT_STATUS.WAITING)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
def is_ready(self):
|
2014-11-14 19:53:35 +00:00
|
|
|
return self._component_status == COMPONENT_STATUS.RUNNING
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-12 19:03:07 +00:00
|
|
|
def start_build(self, build_job):
|
2014-11-14 00:41:17 +00:00
|
|
|
self._current_job = build_job
|
|
|
|
self._build_status = StatusHandler(self.build_logs, build_job.repo_build())
|
|
|
|
self._image_info = {}
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
self._set_status(COMPONENT_STATUS.BUILDING)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
# Retrieve the job's buildpack.
|
2014-11-12 19:03:07 +00:00
|
|
|
buildpack_url = self.user_files.get_file_url(build_job.repo_build().resource_key,
|
|
|
|
requires_cors=False)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-12 19:03:07 +00:00
|
|
|
logger.debug('Retreiving build package: %s' % buildpack_url)
|
2014-11-11 23:23:15 +00:00
|
|
|
buildpack = None
|
|
|
|
try:
|
2014-11-12 19:03:07 +00:00
|
|
|
buildpack = BuildPackage.from_url(buildpack_url)
|
2014-11-11 23:23:15 +00:00
|
|
|
except BuildPackageException as bpe:
|
|
|
|
self._build_failure('Could not retrieve build package', bpe)
|
2014-11-14 00:41:17 +00:00
|
|
|
return
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
# Extract the base image information from the Dockerfile.
|
|
|
|
parsed_dockerfile = None
|
|
|
|
logger.debug('Parsing dockerfile')
|
|
|
|
|
2014-11-12 19:03:07 +00:00
|
|
|
build_config = build_job.build_config()
|
2014-11-11 23:23:15 +00:00
|
|
|
try:
|
2014-11-12 19:03:07 +00:00
|
|
|
parsed_dockerfile = buildpack.parse_dockerfile(build_config.get('build_subdir'))
|
2014-11-11 23:23:15 +00:00
|
|
|
except BuildPackageException as bpe:
|
|
|
|
self._build_failure('Could not find Dockerfile in build package', bpe)
|
2014-11-14 00:41:17 +00:00
|
|
|
return
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
image_and_tag_tuple = parsed_dockerfile.get_image_and_tag()
|
|
|
|
if image_and_tag_tuple is None or image_and_tag_tuple[0] is None:
|
2014-11-14 19:53:35 +00:00
|
|
|
self._build_failure('Missing FROM line in Dockerfile')
|
2014-11-14 00:41:17 +00:00
|
|
|
return
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
base_image_information = {
|
|
|
|
'repository': image_and_tag_tuple[0],
|
|
|
|
'tag': image_and_tag_tuple[1]
|
|
|
|
}
|
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
# Extract the number of steps from the Dockerfile.
|
|
|
|
with self._build_status as status_dict:
|
|
|
|
status_dict['total_commands'] = len(parsed_dockerfile.commands)
|
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
# Add the pull robot information, if any.
|
2014-11-12 19:03:07 +00:00
|
|
|
if build_config.get('pull_credentials') is not None:
|
|
|
|
base_image_information['username'] = build_config['pull_credentials'].get('username', '')
|
|
|
|
base_image_information['password'] = build_config['pull_credentials'].get('password', '')
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
# Retrieve the repository's full name.
|
2014-11-12 19:03:07 +00:00
|
|
|
repo = build_job.repo_build().repository
|
2014-11-11 23:23:15 +00:00
|
|
|
repository_name = repo.namespace_user.username + '/' + repo.name
|
|
|
|
|
|
|
|
# Parse the build queue item into build arguments.
|
|
|
|
# build_package: URL to the build package to download and untar/unzip.
|
|
|
|
# sub_directory: The location within the build package of the Dockerfile and the build context.
|
|
|
|
# repository: The repository for which this build is occurring.
|
|
|
|
# registry: The registry for which this build is occuring. Example: 'quay.io', 'staging.quay.io'
|
|
|
|
# pull_token: The token to use when pulling the cache for building.
|
|
|
|
# push_token: The token to use to push the built image.
|
|
|
|
# tag_names: The name(s) of the tag(s) for the newly built image.
|
|
|
|
# base_image: The image name and credentials to use to conduct the base image pull.
|
|
|
|
# repository: The repository to pull.
|
|
|
|
# tag: The tag to pull.
|
|
|
|
# username: The username for pulling the base image (if any).
|
|
|
|
# password: The password for pulling the base image (if any).
|
|
|
|
build_arguments = {
|
|
|
|
'build_package': buildpack_url,
|
2014-11-12 19:03:07 +00:00
|
|
|
'sub_directory': build_config.get('build_subdir', ''),
|
2014-11-11 23:23:15 +00:00
|
|
|
'repository': repository_name,
|
2014-11-14 00:41:17 +00:00
|
|
|
'registry': '10.0.2.2:5000' or self.server_hostname,
|
2014-11-12 19:03:07 +00:00
|
|
|
'pull_token': build_job.repo_build().access_token.code,
|
|
|
|
'push_token': build_job.repo_build().access_token.code,
|
|
|
|
'tag_names': build_config.get('docker_tags', ['latest']),
|
|
|
|
'base_image': base_image_information,
|
|
|
|
'cached_tag': build_job.determine_cached_tag() or ''
|
2014-11-11 23:23:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 19:03:07 +00:00
|
|
|
# Invoke the build.
|
|
|
|
logger.debug('Invoking build: %s', self.builder_realm)
|
2014-11-11 23:23:15 +00:00
|
|
|
logger.debug('With Arguments: %s', build_arguments)
|
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
return (self.call("io.quay.builder.build", **build_arguments)
|
|
|
|
.add_done_callback(self._build_complete))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __total_completion(statuses, total_images):
|
2014-11-14 19:53:35 +00:00
|
|
|
percentage_with_sizes = float(len(statuses.values())) / total_images
|
2014-11-14 00:41:17 +00:00
|
|
|
sent_bytes = sum([status['current'] for status in statuses.values()])
|
|
|
|
total_bytes = sum([status['total'] for status in statuses.values()])
|
2014-11-14 19:53:35 +00:00
|
|
|
return float(sent_bytes) / total_bytes * percentage_with_sizes
|
2014-11-14 00:41:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __process_pushpull_status(status_dict, current_phase, docker_data, images):
|
|
|
|
if not docker_data:
|
|
|
|
return
|
|
|
|
|
|
|
|
num_images = 0
|
|
|
|
status_completion_key = ''
|
|
|
|
|
|
|
|
if current_phase == 'pushing':
|
|
|
|
status_completion_key = 'push_completion'
|
|
|
|
num_images = status_dict['total_commands']
|
|
|
|
elif current_phase == 'pulling':
|
|
|
|
status_completion_key = 'pull_completion'
|
|
|
|
elif current_phase == 'priming-cache':
|
|
|
|
status_completion_key = 'cache_completion'
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if 'progressDetail' in docker_data and 'id' in docker_data:
|
|
|
|
image_id = docker_data['id']
|
|
|
|
detail = docker_data['progressDetail']
|
|
|
|
|
|
|
|
if 'current' in detail and 'total' in detail:
|
|
|
|
images[image_id] = detail
|
|
|
|
status_dict[status_completion_key] = \
|
|
|
|
BuildComponent.__total_completion(images, max(len(images), num_images))
|
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
def _on_log_message(self, phase, json_data):
|
2014-11-14 00:41:17 +00:00
|
|
|
# Parse any of the JSON data logged.
|
|
|
|
docker_data = {}
|
|
|
|
if json_data:
|
|
|
|
try:
|
|
|
|
docker_data = json.loads(json_data)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
# Extract the current status message (if any).
|
2014-11-14 00:41:17 +00:00
|
|
|
fully_unwrapped = ''
|
|
|
|
keys_to_extract = ['error', 'status', 'stream']
|
|
|
|
for key in keys_to_extract:
|
|
|
|
if key in docker_data:
|
|
|
|
fully_unwrapped = docker_data[key]
|
|
|
|
break
|
|
|
|
|
|
|
|
# Determine if this is a step string.
|
|
|
|
current_step = None
|
|
|
|
current_status_string = str(fully_unwrapped.encode('utf-8'))
|
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
if current_status_string and phase == BUILD_PHASE.BUILDING:
|
2014-11-14 00:41:17 +00:00
|
|
|
step_increment = re.search(r'Step ([0-9]+) :', current_status_string)
|
|
|
|
if step_increment:
|
|
|
|
current_step = int(step_increment.group(1))
|
|
|
|
|
|
|
|
# Parse and update the phase and the status_dict. The status dictionary contains
|
2014-11-14 19:53:35 +00:00
|
|
|
# the pull/push progress, as well as the current step index.
|
2014-11-14 00:41:17 +00:00
|
|
|
with self._build_status as status_dict:
|
2014-11-14 19:53:35 +00:00
|
|
|
self._build_status.set_phase(phase)
|
|
|
|
BuildComponent.__process_pushpull_status(status_dict, phase, docker_data, self._image_info)
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
# If the current message represents the beginning of a new step, then update the
|
|
|
|
# current command index.
|
|
|
|
if current_step is not None:
|
2014-11-14 00:41:17 +00:00
|
|
|
status_dict['current_command'] = current_step
|
|
|
|
|
|
|
|
# If the json data contains an error, then something went wrong with a push or pull.
|
|
|
|
if 'error' in docker_data:
|
|
|
|
self._build_status.set_error(docker_data['error'])
|
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
if current_step is not None:
|
|
|
|
self._build_status.set_command(current_status_string)
|
|
|
|
elif phase == BUILD_PHASE.BUILDING:
|
|
|
|
self._build_status.append_log(current_status_string)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _build_failure(self, error_message, exception=None):
|
2014-11-14 00:41:17 +00:00
|
|
|
self._build_status.set_error(error_message, {
|
|
|
|
'internal_error': exception.message if exception else None
|
|
|
|
})
|
|
|
|
|
|
|
|
build_id = self._current_job.repo_build().uuid
|
|
|
|
logger.warning('Build %s failed with message: %s', build_id, self._error_message)
|
|
|
|
|
|
|
|
# Mark that the build has finished (in an error state)
|
|
|
|
self._build_finished(BUILD_JOB_RESULT.ERROR)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
def _build_complete(self, result):
|
|
|
|
try:
|
2014-11-14 00:41:17 +00:00
|
|
|
# Retrieve the result. This will raise an ApplicationError on any error that occurred.
|
|
|
|
result.result()
|
|
|
|
self._build_status.set_phase(BUILD_PHASE.COMPLETE)
|
2014-11-14 19:53:35 +00:00
|
|
|
self._build_finished(BUILD_JOB_RESULT.COMPLETE)
|
2014-11-11 23:23:15 +00:00
|
|
|
except ApplicationError as ae:
|
2014-11-14 00:41:17 +00:00
|
|
|
worker_error = WorkerError(ae.error, ae.kwargs.get('base_error'))
|
|
|
|
|
|
|
|
# Write the error to the log.
|
|
|
|
self._build_status.set_error(worker_error.public_message(), worker_error.extra_data())
|
|
|
|
|
|
|
|
# Mark the build as completed.
|
|
|
|
if worker_error.is_internal_error():
|
|
|
|
self._build_finished(BUILD_JOB_RESULT.INCOMPLETE)
|
|
|
|
else:
|
|
|
|
self._build_finished(BUILD_JOB_RESULT.ERROR)
|
|
|
|
|
|
|
|
def _build_finished(self, job_status):
|
|
|
|
self.parent_manager.job_completed(self._current_job, job_status, self)
|
|
|
|
self._current_job = None
|
|
|
|
|
|
|
|
# Set the component back to a running state.
|
2014-11-14 19:53:35 +00:00
|
|
|
self._set_status(COMPONENT_STATUS.RUNNING)
|
|
|
|
|
|
|
|
def _ping(self):
|
|
|
|
return 'pong'
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
def _on_ready(self, token):
|
2014-11-14 00:41:17 +00:00
|
|
|
if self._component_status != 'waiting':
|
2014-11-11 23:23:15 +00:00
|
|
|
logger.warning('Build component with token %s is already connected', self.expected_token)
|
|
|
|
return
|
|
|
|
|
|
|
|
if token != self.expected_token:
|
|
|
|
logger.warning('Builder token mismatch. Expected: %s. Found: %s', self.expected_token, token)
|
|
|
|
return
|
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
self._set_status(COMPONENT_STATUS.RUNNING)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
# Start the heartbeat check.
|
|
|
|
loop = trollius.get_event_loop()
|
|
|
|
loop.create_task(self._check_heartbeat(loop))
|
|
|
|
logger.debug('Build worker %s is connected and ready' % self.builder_realm)
|
|
|
|
return True
|
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
def _set_status(self, phase):
|
|
|
|
self._component_status = phase
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
def _on_heartbeat(self):
|
2014-11-14 00:41:17 +00:00
|
|
|
self._last_heartbeat = datetime.datetime.now()
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
def _start_heartbeat_check(self, loop):
|
|
|
|
trollius.set_event_loop(loop)
|
|
|
|
loop.run_until_complete(self._check_heartbeat())
|
|
|
|
|
|
|
|
@trollius.coroutine
|
|
|
|
def _check_heartbeat(self, loop):
|
|
|
|
while True:
|
2014-11-14 19:53:35 +00:00
|
|
|
if (self._component_status != COMPONENT_STATUS.RUNNING and
|
|
|
|
self._component_status != COMPONENT_STATUS.BUILDING):
|
2014-11-11 23:23:15 +00:00
|
|
|
return
|
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
logger.debug('Checking heartbeat on realm %s', self.builder_realm)
|
|
|
|
if not self._last_heartbeat:
|
2014-11-11 23:23:15 +00:00
|
|
|
self._timeout()
|
|
|
|
return
|
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
if self._last_heartbeat < datetime.datetime.now() - HEARTBEAT_DELTA:
|
2014-11-11 23:23:15 +00:00
|
|
|
self._timeout()
|
|
|
|
return
|
|
|
|
|
|
|
|
yield From(trollius.sleep(5))
|
|
|
|
|
|
|
|
def _timeout(self):
|
2014-11-14 19:53:35 +00:00
|
|
|
self._set_status(COMPONENT_STATUS.TIMED_OUT)
|
2014-11-11 23:23:15 +00:00
|
|
|
logger.warning('Build component %s timed out', self.expected_token)
|
|
|
|
self._dispose(timed_out=True)
|
|
|
|
|
|
|
|
def _dispose(self, timed_out=False):
|
|
|
|
# If we still have a running job, then it has not completed and we need to tell the parent
|
|
|
|
# manager.
|
2014-11-14 00:41:17 +00:00
|
|
|
if self._current_job is not None:
|
|
|
|
if timed_out:
|
|
|
|
self._build_status.set_error('Build worker timed out. Build has been requeued')
|
|
|
|
|
|
|
|
self.parent_manager.job_completed(self._current_job, BUILD_JOB_RESULT.INCOMPLETE, self)
|
|
|
|
self._build_status = None
|
|
|
|
self._current_job = None
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
# Unregister the current component so that it cannot be invoked again.
|
|
|
|
self.parent_manager.build_component_disposed(self, timed_out)
|