-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsysbs
executable file
·534 lines (440 loc) · 15.7 KB
/
sysbs
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#!/usr/bin/env bash
set -o pipefail
myself="$0"
hq="$(dirname "$myself")"
source "$hq/applications/bash-std"
function install_python_module
{
python3 -m pip install --upgrade --use-pep517 "$hq/" ||
die "python3-pip is required to install HQ Python module"
}
function symlink_dots
{
for dot in $( find $hq/dots -maxdepth 1 -mindepth 1 -type f,d ); do
local bname="$( basename $dot )"
if [[ $bname == "sshconfig" ]]; then
if (( options_dry_run )); then
say "$dot -> ~/.ssh/config"
else
mkdir -p$verbose ~/.ssh
if [[ -f ~/.ssh/config && ! -L ~/.ssh/config ]]; then
if (( options_move_existing )); then
mv $_verbose ~/.ssh/config ~/.ssh/config.old
else
die "~/.ssh/config exists and is not a symlink, exiting"
fi
else
rm -f$verbose ~/.ssh/config
fi
ln -s$verbose $( realpath $dot ) ~/.ssh/config
fi
elif [[ -n "$( grep $bname < $hq/etc/homedots )" ]]; then # install some files to ~/
if (( options_dry_run )); then
say "$dot -> ~/$bname"
else
if [[ -f ~/$bname && ! -L ~/$bname ]]; then
if (( options_move_existing )); then
mv $_verbose ~/$bname ~/$bname.old
else
die "~/$bname exists and is not a symlink, exiting"
fi
else
rm -rf$verbose ~/$bname
fi
ln -s$verbose $( realpath $dot ) ~/
fi
elif [[ -n "$( grep $bname < $hq/etc/etcdots )" ]]; then # install some files to /etc/
if (( options_no_sudo )); then continue; fi
if (( options_dry_run )); then
say "$dot -> /etc/$bname"
else
if [[ -f ~/$bname && ! -L ~/$bname ]]; then
if (( options_move_existing )); then
sudo mv $_verbose /etc/$bname /etc/$bname.old
else
die "~/$bname exists and is not a symlink, exiting"
fi
else
sudo rm -rf$verbose /etc/$bname
fi
sudo ln -s$verbose $( realpath $dot ) /etc/
fi
else # install remaining files to ~/.config/
if (( options_dry_run )); then
say "$dot -> ~/.config/$bname"
else
mkdir -p$verbose ~/.config
if [[ -f ~/.config/$bname && ! -L ~/.config/$bname ]]; then
if (( options_move_existing )); then
mv $_verbose ~/.config/$bname ~/.config/$bname.old
else
die "~/.config/$bname exists and is not a symlink, exiting"
fi
else
rm -rf$verbose ~/.config/$bname
fi
ln -s$verbose $( realpath $dot ) ~/.config/
fi
fi
done
}
function configure_zsh
{
mkdir -p$verbose ~/.oh-my-zsh/themes
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/.oh-my-zsh/themes/powerlevel10k
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
}
function configure_git
{
git config --global user.email "[email protected]"
git config --global user.name "Hamish Morgan"
git config --global pull.rebase false
git config --global commit.template $(realpath $hq)/etc/gitmessage
git config --global --add push.default current
git config --global --add push.autoSetupRemote true
}
function configure_emacs
{
type -p emacs > /dev/null || say "warning: emacs is not installed"
if [[ -e "~/.emacs.d" ]]; then
if (( options_move_existing )); then
mv ~/.emacs.d ~/.emacs.d.old
else
die "~/.emacs.d already exists, use --move-existing to move it to ~/.emacs.d.old"
fi
fi
git clone [email protected]:hacmorgan/emacs.git ~/.emacs.d
git clone [email protected]:copilot-emacs/copilot.el.git ~/.emacs.d/external-packages/copilot.el
}
function create_shared_mountpoints
{
for mount_point in $( cat $hq/etc/mountpoints | cut -d':' -f1 ); do
sudo mkdir --parents $_verbose $mount_point
sudo chown $_verbose $(whoami):$(whoami) $mount_point
done
}
function install_fonts
{
function fantasque
{
sudo mkdir -p /usr/share/fonts/fantasque
(
cd /usr/share/fonts/fantasque || return
curl -L "https://github.com/belluzj/fantasque-sans/releases/download/v1.8.0/FantasqueSansMono-NoLoopK.tar.gz" \
| sudo tar -zxf$verbose -
)
}
function firacode
{
sudo mkdir -p /usr/share/fonts/firacode
(
cd usr/share/fonts/firacode || return
curl -L "https://github.com/tonsky/FiraCode/releases/download/5.2/Fira_Code_v5.2.zip" \
> /tmp/firacode.zip
sudo unzip /tmp/firacode.zip
rm /tmp/firacode.zip
)
}
fantasque
firacode
}
function install_oh_my_zsh
{
type -p zsh > /dev/null || die "zsh not found, please install it first"
type -p curl > /dev/null || die "curL not found, please install it first"
yes "n" | sh -c "$( curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh )"
}
function symlink_vi
{
sudo ln -s$verbose /usr/bin/nvim /usr/bin/vi
}
function symlink_systemd_services
{
for service in $hq/etc/systemd/*; do
(( options_symlink_systemd_services )) && sudo ln -sf$verbose $( realpath $service ) /etc/systemd/system/
(( options_enable_systemd_services )) && sudo systemctl enable $( basename $service )
done
}
function autostart_abyss_wireguard
{
sudo systemctl enable [email protected]
}
function update_package_list
{
function check_packagelist_exists
{
[[ -f $hq/etc/packagelist ]] || die "$hq/etc/packagelist not found"
}
function remove_additions_file
{
rm $ADDITIONS_FILE
}
function list_installed_packages
{
eval "$( head -n1 $hq/etc/packagelist )"
}
function exclude_known_packages
{
function uncommented_package_list
{
tail -n+3 $hq/etc/packagelist | sed 's/^#//'
}
grep --invert-match \
--fixed-strings \
--file <( uncommented_package_list )
}
function write_to_additions_file
{
function print_instructions
{
echo "## comment out (with #) any packages you don't want automatically installed in future"
}
{ print_instructions; cat; } > $ADDITIONS_FILE
}
function edit_additions_file
{
nvim $ADDITIONS_FILE
}
function confirm_and_append
{
echo "Append these changes to the packagelist? [Y/n]"
read answer
grep --ignore-case 'n' <<< $answer ||
tail -n+2 $ADDITIONS_FILE >> $hq/etc/packagelist
}
ADDITIONS_FILE=/tmp/packagelist.additions
check_packagelist_exists
remove_additions_file
list_installed_packages |
exclude_known_packages |
write_to_additions_file
edit_additions_file
confirm_and_append
remove_additions_file
}
function install_packages
{
function install_command
{
tail -n+2 $hq/etc/packagelist | head -n1
}
function space_separated_packages
{
function list_packages
{
tail -n+3 $hq/etc/packagelist
}
function exclude_commented
{
grep -v '^#'
}
function convert_newlines_to_spaces
{
tr '\n' ' '
}
list_packages |
exclude_commented |
convert_newlines_to_spaces
}
eval "$(install_command) $(space_separated_packages)"
}
function install_cpp_manpages
{
# Install manpages for C++ std lib
if (( options_no_sudo )); then
die "Installing manpages requires sudo"
fi
FOSSDIR="$HOME/src/foss"
mkdir --parents "$FOSSDIR"
git clone https://github.com/jeaye/stdman "$FOSSDIR/stdman" &&
pushd "$FOSSDIR/stdman" &&
./configure &&
sudo make install &&
popd
}
function configure_vnc
{
## Configure the machine as a vnc server
#
# Steps taken from Arch Wiki
function check_vnc_installed
{
command -v vncserver > /dev/null ||
die "${fmt_red}vncserver executable not found, is tigervnc installed?${fmt_reset}"
}
function set_vnc_password
{
say "\n\n${fmt_bold}Creating a vnc password...${fmt_reset}\n\n"
vncpasswd
}
function define_user_mappings
{
say "\n\n${fmt_bold}Defining user mappings...${fmt_reset}\n\n"
sudo nvim /etc/tigervnc/vncserver.users
}
function create_vnc_config
{
if [[ -e ~/.vnc/config ]]; then
say "${fmt_yellow}~/.vnc/config already exists${fmt_reset}"
else
say "\n\n${fmt_bold}Creating the vnc config at ~/.vnc/config...${fmt_reset}\n\n"
echo "session=i3" >> ~/.vnc/config
fi
}
function start_and_enable_systemd_services
{
say "\n\n${fmt_bold}Starting systemd service vncserver@:1${fmt_reset}"
say "If you wish to start a different display number, or enable the systemd service, do that manually\n\n"
sudo systemctl start vncserver@:1
}
function expose_local_display_directly
{
say "\n\n${fmt_bold}Exposing the local display directly (for better performance)${fmt_reset}"
say "You will need to restart X for this to come into effect\n\n"
cat <<EOF | sudo tee /etc/X11/xorg.conf.d/10-vnc.conf
Section "Module"
Load "vnc"
EndSection
Section "Screen"
Identifier "Screen0"
Option "UserPasswdVerifier" "VncAuth"
Option "PasswordFile" "/root/.vnc/passwd"
EndSection
EOF
}
check_vnc_installed
set_vnc_password
define_user_mappings
create_vnc_config
start_and_enable_systemd_services
expose_local_display_directly
}
function configure_tmux
{
mkdir -p ~/.tmux/plugins
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
}
function install_hq_secret
{
git clone [email protected]:hacmorgan/hq-secret.git ~/.hq-secret
}
function configure_cctv
{
# Set up CCTV system on this host with Shinobi and MariaDB
#
# Steps taken from ArchWiki pages for those tools
function setup_db
{
# Install MariaDB (MySQL)
sudo pacman -S mariadb
# Create a DB
sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
# Start the systemd service
sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service
}
function setup_shinobi
{
# Install Shinobi
yay -S shinobi-git
# Create a DB for Shinobi
sudo mysql <<< "CREATE DATABASE ccio; flush privileges; \q"
# Create user majesticflame for shinobi and grant priveliges to the new db
sudo mysql < /usr/share/shinobi/sql/user.sql
# Populate the db with tables
sudo mysql ccio < /usr/share/shinobi/sql/framework.sql
# Copy config into place
sudo cp -v "$hq/etc/shinobi/conf.json" /etc/shinobi/conf.json
# Start the systemd service
sudo systemctl enable shinobi.target
sudo systemctl start shinobi.target
}
setup_db
setup_shinobi
}
# ------------------------------------------------------------------------------------ #
function arg_name_to_var_name
{
local operation="$1"
sed -e 's#^--#options_#' -e 's#-#_#g' <<< "$operation"
}
function prologue
{
echo -e "\n${fmt_dim}${fmt_cyan}sysbs - system bootstrapper${fmt_white}${fmt_reset}"
}
function options
{
cat <<EOF
--autostart-abyss-wireguard; Use systemd to effectively run 'sudo wg-quick up abyss' on boot
--configure-cctv; Configure this host as a CCTV server
--configure-emacs; Clone emacs repositiory to ~/.emacs.d
--configure-vnc; Configure a VNC server
--configure-git; Tell git who you are (so you can commit)
--configure-tmux; Configure tmux with plugins
--configure-zsh; Do this after installing oh-my-zsh
--create-shared-mountpoints; Create all mountpoints in 'etc/mountpoints' and transfer ownership to hamish
--dry-run; Talk the talk but don't walk the walk
--enable-systemd-services; Enable services symlinked by --symlink-systemd-services
--install-cpp-manpages; Install C++ stdlib manpages using https://github.com/jeaye/stdman
--install-fonts; Unpack fantasque (noLoopK) and FiraCode (ligatures) to /usr/share/fonts/fantasque
--install-oh-my-zsh; Self explanatory
--install-hq-secret;
--install-packages; Install all packages in etc/packagelist that aren't commented out
--install-prefix,-p=<path>; default=$HOME/.local; Install prefix for scripts
--install-python-module; Install HQ Python module, including scripts and pip installs
--move-existing; If <file> exists, move it to <file>.old
--no-sudo; Don't do anything that requires sudo
--standard,-s; Do the standard bootstrap operations: ${STANDARD_OPERATIONS[@]}
--symlink-dots; Symlink configs in 'dots' to ~/.config/, unless they are present in 'etc/homedots'
--symlink-systemd-services; Symlink service files in etc/systemd to /etc/systemd/system/
--symlink-vi; Symlink /usr/bin/nvim to /usr/bin/vi
--update-package-list; Find installed packages that aren't already in etc/packagelist
--verbose,-v; Talk the talk AND walk the walk
EOF
}
STANDARD_OPERATIONS=(
--configure-emacs
--configure-git
--configure-tmux
--configure-zsh
--install-oh-my-zsh
--install-python-module
--install-hq-secret
--move-existing
--no-sudo
--symlink-dots
--verbose
)
# Parse args and set verbose variables
bash-std-application-init "$@" < <( options ) ||
die "Failed to parse command line args"
bash-std-set-verbose
# For standard install, set variables that would be set by command line args
if (( options_standard )); then
for operation in "${STANDARD_OPERATIONS[@]}"; do
eval "$(arg_name_to_var_name "$operation")=1"
echo "setting $(arg_name_to_var_name "$operation")=1"
done
fi
# Run desired setup routines
(( options_install_oh_my_zsh )) && install_oh_my_zsh
(( options_configure_cctv )) && configure_cctv
(( options_configure_emacs )) && configure_emacs
(( options_configure_git )) && configure_git
(( options_configure_vnc )) && configure_vnc
(( options_configure_tmux )) && configure_tmux
(( options_configure_zsh )) && configure_zsh
(( options_create_shared_mountpoints )) && create_shared_mountpoints
(( options_install_fonts )) && install_fonts
(( options_symlink_dots )) && symlink_dots
(( options_symlink_systemd_services ||
options_enable_systemd_services )) && symlink_systemd_services
(( options_symlink_vi )) && symlink_vi
(( options_install_python_module )) && install_python_module
(( options_autostart_abyss_wireguard )) && autostart_abyss_wireguard
(( options_update_package_list )) && update_package_list
(( options_install_packages )) && install_packages
(( options_install_cpp_manpages )) && install_cpp_manpages
(( options_install_hq_secret )) && install_hq_secret
exit 0