-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstart-vm.bash
More file actions
executable file
·172 lines (149 loc) · 3.62 KB
/
Copy pathstart-vm.bash
File metadata and controls
executable file
·172 lines (149 loc) · 3.62 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
function usage () {
APP_NAME=$(basename "$0")
cat <<EOT
NAME
$APP_NAME
USAGE
$APP_NAME MACHINE_NAME [MACHINE_SIZE] [UBUNTU_VERSION]
SYNOPSIS
$APP_NAME will start a Multipass virtual machine or
create it if it does not exist. It looks for a cloud init
YAML file using the machine name followed by "-init.yaml".
MACHINE SIZES
The machine sizes are based on AWS T4g series of machine
sizes. Sizes include nano, micro, small, medium, large,
xlarge and 2xlarge. The CPU count and memory settings
see https://aws.amazon.com/ec2/instance-types/ or read
the source file for $APP_NAME.
IMAGE
A specific image we'll be using. The current use case is to
indicate a specific version of ubuntu.
EXAMPLE
In this example we'll start an machine name "invenio" and
if it does not exist it will be created with a size of xlarge
utilizing ubuntu 20.04 (focal).
$APP_NAME invenio xlarge focal
EOT
}
#
# Check if we're providing a machine name or using the existing primary name
#
if [ "$1" = "" ]; then
# MACHINE=$(multipass get client.primary-name)
usage
exit 0
else
case "$1" in
-h*)
usage
exit 0
;;
help)
usage
exit 0
;;
*)
MACHINE="$1"
;;
esac
fi
#
# Machine size is based on AWS T4g ARM machine sizes
# See https://aws.amazon.com/ec2/instance-types/
#
if [ "$2" = "" ]; then
MACHINE_SIZE="--cpus 2 --memory 8G --disk 50G"
else
case "$2" in
nano)
MACHINE_SIZE="--cpus 2 --memory 512M --disk 50G"
;;
micro)
MACHINE_SIZE="--cpus 2 --memory 1G --disk 50G"
;;
small)
MACHINE_SIZE="--cpus 2 --memory 2G --disk 50G"
;;
medium)
MACHINE_SIZE="--cpus 2 --memory 4G --disk 50G"
;;
large)
MACHINE_SIZE="--cpus 2 --memory 8G --disk 50G"
;;
xlarge)
MACHINE_SIZE="--cpus 4 --memory 16G --disk 100G"
;;
2xlarge)
MACHINE_SIZE="--cpus 8 --memory 32G --disk 150G"
;;
esac
fi
#
#Third cli option is what image we're using
#
if [ "$3" != "" ]; then
IMAGE="$3"
else
IMAGE="jammy"
fi
#
# Figure out if we're launching, starting or machine is already active
#
if multipass list | grep "$MACHINE" >/dev/null; then
VM_STATE=$(multipass info "$MACHINE" | grep -i State)
case "${VM_STATE}" in
*Stopped)
multipass start "$MACHINE"
;;
*Running)
echo "$MACHINE is already running"
;;
*)
echo "VM is $VM_STATE, wait and run again"
exit 1
esac
else
CLOUD_INIT="$MACHINE-local.yaml"
if [ ! -f "$CLOUD_INIT" ]; then
# Handle the transition from machine name "-init.yaml" to machine name dot yaml
if [ -f "${MACHINE}-init.yaml" ]; then
CLOUD_INIT="${MACHINE}-init.yaml"
else
CLOUD_INIT="${MACHINE}.yaml"
fi
fi
echo "Launching $MACHINE";
multipass -v launch \
--name "${MACHINE}" \
--cloud-init "${CLOUD_INIT}" \
$MACHINE_SIZE \
$IMAGE
echo "Restart ${MACHINE}"
multipass restart "${MACHINE}"
fi
#
# If a src directory exists mount it
#
#if [ -d src ]; then
# multipass mount src "$MACHINE:src"
#fi
#
# If a Sites directory exists mount it
#
#if [ -d Sites ]; then
# multipass mount Sites "$MACHINE:Sites"
#fi
#
# Include staff-favorites.bash if exists
#
if [ -f scripts/staff-favorites.bash ]; then
multipass transfer scripts/staff-favorites.bash "$MACHINE:."
fi
#if [ -f scripts/setup-self-signed-SSL-certs.bash ]; then
# multipass transfer scripts/setup-self-signed-SSL-certs.bash "$MACHINE:."
#fi
#
# Display state of machine
#
multipass info "$MACHINE"