#!/bin/bash required_bins=( ip sudo debconf-set-selections ) check_bins() { # Append any needed binaries we need to check for, to our list if [[ $1 ]]; then required_bins+=("$1") fi for binary in "${required_bins[@]}"; do if ! [ -x "$(command -v "$binary")" ]; then printf "Error: Necessary program '%s' is not installed. Please fix, aborting now.\n\n" "$binary" >&2 exit 1 fi done } read_config() { if [ ! -f maas.config ]; then printf "Error: missing config file. Please create the file 'maas.config'.\n" exit 1 else shopt -s extglob maas_config="maas.config" source "$maas_config" fi if [ ! -f maas.debconf ]; then printf "Error: missing debconf file. Please create the file 'maas.debconf'.\n" exit 1 fi } # Initialize some vars we'll reuse later in the build, bootstrap init_variables() { echo "MAAS Endpoint: $maas_endpoint" echo "MAAS Proxy: $maas_local_proxy" core_packages=( jq moreutils uuid ) maas_packages=( maas maas-cli maas-proxy maas-dhcp maas-dns maas-rack-controller maas-region-api maas-common ) pg_packages=( postgresql-10 postgresql-client postgresql-client-common postgresql-common ) } remove_maas() { # Drop the MAAS db ("maasdb"), so we don't risk reusing it sudo -u postgres psql -c "select pg_terminate_backend(pid) from pg_stat_activity where datname='maasdb'" sudo -u postgres psql -c "drop database maasdb" # Remove everything, start clean and clear from the top sudo DEBIAN_FRONTEND=noninteractive apt-get -y remove --purge "${maas_packages[@]}" "${pg_packages[@]}" && \ sudo apt-get -fuy autoremove # Yes, they're removed but we want them PURGED, so this becomes idempotent for package in "${maas_packages[@]}" "${pg_packages[@]}"; do sudo dpkg -P "$package" done } install_maas() { # This is separate from the removal, so we can handle them atomically sudo apt-get -fuy --reinstall install "${core_packages}" "${maas_packages[@]}" "${pg_packages[@]}" sudo sed -i 's/DISPLAY_LIMIT=5/DISPLAY_LIMIT=100/' /usr/share/maas/web/static/js/bundle/maas-min.js } purge_admin_user() { read -r -d '' purgeadmin < clouds-"$rand_uuid".yaml <=DEBUG EOF cat > credentials-"$rand_uuid".yaml < config-"$rand_uuid".yaml < Do EVERYTHING (maas, juju cloud, juju bootstrap) -b Build out and bootstrap a new MAAS -c Add a new cloud + credentials -i Just install the dependencies and exit -j Bootstrap the Juju controller called -n Create MAAS kvm nodes (to be imported into chassis) -r Remove the entire MAAS server + dependencies -t Tear down the cloud named " } if [ $# -eq 0 ]; then printf "%s needs options to function correctly. Valid options are:" "$0" show_help exit 0 fi # Load up some initial variables from the config and package arrays init_variables read_config # This is the proxy that MAAS itself uses (the "internal" MAAS proxy) no_proxy="localhost,127.0.0.1,$maas_system_ip,$(echo $maas_ip_range.{100..200} | sed 's/ /,/g')" while getopts ":a:bc:ij:nt:r" opt; do case $opt in a ) check_bins remove_maas install_maas build_maas bootstrap_maas add_cloud "$OPTARG" ;; b ) echo "Building out a new MAAS server" check_bins install_maas build_maas bootstrap_maas exit 0 ;; c ) check_bins maas init_variables add_cloud "$OPTARG" ;; i ) echo "Installing MAAS and PostgreSQL dependencies" install_maas exit 0 ;; j ) echo "Bootstrapping Juju controller $OPTARG" add_cloud "$OPTARG" exit 0 ;; r ) remove_maas exit 0 ;; t ) destroy_cloud "$OPTARG" exit 0 ;; \? ) printf "Unrecognized option: -%s. Valid options are:" "$OPTARG" >&2 show_help exit 1 ;; : ) printf "Option -%s needs an argument.\n" "$OPTARG" >&2 show_help echo "" exit 1 ;; esac done