2022-02-20 13:27:51 -08:00
|
|
|
#!/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 () {
|
2022-03-28 10:06:42 -07:00
|
|
|
dest=${1:-$HOME/.ssh/multipass_key}
|
2022-02-20 13:27:51 -08:00
|
|
|
[[ -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
|
2022-03-28 10:06:42 -07:00
|
|
|
chmod 600 $dest
|
2022-02-20 13:27:51 -08:00
|
|
|
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)
|
2022-03-28 10:06:42 -07:00
|
|
|
[[ $ip = "null" ]] && return 3
|
|
|
|
[[ $ip ]] && echo $ip || return 4
|
2022-02-20 13:27:51 -08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:06:42 -07:00
|
|
|
multipass_ssh_options () {
|
2022-02-20 13:27:51 -08:00
|
|
|
id=$(multipass_get_sshid)
|
2022-03-28 10:06:42 -07:00
|
|
|
[[ $? -gt 0 ]] && return 1
|
|
|
|
echo -o IdentitiesOnly=yes -o IdentityFile=$id
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
multipass_ssh_cmd () {
|
|
|
|
[[ ! $1 ]] && { echo must supply name of instance name; return 1; }
|
|
|
|
ip=$(multipass_get_ip $1)
|
|
|
|
[[ ! $ip ]] && { echo "no ip for instance $1"; return 2; }
|
2022-02-20 13:27:51 -08:00
|
|
|
user=${2:-ubuntu}
|
2022-03-28 10:06:42 -07:00
|
|
|
opts=$(multipass_ssh_options)
|
|
|
|
[[ ! $opts ]] && return 3
|
|
|
|
echo $opts $user@$ip
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
multipass_ssh () {
|
|
|
|
cmd=$(multipass_ssh_cmd $@) || { echo failed: $cmd;return $?; }
|
|
|
|
echo ssh $cmd
|
|
|
|
ssh $cmd
|
2022-02-20 13:27:51 -08:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:06:42 -07:00
|
|
|
multipass_scp () {
|
2022-02-20 13:27:51 -08:00
|
|
|
[[ $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
|
|
|
|
}
|
2022-03-28 10:06:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
multipass_ssh_script () {
|
|
|
|
cmd=$(multipass_ssh_cmd $2) || { echo failed: $cmd;return $?; }
|
|
|
|
module_load remote
|
|
|
|
remote_script $1 "$cmd"
|
|
|
|
}
|