Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | """Helpers to run docker instances as jobs.""" |
| 31 | |
| 32 | import jobset |
| 33 | import tempfile |
| 34 | import time |
| 35 | import uuid |
| 36 | import os |
| 37 | import subprocess |
| 38 | |
| 39 | _DEVNULL = open(os.devnull, 'w') |
| 40 | |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 41 | |
| 42 | def random_name(base_name): |
| 43 | """Randomizes given base name.""" |
| 44 | return '%s_%s' % (base_name, uuid.uuid4()) |
| 45 | |
| 46 | |
| 47 | def docker_kill(cid): |
| 48 | """Kills a docker container. Returns True if successful.""" |
Jan Tattermusch | cc6a2b8 | 2015-10-09 14:53:51 -0700 | [diff] [blame] | 49 | return subprocess.call(['docker','kill', str(cid)], |
Carl Mastrangelo | 2d248a2 | 2015-11-19 13:09:52 -0800 | [diff] [blame] | 50 | stdin=subprocess.PIPE, |
Jan Tattermusch | cc6a2b8 | 2015-10-09 14:53:51 -0700 | [diff] [blame] | 51 | stdout=_DEVNULL, |
| 52 | stderr=subprocess.STDOUT) == 0 |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 53 | |
| 54 | |
Jan Tattermusch | 98c0be5 | 2015-10-09 14:33:34 -0700 | [diff] [blame] | 55 | def docker_mapped_port(cid, port, timeout_seconds=15): |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 56 | """Get port mapped to internal given internal port for given container.""" |
Jan Tattermusch | 98c0be5 | 2015-10-09 14:33:34 -0700 | [diff] [blame] | 57 | started = time.time() |
| 58 | while time.time() - started < timeout_seconds: |
| 59 | try: |
| 60 | output = subprocess.check_output('docker port %s %s' % (cid, port), |
Jan Tattermusch | ab5bc72 | 2015-10-09 14:46:29 -0700 | [diff] [blame] | 61 | stderr=_DEVNULL, |
Jan Tattermusch | 98c0be5 | 2015-10-09 14:33:34 -0700 | [diff] [blame] | 62 | shell=True) |
| 63 | return int(output.split(':', 2)[1]) |
| 64 | except subprocess.CalledProcessError as e: |
| 65 | pass |
| 66 | raise Exception('Failed to get exposed port %s for container %s.' % |
| 67 | (port, cid)) |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def finish_jobs(jobs): |
| 71 | """Kills given docker containers and waits for corresponding jobs to finish""" |
| 72 | for job in jobs: |
| 73 | job.kill(suppress_failure=True) |
| 74 | |
| 75 | while any(job.is_running() for job in jobs): |
| 76 | time.sleep(1) |
| 77 | |
| 78 | |
| 79 | def image_exists(image): |
| 80 | """Returns True if given docker image exists.""" |
| 81 | return subprocess.call(['docker','inspect', image], |
Carl Mastrangelo | 2d248a2 | 2015-11-19 13:09:52 -0800 | [diff] [blame] | 82 | stdin=subprocess.PIPE, |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 83 | stdout=_DEVNULL, |
Jan Tattermusch | 98c0be5 | 2015-10-09 14:33:34 -0700 | [diff] [blame] | 84 | stderr=subprocess.STDOUT) == 0 |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def remove_image(image, skip_nonexistent=False, max_retries=10): |
| 88 | """Attempts to remove docker image with retries.""" |
| 89 | if skip_nonexistent and not image_exists(image): |
| 90 | return True |
| 91 | for attempt in range(0, max_retries): |
Jan Tattermusch | ab5bc72 | 2015-10-09 14:46:29 -0700 | [diff] [blame] | 92 | if subprocess.call(['docker','rmi', '-f', image], |
Carl Mastrangelo | 2d248a2 | 2015-11-19 13:09:52 -0800 | [diff] [blame] | 93 | stdin=subprocess.PIPE, |
Jan Tattermusch | ab5bc72 | 2015-10-09 14:46:29 -0700 | [diff] [blame] | 94 | stdout=_DEVNULL, |
| 95 | stderr=subprocess.STDOUT) == 0: |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 96 | return True |
| 97 | time.sleep(2) |
| 98 | print 'Failed to remove docker image %s' % image |
| 99 | return False |
| 100 | |
| 101 | |
| 102 | class DockerJob: |
| 103 | """Encapsulates a job""" |
| 104 | |
| 105 | def __init__(self, spec): |
| 106 | self._spec = spec |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 107 | self._job = jobset.Job(spec, bin_hash=None, newline_on_success=True, travis=True, add_env={}) |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 108 | self._container_name = spec.container_name |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 109 | |
| 110 | def mapped_port(self, port): |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 111 | return docker_mapped_port(self._container_name, port) |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 112 | |
| 113 | def kill(self, suppress_failure=False): |
| 114 | """Sends kill signal to the container.""" |
| 115 | if suppress_failure: |
| 116 | self._job.suppress_failure_message() |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 117 | return docker_kill(self._container_name) |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 118 | |
| 119 | def is_running(self): |
| 120 | """Polls a job and returns True if given job is still running.""" |
| 121 | return self._job.state(jobset.NoCache()) == jobset._RUNNING |