Joshua Hesketh | 39a0fee | 2013-07-31 12:00:53 +1000 | [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 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 15 | |
| 16 | import git |
| 17 | import logging |
| 18 | import os |
Joshua Hesketh | 221ae74 | 2014-01-22 16:09:58 +1100 | [diff] [blame] | 19 | import requests |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 20 | import select |
Joshua Hesketh | 2e4b611 | 2013-08-12 13:03:06 +1000 | [diff] [blame] | 21 | import shutil |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 22 | import subprocess |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 23 | import swiftclient |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 24 | import time |
| 25 | |
| 26 | |
Michael Still | 9abb2a4 | 2014-01-10 14:13:15 +1100 | [diff] [blame] | 27 | log = logging.getLogger('lib.utils') |
| 28 | |
| 29 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 30 | class GitRepository(object): |
| 31 | |
| 32 | """ Manage a git repository for our uses """ |
Joshua Hesketh | 363d004 | 2013-07-26 11:44:07 +1000 | [diff] [blame] | 33 | log = logging.getLogger("lib.utils.GitRepository") |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 34 | |
| 35 | def __init__(self, remote_url, local_path): |
| 36 | self.remote_url = remote_url |
| 37 | self.local_path = local_path |
| 38 | self._ensure_cloned() |
| 39 | |
| 40 | self.repo = git.Repo(self.local_path) |
| 41 | |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 42 | def _ensure_cloned(self): |
| 43 | if not os.path.exists(self.local_path): |
| 44 | self.log.debug("Cloning from %s to %s" % (self.remote_url, |
| 45 | self.local_path)) |
| 46 | git.Repo.clone_from(self.remote_url, self.local_path) |
| 47 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 48 | def fetch(self, ref): |
| 49 | # The git.remote.fetch method may read in git progress info and |
| 50 | # interpret it improperly causing an AssertionError. Because the |
| 51 | # data was fetched properly subsequent fetches don't seem to fail. |
| 52 | # So try again if an AssertionError is caught. |
| 53 | origin = self.repo.remotes.origin |
| 54 | self.log.debug("Fetching %s from %s" % (ref, origin)) |
| 55 | |
| 56 | try: |
| 57 | origin.fetch(ref) |
| 58 | except AssertionError: |
| 59 | origin.fetch(ref) |
| 60 | |
| 61 | def checkout(self, ref): |
| 62 | self.log.debug("Checking out %s" % ref) |
| 63 | return self.repo.git.checkout(ref) |
| 64 | |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 65 | def reset(self): |
| 66 | self._ensure_cloned() |
| 67 | self.log.debug("Resetting repository %s" % self.local_path) |
| 68 | self.update() |
| 69 | origin = self.repo.remotes.origin |
| 70 | for ref in origin.refs: |
| 71 | if ref.remote_head == 'HEAD': |
| 72 | continue |
| 73 | self.repo.create_head(ref.remote_head, ref, force=True) |
| 74 | |
| 75 | # Reset to remote HEAD (usually origin/master) |
| 76 | self.repo.head.reference = origin.refs['HEAD'] |
| 77 | self.repo.head.reset(index=True, working_tree=True) |
| 78 | self.repo.git.clean('-x', '-f', '-d') |
| 79 | |
| 80 | def update(self): |
| 81 | self._ensure_cloned() |
| 82 | self.log.debug("Updating repository %s" % self.local_path) |
| 83 | origin = self.repo.remotes.origin |
| 84 | origin.update() |
| 85 | # If the remote repository is repacked, the repo object's |
| 86 | # cache may be out of date. Specifically, it caches whether |
| 87 | # to check the loose or packed DB for a given SHA. Further, |
| 88 | # if there was no pack or lose directory to start with, the |
| 89 | # repo object may not even have a database for it. Avoid |
| 90 | # these problems by recreating the repo object. |
| 91 | self.repo = git.Repo(self.local_path) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 92 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 93 | |
| 94 | def execute_to_log(cmd, logfile, timeout=-1, |
| 95 | watch_logs=[ |
| 96 | ('[syslog]', '/var/log/syslog'), |
| 97 | ('[sqlslo]', '/var/log/mysql/slow-queries.log'), |
| 98 | ('[sqlerr]', '/var/log/mysql/error.log') |
| 99 | ], |
Michael Still | e8cadae | 2014-01-06 19:47:27 +1100 | [diff] [blame] | 100 | heartbeat=True, env=None, cwd=None |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 101 | ): |
| 102 | """ Executes a command and logs the STDOUT/STDERR and output of any |
| 103 | supplied watch_logs from logs into a new logfile |
| 104 | |
| 105 | watch_logs is a list of tuples with (name,file) """ |
| 106 | |
| 107 | if not os.path.isdir(os.path.dirname(logfile)): |
| 108 | os.makedirs(os.path.dirname(logfile)) |
| 109 | |
Joshua Hesketh | c7e963b | 2013-09-11 14:11:31 +1000 | [diff] [blame] | 110 | logger = logging.getLogger(logfile) |
Michael Still | 732d25c | 2013-12-05 04:17:25 +1100 | [diff] [blame] | 111 | log_handler = logging.FileHandler(logfile) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 112 | log_formatter = logging.Formatter('%(asctime)s %(message)s') |
Michael Still | 732d25c | 2013-12-05 04:17:25 +1100 | [diff] [blame] | 113 | log_handler.setFormatter(log_formatter) |
| 114 | logger.addHandler(log_handler) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 115 | |
| 116 | descriptors = {} |
| 117 | |
| 118 | for watch_file in watch_logs: |
Michael Still | be74526 | 2014-01-06 19:51:06 +1100 | [diff] [blame] | 119 | if not os.path.exists(watch_file[1]): |
| 120 | logger.warning('Failed to monitor log file %s: file not found' |
| 121 | % watch_file[1]) |
| 122 | continue |
| 123 | |
| 124 | try: |
| 125 | fd = os.open(watch_file[1], os.O_RDONLY) |
| 126 | os.lseek(fd, 0, os.SEEK_END) |
| 127 | descriptors[fd] = {'name': watch_file[0], |
| 128 | 'poll': select.POLLIN, |
| 129 | 'lines': ''} |
| 130 | except Exception as e: |
| 131 | logger.warning('Failed to monitor log file %s: %s' |
| 132 | % (watch_file[1], e)) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 133 | |
| 134 | cmd += ' 2>&1' |
| 135 | start_time = time.time() |
| 136 | p = subprocess.Popen( |
Michael Still | e8cadae | 2014-01-06 19:47:27 +1100 | [diff] [blame] | 137 | cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 138 | env=env, cwd=cwd) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 139 | |
| 140 | descriptors[p.stdout.fileno()] = dict( |
Joshua Hesketh | 1ab465f | 2013-07-26 13:57:28 +1000 | [diff] [blame] | 141 | name='[output]', |
Joshua Hesketh | 09b2f7f | 2013-07-29 09:05:58 +1000 | [diff] [blame] | 142 | poll=(select.POLLIN | select.POLLHUP), |
| 143 | lines='' |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 144 | ) |
| 145 | |
| 146 | poll_obj = select.poll() |
| 147 | for fd, descriptor in descriptors.items(): |
| 148 | poll_obj.register(fd, descriptor['poll']) |
| 149 | |
| 150 | last_heartbeat = time.time() |
| 151 | |
Joshua Hesketh | 1ab465f | 2013-07-26 13:57:28 +1000 | [diff] [blame] | 152 | def process(fd): |
| 153 | """ Write the fd to log """ |
Joshua Hesketh | 3c0490b | 2013-08-12 10:33:40 +1000 | [diff] [blame] | 154 | global last_heartbeat |
Joshua Hesketh | 1ab465f | 2013-07-26 13:57:28 +1000 | [diff] [blame] | 155 | descriptors[fd]['lines'] += os.read(fd, 1024 * 1024) |
| 156 | # Avoid partial lines by only processing input with breaks |
Joshua Hesketh | 09b2f7f | 2013-07-29 09:05:58 +1000 | [diff] [blame] | 157 | if descriptors[fd]['lines'].find('\n') != -1: |
Joshua Hesketh | 1ab465f | 2013-07-26 13:57:28 +1000 | [diff] [blame] | 158 | elems = descriptors[fd]['lines'].split('\n') |
| 159 | # Take all but the partial line |
| 160 | for l in elems[:-1]: |
| 161 | if len(l) > 0: |
| 162 | l = '%s %s' % (descriptors[fd]['name'], l) |
| 163 | logger.info(l) |
| 164 | last_heartbeat = time.time() |
| 165 | # Place the partial line back into lines to be processed |
| 166 | descriptors[fd]['lines'] = elems[-1] |
| 167 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 168 | while p.poll() is None: |
| 169 | if timeout > 0 and time.time() - start_time > timeout: |
| 170 | # Append to logfile |
| 171 | logger.info("[timeout]") |
| 172 | os.kill(p.pid, 9) |
| 173 | |
| 174 | for fd, flag in poll_obj.poll(0): |
Joshua Hesketh | 1ab465f | 2013-07-26 13:57:28 +1000 | [diff] [blame] | 175 | process(fd) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 176 | |
| 177 | if time.time() - last_heartbeat > 30: |
| 178 | # Append to logfile |
| 179 | logger.info("[heartbeat]") |
| 180 | last_heartbeat = time.time() |
| 181 | |
Joshua Hesketh | 1ab465f | 2013-07-26 13:57:28 +1000 | [diff] [blame] | 182 | # Do one last write to get the remaining lines |
| 183 | for fd, flag in poll_obj.poll(0): |
| 184 | process(fd) |
| 185 | |
Joshua Hesketh | 86ab064 | 2013-08-30 13:41:58 +1000 | [diff] [blame] | 186 | # Clean up |
| 187 | for fd, descriptor in descriptors.items(): |
Joshua Hesketh | 8ca96fb | 2013-08-30 18:17:19 +1000 | [diff] [blame] | 188 | poll_obj.unregister(fd) |
Joshua Hesketh | 6ad492c | 2014-04-08 17:12:02 +1000 | [diff] [blame] | 189 | if fd == p.stdout.fileno(): |
| 190 | # Don't try and close the process, it'll clean itself up |
| 191 | continue |
Joshua Hesketh | 105af41 | 2013-09-02 10:24:36 +1000 | [diff] [blame] | 192 | os.close(fd) |
Joshua Hesketh | 721781d | 2013-09-02 16:06:01 +1000 | [diff] [blame] | 193 | try: |
| 194 | p.kill() |
| 195 | except OSError: |
| 196 | pass |
Joshua Hesketh | 86ab064 | 2013-08-30 13:41:58 +1000 | [diff] [blame] | 197 | |
Joshua Hesketh | 363d004 | 2013-07-26 11:44:07 +1000 | [diff] [blame] | 198 | logger.info('[script exit code = %d]' % p.returncode) |
Michael Still | 732d25c | 2013-12-05 04:17:25 +1100 | [diff] [blame] | 199 | logger.removeHandler(log_handler) |
| 200 | log_handler.flush() |
| 201 | log_handler.close() |
Michael Still | 5231d4c | 2013-12-24 17:47:59 +1100 | [diff] [blame] | 202 | return p.returncode |
Joshua Hesketh | 926502f | 2013-07-31 11:56:40 +1000 | [diff] [blame] | 203 | |
Joshua Hesketh | 9f89805 | 2013-08-09 10:52:34 +1000 | [diff] [blame] | 204 | |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 205 | def push_file(results_set_name, file_path, publish_config): |
Joshua Hesketh | 926502f | 2013-07-31 11:56:40 +1000 | [diff] [blame] | 206 | """ Push a log file to a server. Returns the public URL """ |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 207 | method = publish_config['type'] + '_push_file' |
Joshua Hesketh | 2e4b611 | 2013-08-12 13:03:06 +1000 | [diff] [blame] | 208 | if method in globals() and hasattr(globals()[method], '__call__'): |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 209 | return globals()[method](results_set_name, file_path, publish_config) |
Joshua Hesketh | 9f89805 | 2013-08-09 10:52:34 +1000 | [diff] [blame] | 210 | |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 211 | |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 212 | def swift_push_file(results_set_name, file_path, swift_config): |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 213 | """ Push a log file to a swift server. """ |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 214 | def _push_individual_file(results_set_name, file_path, swift_config): |
Joshua Hesketh | 7859fde | 2014-01-22 14:53:17 +1100 | [diff] [blame] | 215 | with open(file_path, 'r') as fd: |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 216 | name = os.path.join(results_set_name, os.path.basename(file_path)) |
Joshua Hesketh | 7859fde | 2014-01-22 14:53:17 +1100 | [diff] [blame] | 217 | con = swiftclient.client.Connection( |
| 218 | authurl=swift_config['authurl'], |
| 219 | user=swift_config['user'], |
| 220 | key=swift_config['password'], |
| 221 | os_options={'region_name': swift_config['region']}, |
| 222 | tenant_name=swift_config['tenant'], |
| 223 | auth_version=2.0) |
| 224 | con.put_object(swift_config['container'], name, fd) |
| 225 | |
| 226 | if os.path.isfile(file_path): |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 227 | _push_individual_file(results_set_name, file_path, swift_config) |
Joshua Hesketh | 7859fde | 2014-01-22 14:53:17 +1100 | [diff] [blame] | 228 | elif os.path.isdir(file_path): |
| 229 | for path, folders, files in os.walk(file_path): |
| 230 | for f in files: |
| 231 | f_path = os.path.join(path, f) |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 232 | _push_individual_file(results_set_name, f_path, swift_config) |
Joshua Hesketh | 7859fde | 2014-01-22 14:53:17 +1100 | [diff] [blame] | 233 | |
| 234 | return (swift_config['prepend_url'] + |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 235 | os.path.join(results_set_name, os.path.basename(file_path))) |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 236 | |
Joshua Hesketh | 9f89805 | 2013-08-09 10:52:34 +1000 | [diff] [blame] | 237 | |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 238 | def local_push_file(results_set_name, file_path, local_config): |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 239 | """ Copy the file locally somewhere sensible """ |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 240 | dest_dir = os.path.join(local_config['path'], results_set_name) |
Joshua Hesketh | c76ecde | 2013-08-12 13:46:54 +1000 | [diff] [blame] | 241 | dest_filename = os.path.basename(file_path) |
| 242 | if not os.path.isdir(dest_dir): |
| 243 | os.makedirs(dest_dir) |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 244 | |
Joshua Hesketh | c76ecde | 2013-08-12 13:46:54 +1000 | [diff] [blame] | 245 | dest_file = os.path.join(dest_dir, dest_filename) |
Joshua Hesketh | 2e4b611 | 2013-08-12 13:03:06 +1000 | [diff] [blame] | 246 | |
Joshua Hesketh | 7859fde | 2014-01-22 14:53:17 +1100 | [diff] [blame] | 247 | if os.path.isfile(file_path): |
| 248 | shutil.copyfile(file_path, dest_file) |
| 249 | elif os.path.isdir(file_path): |
| 250 | shutil.copytree(file_path, dest_file) |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 251 | return local_config['prepend_url'] + os.path.join(results_set_name, |
Joshua Hesketh | 0b3fe58 | 2013-09-27 14:52:35 +1000 | [diff] [blame] | 252 | dest_filename) |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 253 | |
Joshua Hesketh | 9f89805 | 2013-08-09 10:52:34 +1000 | [diff] [blame] | 254 | |
Joshua Hesketh | 5a2edd4 | 2014-01-22 15:02:45 +1100 | [diff] [blame] | 255 | def scp_push_file(results_set_name, file_path, local_config): |
Joshua Hesketh | 11ed32c | 2013-08-09 10:42:36 +1000 | [diff] [blame] | 256 | """ Copy the file remotely over ssh """ |
Joshua Hesketh | 7859fde | 2014-01-22 14:53:17 +1100 | [diff] [blame] | 257 | # TODO! |
Joshua Hesketh | 926502f | 2013-07-31 11:56:40 +1000 | [diff] [blame] | 258 | pass |
Joshua Hesketh | 2500696 | 2013-09-24 16:22:40 +1000 | [diff] [blame] | 259 | |
| 260 | |
| 261 | def determine_job_identifier(zuul_arguments, job, unique): |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 262 | # use new determined path from zuul |
| 263 | path = zuul_arguments['LOG_PATH'] |
Michael Still | 9abb2a4 | 2014-01-10 14:13:15 +1100 | [diff] [blame] | 264 | return path |
Joshua Hesketh | 221ae74 | 2014-01-22 16:09:58 +1100 | [diff] [blame] | 265 | |
| 266 | |
| 267 | def zuul_swift_upload(file_path, job_arguments): |
| 268 | """Upload working_dir to swift as per zuul's instructions""" |
| 269 | # NOTE(jhesketh): Zuul specifies an object prefix in the destination so |
| 270 | # we don't need to be concerned with results_set_name |
| 271 | |
| 272 | file_list = [] |
| 273 | if os.path.isfile(file_path): |
| 274 | file_list.append(file_path) |
| 275 | elif os.path.isdir(file_path): |
| 276 | for path, folders, files in os.walk(file_path): |
| 277 | for f in files: |
| 278 | f_path = os.path.join(path, f) |
| 279 | file_list.append(f_path) |
| 280 | |
| 281 | # We are uploading the file_list as an HTTP POST multipart encoded. |
| 282 | # First grab out the information we need to send back from the hmac_body |
| 283 | payload = {} |
| 284 | (object_prefix, |
| 285 | payload['redirect'], |
| 286 | payload['max_file_size'], |
| 287 | payload['max_file_count'], |
| 288 | payload['expires']) = \ |
| 289 | job_arguments['ZUUL_EXTRA_SWIFT_HMAC_BODY'].split('\n') |
| 290 | |
| 291 | url = job_arguments['ZUUL_EXTRA_SWIFT_URL'] |
| 292 | payload['signature'] = job_arguments['ZUUL_EXTRA_SWIFT_SIGNATURE'] |
| 293 | logserver_prefix = job_arguments['ZUUL_EXTRA_SWIFT_LOGSERVER_PREFIX'] |
| 294 | |
| 295 | files = {} |
| 296 | for i, f in enumerate(file_list): |
| 297 | files['file%d' % (i + 1)] = open(f, 'rb') |
| 298 | |
| 299 | requests.post(url, data=payload, files=files) |
| 300 | |
| 301 | return (logserver_prefix + |
| 302 | job_arguments['ZUUL_EXTRA_SWIFT_DESTINATION_PREFIX']) |