This repository has been archived on 2023-05-02. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
neo/man

220 lines
No EOL
6 KiB
Bash
Executable file

#!/bin/sh
PROJECT_NAME=${PROJECT_NAME:-neo}
LIST=docker-compose.*.yml
SAVEFILE=$HOME/.local/faulty/$PROJECT_NAME.save
PROFILES=""
alias dc="docker-compose -p $PROJECT_NAME"
# alias dc="echo docker-compose -p $PROJECT_NAME"
alias _p="printf \"%s\""
default() {
fopts=""
for f in $LIST; do
fopts="$fopts -f $f"
done
profopts=""
for p in $PROFILES; do
profopts="$profopts --profile $p"
done
dc $fopts $profopts $*
}
safe_env_load() {
# $1 = file
# $2 = var
# get env var from regex instead of inclusion
[ ! -f $1 ] && fatal "file doesn't exist"
[ ! -r $1 ] && fatal "file not readable"
res=$(grep -E "^$2=" $1 | sed -E "s/^$2=\"(.*)\"/\1/")
[ -z "$res" ] && fatal "var not found"
_p $res
}
write_savefile() {
[ -d $(dirname $1) ] || mkdir -p $(dirname $1})
echo -n "LIST=\"" > $1
for li in $LIST; do
echo -n " $li" >> $1
done
echo "\"" >> $1
echo -n "PROFILES=\"" >> $1
for p in $PROFILES; do
echo -n " $p" >> $1
done
echo "\"" >> $1
}
load_savefile() {
[ "$1" == "/dev/null" ] && return
[ ! -f $1 ] && return
LIST=$(safe_env_load $1 LIST)
PROFILES=$(save_env_load $1 PROFILES)
}
fatal() {
echo "FATAL: $@"
exit 1
}
includechain=0
handleFallthrough() {
cmd=${1:-}; shift
case $cmd in
delete-project-files|dpf)
[ -n "$1" ] || fatal "no directory specified"
[ -d /srv/$1 ] || fatal "directory doesn't exist"
ls /srv/$1
printf "Are you sure? (y/n) [n]: "
read answer
[ "$answer" = "y" ] && sudo rm -rf /srv/$1 || fatal "cancelled"
;;
-p) # add profile
PROFILES="$PROFILES $1"; shift
handleFallthrough $@
;;
-i) #include
if [ $includechain -eq 0 ]; then
LIST="docker-compose.$1.yml"
includechain=1
else
LIST="$LIST docker-compose.$1.yml"
fi
shift
handleFallthrough $@
;;
-x) #exclude
LIST=$(echo $LIST | sed "s/docker-compose.$1.yml/ /")
shift
handleFallthrough $@
;;
make:*)
WHAT=`echo $cmd | cut -c6-`
case $WHAT in
dc|docker-compose)
[ -n "$1" ] || fatal "no name specified"
FILE="docker-compose.$1.yml"
[ -f $FILE ] && fatal "file exists"
echo -e "version: '2.2'\n" > $FILE
echo -e "services: \n" >> $FILE
;;
backup)
[ -n "$1" ] || fatal "no name specified"
FILE="$1.tar.gz"
[ -f $FILE ] && fatal "file exists"
tar cvf $FILE /srv .
;;
*) fatal "not supported" ;;
esac
;;
setup:*|s:*)
WHAT=`echo $cmd | cut -c8-`
case $WHAT in
ports) sudo ./utils/setup-ports.sh up ./utils/rulelist.rules ;;
def) sudo ./utils/setup.sh ;;
all|a)
sudo ./utils/setup.sh
sudo ./utils/setup-ports.sh up ./utils/rulelist.rules
;;
*) fatal "not supported" ;;
esac
;;
--save) # save behaviour
handleFallthrough $@
write_savefile $SAVEFILE
;;
# preference
logs) default logs --tail=20 -f $@ ;;
up) default up -d $@ ;;
upr) default up -d --remove-orphans $@ ;;
--help|-h)
__() {
colsep="$1"
cmd="$2"
desc="$3"
aliases="$4"
printf "%s %-20s %s %-64s %s %-40s %s \n" "$colsep" "$cmd" "$colsep" "$desc" "$colsep" "$aliases" "$colsep"
}
_hr() {
repchar() {
printf "%$1s" | tr " " "$2"
}
__ "*" ${1:-$(repchar 20 -)} ${2:-$(repchar 64 -)} ${3:-$(repchar 40 -)}
}
_r() {
__ "|" "$1" "$2" "$3"
}
_hr
_r $0 Description Aliases
_hr
_hr "Flags" " " " "
_hr
_r "-x" "Excludes a docker-compose file from the list." " "
_r "-i" "Includes a docker-compose file from the list." " "
_hr
_hr "Commands" " " " "
_hr
_r "dpf" "Delete project files." "delete-project-files <name>"
_r "setup:ports" "Setup ports." "setup:ports"
_r "setup:def" "Setup default." "setup:def"
_r "setup:all" "Setup all." "setup:all"
_r "make:dc <name>" "Make docker-compose file." "make:docker-compose <name>"
_r "make:backup <name>" "Make backup file." "tar cvf <name>.tar.gz /srv"
_hr
_hr "Commands/aliases" "(shortcuts)" " "
_hr
_r "up" "Bring up services. (-d)" "$0 default up -d"
_r "upr" "Bring up services and remove orphans." "$0 default up -d --remove-orphans"
_r "logs" "View logs and follow with a tail of 20." "$0 default logs --tail=20 -f"
_hr
_r "default" "fallback to the main docker-compose command with fileopts." "docker-compose"
_hr
;;
debug)
load_savefile $SAVEFILE
_() {
eval "echo -n \"$1=\"; echo \$$1"
}
_ SAVEFILE
_ LIST
for i in $LIST; do
echo $i
done
return
;;
default) default $@ ;;
*|'') default $cmd $@ ;;
esac
}
main() {
case $1 in
--ignore-save)
SAVEFILE=/dev/null
shift 1
;;
--set-save)
SAVEFILE=$2
shift 2
main $@; exit 0
;;
--del-save)
rm $SAVEFILE
shift
;;
esac
load_savefile $SAVEFILE
handleFallthrough $@
}
main $@