# Copyright (C) 2014-2016 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import logging import bottle import lorrycontroller class GiveMeJob(lorrycontroller.LorryControllerRoute): http_method = 'POST' path = '/1.0/give-me-job' def run(self, **kwargs): logging.info('%s %s called', self.http_method, self.path) statedb = self.open_statedb() with statedb: if statedb.get_running_queue() and not self.max_jobs_reached(statedb): lorry_infos = statedb.get_all_lorries_info() now = statedb.get_current_time() for lorry_info in lorry_infos: if self.ready_to_run(lorry_info, now): metadata = self.get_repo_metadata(statedb, lorry_info) downstream_type = lorrycontroller.downstream_types[ self.app_settings['git-server-type']] downstream_type(self.app_settings) \ .prepare_repo(lorry_info['path'], metadata) self.give_job_to_minion(statedb, lorry_info, now) logging.info( 'Giving job %s to lorry %s to MINION %s:%s', lorry_info['job_id'], lorry_info['path'], bottle.request.forms.host, bottle.request.forms.pid) return lorry_info logging.info('No job to give MINION') return { 'job_id': None } def max_jobs_reached(self, statedb): max_jobs = statedb.get_max_jobs() if max_jobs is None: return False running_jobs = statedb.get_running_jobs() return len(running_jobs) >= max_jobs def ready_to_run(self, lorry_info, now): due = lorry_info['last_run'] + lorry_info['interval'] return (lorry_info['running_job'] is None and due <= now) def get_repo_metadata(self, statedb, lorry_info): '''Get repository head and description.''' if not lorry_info['from_host']: return {} assert lorry_info['from_path'] try: host_info = statedb.get_host_info(lorry_info['from_host']) except lorrycontroller.HostNotFoundError: # XXX We don't know whether upstream is Trove. It should be # possible to set host type for single repositories. host_info = { 'host': lorry_info['from_host'], 'protocol': 'ssh', 'username': None, 'password': None, 'type': 'trove', 'type_params': {}, } metadata = lorrycontroller.get_upstream_host(host_info) \ .get_repo_metadata(lorry_info['from_path']) if 'description' in metadata: # Prepend Upstream Host name metadata['description'] = '{host}: {desc}'.format( host=lorry_info['from_host'], desc=metadata['description']) return metadata def give_job_to_minion(self, statedb, lorry_info, now): path = lorry_info['path'] minion_host = bottle.request.forms.host minion_pid = bottle.request.forms.pid running_job = statedb.get_next_job_id() statedb.set_running_job(path, running_job) statedb.add_new_job( running_job, minion_host, minion_pid, path, int(now)) lorry_info['job_id'] = running_job return lorry_info