various fixes, improvements, shellcheck
[automated-distro-installer] / fai-wrapper
1 #!/bin/bash
2 # This file is part of Ian Kelling's automated-distro-installer
3 # Copyright (C) 2024 Ian Kelling
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # For using some fai commands outside of fai.
20 # Usually this is sourced from another script. Note this has
21 # paths specific to Ian's machine.
22 # to set fai classes, export CLASS_CLASSNAME=true
23 # This wrapper can be detected by using this var:
24 export FAI_WRAPPER=true
25
26 ifclass() {
27 local var=${1/#/CLASS_}
28 [[ $HOSTNAME == "$1" || ${!var} ]]
29 }
30 fai-setclass() {
31 for class in "$@"; do
32 # export class vars with CLASS_ in front to avoid name collisions.
33 classes+=" $class"
34 export CLASS_$class=true
35 done
36 classes="${classes# }"
37 export classes
38 }
39 eval-fai-classfile() {
40 file=$1
41 if [[ ! -s $file ]]; then
42 echo "$0: probably an error: eval-fai-classfile no such file: $file"
43 return 0
44 fi
45 fai-setclass $(bash $file)
46 }
47 export -f ifclass
48 # DEFAULT is used by fcopy
49 classes="DEFAULT $(hostname)"
50 export CLASS_DEFAULT=true
51 if [[ ! -d $FAI_ROOT ]]; then
52 export FAI_ROOT=/
53 fi
54 if [[ ! -d $FAI ]]; then
55 if [[ -d /a/bin/fai/fai/config ]]; then
56 export FAI=/a/bin/fai/fai/config
57 else
58 echo "$0: error: could not find directory to set for FAI. currently FAI=$FAI"
59 return 1
60 fi
61 fi
62
63 eval-fai-classfile $FAI/class/50-host-classes
64 eval-fai-classfile $FAI/class/51-multi-boot