shell-host/install/distro/debian/ubuntu/multipass/multipass.func

94 lines
2.3 KiB
Bash

#!/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_key}
[[ -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
chmod 600 $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 = "null" ]] && return 3
[[ $ip ]] && echo $ip || return 4
return 0
}
multipass_ssh_options () {
id=$(multipass_get_sshid)
[[ $? -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; }
user=${2:-ubuntu}
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
}
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
}
multipass_ssh_script () {
cmd=$(multipass_ssh_cmd $2) || { echo failed: $cmd;return $?; }
module_load remote
remote_script $1 "$cmd"
}