Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 1 | # Copyright 2013 Rackspace Australia |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | |
| 16 | import copy |
| 17 | import json |
| 18 | import logging |
| 19 | import os |
| 20 | |
Joshua Hesketh | 6224554 | 2014-01-16 18:00:56 +1100 | [diff] [blame] | 21 | from turbo_hipster.lib import common |
Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 22 | from turbo_hipster.lib import utils |
| 23 | |
| 24 | |
| 25 | class Task(object): |
| 26 | |
| 27 | log = logging.getLogger("lib.models.Task") |
| 28 | |
| 29 | def __init__(self, global_config, plugin_config, job_name): |
| 30 | self.global_config = global_config |
| 31 | self.plugin_config = plugin_config |
| 32 | self.job_name = job_name |
| 33 | |
| 34 | self.job = None |
| 35 | self.job_arguments = None |
| 36 | self.work_data = None |
| 37 | self.cancelled = False |
| 38 | |
| 39 | # Define the number of steps we will do to determine our progress. |
| 40 | self.current_step = 0 |
| 41 | self.total_steps = 0 |
| 42 | |
| 43 | def stop_worker(self, number): |
| 44 | # Check the number is for this job instance |
| 45 | # (makes it possible to run multiple workers with this task |
| 46 | # on this server) |
| 47 | if number == self.job.unique: |
| 48 | self.log.debug("We've been asked to stop by our gearman manager") |
| 49 | self.cancelled = True |
| 50 | # TODO: Work out how to kill current step |
| 51 | |
Joshua Hesketh | 6224554 | 2014-01-16 18:00:56 +1100 | [diff] [blame] | 52 | @common.task_step |
Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 53 | def _grab_patchset(self, job_args, job_log_file_path): |
| 54 | """ Checkout the reference into config['git_working_dir'] """ |
| 55 | |
| 56 | self.log.debug("Grab the patchset we want to test against") |
| 57 | local_path = os.path.join(self.global_config['git_working_dir'], |
| 58 | self.job_name, job_args['ZUUL_PROJECT']) |
| 59 | if not os.path.exists(local_path): |
| 60 | os.makedirs(local_path) |
| 61 | |
| 62 | git_args = copy.deepcopy(job_args) |
| 63 | git_args['GIT_ORIGIN'] = 'git://git.openstack.org/' |
| 64 | |
| 65 | cmd = os.path.join(os.path.join(os.path.dirname(__file__), |
| 66 | 'gerrit-git-prep.sh')) |
| 67 | cmd += ' https://review.openstack.org' |
| 68 | cmd += ' http://zuul.rcbops.com' |
| 69 | utils.execute_to_log(cmd, job_log_file_path, env=git_args, |
| 70 | cwd=local_path) |
| 71 | return local_path |
| 72 | |
| 73 | def _get_work_data(self): |
| 74 | if self.work_data is None: |
| 75 | hostname = os.uname()[1] |
| 76 | self.work_data = dict( |
| 77 | name=self.job_name, |
| 78 | number=self.job.unique, |
| 79 | manager='turbo-hipster-manager-%s' % hostname, |
| 80 | url='http://localhost', |
| 81 | ) |
| 82 | return self.work_data |
| 83 | |
| 84 | def _send_work_data(self): |
| 85 | """ Send the WORK DATA in json format for job """ |
| 86 | self.log.debug("Send the work data response: %s" % |
| 87 | json.dumps(self._get_work_data())) |
| 88 | self.job.sendWorkData(json.dumps(self._get_work_data())) |
| 89 | |
| 90 | def _do_next_step(self): |
| 91 | """ Send a WORK_STATUS command to the gearman server. |
| 92 | This can provide a progress bar. """ |
| 93 | |
| 94 | # Each opportunity we should check if we need to stop |
| 95 | if self.cancelled: |
| 96 | self.work_data['result'] = "Failed: Job cancelled" |
| 97 | self.job.sendWorkStatus(self.current_step, self.total_steps) |
| 98 | self.job.sendWorkFail() |
| 99 | raise Exception('Job cancelled') |
| 100 | |
| 101 | self.current_step += 1 |
| 102 | self.job.sendWorkStatus(self.current_step, self.total_steps) |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame^] | 103 | |
| 104 | |
| 105 | class ShellTask(Task): |
| 106 | log = logging.getLogger("lib.models.ShellTask") |
| 107 | |
| 108 | def __init__(self, global_config, plugin_config, job_name): |
| 109 | super(ShellTask, self).__init__(global_config, plugin_config, job_name) |
| 110 | # Define the number of steps we will do to determine our progress. |
| 111 | self.total_steps = 4 |
| 112 | |
| 113 | def start_job(self, job): |
| 114 | self.job = job |
| 115 | self.success = True |
| 116 | self.messages = [] |
| 117 | |
| 118 | if self.job is not None: |
| 119 | try: |
| 120 | self.job_arguments = \ |
| 121 | json.loads(self.job.arguments.decode('utf-8')) |
| 122 | self.log.debug("Got job from ZUUL %s" % self.job_arguments) |
| 123 | |
| 124 | # Send an initial WORK_DATA and WORK_STATUS packets |
| 125 | self._send_work_data() |
| 126 | |
| 127 | # Step 1: Checkout updates from git! |
| 128 | self.git_path = self._grab_patchset( |
| 129 | self.job_arguments, |
| 130 | self.job_datasets[0]['job_log_file_path']) |
| 131 | |
| 132 | # Step 3: execute shell script |
| 133 | # TODO |
| 134 | |
| 135 | # Step 4: Analyse logs for errors |
| 136 | # TODO |
| 137 | |
| 138 | # Step 5: handle the results (and upload etc) |
| 139 | # TODO |
| 140 | |
| 141 | # Finally, send updated work data and completed packets |
| 142 | self._send_work_data() |
| 143 | |
| 144 | if self.work_data['result'] is 'SUCCESS': |
| 145 | self.job.sendWorkComplete( |
| 146 | json.dumps(self._get_work_data())) |
| 147 | else: |
| 148 | self.job.sendWorkFail() |
| 149 | except Exception as e: |
| 150 | self.log.exception('Exception handling log event.') |
| 151 | if not self.cancelled: |
| 152 | self.job.sendWorkException(str(e).encode('utf-8')) |