Command Line Tools - Shell Scripting

Command Line Tools follows the Unix principle of each command performing one operation. You can customize its behaviour and output for fairly flexible scripting utilization.

The following example script destroys all disks created in the last 24 hours.

#!/usr/bin/env bash
set -e

# Destroy disks newer than this date (24 hours ago in milliseconds)
yesterday=$(expr $(date +%s) - 86400)000

# Get all disks in format CREATED_AT:ID
disks=$(joviam list disks -l C:I)

# Iterate through each disk
for disk in ${disks}; do

    # Separate into creation date and ID
    parts=(${disk//:/ })

    # If the date exceeds the threshold, add the ID to the list for destruction
    [ ${parts[0]} -gt ${yesterday} ] && disk_ids="${disk_ids} ${parts[1]}"

# Don't run command-line tools if there's nothing to do
if [ -z "${disk_ids}" ]; then
    echo Nothing to destroy
    joviam destroy disks ${disk_ids}

The default behaviour of Command Line Tools is optimised for interactive use in a command-line shell like bash. You can tweak its behaviour to better suit unmanned applications such as backup scripts.

The following example script backs up all unattached disks every 24 hours.

#!/usr/bin/env bash
set -e

# Instead of logging in as a user, we can use our organization's access token.

# We can also choose to cache resource data locally, instead of in the user's home directory.

# Command-line options can be set using the `set defaults` command. They can also be set
# environmentally, which is a better way to ensure your scripts run as expected. Here,
# we disable autoupgrade to ensure systems requiring `sudo` for installation won't cause
# failures when new versions are released.

while true; do
  disk_ids=$(joviam list unattached disks --columns I)
  for disk_id in ${disk_ids}; do
    joviam attach disk ${disk_id} to vm "My Backup Worker"
    ssh root@12.345.67.89 -i path/to/public.key "dd bs=1M < /dev/xvda2 | gzip > /backups/${disk_id}/$(date +%FT%T)"
    joviam detach disk ${disk_id}
  echo See you tomorrow!
  sleep 86400

Please refer to the command-line help pages for more documentation.