mds_plugin/compute.py
def execute_ssh_command(command=None, **kwargs):
    """Execute the given command on the instance

    If no name is given, it will prompt the user for the name.

    Args:
        command (str): The command to execute.
        **kwargs: Additional options

    Keyword Args:
        instance_name (str): The name of the instance.
        instance_id (str): The OCID of the instance
        private_key_file_path (str): The path to the private key
        private_key_passphrase (str): The passphrase of the private key
        compartment_id (str): OCID of the compartment.
        config (object): An OCI config object or None.
        interactive (bool): Whether to prompt the user for input and
            throw exceptions

    Returns:
       None
    """

    instance_name = kwargs.get("instance_name")
    instance_id = kwargs.get("instance_id")
    private_key_file_path = kwargs.get(
        "private_key_file_path", "~/.ssh/id_rsa")
    private_key_passphrase = kwargs.get("private_key_passphrase")
    instance_id = kwargs.get("instance_id")
    compartment_id = kwargs.get("compartment_id")
    config = kwargs.get("config")
    interactive = kwargs.get("interactive", True)

    # Get the active config, compartment and instance
    try:
        config = configuration.get_current_config(config=config)
        compartment_id = configuration.get_current_compartment_id(
            compartment_id=compartment_id, config=config)
        instance_id = configuration.get_current_instance_id(
            instance_id=instance_id, config=config)

        import oci.exceptions
        import mysqlsh

        if not interactive and command is None:
            raise ValueError("No command given.")

        instance = get_instance(
            instance_name=instance_name, instance_id=instance_id,
            compartment_id=compartment_id, config=config)
        if instance is None:
            print("Operation cancelled.")
            return

        public_ip = get_instance_public_ip(
            instance_name=instance_name, instance_id=instance.id,
            compartment_id=compartment_id, config=config,
            private_ip_fallback=True)
        if public_ip == "":
            print("ERROR: No public IP address found.\n")
            return

        # If no command was given and interactive is enabled, loop until
        # an empty command or exit is provided by the user input
        multi_command = command is None and interactive
        output = None

        with SshConnection(
                username="opc", host=public_ip,
                private_key_file_path=private_key_file_path,
                private_key_passphrase=private_key_passphrase) as conn:

            if multi_command:
                print(f"\nConnected to 'opc@{public_ip}' via SSH. "
                      "Type 'exit' to close the connection.")

            # Repeat command execution till the give command is empty or exit
            while command is None or \
                    (command != "" and command.lower() != "exit"):
                if multi_command:
                    command = mysqlsh.globals.shell.prompt(
                        f"{public_ip} opc$ ").strip()
                    if command == "" or command.lower() == "exit":
                        return
                # Execute the command
                output = conn.execute(command)

                # If there was an error, print it
                last_error = conn.get_last_error()
                if last_error != "":
                    output += f"ERROR: {last_error}"

                # If multi_command is not enabled, break after first execution
                if not multi_command:
                    return output
                else:
                    print(output)

    except oci.exceptions.ServiceError as e:
        if not interactive:
            raise
        print(f'ERROR: {e.message}. (Code: {e.code}; Status: {e.status})')
        return
    except (ValueError, oci.exceptions.ClientError) as e:
        if not interactive:
            raise
        print(f'ERROR: {e}')
        return
Execute the given command on the instance

    If no name is given, it will prompt the user for the name.

    Args:
        command (str): The command to execute.
        **kwargs: Additional options

    Keyword Args:
        instance_name (str): The name of the instance.
        instance_id (str): The OCID of the instance
        private_key_file_path (str): The path to the private key
        private_key_passphrase (str): The passphrase of the private key
        compartment_id (str): OCID of the compartment.
        config (object): An OCI config object or None.
        interactive (bool): Whether to prompt the user for input and
            throw exceptions

    Returns:
       None
    
Execute the given command on the instance
If no name is given, it will prompt the user for the name.
Execute the given command on the instance If no name is given it will prompt the user for the name .
def execute_ssh_command(command=None, **kwargs):
    instance_name = kwargs.get("instance_name")
    instance_id = kwargs.get("instance_id")
    private_key_file_path = kwargs.get(
        "private_key_file_path", "~/.ssh/id_rsa")
    private_key_passphrase = kwargs.get("private_key_passphrase")
    instance_id = kwargs.get("instance_id")
    compartment_id = kwargs.get("compartment_id")
    config = kwargs.get("config")
    interactive = kwargs.get("interactive", True)
    try:
        config = configuration.get_current_config(config=config)
        compartment_id = configuration.get_current_compartment_id(
            compartment_id=compartment_id, config=config)
        instance_id = configuration.get_current_instance_id(
            instance_id=instance_id, config=config)
        import oci.exceptions
        import mysqlsh
        if not interactive and command is None:
            raise ValueError("No command given.")
        instance = get_instance(
            instance_name=instance_name, instance_id=instance_id,
            compartment_id=compartment_id, config=config)
        if instance is None:
            print("Operation cancelled.")
            return
        public_ip = get_instance_public_ip(
            instance_name=instance_name, instance_id=instance.id,
            compartment_id=compartment_id, config=config,
            private_ip_fallback=True)
        if public_ip == "":
            print("ERROR: No public IP address found.\n")
            return
        multi_command = command is None and interactive
        output = None
        with SshConnection(
                username="opc", host=public_ip,
                private_key_file_path=private_key_file_path,
                private_key_passphrase=private_key_passphrase) as conn:
            if multi_command:
                print(f"\nConnected to 'opc@{public_ip}' via SSH. "
                      "Type 'exit' to close the connection.")
            while command is None or \
                    (command != "" and command.lower() != "exit"):
                if multi_command:
                    command = mysqlsh.globals.shell.prompt(
                        f"{public_ip} opc$ ").strip()
                    if command == "" or command.lower() == "exit":
                        return
                output = conn.execute(command)
                last_error = conn.get_last_error()
                if last_error != "":
                    output += f"ERROR: {last_error}"
                if not multi_command:
                    return output
                else:
                    print(output)
    except oci.exceptions.ServiceError as e:
        if not interactive:
            raise
        print(f'ERROR: {e.message}. (Code: {e.code}; Status: {e.status})')
        return
    except (ValueError, oci.exceptions.ClientError) as e:
        if not interactive:
            raise
        print(f'ERROR: {e}')
        return
def execute_ssh_command ( command = None , ** kwargs ) : instance_name = kwargs . get ( "instance_name" ) instance_id = kwargs . get ( "instance_id" ) private_key_file_path = kwargs . get ( "private_key_file_path" , "~/.ssh/id_rsa" ) private_key_passphrase = kwargs . get ( "private_key_passphrase" ) instance_id = kwargs . get ( "instance_id" ) compartment_id = kwargs . get ( "compartment_id" ) config = kwargs . get ( "config" ) interactive = kwargs . get ( "interactive" , True ) try : config = configuration . get_current_config ( config = config ) compartment_id = configuration . get_current_compartment_id ( compartment_id = compartment_id , config = config ) instance_id = configuration . get_current_instance_id ( instance_id = instance_id , config = config ) import oci . exceptions import mysqlsh if not interactive and command is None : raise ValueError ( "No command given." ) instance = get_instance ( instance_name = instance_name , instance_id = instance_id , compartment_id = compartment_id , config = config ) if instance is None : print ( "Operation cancelled." ) return public_ip = get_instance_public_ip ( instance_name = instance_name , instance_id = instance . id , compartment_id = compartment_id , config = config , private_ip_fallback = True ) if public_ip == "" : print ( "ERROR: No public IP address found.\n" ) return multi_command = command is None and interactive output = None with SshConnection ( username = "opc" , host = public_ip , private_key_file_path = private_key_file_path , private_key_passphrase = private_key_passphrase ) as conn : if multi_command : print ( f"\nConnected to 'opc@{public_ip}' via SSH. " "Type 'exit' to close the connection." ) while command is None or ( command != "" and command . lower ( ) != "exit" ) : if multi_command : command = mysqlsh . globals . shell . prompt ( f"{public_ip} opc$ " ) . strip ( ) if command == "" or command . lower ( ) == "exit" : return output = conn . execute ( command ) last_error = conn . get_last_error ( ) if last_error != "" : output += f"ERROR: {last_error}" if not multi_command : return output else : print ( output ) except oci . exceptions . ServiceError as e : if not interactive : raise print ( f'ERROR: {e.message}. (Code: {e.code}; Status: {e.status})' ) return except ( ValueError , oci . exceptions . ClientError ) as e : if not interactive : raise print ( f'ERROR: {e}' ) return
Execute the given command on the instance
If no name is given, it will prompt the user for the name.
Execute the given command on the instance If no name is given it will prompt the user for the name .
"""Execute the given command on the instance

    If no name is given, it will prompt the user for the name.

    Args:
        command (str): The command to execute.
        **kwargs: Additional options

    Keyword Args:
        instance_name (str): The name of the instance.
        instance_id (str): The OCID of the instance
        private_key_file_path (str): The path to the private key
        private_key_passphrase (str): The passphrase of the private key
        compartment_id (str): OCID of the compartment.
        config (object): An OCI config object or None.
        interactive (bool): Whether to prompt the user for input and
            throw exceptions

    Returns:
       None
    """ # Get the active config, compartment and instance
 # If no command was given and interactive is enabled, loop until
 # an empty command or exit is provided by the user input
 # Repeat command execution till the give command is empty or exit
 # Execute the command
 # If there was an error, print it
 # If multi_command is not enabled, break after first execution

None
command
The command to execute.
The command to execute .
**kwargs
None