2014-11-11 23:23:15 +00:00
|
|
|
import datetime
|
2014-11-18 21:34:09 +00:00
|
|
|
import time
|
2014-11-11 23:23:15 +00:00
|
|
|
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
|
2015-06-23 21:03:26 +00:00
|
|
|
from trollius import From, Return
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2014-11-18 20:45:56 +00:00
|
|
|
from buildman.server import BuildJobResult
|
2014-11-25 21:14:44 +00:00
|
|
|
from buildman.component.basecomponent import BaseComponent
|
2014-12-31 16:33:56 +00:00
|
|
|
from buildman.jobutil.buildjob import BuildJobLoadException
|
2014-11-25 21:14:44 +00:00
|
|
|
from buildman.jobutil.buildstatus import StatusHandler
|
|
|
|
from buildman.jobutil.workererror import WorkerError
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-09-12 19:30:15 +00:00
|
|
|
from data import model
|
2014-11-14 00:41:17 +00:00
|
|
|
from data.database import BUILD_PHASE
|
2016-08-09 21:58:33 +00:00
|
|
|
from data.model import InvalidRepositoryBuildException
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2016-10-25 16:45:23 +00:00
|
|
|
HEARTBEAT_DELTA = datetime.timedelta(seconds=60)
|
2015-06-26 02:13:01 +00:00
|
|
|
BUILD_HEARTBEAT_DELAY = datetime.timedelta(seconds=30)
|
2014-11-14 20:31:02 +00:00
|
|
|
HEARTBEAT_TIMEOUT = 10
|
2014-11-26 22:02:49 +00:00
|
|
|
INITIAL_TIMEOUT = 25
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-02-11 22:12:53 +00:00
|
|
|
SUPPORTED_WORKER_VERSIONS = ['0.3']
|
2014-12-01 17:11:23 +00:00
|
|
|
|
2014-11-30 22:48:02 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-18 20:45:56 +00:00
|
|
|
class ComponentStatus(object):
|
|
|
|
""" ComponentStatus represents the possible states of a component. """
|
2014-11-14 19:53:35 +00:00
|
|
|
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. """
|
|
|
|
def __init__(self, config, realm=None, token=None, **kwargs):
|
|
|
|
self.expected_token = token
|
|
|
|
self.builder_realm = realm
|
|
|
|
|
2014-11-18 21:34:09 +00:00
|
|
|
self.parent_manager = None
|
2014-12-16 18:41:30 +00:00
|
|
|
self.registry_hostname = None
|
2014-11-18 21:34:09 +00:00
|
|
|
|
|
|
|
self._component_status = ComponentStatus.JOINING
|
|
|
|
self._last_heartbeat = None
|
|
|
|
self._current_job = None
|
|
|
|
self._build_status = None
|
|
|
|
self._image_info = None
|
2015-02-04 02:05:18 +00:00
|
|
|
self._worker_version = None
|
2014-11-18 21:34:09 +00:00
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
BaseComponent.__init__(self, config, **kwargs)
|
|
|
|
|
2015-01-28 22:12:33 +00:00
|
|
|
def kind(self):
|
|
|
|
return 'builder'
|
|
|
|
|
2014-11-18 20:45:56 +00:00
|
|
|
def onConnect(self):
|
2014-11-11 23:23:15 +00:00
|
|
|
self.join(self.builder_realm)
|
|
|
|
|
2015-05-22 19:24:14 +00:00
|
|
|
@trollius.coroutine
|
2014-11-11 23:23:15 +00:00
|
|
|
def onJoin(self, details):
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.debug('Registering methods and listeners for component %s', self.builder_realm)
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self.register(self._on_ready, u'io.quay.buildworker.ready'))
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self.register(self._determine_cache_tag, u'io.quay.buildworker.determinecachetag'))
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self.register(self._ping, u'io.quay.buildworker.ping'))
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self.register(self._on_log_message, u'io.quay.builder.logmessagesynchronously'))
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self.subscribe(self._on_heartbeat, u'io.quay.builder.heartbeat'))
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self._set_status(ComponentStatus.WAITING))
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-12-31 16:33:56 +00:00
|
|
|
@trollius.coroutine
|
2014-11-18 20:45:56 +00:00
|
|
|
def start_build(self, build_job):
|
|
|
|
""" Starts a build. """
|
2016-08-16 21:18:57 +00:00
|
|
|
if self._component_status not in (ComponentStatus.WAITING, ComponentStatus.RUNNING):
|
2016-08-16 20:38:48 +00:00
|
|
|
logger.debug('Could not start build for component %s (build %s, worker version: %s): %s',
|
|
|
|
self.builder_realm, build_job.repo_build.uuid, self._worker_version,
|
|
|
|
self._component_status)
|
|
|
|
raise Return()
|
|
|
|
|
2015-06-26 02:13:01 +00:00
|
|
|
logger.debug('Starting build for component %s (build %s, worker version: %s)',
|
|
|
|
self.builder_realm, build_job.repo_build.uuid, self._worker_version)
|
2015-02-04 02:05:18 +00:00
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
self._current_job = build_job
|
2014-12-23 16:17:23 +00:00
|
|
|
self._build_status = StatusHandler(self.build_logs, build_job.repo_build.uuid)
|
2014-11-14 00:41:17 +00:00
|
|
|
self._image_info = {}
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self._set_status(ComponentStatus.BUILDING))
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-02-03 18:01:42 +00:00
|
|
|
# Send the notification that the build has started.
|
|
|
|
build_job.send_notification('build_start')
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-02-04 02:05:18 +00:00
|
|
|
# Parse the build configuration.
|
2014-11-11 23:23:15 +00:00
|
|
|
try:
|
2014-12-31 16:33:56 +00:00
|
|
|
build_config = build_job.build_config
|
|
|
|
except BuildJobLoadException as irbe:
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_failure('Could not load build job information', irbe))
|
2016-06-17 22:03:27 +00:00
|
|
|
raise Return()
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-02-04 02:05:18 +00:00
|
|
|
base_image_information = {}
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
# Add the pull robot information, if any.
|
2015-01-29 23:01:42 +00:00
|
|
|
if build_job.pull_credentials:
|
|
|
|
base_image_information['username'] = build_job.pull_credentials.get('username', '')
|
|
|
|
base_image_information['password'] = build_job.pull_credentials.get('password', '')
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-20 19:36:22 +00:00
|
|
|
# Retrieve the repository's fully qualified name.
|
2014-12-16 18:41:30 +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.
|
2014-11-18 20:45:56 +00:00
|
|
|
# build_package: URL to the build package to download and untar/unzip.
|
2015-04-21 22:04:25 +00:00
|
|
|
# defaults to empty string to avoid requiring a pointer on the builder.
|
2014-11-18 20:45:56 +00:00
|
|
|
# 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 (e.g. '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.
|
|
|
|
# username: The username for pulling the base image (if any).
|
|
|
|
# password: The password for pulling the base image (if any).
|
2014-11-11 23:23:15 +00:00
|
|
|
build_arguments = {
|
2015-08-14 21:22:19 +00:00
|
|
|
'build_package': build_job.get_build_package_url(self.user_files),
|
2015-04-21 22:04:25 +00:00
|
|
|
'sub_directory': build_config.get('build_subdir', ''),
|
|
|
|
'repository': repository_name,
|
|
|
|
'registry': self.registry_hostname,
|
|
|
|
'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,
|
2014-11-11 23:23:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:09:27 +00:00
|
|
|
# If the trigger has a private key, it's using git, thus we should add
|
|
|
|
# git data to the build args.
|
2015-03-23 16:14:47 +00:00
|
|
|
# url: url used to clone the git repository
|
|
|
|
# sha: the sha1 identifier of the commit to check out
|
|
|
|
# private_key: the key used to get read access to the git repository
|
2015-03-19 22:09:27 +00:00
|
|
|
if build_job.repo_build.trigger.private_key is not None:
|
|
|
|
build_arguments['git'] = {
|
2015-04-21 22:04:25 +00:00
|
|
|
'url': build_config['trigger_metadata'].get('git_url', ''),
|
2015-06-02 19:43:55 +00:00
|
|
|
'sha': BuildComponent._commit_sha(build_config),
|
2015-03-19 22:09:27 +00:00
|
|
|
'private_key': build_job.repo_build.trigger.private_key,
|
|
|
|
}
|
|
|
|
|
2016-06-17 22:03:27 +00:00
|
|
|
# If the build args have no buildpack, mark it as a failure before sending
|
|
|
|
# it to a builder instance.
|
|
|
|
if not build_arguments['build_package'] and not build_arguments['git']:
|
|
|
|
logger.error('%s: insufficient build args: %s',
|
|
|
|
self._current_job.repo_build.uuid, build_arguments)
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_failure('Insufficient build arguments. No buildpack available.'))
|
2016-06-17 22:03:27 +00:00
|
|
|
raise Return()
|
|
|
|
|
2014-11-12 19:03:07 +00:00
|
|
|
# Invoke the build.
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.debug('Invoking build: %s', self.builder_realm)
|
|
|
|
logger.debug('With Arguments: %s', build_arguments)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2016-11-22 21:43:32 +00:00
|
|
|
def build_complete_callback(result):
|
|
|
|
""" This function is used to execute a coroutine as the callback. """
|
2016-11-22 21:46:19 +00:00
|
|
|
trollius.ensure_future(self._build_complete(result))
|
2016-11-22 21:43:32 +00:00
|
|
|
|
|
|
|
self.call("io.quay.builder.build", **build_arguments).add_done_callback(build_complete_callback)
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2015-06-26 02:13:01 +00:00
|
|
|
# Set the heartbeat for the future. If the builder never receives the build call,
|
|
|
|
# then this will cause a timeout after 30 seconds. We know the builder has registered
|
|
|
|
# by this point, so it makes sense to have a timeout.
|
|
|
|
self._last_heartbeat = datetime.datetime.utcnow() + BUILD_HEARTBEAT_DELAY
|
|
|
|
|
2015-06-02 19:43:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def _commit_sha(build_config):
|
|
|
|
""" Determines whether the metadata is using an old schema or not and returns the commit. """
|
|
|
|
commit_sha = build_config['trigger_metadata'].get('commit', '')
|
|
|
|
old_commit_sha = build_config['trigger_metadata'].get('commit_sha', '')
|
|
|
|
return commit_sha or old_commit_sha
|
|
|
|
|
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
@staticmethod
|
2014-11-18 20:45:56 +00:00
|
|
|
def _total_completion(statuses, total_images):
|
|
|
|
""" Returns the current amount completion relative to the total completion of a build. """
|
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
|
2014-11-18 20:45:56 +00:00
|
|
|
def _process_pushpull_status(status_dict, current_phase, docker_data, images):
|
2014-11-20 19:36:22 +00:00
|
|
|
""" Processes the status of a push or pull by updating the provided status_dict and images. """
|
2014-11-14 00:41:17 +00:00
|
|
|
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] = \
|
2014-11-18 20:45:56 +00:00
|
|
|
BuildComponent._total_completion(images, max(len(images), num_images))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
|
|
|
|
@trollius.coroutine
|
2014-11-14 19:53:35 +00:00
|
|
|
def _on_log_message(self, phase, json_data):
|
2014-11-20 19:36:22 +00:00
|
|
|
""" Tails log messages and updates the build status. """
|
2015-06-17 18:11:22 +00:00
|
|
|
# Update the heartbeat.
|
|
|
|
self._last_heartbeat = datetime.datetime.utcnow()
|
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
# Parse any of the JSON data logged.
|
2015-06-30 09:59:24 +00:00
|
|
|
log_data = {}
|
2014-11-14 00:41:17 +00:00
|
|
|
if json_data:
|
|
|
|
try:
|
2015-06-30 09:59:24 +00:00
|
|
|
log_data = json.loads(json_data)
|
2014-11-14 00:41:17 +00:00
|
|
|
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:
|
2015-06-30 09:59:24 +00:00
|
|
|
if key in log_data:
|
|
|
|
fully_unwrapped = log_data[key]
|
2014-11-14 00:41:17 +00:00
|
|
|
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:
|
2016-08-09 21:58:33 +00:00
|
|
|
try:
|
2016-11-18 05:04:50 +00:00
|
|
|
changed_phase = yield From(self._build_status.set_phase(phase, log_data.get('status_data')))
|
|
|
|
if changed_phase:
|
2016-08-09 21:58:33 +00:00
|
|
|
logger.debug('Build %s has entered a new phase: %s', self.builder_realm, phase)
|
2016-10-27 17:18:02 +00:00
|
|
|
elif self._current_job.repo_build.phase == BUILD_PHASE.CANCELLED:
|
|
|
|
build_id = self._current_job.repo_build.uuid
|
|
|
|
logger.debug('Trying to move cancelled build into phase: %s with id: %s', phase, build_id)
|
2016-11-18 05:04:50 +00:00
|
|
|
raise Return(False)
|
2016-08-09 21:58:33 +00:00
|
|
|
except InvalidRepositoryBuildException:
|
|
|
|
build_id = self._current_job.repo_build.uuid
|
|
|
|
logger.info('Build %s was not found; repo was probably deleted', build_id)
|
2016-11-18 05:04:50 +00:00
|
|
|
raise Return(False)
|
2014-11-18 21:34:09 +00:00
|
|
|
|
2015-06-30 09:59:24 +00:00
|
|
|
BuildComponent._process_pushpull_status(status_dict, phase, log_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.
|
2015-06-30 09:59:24 +00:00
|
|
|
if 'error' in log_data:
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_status.set_error(log_data['error']))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2014-11-14 19:53:35 +00:00
|
|
|
if current_step is not None:
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_status.set_command(current_status_string))
|
2014-11-14 19:53:35 +00:00
|
|
|
elif phase == BUILD_PHASE.BUILDING:
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_status.append_log(current_status_string))
|
|
|
|
raise Return(True)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-02-09 19:53:18 +00:00
|
|
|
@trollius.coroutine
|
2015-02-09 19:12:24 +00:00
|
|
|
def _determine_cache_tag(self, command_comments, base_image_name, base_image_tag, base_image_id):
|
2015-02-09 17:13:40 +00:00
|
|
|
with self._build_status as status_dict:
|
2015-02-09 19:12:24 +00:00
|
|
|
status_dict['total_commands'] = len(command_comments) + 1
|
2015-02-09 17:13:40 +00:00
|
|
|
|
|
|
|
logger.debug('Checking cache on realm %s. Base image: %s:%s (%s)', self.builder_realm,
|
|
|
|
base_image_name, base_image_tag, base_image_id)
|
|
|
|
|
2015-02-09 19:53:18 +00:00
|
|
|
tag_found = self._current_job.determine_cached_tag(base_image_id, command_comments)
|
2015-06-23 21:03:26 +00:00
|
|
|
raise Return(tag_found or '')
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
@trollius.coroutine
|
2014-11-11 23:23:15 +00:00
|
|
|
def _build_failure(self, error_message, exception=None):
|
2014-11-18 20:45:56 +00:00
|
|
|
""" Handles and logs a failed build. """
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_status.set_error(error_message, {
|
2016-06-17 22:03:27 +00:00
|
|
|
'internal_error': str(exception) if exception else None
|
2016-11-18 05:04:50 +00:00
|
|
|
}))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2014-12-16 18:41:30 +00:00
|
|
|
build_id = self._current_job.repo_build.uuid
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.warning('Build %s failed with message: %s', build_id, error_message)
|
2014-11-14 00:41:17 +00:00
|
|
|
|
|
|
|
# Mark that the build has finished (in an error state)
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_finished(BuildJobResult.ERROR))
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
@trollius.coroutine
|
2014-11-11 23:23:15 +00:00
|
|
|
def _build_complete(self, result):
|
2014-11-18 20:45:56 +00:00
|
|
|
""" Wraps up a completed build. Handles any errors and calls self._build_finished. """
|
2016-09-12 19:30:15 +00:00
|
|
|
build_id = self._current_job.repo_build.uuid
|
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
try:
|
2014-11-14 00:41:17 +00:00
|
|
|
# Retrieve the result. This will raise an ApplicationError on any error that occurred.
|
2015-02-24 20:13:51 +00:00
|
|
|
result_value = result.result()
|
|
|
|
kwargs = {}
|
|
|
|
|
|
|
|
# Note: If we are hitting an older builder that didn't return ANY map data, then the result
|
|
|
|
# value will be a bool instead of a proper CallResult object (because autobahn sucks).
|
|
|
|
# Therefore: we have a try-except guard here to ensure we don't hit this pitfall.
|
|
|
|
try:
|
|
|
|
kwargs = result_value.kwresults
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
try:
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_status.set_phase(BUILD_PHASE.COMPLETE))
|
2016-08-09 21:58:33 +00:00
|
|
|
except InvalidRepositoryBuildException:
|
|
|
|
logger.info('Build %s was not found; repo was probably deleted', build_id)
|
2016-11-18 05:04:50 +00:00
|
|
|
raise Return()
|
2016-08-09 21:58:33 +00:00
|
|
|
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_finished(BuildJobResult.COMPLETE))
|
2015-02-03 18:01:42 +00:00
|
|
|
|
2016-09-12 19:30:15 +00:00
|
|
|
# Label the pushed manifests with the build metadata.
|
|
|
|
manifest_digests = kwargs.get('digests') or []
|
|
|
|
for digest in manifest_digests:
|
|
|
|
try:
|
|
|
|
manifest = model.tag.load_manifest_by_digest(self._current_job.namespace,
|
|
|
|
self._current_job.repo_name, digest)
|
|
|
|
model.label.create_manifest_label(manifest, model.label.INTERNAL_LABEL_BUILD_UUID,
|
|
|
|
build_id, 'internal', 'text/plain')
|
|
|
|
except model.InvalidManifestException:
|
|
|
|
logger.debug('Could not find built manifest with digest %s under repo %s/%s for build %s',
|
|
|
|
digest, self._current_job.namespace, self._current_job.repo_name,
|
|
|
|
build_id)
|
|
|
|
continue
|
|
|
|
|
2015-02-03 18:01:42 +00:00
|
|
|
# Send the notification that the build has completed successfully.
|
2016-09-12 19:30:15 +00:00
|
|
|
self._current_job.send_notification('build_success',
|
|
|
|
image_id=kwargs.get('image_id'),
|
|
|
|
manifest_digests=manifest_digests)
|
2014-11-18 20:45:56 +00:00
|
|
|
except ApplicationError as aex:
|
|
|
|
worker_error = WorkerError(aex.error, aex.kwargs.get('base_error'))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
|
|
|
# Write the error to the log.
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_status.set_error(worker_error.public_message(),
|
|
|
|
worker_error.extra_data(),
|
|
|
|
internal_error=worker_error.is_internal_error(),
|
|
|
|
requeued=self._current_job.has_retries_remaining()))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2015-02-03 18:01:42 +00:00
|
|
|
# Send the notification that the build has failed.
|
2015-02-03 18:08:38 +00:00
|
|
|
self._current_job.send_notification('build_failure',
|
|
|
|
error_message=worker_error.public_message())
|
2014-11-14 00:41:17 +00:00
|
|
|
|
|
|
|
# Mark the build as completed.
|
|
|
|
if worker_error.is_internal_error():
|
2016-10-31 17:45:46 +00:00
|
|
|
logger.exception('[BUILD INTERNAL ERROR: Remote] Build ID: %s: %s', build_id,
|
|
|
|
worker_error.public_message())
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_finished(BuildJobResult.INCOMPLETE))
|
2014-11-14 00:41:17 +00:00
|
|
|
else:
|
2015-11-30 19:30:55 +00:00
|
|
|
logger.debug('Got remote failure exception for build %s: %s', build_id, aex)
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_finished(BuildJobResult.ERROR))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-11-28 18:29:04 +00:00
|
|
|
# Remove the current job.
|
|
|
|
self._current_job = None
|
|
|
|
|
|
|
|
|
2014-12-22 17:14:16 +00:00
|
|
|
@trollius.coroutine
|
2014-11-14 00:41:17 +00:00
|
|
|
def _build_finished(self, job_status):
|
2014-11-18 20:45:56 +00:00
|
|
|
""" Alerts the parent that a build has completed and sets the status back to running. """
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self.parent_manager.job_completed(self._current_job, job_status, self))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
|
|
|
# Set the component back to a running state.
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self._set_status(ComponentStatus.RUNNING))
|
2014-11-14 19:53:35 +00:00
|
|
|
|
2014-11-20 21:06:23 +00:00
|
|
|
@staticmethod
|
2014-11-18 20:45:56 +00:00
|
|
|
def _ping():
|
|
|
|
""" Ping pong. """
|
2014-11-14 19:53:35 +00:00
|
|
|
return 'pong'
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-12-31 16:33:56 +00:00
|
|
|
@trollius.coroutine
|
2014-12-01 17:11:23 +00:00
|
|
|
def _on_ready(self, token, version):
|
2015-05-22 19:24:14 +00:00
|
|
|
logger.debug('On ready called (token "%s")', token)
|
2015-02-04 02:05:18 +00:00
|
|
|
self._worker_version = version
|
|
|
|
|
2014-12-01 17:11:23 +00:00
|
|
|
if not version in SUPPORTED_WORKER_VERSIONS:
|
2014-12-31 16:33:56 +00:00
|
|
|
logger.warning('Build component (token "%s") is running an out-of-date version: %s', token,
|
|
|
|
version)
|
2015-06-23 21:03:26 +00:00
|
|
|
raise Return(False)
|
2014-12-01 17:11:23 +00:00
|
|
|
|
2015-02-02 17:24:32 +00:00
|
|
|
if self._component_status != ComponentStatus.WAITING:
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.warning('Build component (token "%s") is already connected', self.expected_token)
|
2015-06-23 21:03:26 +00:00
|
|
|
raise Return(False)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
if token != self.expected_token:
|
2014-12-31 16:33:56 +00:00
|
|
|
logger.warning('Builder token mismatch. Expected: "%s". Found: "%s"', self.expected_token,
|
|
|
|
token)
|
2015-06-23 21:03:26 +00:00
|
|
|
raise Return(False)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self._set_status(ComponentStatus.RUNNING))
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-14 20:31:02 +00:00
|
|
|
# Start the heartbeat check and updating loop.
|
2014-11-11 23:23:15 +00:00
|
|
|
loop = trollius.get_event_loop()
|
2014-11-18 20:45:56 +00:00
|
|
|
loop.create_task(self._heartbeat())
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.debug('Build worker %s is connected and ready', self.builder_realm)
|
2015-06-23 21:03:26 +00:00
|
|
|
raise Return(True)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-12-31 16:33:56 +00:00
|
|
|
@trollius.coroutine
|
2014-11-14 00:41:17 +00:00
|
|
|
def _set_status(self, phase):
|
2014-12-16 18:41:30 +00:00
|
|
|
if phase == ComponentStatus.RUNNING:
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self.parent_manager.build_component_ready(self))
|
2014-12-16 18:41:30 +00:00
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
self._component_status = phase
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
def _on_heartbeat(self):
|
2014-11-18 20:45:56 +00:00
|
|
|
""" Updates the last known heartbeat. """
|
2015-06-22 22:12:20 +00:00
|
|
|
if self._component_status == ComponentStatus.TIMED_OUT:
|
2015-05-22 19:24:14 +00:00
|
|
|
return
|
|
|
|
|
2016-08-30 17:38:22 +00:00
|
|
|
logger.debug('Got heartbeat on realm %s', self.builder_realm)
|
2014-12-22 17:14:16 +00:00
|
|
|
self._last_heartbeat = datetime.datetime.utcnow()
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
@trollius.coroutine
|
2014-11-18 20:45:56 +00:00
|
|
|
def _heartbeat(self):
|
2014-11-14 20:31:02 +00:00
|
|
|
""" Coroutine that runs every HEARTBEAT_TIMEOUT seconds, both checking the worker's heartbeat
|
|
|
|
and updating the heartbeat in the build status dictionary (if applicable). This allows
|
|
|
|
the build system to catch crashes from either end.
|
|
|
|
"""
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(trollius.sleep(INITIAL_TIMEOUT))
|
2014-11-26 22:02:49 +00:00
|
|
|
|
2014-11-11 23:23:15 +00:00
|
|
|
while True:
|
2014-11-14 20:31:02 +00:00
|
|
|
# If the component is no longer running or actively building, nothing more to do.
|
2014-11-18 20:45:56 +00:00
|
|
|
if (self._component_status != ComponentStatus.RUNNING and
|
|
|
|
self._component_status != ComponentStatus.BUILDING):
|
2015-06-23 21:03:26 +00:00
|
|
|
raise Return()
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-11-14 20:31:02 +00:00
|
|
|
# If there is an active build, write the heartbeat to its status.
|
2016-11-18 05:04:20 +00:00
|
|
|
if self._build_status is not None:
|
|
|
|
with self._build_status as status_dict:
|
2014-11-18 21:34:09 +00:00
|
|
|
status_dict['heartbeat'] = int(time.time())
|
2014-11-14 20:31:02 +00:00
|
|
|
|
2014-11-21 19:27:06 +00:00
|
|
|
# Mark the build item.
|
|
|
|
current_job = self._current_job
|
|
|
|
if current_job is not None:
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self.parent_manager.job_heartbeat(current_job))
|
2014-11-21 19:27:06 +00:00
|
|
|
|
2014-11-14 20:31:02 +00:00
|
|
|
# Check the heartbeat from the worker.
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.debug('Checking heartbeat on realm %s', self.builder_realm)
|
2014-12-22 17:14:16 +00:00
|
|
|
if (self._last_heartbeat and
|
|
|
|
self._last_heartbeat < datetime.datetime.utcnow() - HEARTBEAT_DELTA):
|
2015-05-22 19:24:14 +00:00
|
|
|
logger.debug('Heartbeat on realm %s has expired: %s', self.builder_realm,
|
|
|
|
self._last_heartbeat)
|
|
|
|
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self._timeout())
|
|
|
|
raise Return()
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2015-06-26 02:13:01 +00:00
|
|
|
logger.debug('Heartbeat on realm %s is valid: %s (%s).', self.builder_realm,
|
|
|
|
self._last_heartbeat, self._component_status)
|
2015-05-22 19:24:14 +00:00
|
|
|
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(trollius.sleep(HEARTBEAT_TIMEOUT))
|
2014-11-11 23:23:15 +00:00
|
|
|
|
2014-12-31 16:33:56 +00:00
|
|
|
@trollius.coroutine
|
2014-11-11 23:23:15 +00:00
|
|
|
def _timeout(self):
|
2015-01-29 23:21:32 +00:00
|
|
|
if self._component_status == ComponentStatus.TIMED_OUT:
|
2015-06-23 21:03:26 +00:00
|
|
|
raise Return()
|
2015-01-29 23:21:32 +00:00
|
|
|
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self._set_status(ComponentStatus.TIMED_OUT))
|
2014-11-30 22:48:02 +00:00
|
|
|
logger.warning('Build component with realm %s has timed out', self.builder_realm)
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
# 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:
|
2016-11-18 05:04:50 +00:00
|
|
|
yield From(self._build_status.set_error('Build worker timed out', internal_error=True,
|
|
|
|
requeued=self._current_job.has_retries_remaining()))
|
2014-11-14 00:41:17 +00:00
|
|
|
|
2016-10-31 17:45:46 +00:00
|
|
|
build_id = self._current_job.build_uuid
|
|
|
|
logger.error('[BUILD INTERNAL ERROR: Timeout] Build ID: %s', build_id)
|
2015-06-23 21:03:26 +00:00
|
|
|
yield From(self.parent_manager.job_completed(self._current_job,
|
2016-09-30 12:54:49 +00:00
|
|
|
BuildJobResult.INCOMPLETE,
|
|
|
|
self))
|
2014-11-11 23:23:15 +00:00
|
|
|
|
|
|
|
# Unregister the current component so that it cannot be invoked again.
|
2015-01-29 23:13:31 +00:00
|
|
|
self.parent_manager.build_component_disposed(self, True)
|
2016-11-28 18:29:04 +00:00
|
|
|
|
|
|
|
# Remove the job reference.
|
|
|
|
self._current_job = None
|
2016-12-05 19:56:18 +00:00
|
|
|
|
|
|
|
@trollius.coroutine
|
|
|
|
def cancel_build(self):
|
|
|
|
self.parent_manager.build_component_disposed(self, True)
|
|
|
|
self._current_job = None
|
|
|
|
yield From(self._set_status(ComponentStatus.RUNNING))
|