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=$(tamcloud 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]}" done # Don't run command-line tools if there's nothing to do if [ -z "${disk_ids}" ]; then echo Nothing to destroy else tamcloud destroy disks ${disk_ids} fi
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. TAMCLOUD_ACCESS_TOKEN=8deb14a832a663e40d79916404f5b96ceee2985a1488bcf057917e8c67b5bf6e # We can also choose to cache resource data locally, instead of in the user's home directory. TAMCLOUD_HOME=$(pwd) # 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. TAMCLOUD_AUTOUPGRADE=no while true; do disk_ids=$(tamcloud list unattached disks --columns I) for disk_id in ${disk_ids}; do tamcloud attach disk ${disk_id} to vm "My Backup Worker" ssh [email protected] -i path/to/public.key "dd bs=1M < /dev/xvda2 | gzip > /backups/${disk_id}/$(date +%FT%T)" tamcloud detach disk ${disk_id} done echo See you tomorrow! sleep 86400 done
Please refer to the command-line help pages for more documentation.