Skip to content

Commit

Permalink
Add installer script for formula
Browse files Browse the repository at this point in the history
This script will let the user automatically install formula after
editing the pillar.
It is usefull to abstract salt logic and provide a simple installer
script, minimalistic to the user which are not familiar with salt.
  • Loading branch information
MalloZup committed Sep 30, 2020
1 parent 1a450f6 commit 25fd8e2
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 23 deletions.
8 changes: 4 additions & 4 deletions config/minion
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# we need to have different top file per formulas so call it with postfix formula
# top-hana.sls , top-netweaver.sls etc etc

state_top: top-hana.sls
module_dirs: ["/srv/salt"]
file_roots:
base:
- /usr/share/salt-formulas/states
pillar_roots:
base:
- /usr/share/salt-formulas/config/hana/pillar
use_superseded:
- module.run
2 changes: 1 addition & 1 deletion config/pillar/top.sls
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
base:
'*'
'*':
- hana.hana
3 changes: 0 additions & 3 deletions config/top.sls

This file was deleted.

3 changes: 0 additions & 3 deletions hana-top.sls

This file was deleted.

File renamed without changes.
96 changes: 96 additions & 0 deletions run-saphanabootstrap-formula
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash -e

# Script to install salt formulas

# these variables are formula specific:
FORMULA_LOG="/var/log/salt-hana-formula.log"
FORMULA_NAME='hana'
FORMULA_CONFIG='/usr/share/salt-formulas/config/hana'

# we need to prevent salt-call running in circustance which we don't want to
prevalidate () {
# check if we do have a config directory, otherwise fail. (salt will try to load default, which is not reproducible situation)
if [ ! -d "$FORMULA_CONFIG" ]; then
log_error "$FORMULA_CONFIG doesn't exists. Do you have installed formula $FORMULA_NAME correctly?"
fi
log_info "$FORMULA_CONFIG exists"
}

LOG_LEVEL='error'

print_help () {
cat <<-EOF
Install hana-formula, executing salt-call.
Supported Options:
-l [LOG_LEVEL] Set log level of saltstack. Default: error.
-h Show this help.
EOF
}

log_info () {
NODE=`hostname`
TIMESTAMP=`date -u`
GREEN='\033[0;32m'
NC='\033[0m' # No Color
printf "${GREEN}$TIMESTAMP::$NODE $1 ${NC}\n"
}

log_error () {
NODE=`hostname`
TIMESTAMP=`date -u`
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "${RED}$TIMESTAMP::$NODE::[ERROR] $1 ${NC}\n"
exit 1
}


run_salt () {
log_info "[PREFLIGHT]: Ensuring systems is satisfying pre-requisites ..."
prevalidate

log_info "[PREFLIGHT]: ------------------------------------------> DONE!"
# get log level
if [ ! -z "$1" ]
then
LOG_LEVEL="$1"
fi
log_info "[SETUP]: Using LOG level \"$LOG_LEVEL\" for running salt-call..."

log_info "[SETUP]: copying existing grains from /etc/salt/grains to salt-call working dir..."
cp /etc/salt/grains /usr/share/salt-formulas/config/hana/

log_info "[SETUP]: ----------------------------------------------> DONE!"
log_info "[FORMULA]: formula $FORMULA_NAME will be executed. Please wait.."
salt-call --local \
--log-level=$LOG_LEVEL \
--log-file=$FORMULA_LOG \
--log-file-level=debug \
--retcode-passthrough \
--force-color \
--config=$FORMULA_CONFIG \
state.apply $FORMULA_NAME || log_error "formula $FORMULA_NAME installation failed. Please check log file $FORMULA_LOG"

}

while getopts ":hl:" opt; do
case $opt in
h)
print_help
exit 0
;;
l)
log_level=$OPTARG
;;
*)
echo "Invalid option -$OPTARG" >&2
print_help
exit 1
;;
esac
done
# The variable OPTIND holds the number of options parsed by the last call to getopts.
# It is common practice to call the shift command at the end of your processing loop to remove options that have already been handled from $@.
shift $((OPTIND -1))
run_salt $log_level
10 changes: 0 additions & 10 deletions run-suse-hana-formula.sh

This file was deleted.

6 changes: 6 additions & 0 deletions saphanabootstrap-formula.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Wed Sep 30 16:43:32 UTC 2020 - Dario Maiocchi <dmaiocchi@suse.com>

- Version 0.6.2:
* add runner for executing formula as standalone with pillar modification

-------------------------------------------------------------------
Thu Sep 24 20:23:13 UTC 2020 - Simranpal Singh <simranpal.singh@suse.com>

Expand Down
18 changes: 16 additions & 2 deletions saphanabootstrap-formula.spec
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ formulas with forms, available on SUSE Manager 4.0.

mkdir -p %{buildroot}%{fdir}/states/%{fname}
mkdir -p %{buildroot}%{fdir}/metadata/%{fname}
mkdir -p %{buildroot}%{fdir}/config/%{fname}
mkdir -p %{buildroot}/usr/bin
mkdir -p %{buildroot}%{fdir}/config/%{fname}/pillar/%{fname}
install -m755 pillar.example %{buildroot}%{fdir}/config/%{fname}/pillar/%{fname}/hana.sls
install -m755 config/minion %{buildroot}%{fdir}/config/%{fname}/
install -m755 config/pillar/top.sls %{buildroot}%{fdir}/config/%{fname}/pillar/
install -m755 run-saphanabootstrap-formula %{buildroot}/usr/bin/run-saphanabootstrap-formula
cp -R %{fname} %{buildroot}%{fdir}/states
cp -R %{ftemplates} %{buildroot}%{fdir}/states/%{fname}
cp -R form.yml pillar.example %{buildroot}%{fdir}/metadata/%{fname}
Expand All @@ -60,7 +67,6 @@ then
cp -R metadata.yml %{buildroot}%{fdir}/metadata/%{fname}
fi


%files
%defattr(-,root,root,-)
%if 0%{?sle_version} < 120300
Expand All @@ -73,7 +79,15 @@ fi
%dir %attr(0755, root, salt) %{fdir}
%dir %attr(0755, root, salt) %{fdir}/states
%dir %attr(0755, root, salt) %{fdir}/metadata

%dir %attr(0755, root, salt) %{fdir}/config
%dir %attr(0755, root, salt) %{fdir}/config/hana/
%dir %attr(0755, root, salt) %{fdir}/config/hana/pillar
%dir %attr(0755, root, salt) %{fdir}/config/hana/pillar/hana

%attr(0755, root, salt) /usr/bin/run-saphanabootstrap-formula
%attr(0755, root, salt) %{fdir}/config/%{fname}/pillar/top.sls
%attr(0755, root, salt) %{fdir}/config/%{fname}/pillar/hana/hana.sls
%attr(0755, root, salt) %{fdir}/config/%{fname}/minion
%attr(0755, root, salt) %{fdir}/states/%{fname}
%attr(0755, root, salt) %{fdir}/states/%{fname}/%{ftemplates}
%attr(0755, root, salt) %{fdir}/metadata/%{fname}
Expand Down

0 comments on commit 25fd8e2

Please sign in to comment.