Oracle Cloud Infrastructure (OCI) CLI

repository·master·Indexed 20 days ago

https://github.com/oracle/oci-cli

An open-source command-line interface for managing Oracle Cloud Infrastructure resources. Includes 'o', a smart wrapper that provides fuzzy-matching command shortcuts, resource name resolution to replace complex OCIDs, and human-readable output formatting (tables, CSV, text) for OCI CLI commands.

Tokens
17.1K
Snippets
70
Records
97
Agent score
70%

What's inside oci-cli

  1. What is o - a smart oci-cli wrapper

    master

    o is a wrapper for the Oracle Cloud Infrastructure oci command line interface designed to improve user experience. It provides several key capabilities:

    • Command Shortcuts: Quickly find commands and get usage info using intuitive, automatic shortcuts (e.g., list subn instead of network subnet list).
    • Resource Name Resolution: Run commands using human-readable resource names instead of long, complex OCIDs.
    • Human-Readable Output: Transform JSON output into clean, formatted tables. You can specify exactly which fields to display using a selector syntax like name#shape#state.

    Note: o is not a replacement for oci. The oci CLI must be installed and configured for o to function.

    # Example of transforming a short command into a full OCI command
    $ o list subn -c sales -v west -a
    
    # Example of human-readable output with field selection
    $ o -o name#shape#shape-conf.ocpus#state list comp inst -c kevco.
    
    display-name shape               ocpus lifecycle-state
    atos         VM.Standard.E4.Flex 1.0   STOPPED
    cron         VM.Standard.A1.Flex 1.0   RUNNING
  2. Use 'o' as a smart OCI CLI wrapper

    master

    The o tool is a fuzzy-matching wrapper for the oci CLI designed to simplify command discovery and execution. It allows you to use shortcuts for services, resources, and commands, and provides instant usage help.

    Key Capabilities:

    • Command Discovery: Find oci commands from thousands of possibilities using fuzzy matching.
    • Shortcuts: Use abbreviated service, resource, or command names in any order.
    • Parameter Expansion: Expand multi-word parameter shortcuts (e.g., -wfs to --wait-for-state).
    • Resource Resolution: Use resource names or partial OCIDs (e.g., the last 4-6 characters) instead of full OCIDs.
    • Simplified Datetime: Use human-readable strings like today, today-36h, or now for time parameters.
    • Execution: Append go or . to the end of a command to execute the underlying oci command. Use ! to force execution.

    Command Ordering Rule: Options for o (like -o for formatting) must appear before the <command> specification. Options for oci must appear after the <command>.

    o <command> go
    o <command> .
    o <command> !
    o <command> help
  3. Resolve resource names to OCIDs with 'o'

    master

    The o tool maintains a local cache of resource names and OCIDs in $HOME/.oci/ocids. This allows you to use human-readable names or partial OCIDs in place of full OCIDs.

    Usage Patterns:

    • Partial OCIDs: Use the last four to six characters of an OCID to uniquely identify a resource.
    • Compartment/Name: Identify resources using the compartment/name pattern.
    • List OCIDs: Use o ocids <service> to instantly show full OCIDs for all resources in a service (e.g., o ocids compartment).
    • Specific Lookup: o ocid <compartment>/<name> shows the full OCID for that specific resource.
    o ocids compartment
    o ocid sales/bastion
  4. Manage the OCIDs mapping file

    master

    The o wrapper uses a local cache of OCID-to-name mappings stored at $HOME/.oci/ocids. This file is populated during the initial setup by running o <tenancy_ocid>.

    Working with Multiple Tenancies

    • Using Profiles: To set up or use a specific profile, set the OCI_CLI_PROFILE environment variable before running o <tenancy_ocid> or other o commands.
    • Separating OCID Files: By default, o uses a single file for all tenancies. To keep them separate, copy the $HOME/.oci/ocids file to a tenancy-specific directory. o will check the current working directory for ./ocids before checking $HOME/.oci/ocids.

    Maintenance

    • Pruning Stale Entries: If stale OCIDs cause name-matching issues, remove them using: o prune <resource-name>.
    • Fresh Start: To reset the cache, remove the file and re-run setup: rm $HOME/.oci/ocids && o <tenancy_ocid>.

    Security Note: Ensure your ocids files are secured with permissions set to 0600.

    o <tenancy_ocid>
    o prune <resource-name>
  5. Use the OCI CLI Interactive Mode

    master

    The OCI CLI provides an interactive mode to assist with command composition. Enabling this mode provides:

    • Suggestions and autocompletion for commands.
    • Color-coded suggestions to distinguish between required and optional parameters.
    • Quick references displayed alongside suggestions to explain command and parameter purposes.

    To enter interactive mode, use the -i flag.

    oci -i
  6. Advanced configuration for 'o'

    master

    While o does not have its own configuration file, it relies on standard OCI CLI environment variables to function correctly.

    Authentication

    If using instance_principal authentication, set the following environment variable to avoid passing --auth instance_principal to every command: OCI_CLI_AUTH=instance_principal

    Profile Selection

    To use a specific profile from your ~/.oci/config file, set: OCI_CLI_PROFILE=<profile_name>

    Passing Parameters

    Use standard CLI Environment Variables to ensure settings are passed through to the underlying oci commands executed by o.

  7. Locate OCI CLI service-specific examples

    master

    Examples for specific Oracle Cloud Infrastructure (OCI) services are organized within the services/ directory of the repository. To find examples for a particular service, navigate to its corresponding service directory under services/examples_and_test_scripts/.

    Common locations include:

    • Database examples: services/database/examples_and_test_scripts/
    • Core service examples (Compute, Networking, Block Volume, etc.): services/core/examples_and_test_scripts/
  8. Install o on Windows

    master

    On Windows, o runs in PowerShell or Command shell. Installation is not automated.

    1. Download o using curl:
      curl -so o.py https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/examples/project_o/o
    2. Copy the downloaded file to a directory in your PATH.
    3. Update your PATHEXT environment variable so that you can execute o directly instead of typing o.py.
    curl -so o.py https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/examples/project_o/o
  9. Maintain and Update o

    master

    To keep o and its command cache up to date with the latest oci CLI services:

    Updating o

    Re-run the installation command used during initial setup.

    Updating Command Cache

    After upgrading the oci CLI, you must refresh the command list to include new services:

    rm $HOME/.oci/oci_commands
    o oci_commands

    Updating OCID Cache

    If your $HOME/.oci/ocids file becomes unusable or you want a fresh list of compartments, remove it and re-initialize:

    rm $HOME/.oci/ocids
    o <tenancy_ocid>
  10. Install the OCI CLI on Windows

    master

    Use PowerShell to download and execute the installation script for Windows.

    powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.ps1'))"