Files
spark/tools/sparkd_data.sh

251 lines
5.5 KiB
Bash
Executable File

#!/usr/bin/env bash
help_text="\
Manage files in sparkd data volumes
Usage: sparkd_data.sh [OPTIONS] [COMMAND] [...]
Commands:
get Copy files FROM data volume
set Copy files TO data volume
shell Start shell in data volume
tail Tail sparkd log file
Options:
-h,--help Print this message and exit
"
cp_help_text="\
Copy files to/from sparkd data volume
Usage: sparkd_data.sh <get|set> [OPTIONS] [POSITIONALS]
Positionals:
SOURCE Source file
DEST Dest file
Options:
--clean Recreate the container that mounts to the volume (even if it already exists)
--source Source file
--dest Dest file
--config-port The config port for the volume to manage
-h,--help Print this message and exit
"
shell_help_text="\
Start shell in sparkd data volume
Usage: sparkd_data.sh shell [OPTIONS]
Options:
--clean Recreate the container that mounts to the volume (even if it already exists)
--config-port The config port for the volume to manage
-h,--help Print this message and exit
"
tail_help_text="\
Tail sparkd log file
Usage: sparkd_data.sh tail [OPTIONS]
Options:
-h,--help Print this message and exit
"
print_help_and_exit() {
echo "$1"
exit "${2:-0}"
}
bounce_container() {
if [ "$#" -ne 3 ]; then
echo "bounce_container invalid args '$@'."
echo "Usage: bounce_container <clean:0|1> <container_name> <volume_name>"
return 1
fi
clean="$1"
cont_name="$2"
vol_name="$3"
image_name="bluetradingsystems/linux_dotnet7"
if [ -n "$(sudo docker container ls -a -q --filter "name=$cont_name")" ]; then
if [[ $clean -eq 1 ]]; then
sudo docker stop $cont_name
sudo docker rm $cont_name > /dev/null 2>&1
sudo docker run --name $cont_name --rm -it -d -v $vol_name:/home/bts/data $image_name sh
fi
else
sudo docker run --name $cont_name --rm -it -d -v $vol_name:/home/bts/data $image_name sh
fi
}
get_container_name() {
if [ "$#" -ne 1 ]; then
echo "get_container_name invalid args '$@'."
echo "Usage: container_name <config_port>"
return 1
fi
echo "sparkd_data_man_${1}"
}
get_volume_name() {
if [ "$#" -ne 1 ]; then
echo "get_volume_name invalid args '$@'."
echo "Usage: get_volume_name <config_port>"
return 1
fi
echo "sparkd_${1}"
}
cp_file() {
config_port=${SPARKD__CONFIG_PORT:-8000}
is_get=${1:-1}
shift 1
source=""
dest=""
clean=0
while [[ $# -gt 0 ]]; do
case "$1" in
--clean)
clean=1
shift 1
;;
--source)
if [[ -z "$2" ]]; then
echo "Source (--source) must be non-empty"
print_help_and_exit "$cp_help_text" 1
fi
source="$2"
shift 2
;;
--dest)
if [[ -z "$2" ]]; then
echo "Dest (--Dest) must be non-empty"
print_help_and_exit "$cp_help_text" 1
fi
dest="$2"
shift 2
;;
--config-port)
if [[ -z "$2" ]]; then
echo "Config port (--config-port) must be non-empty"
print_help_and_exit "$cp_help_text" 1
fi
config_port="$2"
shift 2
;;
-h)
;&
--help)
print_help_and_exit "$cp_help_text" 0
;;
*)
if [[ -z "$source" ]]; then
source="$1"
elif [[ -z "$dest" ]]; then
dest="$1"
else
echo "Invalid positional argument '$1'"
print_help_and_exit "$cp_help_text" 1
fi
shift 1
;;
esac
done
if [ -z "$source" ]; then
echo -e "Error: No source specified to copy\n"
print_help_and_exit "$cp_help_text" 1
fi
if [ -z "$dest" ]; then
dest="."
fi
cont_name="$(get_container_name "$config_port")"
vol_name="$(get_volume_name "$config_port")"
bounce_container "$clean" "$cont_name" "$vol_name"
if [[ $is_get -eq 1 ]]; then
sudo docker cp $cont_name:/home/bts/data/$source $dest
else
sudo docker cp $source $cont_name:/home/bts/data/$dest
fi
}
shell() {
clean=0
config_port=${SPARKD__CONFIG_PORT:-8000}
while [[ $# -gt 0 ]]; do
case "$1" in
--clean)
clean=1
shift 1
;;
--config-port)
if [[ -z "$2" ]]; then
echo "Config port (--config-port) must be non-empty"
print_help_and_exit "$shell_help_text" 1
fi
config_port="$2"
shift 2
;;
-h)
;&
--help)
print_help_and_exit "$shell_help_text" 0
;;
*)
echo "Invalid positional argument '$1'"
print_help_and_exit "$shell_help_text" 1
;;
esac
done
cont_name="$(get_container_name "$config_port")"
vol_name="$(get_volume_name "$config_port")"
bounce_container "$clean" "$cont_name" "$vol_name"
sudo docker exec -it -w /home/bts/data $cont_name bash
}
_tail() {
service="sparkd-tail"
while [[ $# -gt 0 ]]; do
case "$1" in
-h)
;&
--help)
print_help_and_exit "$tail_help_text" 0
;;
*)
echo "Invalid positional argument '$1'"
print_help_and_exit "$tail_help_text" 1
;;
esac
done
sudo docker compose rm --force "$service"
sudo docker compose up "$service"
}
case "$1" in
get)
shift 1
cp_file 1 $@
;;
set)
shift 1
cp_file 0 $@
;;
shell)
shift 1
shell $@
;;
tail)
shift 1
_tail $@
;;
*)
echo "Invalid argument '$1'"
print_help_and_exit "$help_text" 1
;;
esac