#!/bin/bash command -v multipass >/dev/null 2>&1 || return multipass_launch () { subdir=${MULTIPASS_HOME:-multipass} [[ $1 ]] && name="-n $1" if [[ $2 ]]; then file="$HOME/$subdir/cloud-init/$2.yaml" if [[ -f $file ]]; then init="--cloud-init $file" else echo no cloud init file $file return 1 fi fi echo running: multipass launch $name $init multipass launch $name $init } multipass_remove () { [[ ! $1 ]] && echo must supply an instance name && return 1 module_load confirm confirm "delete and purge instance $1?" || return 1 multipass delete -p $1 } multipass_get_sshid () { dest=${1:-$HOME/.ssh/multipass_id_rsa} [[ -f $dest ]] && echo $dest && return 0 src=${SNAP_VAR:-/var/snap}/multipass/common/data/multipassd/ssh-keys/id_rsa [[ ! -f $src ]] && return 1 sudo cp $src $dest sudo chown $USER:$USER $dest echo $dest } multipass_get_ip () { [[ ! $1 ]] && return 1 json=$(multipass info --format json $1 2> /dev/null) [[ ! $json ]] && return 2 ip=$(echo "$json" | jq -r .info.$1.ipv4[0] 2> /dev/null) [[ $ip ]] && echo $ip || return 3 return 0 } multipass_ssh () { [[ ! $1 ]] && echo must supply name of instance name && return 1 ip=$(multipass_get_ip $1) [[ ! $ip ]] && echo "no ip for instance $1, aborting" && return id=$(multipass_get_sshid) user=${2:-ubuntu} echo ssh -i $id $user@$ip ssh -i $id $user@$ip } multipass_scp () { [[ $1 = "-u" ]] && user=$2 && shift 2 || user=ubuntu [[ ! $1 ]] && echo must supply name of instance name && return 1 ip=$(multipass_get_ip $1) [[ ! $ip ]] && echo "no ip for instance $1, aborting" && return id=$(multipass_get_sshid) cat $id return echo sudo scp -i $id -r $2 $user@$ip:$3 sudo scp -i $id $2 $user@$ip:$3 }