diff --git a/src/oci_cli/cli_setup_bootstrap.py b/src/oci_cli/cli_setup_bootstrap.py index d379f12ff..1946716c4 100644 --- a/src/oci_cli/cli_setup_bootstrap.py +++ b/src/oci_cli/cli_setup_bootstrap.py @@ -15,6 +15,7 @@ import oci._vendor.jwt as jwt import oci import oci.regions as regions +import socket import os import sys import uuid @@ -39,6 +40,15 @@ @cli_util.help_option @click.pass_context @cli_util.wrap_exceptions +def is_port_available(port): + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(('',port)) + return True + except OSError as e: + return False + + def bootstrap_oci_cli(ctx, profile_name, config_location): region_param = ctx.obj['region'] if ctx.obj['region'] else '' user_session = create_user_session(region=region_param) @@ -109,24 +119,56 @@ def bootstrap_oci_cli(ctx, profile_name, config_location): oci iam region list --config-file {config_file} --profile {profile} """.format(config_file=config_location, profile=profile_name)) - -def create_user_session(region='', tenancy_name=None): +@click.command() +@click.option('-e', '--env-port', default=None, envvar='BOOTSTRAP_PORT', help='Specify the port to use (can be set via BOOTSTRAP_PORT environment variable)') +def create_user_session(env_port,region='', tenancy_name=None,): if region == '': region = cli_setup.prompt_for_region() - - # try to set up http server so we can fail early if the required port is in use + try: - server_address = ('', BOOTSTRAP_SERVICE_PORT) - httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler) + env_port1 = os.getenv('BOOTSTRAP_PORT') + port_number=env_port1 or env_port + + if not port_number.isdigit(): + click.echo("Invalid port number") + raise ValueError("Invalid port number") + + if env_port is None: + boot_strap_service_port = is_port_available(BOOTSTRAP_SERVICE_PORT) + if boot_strap_service_port: + click.echo("Default port {} is available, establishing connection...".format(BOOTSTRAP_SERVICE_PORT)) + server_address = ('', int(BOOTSTRAP_SERVICE_PORT)) + httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler) + click.echo("Connected to default port {}.".format(BOOTSTRAP_SERVICE_PORT)) + else: + click.echo("Could not complete bootstrap process because default port {} is already in use.".format( + BOOTSTRAP_SERVICE_PORT)) + elif env_port1: + if is_port_available(int(env_port1)): + click.echo("Port {} passed as env. variable is available, establishing connection...".format(env_port1)) + server_address = ('', int(env_port1)) + httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler) + click.echo("Connected to port {}".format(env_port1)) + else: + click.echo("Could not complete bootstrap process because port {} is already in use.".format(env_port)) + + elif env_port: + if is_port_available(int(env_port)): + click.echo("Port {} passed as env. variable is available, establishing connection...".format(env_port)) + server_address = ('', int(env_port)) + httpd = StoppableHttpServer(server_address, StoppableHttpRequestHandler) + click.echo("Connected to port {}".format(env_port)) + + else: + click.echo("Could not complete bootstrap process because port {} is already in use.".format(env_port)) + + else: + click.echo("No input of port received. Exiting...") + sys.exit(0) except OSError as e: if e.errno == errno.EADDRINUSE: - click.echo("Could not complete bootstrap process because port {port} is already in use.".format( - port=BOOTSTRAP_SERVICE_PORT) - ) - - sys.exit(1) - - raise e + click.echo("Could not complete bootstrap process. Failed to find an empty port") + sys.exit(0) # create new key pair # this key pair is used to get the initial token and also uploaded as a new API key for the user