|
15 | 15 | import oci._vendor.jwt as jwt |
16 | 16 | import oci |
17 | 17 | import oci.regions as regions |
| 18 | +import socket |
18 | 19 | import os |
19 | 20 | import sys |
20 | 21 | import uuid |
|
39 | 40 | @cli_util.help_option |
40 | 41 | @click.pass_context |
41 | 42 | @cli_util.wrap_exceptions |
| 43 | +def is_port_available(port): |
| 44 | + try: |
| 45 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 46 | + s.bind('',port) |
| 47 | + return True |
| 48 | + except OSError as e: |
| 49 | + return False |
| 50 | + |
| 51 | + |
| 52 | +def find_port(start_port,max_attempts=100): |
| 53 | + """Find an available port starting from start_port""" |
| 54 | + if is_port_available(start_port): |
| 55 | + return start_port |
| 56 | + for port in range(start_port+1,start_port+max_attempts+1): |
| 57 | + if is_port_available(port): |
| 58 | + return port |
| 59 | + raise OSError(f"Could not find an available port in the range {start_port + 1} to {start_port + max_attempts}.") |
| 60 | + |
| 61 | + |
42 | 62 | def bootstrap_oci_cli(ctx, profile_name, config_location): |
43 | 63 | region_param = ctx.obj['region'] if ctx.obj['region'] else '' |
44 | 64 | user_session = create_user_session(region=region_param) |
@@ -114,8 +134,12 @@ def create_user_session(region='', tenancy_name=None): |
114 | 134 | if region == '': |
115 | 135 | region = cli_setup.prompt_for_region() |
116 | 136 |
|
| 137 | + |
| 138 | + |
117 | 139 | # try to set up http server so we can fail early if the required port is in use |
118 | 140 | try: |
| 141 | + # Firstly, we will check if PORT is available or not |
| 142 | + BOOTSTRAP_SERVICE_PORT=is_port_available(BOOTSTRAP_SERVICE_PORT) |
119 | 143 | server_address = ('', BOOTSTRAP_SERVICE_PORT) |
120 | 144 | httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler) |
121 | 145 | except OSError as e: |
|
0 commit comments