Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 1 | # Copyright 2015 gRPC authors. |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 2 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 6 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 14 | |
Craig Tiller | ddc8a82 | 2017-05-05 14:02:35 -0700 | [diff] [blame] | 15 | import urllib |
Craig Tiller | 6911d08 | 2017-02-07 10:30:44 -0800 | [diff] [blame] | 16 | import jobset |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 17 | import logging |
| 18 | import os |
Craig Tiller | 37bfff3 | 2017-02-14 22:35:39 -0800 | [diff] [blame] | 19 | import socket |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 20 | import subprocess |
| 21 | import sys |
| 22 | import tempfile |
| 23 | import time |
| 24 | |
Craig Tiller | cba864b | 2017-02-17 10:27:56 -0800 | [diff] [blame] | 25 | # must be synchronized with test/core/utils/port_server_client.h |
| 26 | _PORT_SERVER_PORT = 32766 |
| 27 | |
ncteisen | 05687c3 | 2017-12-11 16:54:47 -0800 | [diff] [blame] | 28 | |
Craig Tiller | cba864b | 2017-02-17 10:27:56 -0800 | [diff] [blame] | 29 | def start_port_server(): |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 30 | # check if a compatible port server is running |
| 31 | # if incompatible (version mismatch) ==> start a new one |
| 32 | # if not running ==> start a new one |
| 33 | # otherwise, leave it up |
| 34 | try: |
| 35 | version = int( |
ncteisen | 05687c3 | 2017-12-11 16:54:47 -0800 | [diff] [blame] | 36 | urllib.urlopen('http://localhost:%d/version_number' % |
| 37 | _PORT_SERVER_PORT).read()) |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 38 | logging.info('detected port server running version %d', version) |
| 39 | running = True |
| 40 | except Exception as e: |
| 41 | logging.exception('failed to detect port server') |
| 42 | running = False |
| 43 | if running: |
| 44 | current_version = int( |
| 45 | subprocess.check_output([ |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 46 | sys.executable, |
| 47 | os.path.abspath('tools/run_tests/python_utils/port_server.py'), |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 48 | 'dump_version' |
| 49 | ])) |
| 50 | logging.info('my port server is version %d', current_version) |
| 51 | running = (version >= current_version) |
| 52 | if not running: |
| 53 | logging.info('port_server version mismatch: killing the old one') |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 54 | urllib.urlopen( |
| 55 | 'http://localhost:%d/quitquitquit' % _PORT_SERVER_PORT).read() |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 56 | time.sleep(1) |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 57 | if not running: |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 58 | fd, logfile = tempfile.mkstemp() |
| 59 | os.close(fd) |
| 60 | logging.info('starting port_server, with log file %s', logfile) |
| 61 | args = [ |
| 62 | sys.executable, |
Craig Tiller | cba864b | 2017-02-17 10:27:56 -0800 | [diff] [blame] | 63 | os.path.abspath('tools/run_tests/python_utils/port_server.py'), |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 64 | '-p', |
| 65 | '%d' % _PORT_SERVER_PORT, '-l', logfile |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 66 | ] |
| 67 | env = dict(os.environ) |
| 68 | env['BUILD_ID'] = 'pleaseDontKillMeJenkins' |
| 69 | if jobset.platform_string() == 'windows': |
| 70 | # Working directory of port server needs to be outside of Jenkins |
| 71 | # workspace to prevent file lock issues. |
| 72 | tempdir = tempfile.mkdtemp() |
| 73 | port_server = subprocess.Popen( |
| 74 | args, |
| 75 | env=env, |
| 76 | cwd=tempdir, |
| 77 | creationflags=0x00000008, # detached process |
| 78 | close_fds=True) |
| 79 | else: |
| 80 | port_server = subprocess.Popen( |
| 81 | args, env=env, preexec_fn=os.setsid, close_fds=True) |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 82 | time.sleep(1) |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 83 | # ensure port server is up |
| 84 | waits = 0 |
| 85 | while True: |
| 86 | if waits > 10: |
| 87 | logging.warning( |
| 88 | 'killing port server due to excessive start up waits') |
| 89 | port_server.kill() |
| 90 | if port_server.poll() is not None: |
| 91 | logging.error('port_server failed to start') |
| 92 | # try one final time: maybe another build managed to start one |
| 93 | time.sleep(1) |
| 94 | try: |
Mehrdad Afshari | 87cd994 | 2018-01-02 14:40:00 -0800 | [diff] [blame] | 95 | urllib.urlopen( |
| 96 | 'http://localhost:%d/get' % _PORT_SERVER_PORT).read() |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 97 | logging.info( |
| 98 | 'last ditch attempt to contact port server succeeded') |
| 99 | break |
| 100 | except: |
| 101 | logging.exception( |
| 102 | 'final attempt to contact port server failed') |
| 103 | port_log = open(logfile, 'r').read() |
Craig Tiller | ddc8a82 | 2017-05-05 14:02:35 -0700 | [diff] [blame] | 104 | print port_log |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 105 | sys.exit(1) |
| 106 | try: |
Craig Tiller | 5474c6b | 2017-02-17 10:29:40 -0800 | [diff] [blame] | 107 | port_server_url = 'http://localhost:%d/get' % _PORT_SERVER_PORT |
Craig Tiller | ddc8a82 | 2017-05-05 14:02:35 -0700 | [diff] [blame] | 108 | urllib.urlopen(port_server_url).read() |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 109 | logging.info('port server is up and ready') |
| 110 | break |
| 111 | except socket.timeout: |
| 112 | logging.exception('while waiting for port_server') |
| 113 | time.sleep(1) |
| 114 | waits += 1 |
Craig Tiller | ddc8a82 | 2017-05-05 14:02:35 -0700 | [diff] [blame] | 115 | except IOError: |
David Garcia Quintas | 46a1cca | 2017-02-16 11:59:42 -0800 | [diff] [blame] | 116 | logging.exception('while waiting for port_server') |
| 117 | time.sleep(1) |
| 118 | waits += 1 |
| 119 | except: |
| 120 | logging.exception('error while contacting port server at "%s".' |
| 121 | 'Will try killing it.', port_server_url) |
| 122 | port_server.kill() |
| 123 | raise |