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 |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 33 | self._reset() |
Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 34 | |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 35 | # Define the number of steps we will do to determine our progress. |
| 36 | self.total_steps = 0 |
| 37 | |
| 38 | def _reset(self): |
Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 39 | self.job = None |
| 40 | self.job_arguments = None |
| 41 | self.work_data = None |
| 42 | self.cancelled = False |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 43 | self.success = True |
| 44 | self.messages = [] |
Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 45 | self.current_step = 0 |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 46 | |
| 47 | def start_job(self, job): |
| 48 | self._reset() |
| 49 | self.job = job |
| 50 | |
| 51 | if self.job is not None: |
| 52 | try: |
| 53 | self.job_arguments = \ |
| 54 | json.loads(self.job.arguments.decode('utf-8')) |
| 55 | self.log.debug("Got job from ZUUL %s" % self.job_arguments) |
| 56 | |
| 57 | # Send an initial WORK_DATA and WORK_STATUS packets |
| 58 | self._send_work_data() |
| 59 | |
| 60 | # Execute the job_steps |
| 61 | self.do_job_steps() |
| 62 | |
| 63 | # Finally, send updated work data and completed packets |
| 64 | self._send_final_results() |
| 65 | |
| 66 | except Exception as e: |
| 67 | self.log.exception('Exception handling log event.') |
| 68 | if not self.cancelled: |
| 69 | self.job.sendWorkException(str(e).encode('utf-8')) |
Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 70 | |
| 71 | def stop_worker(self, number): |
| 72 | # Check the number is for this job instance |
| 73 | # (makes it possible to run multiple workers with this task |
| 74 | # on this server) |
| 75 | if number == self.job.unique: |
| 76 | self.log.debug("We've been asked to stop by our gearman manager") |
| 77 | self.cancelled = True |
| 78 | # TODO: Work out how to kill current step |
| 79 | |
Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 80 | def _get_work_data(self): |
| 81 | if self.work_data is None: |
| 82 | hostname = os.uname()[1] |
| 83 | self.work_data = dict( |
| 84 | name=self.job_name, |
| 85 | number=self.job.unique, |
| 86 | manager='turbo-hipster-manager-%s' % hostname, |
| 87 | url='http://localhost', |
| 88 | ) |
| 89 | return self.work_data |
| 90 | |
| 91 | def _send_work_data(self): |
| 92 | """ Send the WORK DATA in json format for job """ |
| 93 | self.log.debug("Send the work data response: %s" % |
| 94 | json.dumps(self._get_work_data())) |
| 95 | self.job.sendWorkData(json.dumps(self._get_work_data())) |
| 96 | |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 97 | def _send_final_results(self): |
| 98 | self._send_work_data() |
| 99 | |
Joshua Hesketh | b5f99b6 | 2014-01-30 16:03:19 +1100 | [diff] [blame] | 100 | if self.success: |
Joshua Hesketh | 48de067 | 2014-01-30 16:33:22 +1100 | [diff] [blame] | 101 | self.work_data['result'] = 'SUCCESS' |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 102 | self.job.sendWorkComplete( |
| 103 | json.dumps(self._get_work_data())) |
| 104 | else: |
Joshua Hesketh | 48de067 | 2014-01-30 16:33:22 +1100 | [diff] [blame] | 105 | self.work_data['result'] = '\n'.join(self.messages) |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 106 | self.job.sendWorkFail() |
| 107 | |
Joshua Hesketh | e76a0dd | 2014-01-16 17:57:45 +1100 | [diff] [blame] | 108 | def _do_next_step(self): |
| 109 | """ Send a WORK_STATUS command to the gearman server. |
| 110 | This can provide a progress bar. """ |
| 111 | |
| 112 | # Each opportunity we should check if we need to stop |
| 113 | if self.cancelled: |
| 114 | self.work_data['result'] = "Failed: Job cancelled" |
| 115 | self.job.sendWorkStatus(self.current_step, self.total_steps) |
| 116 | self.job.sendWorkFail() |
| 117 | raise Exception('Job cancelled') |
| 118 | |
| 119 | self.current_step += 1 |
| 120 | self.job.sendWorkStatus(self.current_step, self.total_steps) |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 121 | |
| 122 | |
| 123 | class ShellTask(Task): |
| 124 | log = logging.getLogger("lib.models.ShellTask") |
| 125 | |
| 126 | def __init__(self, global_config, plugin_config, job_name): |
| 127 | super(ShellTask, self).__init__(global_config, plugin_config, job_name) |
| 128 | # Define the number of steps we will do to determine our progress. |
Joshua Hesketh | c73328c | 2014-01-18 16:09:54 +1100 | [diff] [blame] | 129 | self.total_steps = 5 |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 130 | |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 131 | def _reset(self): |
| 132 | super(ShellTask, self)._reset() |
| 133 | self.git_path = None |
Joshua Hesketh | c73328c | 2014-01-18 16:09:54 +1100 | [diff] [blame] | 134 | self.job_working_dir = None |
| 135 | self.shell_output_log = None |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 136 | |
Joshua Hesketh | 235b13d | 2014-01-30 15:14:04 +1100 | [diff] [blame] | 137 | def do_job_steps(self): |
Joshua Hesketh | 1f2d1a2 | 2014-01-30 15:41:21 +1100 | [diff] [blame] | 138 | # Step 1: Prep job working dir |
Joshua Hesketh | c73328c | 2014-01-18 16:09:54 +1100 | [diff] [blame] | 139 | self._prep_working_dir() |
| 140 | |
Joshua Hesketh | 1f2d1a2 | 2014-01-30 15:41:21 +1100 | [diff] [blame] | 141 | # Step 2: Checkout updates from git |
| 142 | self._grab_patchset(self.job_arguments) |
| 143 | |
Joshua Hesketh | c73328c | 2014-01-18 16:09:54 +1100 | [diff] [blame] | 144 | # Step 3: Run shell script |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 145 | self._execute_script() |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 146 | |
Joshua Hesketh | c73328c | 2014-01-18 16:09:54 +1100 | [diff] [blame] | 147 | # Step 4: Analyse logs for errors |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 148 | self._parse_and_check_results() |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 149 | |
Joshua Hesketh | c73328c | 2014-01-18 16:09:54 +1100 | [diff] [blame] | 150 | # Step 5: handle the results (and upload etc) |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 151 | self._handle_results() |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 152 | |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 153 | @common.task_step |
Joshua Hesketh | 1f2d1a2 | 2014-01-30 15:41:21 +1100 | [diff] [blame] | 154 | def _prep_working_dir(self): |
Joshua Hesketh | 9900554 | 2014-01-30 16:34:36 +1100 | [diff] [blame^] | 155 | self.job_identifier = utils.determine_job_identifier( |
| 156 | self.job_arguments, |
| 157 | self.plugin_config['function'], |
| 158 | self.job.unique |
| 159 | ) |
Joshua Hesketh | 1f2d1a2 | 2014-01-30 15:41:21 +1100 | [diff] [blame] | 160 | self.job_working_dir = os.path.join( |
| 161 | self.global_config['jobs_working_dir'], |
Joshua Hesketh | 9900554 | 2014-01-30 16:34:36 +1100 | [diff] [blame^] | 162 | self.job_identifier |
Joshua Hesketh | 1f2d1a2 | 2014-01-30 15:41:21 +1100 | [diff] [blame] | 163 | ) |
| 164 | self.shell_output_log = os.path.join( |
| 165 | self.job_working_dir, |
| 166 | 'shell_output.log' |
| 167 | ) |
| 168 | |
| 169 | if not os.path.isdir(os.path.dirname(self.shell_output_log)): |
| 170 | os.makedirs(os.path.dirname(self.shell_output_log)) |
| 171 | |
| 172 | @common.task_step |
| 173 | def _grab_patchset(self, job_args): |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 174 | """ Checkout the reference into config['git_working_dir'] """ |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 175 | |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 176 | self.log.debug("Grab the patchset we want to test against") |
| 177 | local_path = os.path.join(self.global_config['git_working_dir'], |
| 178 | self.job_name, job_args['ZUUL_PROJECT']) |
| 179 | if not os.path.exists(local_path): |
| 180 | os.makedirs(local_path) |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 181 | |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 182 | git_args = copy.deepcopy(job_args) |
| 183 | git_args['GIT_ORIGIN'] = 'git://git.openstack.org/' |
Joshua Hesketh | 9177876 | 2014-01-16 18:24:46 +1100 | [diff] [blame] | 184 | |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 185 | cmd = os.path.join(os.path.join(os.path.dirname(__file__), |
| 186 | 'gerrit-git-prep.sh')) |
| 187 | cmd += ' https://review.openstack.org' |
| 188 | cmd += ' http://zuul.rcbops.com' |
Joshua Hesketh | 1f2d1a2 | 2014-01-30 15:41:21 +1100 | [diff] [blame] | 189 | utils.execute_to_log(cmd, self.shell_output_log, env=git_args, |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 190 | cwd=local_path) |
| 191 | self.git_path = local_path |
| 192 | return local_path |
| 193 | |
| 194 | @common.task_step |
| 195 | def _execute_script(self): |
| 196 | # Run script |
Joshua Hesketh | c73328c | 2014-01-18 16:09:54 +1100 | [diff] [blame] | 197 | cmd = self.plugin_config['shell_script'] |
| 198 | cmd += ( |
| 199 | (' %(git_path)s %(job_working_dir)s %(unique_id)s') |
| 200 | % { |
| 201 | 'git_path': self.git_path, |
| 202 | 'job_working_dir': self.job_working_dir, |
| 203 | 'unique_id': self.job.unique |
| 204 | } |
| 205 | ) |
| 206 | self.script_return_code = utils.execute_to_log( |
| 207 | cmd, |
| 208 | self.shell_output_log |
| 209 | ) |
Joshua Hesketh | 81f87ed | 2014-01-18 15:24:48 +1100 | [diff] [blame] | 210 | |
| 211 | @common.task_step |
| 212 | def _parse_and_check_results(self): |
| 213 | if self.script_return_code > 0: |
| 214 | self.success = False |
| 215 | self.messages.append('Return code from test script was non-zero ' |
| 216 | '(%d)' % self.script_return_code) |
| 217 | |
| 218 | @common.task_step |
| 219 | def _handle_results(self): |
Joshua Hesketh | 6055f31 | 2014-01-22 15:04:54 +1100 | [diff] [blame] | 220 | """Upload the contents of the working dir either using the instructions |
| 221 | provided by zuul and/or our configuration""" |
Joshua Hesketh | 221ae74 | 2014-01-22 16:09:58 +1100 | [diff] [blame] | 222 | |
Joshua Hesketh | 6055f31 | 2014-01-22 15:04:54 +1100 | [diff] [blame] | 223 | self.log.debug("Process the resulting files (upload/push)") |
Joshua Hesketh | 221ae74 | 2014-01-22 16:09:58 +1100 | [diff] [blame] | 224 | |
| 225 | if 'publish_logs' in self.global_config: |
Joshua Hesketh | 9900554 | 2014-01-30 16:34:36 +1100 | [diff] [blame^] | 226 | index_url = utils.push_file(self.job_identifier, |
Joshua Hesketh | 221ae74 | 2014-01-22 16:09:58 +1100 | [diff] [blame] | 227 | self.job_working_dir, |
| 228 | self.global_config['publish_logs']) |
| 229 | self.log.debug("Index URL found at %s" % index_url) |
| 230 | self.work_data['url'] = index_url |
| 231 | |
| 232 | if 'ZUUL_EXTRA_SWIFT_URL' in self.job_arguments: |
| 233 | # Upload to zuul's url as instructed |
| 234 | utils.zuul_swift_upload(self.job_working_dir, self.job_arguments) |
Joshua Hesketh | 9900554 | 2014-01-30 16:34:36 +1100 | [diff] [blame^] | 235 | self.work_data['url'] = self.job_identifier |