#!/bin/bash
# cloud - Cloud management support
set -e
######
# Settings
sys_cloud_config_init() {
lib_setting_vars -ro cloud_confdir cloud_datadir
lib_setting_vars --null cloud_domain
lib_setting_arrays \
cloud_net_dns cloud_net_addrs cloud_net_masks
cloud_confdir="$confdir/cloud"
cloud_datadir="$datadir/cloud"
}
######
# Clouds
cloud_filename() {
local -a parts=( "$cloud_confdir" "$@" )
local IFS="/"
echo "${parts[*]}"
}
cloud_list() {
[ -d "$cloud_confdir" ] || error "no clouds exist"
(cd "$cloud_confdir" && find . -type d | sed -e 's,^\./,,' -e '/^\.$/d')
}
cloud_exists() {
has_args 1 "$@"
local cloud=$1
local dir
dir=$(cloud_filename "$cloud")
test -d "$dir"
}
cloud_create() {
has_args 1 "$@"
local cloud=$1
! cloud_exists "$cloud" || error "$cloud: cloud exists"
local dir
dir=$(cloud_filename "$cloud")
run_mkdir "$dir"
run touch "$dir/hosts"
}
cloud_delete() {
has_args 1 "$@"
local cloud=$1
cloud_exists "$cloud" || error "$cloud: cloud does not exist"
local dir
dir=$(cloud_filename "$cloud")
run rm -rf "$dir"
}
for_each_cloud() { min_args 1 "$@"; for_each "$*" $(cloud_list); }
######
# Cloud networks
# $cloud/networks file format:
# $netname:$netaddr:$netmask
cloud_networks() {
has_args 1 "$@";
sed -e 's,:.*$,,' <"$(cloud_filename "$1" networks)"
}
######
# Cloud hosts
# $cloud/hosts file format:
# $hostname
cloud_hosts_filename() { echo "$(cloud_filename "$1" hosts)"; }
cloud_hosts() {
has_args 1 "$@";
grep -v -e '^#' -e '^$' "$(cloud_hosts_filename "$1")"
}
cloud_host_exists() {
has_args 2 "$@"
grep "^$2$" "$(cloud_hosts_filename "$1")" >/dev/null
}
cloud_host_enable() {
has_args 1 "$@";
run sed -i "s,^# *$2$,$2," "$(cloud_hosts_filename "$1")"
}
cloud_host_disable() {
has_args 1 "$@";
run sed -i "s,^$2$,# $2," "$(cloud_hosts_filename "$1")"
}
cloud_host_add() {
min_args 2 "$@"
local cloud=$1
shift
for_each _cloud_host_add "$@"
}
_cloud_host_add() {
local name=$1
if cloud_host_exists "$cloud" "$name"; then
error "$cloud: host '$name' already part of cloud"
fi
echo "$name" >>"$(cloud_hosts_filename "$cloud")"
}
cloud_host_remove() {
min_args 2 "$@"
local cloud=$1
shift
for_each _cloud_host_remove "$@"
}
_cloud_host_remove() {
local name=$1
if ! cloud_host_exists "$cloud" "$name"; then
error "$cloud: host '$name' not part of cloud"
fi
local hosts tmp
hosts=$(cloud_hosts_filename "$cloud")
tmp=$(cmd_tempfile)
run grep -v "^$name$" "$hosts" >"$tmp"
run mv "$tmp" "$hosts"
rm -f "$tmp"
}
for_each_cloud_host() {
min_args 2 "$@"
local cloud=$1
shift
for_each "$*" $(cloud_hosts "$cloud")
}
######
# Cloud package management
# $1 - cloud name
# $2 - package class name
cloud_class_install() {
min_args 2 "$@"
local cloud=$1
for_each_cloud_host $cloud cloud_host_install "$@"
}
# $1 - cloud name
# $2 - package class name
# $3 - host name
cloud_class_install_host() {
min_args 3 "$@"
local cloud=$1
local class=$2
shift 2
local args=( "$@" )
local name
name=$(list_pop args)
info "$cloud: $name: cloud host $class install: starting..."
if is_localhost $name; then
tool_class_install $class "${args[@]}"
else
remote_class_install $name $class "${args[@]}"
fi
info "$cloud: $name: cloud host $class install: ... done!"
}
# $1 - cloud name
cloud_upgrade() {
has_args 1 "$@"
for_each_cloud_host $1 cloud_upgrade_host $cloud
}
# $1 - cloud name
# $2 - host name
cloud_upgrade_host() {
has_args 2 "$@"
local cloud=$1
local name=$2
info "$cloud: $name: cloud host upgrade: starting..."
if is_localhost "$name"; then
apt_upgrade
else
local -a opts=( --yes --quiet )
$verbose || list_append opts --quiet
local tmp
tmp=$(cmd_tempfile)
cat <<EOF >$tmp
apt update ${opts[*]}
apt dist-upgrade ${opts[*]}
apt autoremove ${opts[*]}
EOF
local installer="cloud_upgrade.sh"
remote_copy_to "$name" "$tmp" "$installer"
remote_sudo $name bash "$installer"
remote_run $name rm -f "$installer"
fi
info "$cloud: $name: cloud host upgrade: ... done!"
}
######
# Cloud backup and restore
cloud_backup() {
has_args 2 "$@"
local cloud=$1
local target=$2
for_each_cloud_host "$cloud" cloud_backup_host "$cloud" "$target"
}
cloud_backup_host() {
local cloud=$1
local target=$2
local name=$3
info "$cloud: $name: cloud host $target backup: starting..."
if [ "$name" = "$host" ]; then
backup_host "$name" "$target"
else
remote_backup "$name" "$target"
fi
info "$cloud: $name: cloud host $target backup: ... done!"
}
cloud_restore() {
has_args 2 "$@"
local cloud=$1
local target=$2
for_each_cloud_host "$cloud" cloud_restore_host "$cloud" "$target"
}
cloud_restore_host() {
local cloud=$1
local target=$2
local name=$3
info "$cloud: $name: cloud host $target restore: starting..."
if [ "$name" = "$host" ]; then
restore_host "$name" "$target"
else
remote_restore "$name" "$target"
fi
info "$cloud: $name: cloud host $target restore: ... done!"
}
Generated on Tue Apr 25 21:21:18 PDT 2017 by mcsh i7 v0.18.0.