#!/bin/bash ## Helper funcs pinfo() { echo -e "\e[32m$@\e[39m" } perror() { echo -e "\e[31m$@\e[39m" exit 1 } dls() { path=$1 shift curl -s -H 'Content-Type: application/json' -H 'Authorization: '$DLS_ADM_TOKEN'' http://127.0.0.1:7606${path} "$@" } kctl() { kubectl --kubeconfig $ctxdir/kubeconfig "$@" } extract_var() { where=$1 what=$2 grep -rh ' $what: ' $ctxdir/$where/*.yaml|awk '{print $2}' } ## Run funcs prereqs() { [ $UID != 0 ] && perror This program needs to be run as root. Aborting... set -ex cd .. ctxdir=$PWD } check_conf() { all_clusters=$(ls $ctxdir/clusters|wc -l) if [ "$all_clusters" != "1" ]; then perror "Those helper scripts are not capable of running several clusters at once, check your configuration. Aborting..." fi } fresh_start() { rm -rf $ctxdir/secrets } unlock_store() { # Unlock DLS store after checking if online # store_state=$(curl -sw %{http_code} localhost:7606/hosts -o /dev/null) [ "$?" != 0 ] && perror "Direktil Local Server seems not up, please fix. Aborting." if [ "$store_state" == "401" ]; then pinfo Unlocking the DLS store ... DLS_ADM_TOKEN=$(dls /public/unlock-store -d "\"${DLS_UNLOCK_TOKEN}\""|tr -d \") pinfo Admin access token is $DLS_ADM_TOKEN echo $DLS_ADM_TOKEN > $ctxdir/secrets/.dls_adm_token chmod 400 > $ctxdir/secrets/.dls_adm_token fi } get_hosts() { hosts_files=$(basename $ctxdir/hosts/*.yaml|sed 's/.yaml//') for h in ${hosts_files}; do ip=$(grep ip: $ctxdir/hosts/${h}.yaml|awk '{print $2}') hosts[$h]="$ip" done } get_parts() { for host in ${!hosts[*]}; do mkdir -p $ctxdir/data/$host for part in kernel initrd-v2 do partfile=$ctxdir/data/$host/$part test -f $partfile || dls /hosts/$host/$part -o $partfile done diskfile=$ctxdir/data/$host/disk.raw test -f $diskfile || truncate -s ${QEMU_DISK_SIZE:-30G} $diskfile done } create_kubeconfig() { adm_token=$(dls /clusters/base/tokens/admin) ca_cert=$(dls clusters/base/CAs/cluster/certificate|base64 -w0) vip=$(extract_var clusters vip) vip=$(extract_var clusters api_port) pinfo "Writing new kubeconfig conf in $ctxdir directory, you may want to move it to \~/.kube/ directory for usability" sed -e "s/__VIP_IP__/$vip/" \ -e "s/__VIP_API_PORT__/$vip_api_port/" \ -e "s/__CA_CERT__/$ca_cert/" \ -e "s/__ADM_TOKEN__/$adm_token/" \ scripts/.template.kubeconfig > kubeconfig } clean() { sudo iptables -t nat -D POSTROUTING -j MASQUERADE -s $QEMU_BR_IP \! -o $QEMU_BR_NAME &>/dev/null sudo iptables -D FORWARD -o $QEMU_BR_NAME -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT &>/dev/null sudo iptables -D FORWARD -j ACCEPT -i $QEMU_BR_NAME &>/dev/null }