Files
spark/tools/sparkd_profile.sh
T

137 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
help_text="\
Manage sparkd config profiles.
Usage: sparkd_profile.sh [OPTIONS] [POSITIONALS]
Positionals:
PROFILE The profile to use
Options:
--profile PROFILE The profile to use
-l,--list List profiles
--save-as PROFILE Saves current conf to PROFILE
-h,--help Print this message
"
print_help_and_exit() {
echo "$help_text"
exit "${1:-0}"
}
this_dir="$(dirname "${BASH_SOURCE[0]:-$0}")"
profiles_dir="$(realpath "$this_dir/../src/app_sparkd/configurations")"
declare -r spark_data_dir="${HOME:-/home/bts}/sparkdata"
declare -r spark_conf_dir="$spark_data_dir/conf"
mkdir -p "$spark_data_dir"
list() {
if [ ! -d "$profiles_dir" ]; then
echo "Profiles dir not found '${profiles_dir}'"
return 1
fi
curr_profile_path="$(realpath "$(readlink -f "$spark_conf_dir")")"
cd "$profiles_dir"
for p in *; do
if [[ ! -d "$p" ]]; then
continue
fi
full_path="$(realpath "$(readlink -f "$p")")"
if [ "$full_path" == "$curr_profile_path" ]; then
echo -n " * "
else
echo -n " "
fi
echo "$p"
done
}
save_as() {
if [ $# -ne 1 ]; then
echo "'save_as' received invalid arguments $@"
echo "Usage: save_as PROFILE"
echo
return 1
fi
if [ ! -d "$spark_conf_dir" ]; then
echo "Spark conf dir not found. '$spark_conf_dir'"
return 1
fi
dest="$profiles_dir/$1"
if [ -e "$dest" ]; then
declare -l res
read -p "Overwrite existing profile (y/n)? " res
if [ "$res" != "y" ]; then
echo "Did not save profile"
return 0
fi
rm -rf "$dest"
fi
real_spark_conf_dir="$(readlink -f "$spark_conf_dir")"
if [ ! -d "$real_spark_conf_dir" ]; then
echo "Spark conf dir not a valid directory: $spark_conf_dir"
return 1
fi
echo "Saving $real_spark_conf_dir to $dest"
cp -r "$real_spark_conf_dir" "$dest"
}
change_profile() {
if [ $# -ne 1 ]; then
echo "'change_profile' received invalid arguments $@"
echo "Usage: change_profile PROFILE"
echo
return 1
fi
profile_conf_dir="$profiles_dir/$1"
if [ ! -e "$profile_conf_dir" ]; then
echo "Profile '$1' not found. Create dir here (add spark.conf, etc.): $profile_conf_dir"
print_help_and_exit 1
fi
if [ -e "$spark_conf_dir" ]; then
if stat -c %F "$spark_conf_dir" | grep -q "symbolic"; then
rm "$spark_conf_dir"
else
echo "$spark_conf_dir exists but is not a symlink. Please remove it."
print_help_and_exit 1
fi
fi
ln -s "$profile_conf_dir" "$spark_conf_dir"
}
if [ $# -eq 0 ]; then
list
exit $?
fi
profile=""
while [[ $# -gt 0 ]]; do
case "$1" in
-l)
;&
--list)
list
exit $?
;;
--save-as)
save_as "$2"
exit $?
;;
-h)
;&
--help)
print_help_and_exit
;;
--profile)
change_profile "$2"
exit $?
;;
*)
change_profile "$1"
exit $?
;;
esac
done