This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.sh
executable file
·317 lines (276 loc) · 8.17 KB
/
install.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/bin/bash
set -e -E -u -o pipefail
cd "$(dirname "$0")"
# command line arguments
RAM="${1:-4096}"
DISKSIZE="${2:-50}"
SHELL_INTEGRATION="${3:-auto}"
SUBNET="${4:-192.168.42}"
HOST_IP="${SUBNET}.1" # when given a 192.168.x.y IP, vagrant assigns 192.168.x.1 to the host
VM_IP="${SUBNET}.2"
ENV_LINE="export DOCKER_HOST='tcp://${VM_IP}:2376'"
ENV_LINE_FISH="set -x DOCKER_HOST 'tcp://${VM_IP}:2376';"
DOCKER_MACHINE_LINE="docker-machine env"
NFS_HOME="/System/Volumes/Data${HOME}"
EXPORTS_LINE="${NFS_HOME} ${VM_IP} -fspath=/System/Volumes/Data -alldirs -mapall=$(id -u):$(id -g)"
PLIST="$HOME/Library/LaunchAgents/com.dziemba.mobymac.plist"
error_banner() {
echo
echo
echo "+===============================================+"
echo "| ERRORS FOUND |"
echo "| Inspect logs and ask for assistance if needed |"
echo "+===============================================+"
echo
echo "For troubleshooting help, go to:"
echo "https://github.com/dziemba/mobymac#troubleshooting"
}
trap error_banner ERR
lineinfile() {
FILE="$1"
LINE="$2"
SUDO="${3-}"
TMP="$(mktemp)"
MARKER_BEGIN="# mobymac-begin"
MARKER_END="# mobymac-end"
$SUDO mkdir -p "$(dirname "$FILE")"
$SUDO touch "$FILE"
(
sed "/^${MARKER_BEGIN}/,/^${MARKER_END}/d" "$FILE"
echo "$MARKER_BEGIN"
echo "$LINE"
echo "$MARKER_END"
) > "$TMP"
# shellcheck disable=SC2002
cat "$TMP" |$SUDO tee "$FILE" > /dev/null
rm "$TMP"
}
removefromfile() {
FILE="$1"
EXPR="$2"
TMP="$(mktemp)"
sed "/${EXPR}/d" "$FILE" > "$TMP"
cat "$TMP" > "$FILE"
rm "$TMP"
}
gen_vagrantfile() {
cat <<EOD
# AUTOGENERATED DO NOT EDIT - see install.sh
VM_DIR = %x(VBoxManage list systemproperties).lines
.find { |l| l.start_with?('Default machine folder:') }
.split(':').last.strip
Vagrant.configure('2') do |config|
config.vm.box = 'debian/bullseye64'
config.vm.provider 'virtualbox' do |v|
v.name = 'mobymac'
v.cpus = $(sysctl -n hw.physicalcpu)
v.memory = ${RAM}
data_disk = "#{VM_DIR}/#{v.name}/data.vdi"
v.customize ['createhd', '--filename', data_disk, '--size', ${DISKSIZE} * 1024]
v.customize ['storageattach', :id, '--storagectl', 'SATA Controller',
'--port', 1, '--device', 0, '--type', 'hdd', '--medium', data_disk]
end
config.vm.synced_folder '.', '/vagrant', disabled: true
config.vm.provision :shell, path: 'provision_vm.sh', args: ['${HOST_IP}', '${HOME}', '${NFS_HOME}']
config.vm.network :private_network, ip: '${VM_IP}'
end
EOD
}
gen_plist() {
cat <<EOD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.dziemba.mobymac</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>$(pwd)/start.sh</string>
<string>boot</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOD
}
function main() {
echo "========="
echo " mobymac "
echo "========="
echo
echo "Creating a fresh docker VM with these settings:"
echo "RAM: ${RAM}MB"
echo "Data disk: ${DISKSIZE}GB"
echo "Shell integration: ${SHELL_INTEGRATION}"
echo "VM IP: ${VM_IP}"
echo
echo "WARNING: this will uninstall docker4mac and destroy all existing docker data"
echo "Press CTRL-C to abort (sleeping for 5 seconds)"
sleep 5
echo
if [ ! -e /usr/local/bin/brew ]; then
echo "Homebrew not installed - Aborting!"
exit 1
fi
echo "=== Checking for mobymac updates (set MOBYMAC_NO_UPDATES=1 to skip)..."
if [ -z "${MOBYMAC_NO_UPDATES-}" ]; then
git fetch
if [ "$(git rev-list HEAD...origin/master --count)" -ne 0 ]; then
echo "==> Updates available - please 'git pull', then run the installer again."
exit 2
fi
echo "==> OK"
else
echo "==> Skipping"
fi
echo
echo "=== Writing Vagrantfile..."
gen_vagrantfile > Vagrantfile
echo "=== OK"
echo
echo "=== Uninstalling Docker for Mac (brew cask)..."
if brew list --cask |grep ^docker$; then
brew uninstall --cask docker
echo "==> OK"
else
echo "==> Not found - skipping"
fi
echo
echo "=== Uninstalling Docker for Mac (native)..."
if [ -e /Applications/Docker.app ]; then
/Applications/Docker.app/Contents/MacOS/Docker --uninstall
rm -rf /Applications/Docker.app
echo "==> OK"
else
echo "==> Not found - skipping"
fi
echo
echo "=== Installing Virtualbox..."
if [ ! -e /Applications/VirtualBox.app ]; then
brew install --cask virtualbox
echo "==> OK"
else
echo "==> Already installed - skipping"
fi
echo "=== Version: $(VBoxManage --version)"
echo
echo "=== Installing Vagrant..."
if [ ! -e /usr/local/bin/vagrant ]; then
brew install --cask vagrant
echo "==> OK"
else
echo "==> Already installed - skipping"
fi
echo "=== Version: $(vagrant --version)"
echo
echo "=== Removing old docker packages..."
brew uninstall --force docker docker-compose docker-machine docker-machine-nfs
rm -f /usr/local/bin/{docker,docker-compose,docker-machine,docker-machine-nfs}
echo "==> OK"
echo
echo "=== Installing docker packages..."
brew install docker docker-compose
echo "==> OK"
echo
echo "=== Removing old mobymac/docker-machine VMs..."
for VM in default mobymac; do
if VBoxManage showvminfo "$VM" &>/dev/null; then
echo "=== Shut down and destroy: '$VM'"
VBoxManage controlvm "$VM" poweroff || true
sleep 10
VBoxManage unregistervm "$VM" --delete
else
echo "=== No VM with name: '$VM'"
fi
done
echo "=== Remaining VMs (please clean up manually if needed):"
VBoxManage list vms
echo "==> OK"
echo
echo "=== Removing existing docker configuration..."
if [ -e "$HOME/.docker" ]; then
rm -rfv "$HOME/.docker"
echo "==> OK"
else
echo "==> Not found - skipping"
fi
echo
echo "=== Removing leftover vagrant state..."
rm -rf .vagrant
echo "==> OK"
echo
echo "=== Setting up NFS mount on host..."
echo "=== Mounted homedir: ${NFS_HOME}"
echo "=== NOTE: sudo will be required to edit /etc/exports and restart NFS server"
lineinfile /etc/exports "$EXPORTS_LINE" sudo
nfsd checkexports
sudo nfsd restart
echo "==> OK"
echo
echo "=== Creating docker VM..."
vagrant up
echo "==> OK"
echo
echo "=== Rebooting docker VM..."
# we can't use `vagrant reload` because the VM creation is not idempotent
vagrant halt
./start.sh
sleep 60
echo "==> OK"
echo
echo "=== Setting up VM launch-at-login plist..."
mkdir -p "$(dirname "$PLIST")"
gen_plist > "$PLIST"
launchctl load -w "$PLIST"
echo "==> OK"
echo
echo "=== Applying docker config..."
eval "${ENV_LINE}"
# unset potential leftovers from `docker-machine env`
export DOCKER_TLS_VERIFY=""
export DOCKER_TLS=""
echo "==> OK"
echo
echo "=== Testing docker and NFS volume mounts..."
docker run --rm -w "$(pwd)" -v "$(pwd):$(pwd):ro" busybox ls install.sh >/dev/null
echo "==> Finished. It works!"
echo
echo "=== Adding docker host env to shell config..."
case "$SHELL_INTEGRATION" in
auto)
lineinfile "$HOME/.bash_profile" "$ENV_LINE"
lineinfile "$HOME/.zprofile" "$ENV_LINE"
lineinfile "$HOME/.config/fish/config.fish" "$ENV_LINE_FISH"
# remove leftover docker-machine config which would now cause errors
removefromfile "$HOME/.bash_profile" "$DOCKER_MACHINE_LINE"
removefromfile "$HOME/.zprofile" "$DOCKER_MACHINE_LINE"
removefromfile "$HOME/.config/fish/config.fish" "$DOCKER_MACHINE_LINE"
;;
manual)
echo "==="
echo "=== ACTION REQUIRED: Add docker config to your shell's profile file"
echo "==="
echo "=== Please add the appropriate line to your shell's configuration:"
echo "==="
echo
echo ".bash_profile (bash) / .zprofile (zsh):"
echo " ${ENV_LINE}"
echo
echo "config.fish (fish):"
echo " ${ENV_LINE_FISH}"
echo
echo "=== Make sure to remove any leftover 'docker-machine env' lines!"
;;
*)
echo "Unknown shell integration type. Aborting!"
exit 1
;;
esac
echo "==> OK"
echo
echo "=== Please re-open all your terminal windows for the new docker settings to apply!"
}
main