Civo CLI

repository·master·Indexed 41 days ago

https://github.com/civo/cli

A command-line tool for managing Civo.com accounts, enabling automation of infrastructure management for Kubernetes clusters, instances, and networking. Features include instance lifecycle control, snapshot management, firewall configuration, and Kubernetes cluster orchestration.

Tokens
74.7K
Snippets
372
Records
408
Agent score
48%

What's inside Civo CLI

  1. Understand the Civo CLI architecture

    master

    The Civo CLI is built using the following core components:

    • Cobra: The base framework used to build the CLI structure and commands.
    • Civogo: The Go interface used to interact with the Civo API.
    • Go-update: Enables the binary to perform auto-updates.
    • UI Libraries: Uses spinner for waiting indicators, color for terminal messages (Windows compatible), and tablewriter for generating text-based tables.
    • Go-homedir: Used to resolve the system user's home directory.

    For detailed information on the underlying API, refer to the Civo API documentation.

  2. Manage Domains and DNS Records

    master

    Civo allows you to host DNS for domains registered elsewhere. To use Civo DNS, you must point your domain's nameservers to ns0.civo.com and ns1.civo.com via your registrar.

    Domain Management

    • Create a domain: civo domain create <domainname>
    • List domains: civo domain list
    • Delete a domain: civo domain remove <domain_id> (Warning: This immediately removes the domain and all associated DNS records).

    DNS Record Management

    To create a record, use civo domain record create <domain_id> [options].

    Record Creation Options:

    • -n, --name <string>: The name of the record.
    • -p, --priority <int>: The priority (required for MX records).
    • -t, --ttl <int>: The TTL of the record (default: 600).
    • -e, --type <string>: The record type (A, CNAME, TXT, SRV, MX).
    • -v, --value <string>: The value of the record.

    Record Operations:

    • List records for a domain: civo domain record list <domain.name>
    • Delete a record: civo domain record remove <record_id>
    # Create a domain
    civo domain create civoclidemo.xyz
    
    # Create an A record
    civo domain record create civoclidemo.xyz -n www -t 600 -e A -v 192.168.1.1
    
    # List records
    civo domain record list civoclidemo.xyz
    
    # Remove a record
    civo domain record remove 4e181dde-bde8-4744-8984-067f957a7d59
  3. Run Civo CLI via Docker

    master

    If you prefer to keep your base OS clean, you can run the Civo CLI inside a Docker container. To persist your credentials, you must mount your .civo.json file as a volume.

    Basic usage with API key persistence:

    touch $HOME/.civo.json
    docker run -it --rm -v $HOME/.civo.json:/.civo.json civo/cli:latest

    Usage with Kubernetes (mounting kubeconfig): To use kubectl (which is included in the image), mount both your .civo.json and your ~/.kube/config.

    Recommended Alias (Ubuntu/Debian): Add this to your ~/.bash_profile or ~/.zshrc to use the Docker container as if it were a local binary:

    alias civo="docker run -it --rm -v $HOME/.civo.json:/.civo.json civo/cli:latest"
    # Basic Docker run
    touch $HOME/.civo.json
    docker run -it --rm -v $HOME/.civo.json:/.civo.json civo/cli:latest
    
    # Docker run with Kubernetes config
    touch $HOME/.civo.json
    mkdir $HOME/.kube/
    touch $HOME/.kube/config
    docker run -it --rm -v $HOME/.civo.json:/.civo.json -v $HOME/.kube/config:/root/.kube/config civo/cli:latest
  4. Install applications onto a new Kubernetes cluster

    master

    When creating a new Kubernetes cluster, you can specify a comma-separated list of marketplace applications to install by using the --applications flag.

    Note: Certain applications like metrics-server are installed by default. To prevent default applications from being installed, use the --remove-applications flag.

    # Install specific applications during cluster creation
    civo kubernetes create apps-demo-cluster --nodes=2 --applications=Redis,Linkerd
    
    # Create a cluster while removing specific default applications
    civo kubernetes create apps-demo-cluster --nodes=2 --applications=Redis,Linkerd --remove-applications=metrics-server
  5. Manage Private Networks

    master

    Civo provides private networking to isolate instances from each other (e.g., separating database servers from application servers).

    Network Operations

    • Create a network: civo network create <network_label>
    • List networks: civo network list or civo network ls
    • Remove a network: civo network remove <network_id>
    # Create a new private network
    civo network create cli-demo
    
    # List all networks
    civo network ls
    
    # Remove a network
    civo network remove 74b69006-ea59-46a0-96c4-63f5bfa290e1
  6. Enable bash autocompletion for Civo CLI

    master

    The Civo CLI supports bash autocompletion. Before enabling Civo's completion, you must ensure bash-completion is installed and sourced in your shell. You can verify bash-completion is working by running type _init_completion.

    1. Install and verify bash-completion

    Install via your package manager (e.g., apt-get install bash-completion or yum install bash-completion). If type _init_completion fails after installation, add the following to your ~/.bashrc:

    source /usr/share/bash-completion/bash_completion

    2. Enable Civo completion

    You can enable Civo autocompletion using one of two methods:

    Method A: Source in ~/.bashrc (Recommended for users)

    echo 'source <(civo completion bash)' >> ~/.bashrc

    Method B: System-wide via /etc/bash_completion.d

    civo completion bash > /etc/bash_completion.d/civo

    3. Handle aliases

    If you use an alias for civo (e.g., alias c=civo), extend the completion by adding this to your ~/.bashrc:

    echo 'alias c=civo' >> ~/.bashrc
    echo 'complete -F __start_civo c' >> ~/.bashrc
    echo 'source <(civo completion bash)' >> ~/.bashrc
  7. Authenticate with Civo API Keys

    master

    To use the CLI, you need an API key from https://www.civo.com/api.

    Using Environment Variables: You can set the CIVO_TOKEN environment variable. If set, the CLI will use this token instead of the one in the config file. This is ideal for CI/CD pipelines.

    Managing Keys via CLI:

    1. Add a key: civo apikey add <apikey_name> <apikey>
    2. Set default key: civo apikey current <apikey_name> (sets the key for subsequent commands).
    3. List keys: civo apikey list
    4. Remove a key: civo apikey remove <apikey_name>
    5. Show secret key: civo apikey show <apikey_name> (if no name is provided, it shows the default key).

    Configuration File: By default, credentials and settings are saved to $HOME/.civo.json. You can change this location by setting the CIVO_CONFIG environment variable.

    # Add a new API key
    civo apikey add Demo_Test_Key DAb75oyqVeaE7BI6Aa74FaRSP0E2tMZXkDWLC9wNQdcpGfH51r
    
    # Set the default key to use
    civo apikey current Demo_Test_Key
    
    # List all stored keys
    civo apikey list
    
    # Show the secret for a specific key
    civo apikey show my_username
  8. List available Kubernetes applications from the Marketplace

    master

    You can browse the available applications in the Civo Kubernetes Marketplace directly from your terminal. This allows you to see which applications are currently supported for installation on your clusters.

    Use the civo kubernetes apps list command to retrieve the most up-to-date list of applications.

    civo kubernetes apps list
  9. Configure Firewalls and Rules

    master

    Firewalls allow you to control traffic to and from your instances. By default, all ports are closed for custom firewalls.

    Firewall Management

    • Create a firewall: civo firewall create <new_firewall_name>
      • Use --no-default-rules to create a firewall without any default rules.
      • Note: --create-rules=false is a deprecated way to achieve the same result.
    • List firewalls: civo firewall list
    • Delete a firewall: civo firewall remove <firewall_id>

    Firewall Rule Management

    Rules are added to a firewall using civo firewall rule create <firewall_id> [options].

    Rule Creation Options:

    • -a, --action <string>: allow or deny (default: allow).
    • -c, --cidr <string>: The CIDR range (default: 0.0.0.0/0).
    • -d, --direction <string>: ingress or egress (default: ingress).
    • -e, --endport <string>: The end port of the rule.
    • -l, --label <string>: A display name/reference for the rule.
    • -p, --protocol <string>: TCP, UDP, or ICMP (default: TCP).
    • -s, --startport <string>: The start port of the rule.

    Rule Operations:

    • List rules for a firewall: civo firewall rule list <firewall_id> (or civo firewall rule ls <firewall_id>)
    • Delete a rule: civo firewall rule remove <firewall_id> <rule_id>
    # Create a firewall without default rules
    civo firewall create my_secure_firewall --no-default-rules
    
    # Create an ingress rule for SSH
    civo firewall rule create <firewall_id> --startport=22 --direction=ingress --label='SSH access' -a allow
    
    # List rules
    civo firewall rule list <firewall_id>
    
    # Remove a rule
    civo firewall rule remove <firewall_id> <rule_id>
  10. Install applications that require storage plans

    master

    Some applications (such as database apps) require a storage plan. If you do not specify a plan during installation, the CLI will prompt you to choose from available sizes.

    To bypass the interactive prompt in the future, you can use the syntax AppName:Size (e.g., mariadb:10GB) when adding the application.

    # Interactive installation (will prompt for plan)
    civo kubernetes applications add mariadb --cluster=apps-demo
    
    # Automated installation using the 'Name:Size' syntax
    # (Note: The exact syntax for automated selection is suggested by the CLI prompt)
    # Example: civo kubernetes applications add "mariadb:10GB" --cluster=apps-demo
  11. Install the Civo CLI on Linux

    master

    There are several ways to install the Civo CLI on Linux:

    1. Direct Shell Script: Use curl to run the installer.
    2. Homebrew: Use the brew package manager.
    3. Manual Download (wget): Download the specific release tarball, extract it, and move the binary to /usr/local/bin/.
    4. Build from Source: Clone the repository and use make (requires Go).
    # Direct Shell Script
    curl -sL https://civo.com/get | sh
    
    # Manual Download (Example for v1.0.40)
    wget https://github.com/civo/cli/releases/download/v1.0.40/civo-1.0.40-linux-amd64.tar.gz
    tar -xvf civo-1.0.40-linux-amd64.tar.gz
    chmod +x civo
    mv ./civo /usr/local/bin/
    
    # Build from Source
    git clone https://github.com/civo/cli.git
    cd cli
    make
    cd ..
    cp -r cli $HOME
    export PATH="$HOME/cli:$PATH"
  12. Enable zsh autocompletion for Civo CLI

    master

    To enable Civo autocompletion in Zsh, you need to source the completion script in your ~/.zshrc file.

    1. Basic Setup

    Add the following to your ~/.zshrc:

    source <(civo completion zsh)

    2. Troubleshooting compdef errors

    If you encounter the error complete:13: command not found: compdef, add these lines to the beginning of your ~/.zshrc:

    autoload -Uz compinit
    compinit

    3. Alternative: Auto-load via fpath

    To set the completion code to auto-load on startup, you can run:

    civo completion zsh > "${fpath[1]}/_civo"

    4. Handle aliases

    If you use an alias for civo (e.g., alias c=civo), add these to your ~/.zshrc:

    echo 'alias c=civo' >> ~/.zshrc
    echo 'complete -F __start_civo c' >> ~/.zshrc
    source <(civo completion zsh)