Skip to content

Commit fc2bb85

Browse files
committed
Added command-line parameter
Signed-off-by: Anurav Modak <[email protected]>
1 parent afa06c7 commit fc2bb85

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

src/oci_cli/cli_setup_bootstrap.py

+43-31
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,12 @@
4343
def is_port_available(port):
4444
try:
4545
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
46-
s.bind('',port)
46+
s.bind(('',port))
4747
return True
4848
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-
49+
return False
50+
51+
6252
def bootstrap_oci_cli(ctx, profile_name, config_location):
6353
region_param = ctx.obj['region'] if ctx.obj['region'] else ''
6454
user_session = create_user_session(region=region_param)
@@ -129,28 +119,50 @@ def bootstrap_oci_cli(ctx, profile_name, config_location):
129119
oci iam region list --config-file {config_file} --profile {profile}
130120
""".format(config_file=config_location, profile=profile_name))
131121

132-
133-
def create_user_session(region='', tenancy_name=None):
122+
@click.command()
123+
@click.option('-e', '--env-port', default=None, envvar='BOOTSTRAP_PORT', help='Specify the port to use (can be set via BOOTSTRAP_PORT environment variable)')
124+
def create_user_session(env_port,region='', tenancy_name=None,):
134125
if region == '':
135126
region = cli_setup.prompt_for_region()
136-
137-
138-
139-
# try to set up http server so we can fail early if the required port is in use
127+
140128
try:
141-
# Firstly, we will check if PORT is available or not
142-
BOOTSTRAP_SERVICE_PORT=is_port_available(BOOTSTRAP_SERVICE_PORT)
143-
server_address = ('', BOOTSTRAP_SERVICE_PORT)
144-
httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler)
129+
env_port1 = os.getenv('BOOTSTRAP_PORT')
130+
if env_port is None:
131+
boot_strap_service_port = is_port_available(BOOTSTRAP_SERVICE_PORT)
132+
click.echo("Port {} is available, establishing connection...".format(BOOTSTRAP_SERVICE_PORT))
133+
if boot_strap_service_port:
134+
server_address = ('', int(BOOTSTRAP_SERVICE_PORT))
135+
httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler)
136+
click.echo("Connected to port {}.".format(BOOTSTRAP_SERVICE_PORT))
137+
else:
138+
click.echo("Could not complete bootstrap process because default port {} is already in use.".format(
139+
BOOTSTRAP_SERVICE_PORT))
140+
elif env_port1:
141+
if (is_port_available(int(env_port1)) and env_port1.isdigit()):
142+
click.echo("Environment port {} is available, establishing connection...".format(env_port1))
143+
server_address = ('', int(env_port1))
144+
httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler)
145+
click.echo("Connected to port {}".format(env_port1))
146+
else:
147+
click.echo("Could not complete bootstrap process because port {} is already in use.".format(env_port))
148+
149+
elif env_port:
150+
if is_port_available(int(env_port)) and env_port.isdigit():
151+
click.echo("Environment port {} is available, establishing connection...".format(env_port))
152+
server_address = ('', int(env_port))
153+
httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler)
154+
click.echo("Connected to port {}".format(env_port))
155+
156+
else:
157+
click.echo("Could not complete bootstrap process because port {} is already in use.".format(env_port))
158+
159+
else:
160+
click.echo("No input of port received. Exiting...")
161+
sys.exit(0)
145162
except OSError as e:
146163
if e.errno == errno.EADDRINUSE:
147-
click.echo("Could not complete bootstrap process because port {port} is already in use.".format(
148-
port=BOOTSTRAP_SERVICE_PORT)
149-
)
150-
151-
sys.exit(1)
152-
153-
raise e
164+
click.echo("Could not complete bootstrap process. Failed to find an empty port")
165+
sys.exit(0)
154166

155167
# create new key pair
156168
# this key pair is used to get the initial token and also uploaded as a new API key for the user

0 commit comments

Comments
 (0)