#!/bin/bash # Note: all up scripts use set -a to export all variables created, set +a can be used to then turn off this effect trap exiting EXIT #Print done when program exits export VARIABLES_DIR=${PWD%/*}/variables/compose source $VARIABLES_DIR source $PATH_VARS_DIR export RUN_PULL_CONTAINER=N export CREATE_VOLUME_DIR=N export CREATE_VOLUME_DIR_ONLY=N COMMAND=$1 CONTAINERS="" BASE_DIR=$PWD all () { ALL=$(ls -d */ | sed 's/\/$//') #Using -d and */ to get just directories, sed removes the trailing backslash if there is one } #This method only works for one worded variables as all spaces will split variables! # If running comamnds, put all commands into an array if [[ "$1" == "run" ]]; then # PROGRAMS=$(echo "${@:3}" | tr ';' '\n') mapfile -t PROGRAMS < <(echo "${@:3}" | tr ';' '\n') # This uses an array which allows for spaces! fi # If wanting to run the specified command under all containers, put all containers in a list, otherwise use user specified if [[ "$2" == "ALL" ]]; then all CONTAINERS=$ALL else CONTAINERS=$(echo "$2" | tr '|' '\n') #This method only works for one worded variables as all spaces will split variables! fi # Pull if run or restart has the -P or --pull option as the 3rd arg or later (shouldn't be before 3rd) if [[ "$1" == "up" || "$1" == "restart" ]]; then for arg in "${@:3}" do if [[ "$arg" == "-P" || "$arg" == "--pull" ]]; then RUN_PULL_CONTAINER=Y fi done fi # if -V or --volume is passed, create a volume directory along with a compose directory for the service being created if [[ "$1" == "create" ]]; then for arg in "${@:3}" do if [[ "$arg" == "-V" || "$arg" == "--volume" ]]; then CREATE_VOLUME_DIR=Y fi if [[ "$arg" == "--volume-only" ]]; then CREATE_VOLUME_DIR_ONLY=Y CREATE_VOLUME_DIR=Y fi done fi process () { if [[ -z "$1" ]]; then # There will be a trailing empty command in the array return; fi exe="${1/\[CONTAINER\]/"$2"}" #exe=$(echo "$1" | sed "s/\[CONTAINER\]/$2/g") # This works too, but using shorter method instead echo "Running under \"$2\":" echo "* $exe *" bash -c "$exe" # Actually must use bash -c, otherwises running something like echo hi > test.txt will print everything instead of echoing it } # might be completely redundant to have a separate function for each, but allows for overrides. Also having a restart script for more customizability as well as I cannot easily keep track of what processes are in background, so having a restart script will manage all that for each service up () { for container in $CONTAINERS; do $PWD/$container/up & done } down () { for container in $CONTAINERS; do $PWD/$container/down & done } restart () { for container in $CONTAINERS; do $PWD/$container/restart & done } run () { for container in $CONTAINERS; do cd $BASE_DIR cd $container # echo "before loop" #for program in $PROGRAMS; do # echo "before prog" #echo "$program" #PROGRAMS=$(echo "${@:3}" | tr ';' '\n') #echo $PROGRAMS echo "$container:" for i in "${PROGRAMS[@]}"; do process "$i" $container done # echo "after prog" #done # echo "after loop" done } #manage-var() { # VAR="PATH_VAR_$2" # VAR=${!VAR} # if [ -z "$VAR" ] # then # prt-var # exit 0 # fi # return $VAR #} ed-cmp () { for container in $CONTAINERS; do nano $PWD/$container/$3 done } ed-vol () { for container in $CONTAINERS; do nano $VOLUMES_DIR/$container/$3 done } ed-var () { VAR="PATH_VAR_$2" VAR=${!VAR} if [ -z "$VAR" ] then prt-var return 0 fi nano $VAR$3 } ls-cmp () { for container in $CONTAINERS; do ls -al $PWD/$container/$3 done } ls-vol () { for container in $CONTAINERS; do ls -al $VOLUMES_DIR/$container/$3 done } ls-var () { VAR="PATH_VAR_$2" VAR=${!VAR} if [ -z "$VAR" ] then prt-var return 0 fi ls $VAR$3 } prt-var () { cat $PATH_VARS_DIR } create () { for container in $CONTAINERS; do if [[ ! -d "$container" && "$CREATE_VOLUME_DIR_ONLY" != "Y" ]]; then mkdir $container export SERVICE_NAME="$container" envsubst '${SERVICE_NAME}' < /data/docker/default/compose/docker-compose.yml > $container/docker-compose.yml cp $DEFAULT_DIR/compose/vars $container cd $container ln -s ../../default/compose/up . ln -s ../../default/compose/restart . ln -s ../../default/compose/down . cd .. sudo chown "$CREATE_OWNER:$CREATE_GROUP" $container -R sudo chmod "$CREATE_PERMISSION" $container -R sudo chmod +X $container elif [[ "$CREATE_VOLUME_DIR_ONLY" != "Y" ]]; then echo "Cannot create \"$container,\" it already exists." fi if [[ "$CREATE_VOLUME_DIR" == "Y" ]]; then if [[ ! -d "$VOLUMES_DIR/$container" ]]; then mkdir $VOLUMES_DIR/$container sudo chown "$CREATE_OWNER:$CREATE_GROUP" $VOLUMES_DIR/$container sudo chmod "$CREATE_PERMISSION" $VOLUMES_DIR/$container sudo chmod +X $VOLUMES_DIR/$container else echo "Cannot create \"$container\" volume, it already exists." fi fi done } help () { echo Local Docker-Compose Manager echo "Syntax: ./manage [command] [container] [additional parameters]" echo "Container Paremeter:" echo " * Single: " echo " * Multiple: " echo " * All: " echo "Command Parameter:" echo " * Runs in the directory of the container's docker compose (one level deep from this manage script)" echo " * Must be wrapped in quotes or escape semicolons to run multiple commands per container." echo "Available Commands: command, parameters" echo " up [container]" echo " down [container] -P/--pull" echo " restart [container] -P/--pull" echo " run [container] [command]" echo " ed-cmp [container] [extra path]" echo " ed-vol [container] [extra path]" echo " ed-var [container] [extra path]" echo " ls-cmp [container] [extra path]" echo " ls-vol [container] [extra path]" echo " ls-var [container] [extra path]" echo " prt-var [container]" echo " create [container] -V/--volume --volume-only" } case $COMMAND in up) up ;; down) down ;; restart) restart ;; run) run "$@" #I originally just had this as $@, without quotes, which separated all of the variables and caused massive issues ;; help) help;; --help) help ;; ed-cmp) ed-cmp "$@" ;; ed-vol) ed-vol "$@" ;; ed-var) ed-var "$@" ;; ls-cmp) ls-cmp "$@" ;; ls-vol) ls-vol "$@" ;; ls-var) ls-var "$@" ;; prt-var) prt-var "$@" ;; create) create "$@" ;; *) echo "No such command '$COMMAND'" help ;; esac exiting () { echo "done" } wait #Wait for all background processes to end