-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrr.sh
executable file
·1214 lines (1083 loc) · 36.8 KB
/
rr.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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
: <<AEOF
SOURCE_THESE_VIMS_START
nnoremap <leader>ueo <cmd>silent exec "!tmux send-keys -t :.+ 'ANSIBLE_DEBUG=false ANSIBLE_VERBOSITY=1 ./rr.sh edit ./inventory/localhost.yaml' Enter"<cr>
nnoremap <leader>ued <cmd>silent exec "!tmux send-keys -t :.+ 'ANSIBLE_DEBUG=false ANSIBLE_VERBOSITY=1 ./rr.sh edit ./inventory/docker-enc.yaml' Enter"<cr>
nnoremap <leader>us <cmd>silent exec "!tmux send-keys -t :.+ 'ANSIBLE_DEBUG=false ANSIBLE_VERBOSITY=1 ./rr.sh role -r scratch -g asus' Enter"<cr>
nnoremap <leader>udh <cmd>silent exec "!tmux send-keys -t :+ 'ANSIBLE_DEBUG=false ANSIBLE_VERBOSITY=1 ./rr.sh role-i -r home-manager -t fast' Enter"<cr>
nnoremap <leader>udi <cmd>silent exec "!tmux send-keys -t :+ 'ANSIBLE_DEBUG=false ANSIBLE_VERBOSITY=1 ./rr.sh install-i -t fast' Enter"<cr>
let @h="yoecho \"\<c-r>\" = \$\<c-r>\"\"\<esc>j"
echom 'Sourced'
SOURCE_THESE_VIMS_END
AEOF
# get location of this folder
SCRIPT_DIR="$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )"
PROJECT_DIR="$SCRIPT_DIR"
# make sure we are in the correct folder
pushd "$SCRIPT_DIR" &>/dev/null
# container tag
CONTAINER_TAG=devenvansible:1.0
# ansible workspace path
ANSIBLE_DEV_ENV_ANSIBLE_PATH="$SCRIPT_DIR"
# docker volumn mount
DOCKER_VOLUME_MOUNT=()
lxc_volume_mount=()
# docker files dir
DOCKER_FILE_DIR=./dockerfiles
# docker file name
DOCKER_FILE_UBUNTU_24="$DOCKER_FILE_DIR/ubuntu2404/Dockerfile"
DOCKER_FILE_UBUNTU_22="$DOCKER_FILE_DIR/ubuntu2204/Dockerfile"
DOCKER_FILE_UBUNTU_20="$DOCKER_FILE_DIR/ubuntu2004/Dockerfile"
DOCKER_FILE_UBUNTU_18="$DOCKER_FILE_DIR/ubuntu1804/Dockerfile"
DOCKER_FILE_UBUNTU_16="$DOCKER_FILE_DIR/ubuntu1604/Dockerfile"
# docker repo name to be managed by rr.sh
DEV_ENV_REPOSITORY_NAME=devenvansible
RUN_PREFIX_FOR_NAME=run
USE_PREFIX_FOR_NAME=use
# Temp playbook
WHOLE_PLAYBOOK_PATH="$SCRIPT_DIR/playbooks/everything.yml"
WORKFLOW_PATH="$SCRIPT_DIR/.github/workflows/main.yml"
# Write playbook
# @brief Return the name for the playbook
# @param role - The name of the role to put in the playbook
playbookName() {
# role is
role="$1"
# Find all the roles
# if ! ls "$SCRIPT_DIR/playbooks/roles" | grep -q "$role"; then
if [[ ! -d "$SCRIPT_DIR/playbooks/roles/$role" ]]; then
echo "role '$role' cannot be found in the ./playbooks/roles folder" >&2
exit 1
fi
echo -n "$SCRIPT_DIR/playbooks/testbook_$role.yml"
}
# Write playbook
# @brief Create temp playbook for a role
# @param role - The name of the role to put in the playbook
writePlaybook() {
# role is
role="$1"
# playname
playpath="$(playbookName "$role")"
ret="$?"
if [[ "$ret" -ne 0 ]]; then
exit "$ret"
fi
cat <<EOF > "$playpath"
---
- hosts: "{{ playbook_target }}"
gather_facts: true
roles:
- $role
EOF
# return the name
echo -n "$playpath"
}
# define a function to perform check on role
roleCheck() {
log="$1"
# perform test
if ! grep -q 'failed=0' "$log"; then
echo "failed should be 0 at the secound run" >&2
exit 2
fi
}
# define a function to perform check
ansibleCheck() {
log="$1"
# perform test
if ! grep -q 'changed=0' "$log"; then
echo "changed should be 0 at the secound run" >&2
exit 1
fi
roleCheck "$log"
if grep -q 'not installed properly' "$log"; then
echo "checking exe that some are not done right" >&2
exit 3
fi
}
# use command to check if cmd is present
checkCmd() {
cmd=$1
if ! command -v "$cmd" &>/dev/null; then
echo "$cmd not installed properly"
return 1
fi
}
# When spawning a docker container, use a easily recognized name
compose_container_name() {
prefix=$1
tag=$2
suffix=$RANDOM
echo -n "${prefix}_${tag}_${suffix}"
}
displayHelpLxc() {
# print help here
echo "${BASH_SOURCE[0]} lxc [...]"
echo " Commands to handle the lxc containers setup"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Display Help"
echo " -h|--help|h|hel|help : Print this help command"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Manage a bespoked lxc container for testing this repository"
echo " start: [-r] [-w DIR]... [-v] [-d]"
echo " Start a LXC container"
echo ""
echo " stop: [-v]"
echo " Stop a LXC container"
echo ""
echo " mount: [-w DIR]... [-v]"
echo " Mount this additional paths plus all the default paths"
echo ""
echo " shell: [-v]"
echo " Spawn a bash shell to the LXC container"
echo ""
echo " where"
echo " -v > Enable verbose trace in the script"
echo " -d > Enable desktop environment install via VNC"
echo " -r > Stop and remove the running container"
echo " -w dir > Bind mount this folder to the container"
}
displayHelpMain() {
# print help here
echo "${BASH_SOURCE[0]} [...]"
echo " One-stop shop to do everything related to this repository"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Pre-requisite"
echo " Please run './shell.sh' to start a nix shell before running any command below"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Display Help"
echo " -h|--help|h|hel|help : Print this help command"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Running the ansible commands or related check"
echo " install -g HOSTS [-v] [-t 'tag1[,tag2 ...]']"
echo " Install on the host system (when do it on your production machine) prompting for password" # desc
echo ""
echo " install-i [-g HOSTS] [-v] [-t 'tag1[,tag2 ...]']"
echo " Install on the host system (mostly container) w/o asking for password" # desc
echo ""
echo " tags"
echo " List all the tags" # desc
echo ""
echo " roles"
echo " List all the roles" # desc
echo ""
echo " role -g HOSTS [-v] [-t 'tag1[,tag2 ...]'] -r <role>"
echo " Run a role on the host system (mostly container) prompting for password" # desc
echo ""
echo " role-i [-g HOSTS] [-v] [-t 'tag1[,tag2 ...]'] -r <role>"
echo " Run a role on the host system (mostly container) w/o asking for password" # desc
echo ""
echo " check"
echo " Do simple check on the host system for all executable" # desc
echo ""
echo " preupgrade"
echo " Do the necessary stuff to get the system upgraded" # desc
echo ""
echo " where"
echo " -g > An inventory host name or a group of hosts in the inventory file"
echo " -v > Provide the -vvv option to the ansigle command to have debug output"
echo " -u > Will update the dotfile repo"
echo " -a > Install all, like xmonad, etc"
echo " -t > Tags to use, comma separated, no space"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Manage and handle the ansible vault for inventory file"
echo " edit FILE: Edit the inventory file"
echo ""
echo " where:"
echo " FILE : Inventory file"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Create a new row template"
echo " new-role NAME : Create a new role with some default"
echo ""
echo " where:"
echo " NAME : The name of the role appears in the folder"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Manage the LXC containers for testing the ansible locally"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Spawn a tmux session to develop this repo"
echo " tmux : Start the tmux development session"
echo ""
echo "--------------------------------------------------------------------------------"
echo "Manage a bespoked lxc container for testing this repository"
echo " lxc [...]: Use '${BASH_SOURCE[0]} lxc -h' subcommand to learn more"
echo ""
echo "--------------------------------------------------------------------------------"
echo "This is used to manage the lifetime of the containers"
echo " run [-n UBUNTU_VER] [-w dir]"
echo " Start a new docker container only" # desc
echo ""
echo " run-build [ver]"
echo " Start a new docker container and run ansible-playbook" # desc
echo ""
echo " commit ID TAG"
echo " Commit a running docker container ID with the TAG specified" # desc
echo ""
echo " use -d TAG [-w dir]"
echo " Start a committed image only" # desc
echo ""
echo " list"
echo " List all the images" # desc
echo ""
echo " use-build TAG"
echo " Start a committed image and run ansible-playbook" # desc
echo ""
echo " run-test [ver]"
echo " Start a new docker container and run simple CI test" # desc
echo ""
echo " run-role <ver> <role>"
echo " Start a new docker container and run a role" # desc
echo ""
echo " use-role <ver> <role>"
echo " Start a committed image and run a role" # desc
echo ""
echo " where"
echo " -n UBUNTU_VER > Ubuntu version to spawn a docker container. One of 18, 20, 22, 24"
echo " -d TAG > One of the docker tag from image devenvansible"
echo " -w dir > Bind mount this folder to the container"
echo " arguments:"
echo " ver > Specify the version to use. Default 18. One of 18, 20, 22, 24"
echo " ID > This is the container name or ID to use to make a commit, full name required"
echo " TAG > This is by your preference on how the commited container to be tagged"
}
# see what version to use
select_docker_ver() {
# get the arg
ver=$1
# select the version
case "$ver" in
24)
echo -n "$DOCKER_FILE_UBUNTU_24"
;;
22)
echo -n "$DOCKER_FILE_UBUNTU_22"
;;
20)
echo -n "$DOCKER_FILE_UBUNTU_20"
;;
18)
echo -n "$DOCKER_FILE_UBUNTU_18"
;;
16)
echo -n "$DOCKER_FILE_UBUNTU_16"
;;
*)
echo "Invalid version tag $ver" >&2
displayHelpMain
exit 1
;;
esac
}
# Build container
build_image() {
# Get args
tag="$1"
dfile="$2"
# Infer arg
args=(build --tag "$tag" -f "$dfile")
args+=(--build-arg SHELL_USER="$USER")
args+=(--build-arg SHELL_UID="$(id -u "$USER")")
args+=(--build-arg SHELL_GID="$(id -g "$USER")")
args+=(.)
docker "${args[@]}"
}
# Setup nix
setup_nix() {
export LOCALE_ARCHIVE=/usr/lib/locale/locale-archive
# Source nix if using nix-installer
if [[ -r "/nix/var/nix/profiles/default/etc/profile.d/nix.sh" ]]; then
. "/nix/var/nix/profiles/default/etc/profile.d/nix.sh"
fi
# Source nix if using nix official installer
if [[ -r "$HOME/.nix-profile/etc/profile.d/nix.sh" ]]; then
. "$HOME/.nix-profile/etc/profile.d/nix.sh"
fi
if [[ -r /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
fi
}
# instal nix
install_nix() {
setup_nix
# Check if nix is available
if ! command -v nix &>/dev/null; then
# Ask people to install
echo 'WARN: nix is not found in your PATH' >&2
echo 'WARN: --------------------------------------------------------------------------' >&2
echo 'WARN: To install nix, the following commands would be run.' >&2
echo "WARN: \$ curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install" >&2
echo 'WARN: --------------------------------------------------------------------------' >&2
totalCount=3
for count in $(seq $totalCount); do
echo "WARN: Asking $count/$totalCount. Select 'y' to automatically run the scripts above, or 'n' to abort." >&2
read -r -p "> " res
if [[ "$res" == 'y' || "$res" == 'Y' ]]; then
if curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install; then
echo 'INFO: Installation completed.' >&2
if [[ ! -r /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
echo 'ERR: Nix installation failed or nix not found' >&2
exit 1
fi
setup_nix
break
else
echo 'ERR: Installation failed. Check error above.' >&2
exit 1
fi
fi
if [[ "$res" == 'n' || "$res" == 'N' ]]; then
echo 'INFO: Installation aborted.' >&2
exit 0
fi
done
fi
if ! command -v nix &>/dev/null; then
echo 'ERR: Dependencies are not met. See error message above.' >&2
exit 1
fi
}
# Setup env
setup_brew() {
if [[ -x /home/linuxbrew/.linuxbrew/bin/brew && -z "$HOMEBREW_PREFIX" ]]; then
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
fi
}
# instal brew
install_brew() {
setup_brew
if ! command -v brew &>/dev/null; then
/bin/bash -c "export NONINTERACTIVE=1 ; $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install ansible
setup_brew
fi
}
# fnuction that check and set a docker mount when not fonud
append_docker_mount_global() {
# Get arguments
local args=("$@")
# Need 1 argument
if [[ "${#args[@]}" -ne 1 ]]; then
echo "ERR (append_docker_mount_global): need 1 argument only, but found ${#args[@]}" >&2
return 1
fi
local path="${args[0]}"
if [[ "${DOCKER_VOLUME_MOUNT[*]}" == *"$path"* ]]; then
return 0
fi
DOCKER_VOLUME_MOUNT+=(-v "$path")
# echo "DEBUG (append_docker_mount_global): Add '$path'" >&2
}
append_lxc_mount_global() {
# Get arguments
local args=("$@")
# Need 1 argument
if [[ "${#args[@]}" -ne 1 ]]; then
echo "ERR (append_lxc_mount_global): need 1 argument only, but found ${#args[@]}" >&2
return 1
fi
local path="${args[0]}"
if [[ "${lxc_volume_mount[*]}" == *"$path"* ]]; then
return 0
fi
lxc_volume_mount+=(-w "$path")
# echo "DEBUG (append_lxc_mount_global): Added '$path'" >&2
}
# function to populate the docker mount in a function
set_mounts_global() {
# docker volumn mount
for each in \
"$HOME/repos/dotfiles:$HOME/repos/dotfiles" \
"$SCRIPT_DIR:$ANSIBLE_DEV_ENV_ANSIBLE_PATH" \
"$HOME/repos/focus-side.vim:$HOME/repos/focus-side.vim" \
"$HOME/repos/jerry-nixos:$HOME/repos/jerry-nixos" \
"$HOME/.ssh/id_ed25519:$HOME/.ssh/id_ed25519" \
; do
append_docker_mount_global "$each"
append_lxc_mount_global "$each"
done
}
# Set the mounts to a list
set_mounts_global
# A submain just for the lxc command
sub_main_lxc() {
# if args, print help
if test "$#" -eq 0; then
displayHelpLxc
exit 0
fi
# if there are arg for test, run test
subcmd="$1"
shift
# Get a list of args here before they get consumed
local args=("$@")
# Iterate each subcommand
case "$subcmd" in
'-h'|'--help'|'h'|'hel'|'help')
displayHelpLxc
exit 1
;;
'stop')
startarg=(-r)
# parse the argumetns
while getopts 'v' opt; do
case "$opt" in
v)
startarg+=(-v)
;;
*)
echo "Unrecognized option $opt" >&2
displayHelpLxc
exit 1
;;
esac
done
"$PROJECT_DIR/scripts/start_lxc_container.sh" "${startarg[@]}"
;;
'shell')
startarg=(-s)
# parse the argumetns
while getopts 'v' opt; do
case "$opt" in
v)
startarg+=(-v)
;;
*)
echo "Unrecognized option $opt" >&2
displayHelpLxc
exit 1
;;
esac
done
"$PROJECT_DIR/scripts/start_lxc_container.sh" "${startarg[@]}"
;;
'mount')
startarg=()
# parse the argumetns
while getopts ':vw:' opt; do
case "$opt" in
v)
startarg+=(-v)
;;
w)
each="$OPTARG"
each="$(realpath "$each")"
append_lxc_mount_global "$each:$each"
;;
*)
echo "Unrecognized option $opt" >&2
displayHelpLxc
exit 1
;;
esac
done
startarg+=("${lxc_volume_mount[@]}")
"$PROJECT_DIR/scripts/start_lxc_container.sh" "${startarg[@]}"
;;
'start')
startarg=()
# parse the argumetns
while getopts ':rdvw:' opt; do
case "$opt" in
d)
startarg+=(-d)
;;
v)
startarg+=(-v)
;;
w)
each="$OPTARG"
each="$(realpath "$each")"
append_lxc_mount_global "$each:$each"
;;
r)
startarg+=(-r)
;;
*)
echo "Unrecognized option $opt" >&2
displayHelpLxc
exit 1
;;
esac
done
startarg+=("${lxc_volume_mount[@]}")
"$PROJECT_DIR/scripts/start_lxc_container.sh" "${startarg[@]}"
;;
esac
}
# Main for the entire program
main() {
# if args, print help
if test "$#" -eq 0; then
displayHelpMain
exit 0
fi
# if there are arg for test, run test
subcmd="$1"
shift
# Get a list of args here before they get consumed
local args=("$@")
# Handle to subcommands hele
case "$subcmd" in
'lxc')
# delegate to the lxc submain
sub_main_lxc "${args[@]}"
exit "$?"
;;
esac
# Main command required nix shell
if [[ -z "$IN_NIX_RR_SHELL" ]]; then
echo "ERR: Please run ./shell.sh to start a nix shell, then run ./rr.sh ..." >&2
exit 1
fi
# Iterate each subcommand
case "$subcmd" in
'-h'|'--help'|'h'|'hel'|'help')
displayHelpMain
exit 1
;;
'new-role')
# need 1 argrs
if test "$#" -ne 1; then
echo "Need 1 argument <NAME> for the role to be created" >&2
exit 1
fi
name="$1"
# create the folders
mkdir -p "$SCRIPT_DIR/playbooks/roles/$name/meta"
mkdir -p "$SCRIPT_DIR/playbooks/roles/$name/tasks"
mkdir -p "$SCRIPT_DIR/playbooks/roles/$name/defaults"
# write meta
echo '---' >> "$SCRIPT_DIR/playbooks/roles/$name/meta/main.yml"
echo 'dependencies:' >> "$SCRIPT_DIR/playbooks/roles/$name/meta/main.yml"
echo ' - common_settings' >> "$SCRIPT_DIR/playbooks/roles/$name/meta/main.yml"
echo '' >> "$SCRIPT_DIR/playbooks/roles/$name/meta/main.yml"
echo '# vim:et ts=2 sts=2 sw=2' >> "$SCRIPT_DIR/playbooks/roles/$name/meta/main.yml"
# write tasks
echo '---' >> "$SCRIPT_DIR/playbooks/roles/$name/tasks/main.yml"
echo '- name: PLACEHOLDER' >> "$SCRIPT_DIR/playbooks/roles/$name/tasks/main.yml"
echo ' fail:' >> "$SCRIPT_DIR/playbooks/roles/$name/tasks/main.yml"
echo " msg: 'need to implement $name tasks'" >> "$SCRIPT_DIR/playbooks/roles/$name/tasks/main.yml"
echo '' >> "$SCRIPT_DIR/playbooks/roles/$name/tasks/main.yml"
echo '# vim:et ts=2 sts=2 sw=2' >> "$SCRIPT_DIR/playbooks/roles/$name/tasks/main.yml"
# write defaults
echo '---' >> "$SCRIPT_DIR/playbooks/roles/$name/defaults/main.yml"
echo 'home_dir: "{{ ansible_env.HOME }}" ' >> "$SCRIPT_DIR/playbooks/roles/$name/defaults/main.yml"
echo '' >> "$SCRIPT_DIR/playbooks/roles/$name/defaults/main.yml"
echo '# vim:et ts=2 sts=2 sw=2' >> "$SCRIPT_DIR/playbooks/roles/$name/defaults/main.yml"
# add to playbook in case I forgot
awk "1; /roles:/ { if (found == 0) { print \" - $name\" }; found++ }" "$WHOLE_PLAYBOOK_PATH" > "$WHOLE_PLAYBOOK_PATH.tmp"
if test "$?" -eq 0; then
test -r "$WHOLE_PLAYBOOK_PATH.tmp" && mv "$WHOLE_PLAYBOOK_PATH.tmp" "$WHOLE_PLAYBOOK_PATH"
fi
# add to workflow in case I forgot
awk "1; /role:/ { if (found == 0) { print \" '$name',\" }; found++ }" "$WORKFLOW_PATH" > "$WORKFLOW_PATH.tmp"
if test "$?" -eq 0; then
test -r "$WORKFLOW_PATH.tmp" && mv "$WORKFLOW_PATH.tmp" "$WORKFLOW_PATH"
fi
;;
'check')
# if not being called from the rr.sh script itself
if [[ -z $FROM_RR_CHECK ]]; then
# recursively call itself
FROM_RR_CHECK=1 bash -i -c "$SCRIPT_DIR/${BASH_SOURCE[0]#./*} check"
# edit when done
exit $?
fi
# below are for the best effort
. "$HOME/.bashrc"
. "$HOME/.bashrc_append"
sdev
nv
# # do it here, as I don't want it in bashrc to slow it down
# nvm use --lts
# get a list of commands to check
cmds=( \
'ansible' \
'ansible-playbook' \
'bash-language-server' \
'bat' \
'caddy' \
'cargo' \
'clang' \
'clangd' \
'cmake' \
'conda' \
'ctags' \
'dmenu' \
'doas' \
'docker' \
'docker-langserver' \
'doxygen' \
'dust' \
'eza' \
'fd' \
'git' \
'gtop' \
'hdl_checker' \
'java' \
'javac' \
'kitty' \
'lua' \
'luarocks' \
'nethogs' \
'node' \
'npm' \
'nvim' \
'nvm' \
'openconnect' \
'p4' \
'p4p' \
'p4v' \
'procs' \
'pwsh' \
'python3' \
'rg' \
'rofi' \
'rustc' \
'rustup' \
'sd' \
'starship' \
'svls' \
'tmux' \
'toclip' \
'tokei' \
'tracecompass' \
'tree-sitter' \
'tshark' \
'typescript-language-server' \
'vncserver' \
'vscode-json-language-server' \
'wireshark' \
'xclip' \
'xmobar' \
'xmonad' \
'yaml-language-server' \
'yarn' \
)
ret=0
for c in "${cmds[@]}"; do
if ! checkCmd "$c"; then
ret=1
fi
done
exit $ret
;;
'preupgrade')
# if not being called from the rr.sh script itself
if [[ -z $FROM_RR_CHECK ]]; then
# recursively call itself
FROM_RR_CHECK=1 bash -i -c "$SCRIPT_DIR/${BASH_SOURCE[0]#./*} preupgrade"
# edit when done
exit $?
fi
# below are for the best effort
. "$HOME/.bashrc"
. "$HOME/.bashrc_append"
sdev
# do it here, as I don't want it in bashrc to slow it down
nvm use --lts
# Do the node upgrade
nvm install-latest-npm
;;
'roles')
# List all the roles
while read -d $'\0' -r each; do
eachrole="${each##*/roles}"
if [[ -z "${eachrole:1}" ]]; then
continue
fi
echo "role: ${eachrole:1}"
done < <(find "$SCRIPT_DIR/playbooks/roles" -maxdepth 1 -type d -print0)
;;
'tags')
# List all the tags
echo "Listing all the tags"
ansible-playbook -i ./inventory/localhost.yaml -e "ansible_python_interpreter=$EXPLICIT_PYTHON_PATH_FOR_ANSIBLE" -e "playbook_target=docker" "$WHOLE_PLAYBOOK_PATH" --list-tags
;;
'role-i')
# var
verbose=false
tags='untagged'
role=''
# parse the argumetns
while getopts ':vt:r:' opt; do
case "$opt" in
v)
verbose=true
;;
r)
role="$OPTARG"
;;
t)
tags="$tags,$OPTARG"
;;
*)
echo "Unrecognized option $opt" >&2
displayHelpMain
exit 1
;;
esac
done
# need a role
if test -z "$role"; then
echo "ERR: No role specified, use -r <role> to run a role" >&2
exit 1
fi
install_nix
# Write the role
playpath="$(writePlaybook "$role")"
ret="$?"
if [[ "$ret" -ne 0 ]]; then
exit "$ret"
fi
# trap remove
trap "rm -f '$playpath'" EXIT SIGINT SIGTERM
# install with ansible playbook
if [[ "$verbose" == 'true' ]]; then
time ansible-playbook -i ./inventory/localhost.yaml -e 'playbook_target=localhost' -e "ansible_python_interpreter=$EXPLICIT_PYTHON_PATH_FOR_ANSIBLE" -vvv "$playpath" --tags "$tags"
elif [[ "$verbose" == 'false' ]]; then
time ansible-playbook -i ./inventory/localhost.yaml -e 'playbook_target=localhost' -e "ansible_python_interpreter=$EXPLICIT_PYTHON_PATH_FOR_ANSIBLE" "$playpath" --tags "$tags"
fi
;;
'edit')
# Get arguments
args=("$@")
# Need 1 argument
if [[ "${#args[@]}" -ne 1 ]]; then
echo "ERR: edit command need an encrypted inventory file, but you have ${#args[@]}."
exit 0
fi
"$PROJECT_DIR/scripts/edit_inventory.sh" "${args[0]}"
;;
'role')
# var
verbose=false
tags='untagged'
role=''
# parse the argumetns
while getopts ':vt:r:' opt; do
case "$opt" in
v)
verbose=true
;;
t)
tags="$tags,$OPTARG"
;;
r)
role="$OPTARG"
;;
*)
echo "Unrecognized option $opt" >&2
displayHelpMain
exit 1
;;
esac
done
# need a role
if test -z "$role"; then
echo "ERR: No role specified, use -r <role> to run a role" >&2
exit 1
fi
install_nix
# Write the role
playpath="$(writePlaybook "$role")"
ret="$?"
if [[ "$ret" -ne 0 ]]; then
exit "$ret"
fi
# trap remove
trap "rm -f '$playpath'" EXIT SIGINT SIGTERM
# buil args
aargs=()
# install with ansible playbook
if [[ "$verbose" == 'true' ]]; then
aargs+=("-vvv")
fi
aargs+=(-e "playbook_target=localhost")
aargs+=(-e "ansible_python_interpreter=$EXPLICIT_PYTHON_PATH_FOR_ANSIBLE")
aargs+=(-i "./inventory/localhost.yaml")
aargs+=("$playpath")
aargs+=("--tags")
aargs+=("$tags")
time PY_COLORS=1 \
ANSIBLE_FORCE_COLOR=1 \
ansible-playbook "${aargs[@]}"
;;
'install-i')
# var
verbose=false
tags='untagged'
# parse the argumetns
while getopts ':vt:r:' opt; do
case "$opt" in
v)
verbose=true
;;
t)
tags="$tags,$OPTARG"
;;
*)
echo "Unrecognized option $opt" >&2
displayHelpMain
exit 1
;;
esac
done
install_nix
# install with ansible playbook
if [[ "$verbose" == 'true' ]]; then
time ansible-playbook -i ./inventory/localhost.yaml -e 'playbook_target=localhost' -e "playbook_target=localhost" -vvv "$WHOLE_PLAYBOOK_PATH" --tags "$tags"
elif [[ "$verbose" == 'false' ]]; then
time ansible-playbook -i ./inventory/localhost.yaml -e 'playbook_target=localhost' -e "playbook_target=localhost" "$WHOLE_PLAYBOOK_PATH" --tags "$tags"
fi
;;
'install')
# var
verbose=false
tags='untagged'
# parse the argumetns
while getopts ':vt:r:' opt; do
case "$opt" in
v)
verbose=true
;;
t)
tags="$tags,$OPTARG"
;;
*)
echo "Unrecognized option $opt" >&2
displayHelpMain
exit 1
;;
esac
done
install_nix
# buil args
aargs=()
# install with ansible playbook
if [[ "$verbose" == 'true' ]]; then
aargs+=("-vvv")
fi
aargs+=(-i "./inventory/localhost.yaml")
aargs+=(-e "playbook_target=localhost")
aargs+=(-e "ansible_python_interpreter=$EXPLICIT_PYTHON_PATH_FOR_ANSIBLE")
aargs+=("$WHOLE_PLAYBOOK_PATH")
aargs+=("--tags")
aargs+=("$tags")
time PY_COLORS=1 \
ANSIBLE_FORCE_COLOR=1 \
ansible-playbook "${aargs[@]}"
;;
'tmux')
if ! (tmux ls | grep -q blah); then
tmux new-session -d -s blah -n dev-env-ansible -c "$SCRIPT_DIR"
tmux new-window -d -t blah: -n dockers -c "$SCRIPT_DIR"
[[ -d "$SCRIPT_DIR/../dotfiles" ]] && tmux new-window -d -t blah: -n dotfiles -c "$SCRIPT_DIR/../dotfiles"
[[ -d "$SCRIPT_DIR/../kinesisadv360pro" ]] && tmux new-window -d -t blah: -n adv360pro -c "$SCRIPT_DIR/../kinesisadv360pro"
# tmux new-window -d -t blah: -n focusside -c "$SCRIPT_DIR/../focus-side.vim"
fi
tmux attach-session -t blah
;;
'run')
# var
ver="$DOCKER_FILE_UBUNTU_22"
# parse the argumetns
while getopts ':d:w:' opt; do
case "$opt" in
d)
ver="$(select_docker_ver "$OPTARG")"
;;
w)