-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-instance.sh
executable file
·110 lines (91 loc) · 3.67 KB
/
init-instance.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
if [[ $DEBUG = "1" ]]; then
set -x
fi
set -e
set -o pipefail
if [[ $# -eq 0 ]]; then
echo "Usage: $0 <instance-name>"
exit 1
fi
NAME=$1
HYPERVISOR="hyperkit"
if [[ $2 = "--virtualbox" ]]; then
HYPERVISOR="virtualbox"
fi
# Configure Multipass to use the correct hypervisor
CURRENT=$(sudo multipass get local.driver)
if [[ $HYPERVISOR = "virtualbox" ]]; then
if [[ ! $CURRENT = "virtualbox" ]]; then
sudo multipass set local.driver=virtualbox
sleep 5
echo "==> Configured Multipass to use VirtualBox; exiting, please re-run the script with the same parameters again!"
exit 0
else
echo "--> Multipass is already configured to use VirtualBox, continuing…"
fi
else
if [[ ! $CURRENT = "hyperkit" ]]; then
sudo multipass set local.driver=hyperkit
sleep 5
echo "==> Configured Multipass to use HyperKit; exiting, please re-run the script with the same parameters again!"
exit 0
else
echo "--> Multipass is already configured to use HyperKit, continuing…"
fi
fi
# Spawn a new instance with a name given as first argument to the script
# If needed, can specify `focal` or other Ubuntu codename as last argument
# By default, Multipass will spawn a new instance with the latest stable Ubuntu,
# which is `focal` at the time of the script creation
echo "==> Creating a new VM, this might take a while…"
multipass launch --name "$NAME"
# Transfer setup script to the new instance
echo "==> Installing Docker inside the VM…"
multipass transfer setup-instance.sh "$NAME":/home/ubuntu/setup-instance.sh
# Execute script
multipass exec "$NAME" -- /bin/bash -x "/home/ubuntu/setup-instance.sh"
# Enable passwordless SSH to the Multipass instance
# First, let’s create a new SSH key pair
if [ -f ~/.ssh/id_multipass_docker ]; then
echo "--> SSH key already exists, skipping and reusing ~/.ssh/id_multipass_docker…"
else
echo "==> Creating SSH key pair…"
ssh-keygen -t ed25519 -a 100 -f ~/.ssh/id_multipass_docker -N ""
fi
# Add the key to list of authorized keys
echo "==> Setting up passwordless SSH…"
multipass transfer ~/.ssh/id_multipass_docker.pub "$NAME":/home/ubuntu/.ssh/id_multipass_docker.pub
multipass exec "$NAME" -- /bin/bash -c "cat /home/ubuntu/.ssh/id_multipass_docker.pub >> /home/ubuntu/.ssh/authorized_keys"
# Find out IP address & connection method to the instance
IP_ADDRESS=$(multipass info docker | grep IPv4 | awk '{ print $2; }')
PORT=22
if [[ $HYPERVISOR = "virtualbox" ]]; then
IP_ADDRESS="localhost"
PORT=$(sudo VBoxManage showvminfo docker --machinereadable | grep ssh | sed 's/^.*="\(.*\)"$/\1/' | awk 'BEGIN { FS = "," }; { print $4 }')
fi
echo "--> VM detected at ${IP_ADDRESS}:${PORT}…"
# Alter ~/.ssh/config
if grep "# -- ml-multipass-docker - begin" ~/.ssh/config > /dev/null; then
echo "==> Existing host multipass found in SSH config, removing…"
perl -i.backup -p0e 's/# -- ml-multipass-docker - begin --.*# -- ml-multipass-docker - end --//sm' ~/.ssh/config
fi
echo "==> Modifying ~/.ssh/config…"
{
echo
echo "# -- ml-multipass-docker - begin --"
echo "Host multipass"
echo " HostName $IP_ADDRESS"
echo " Port $PORT"
echo " User ubuntu"
echo " IdentityFile ~/.ssh/id_multipass_docker"
echo "# -- ml-multipass-docker - end --"
} >> ~/.ssh/config
if docker context ls | grep multipass > /dev/null; then
echo "--> Docker context multipass exists, skipping creation…"
else
echo "==> Creating Docker context…"
docker context create multipass --docker "host=ssh://multipass"
fi
docker context use multipass
echo "=> You should be good to go! Try running \`docker container run hello-world\` now!"