Skip to content

Instantly share code, notes, and snippets.

@mak001
Last active November 11, 2020 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mak001/bf899cacd3343e3b7aaac91b38a97211 to your computer and use it in GitHub Desktop.
Save mak001/bf899cacd3343e3b7aaac91b38a97211 to your computer and use it in GitHub Desktop.
#!/bin/bash
command=$1
box=$2
var=$3
topDomain="localhost"
cd /hdd/docker
function getDir() {
path=()
for partial in `echo $1 | tr "/" "\n"`; do
tempPath=$(printf "%s/" "${path[@]}")
directory=$(find ./$tempPath* -maxdepth 0 -type d -print | grep "$partial" | head -n1)
end=${directory##*/}
path+=($end)
done
printf "%s/" "${path[@]}"
}
# gets the root directory of a silverstripe (or composer) box
function getRootDir() {
if [[ $(docker exec -it -u 1000 $1 test -d /var/www/public/vendor && echo 'true') ]]; then
echo '/var/www/public'
else
echo '/var/www'
fi
}
# gets the container's name. defaults to web
function getContainerName() {
type=${2:-web}
echo $(docker ps --format "{{.Names}}" -q | grep "$1.*$type")
}
function getBoxName() {
name=()
for partial in `echo $1 | tr "/" "\n"`; do
name+=($partial)
done
echo $(printf "%s_" "${name[@]}" | sed 's/_*$//')
}
function getBoxURL() {
url=()
for partial in `echo $1 | tr "/" "\n"`; do
url=("$partial" "${url[@]}")
done
echo $(printf "%s." "${url[@]}" | sed 's/\.*$//')
}
if [[ $box == '' && $command != 'help' ]]; then
echo "please specifiy a container";
exit;
fi
# Start a docker-compose
if [[ $command == 'up' ]]; then
if [[ ! "$(docker ps -q | grep nginx-proxy)" ]]; then
docker-compose up -d
else
echo 'reverse proxy is runnig'
fi
if [[ $box != "" ]] && [[ ! "$(docker ps -q | grep $box)" ]]; then
dir=$(getDir $box)
if [[ $dir == "" ]]; then
echo "no folder with that name"
else
cd $dir
docker-compose up -d ${@:3}
fi
else
echo "$box is running"
fi
# Stop a docker-compose
elif [[ $command == 'halt' ]] || [[ $command == 'stop' ]]; then
if [[ $box == 'all' ]]; then
docker stop $(docker ps -q)
else
dir=$(getDir $box)
if [[ $dir == "" ]]; then
echo "no folder with that name"
else
cd $dir
docker-compose stop
fi
fi
# SSH into web
elif [[ $command == 'ssh' ]]; then
webContainer=$(getContainerName $box)
# If no container is found stop
if [[ ! $webContainer ]]; then
echo "No container found containing '$box'"
exit 1;
else
folder=$(getRootDir $webContainer)
# if additional var is found use it as a user
if [[ $var != '' ]]; then
docker exec -it -u $var -w $folder $webContainer bash
else
docker exec -it -w $folder $webContainer bash
fi
fi
elif [[ $command == 'test' ]]; then
webContainer=$(getContainerName $box)
# If no container is found stop
if [[ ! $webContainer ]]; then
echo "No container found containing '$box'"
exit 1;
else
vars=${@:3}
folder=$(getRootDir $webContainer)
docker exec -it -u 1000 -w $folder $webContainer vendor/bin/phpunit $vars
fi
elif [[ $command == 'sspak' ]]; then
webContainer=$(getContainerName $box)
# If no container is found stop
if [[ ! $webContainer ]]; then
echo "No container found containing '$box'"
exit 1;
else
folder=$(getRootDir $webContainer)
if [[ $var == 'upgrade' || $var == 'install' ]]; then
docker exec -it -u 1000 -w $folder $webContainer bash -c "curl -sS https://silverstripe.github.io/sspak/install | php -- /usr/local/bin"
elif [[ $var == 'load' ]]; then
docker exec -it -u 1000 -w $folder $webContainer sspak load ${@:5} $4 ./
elif [[ $var == 'save' ]]; then
docker exec -it -u 1000 -w $folder $webContainer sspak save ${@:5} ./ $4
else
docker exec -it -u 1000 -w $folder $webContainer sspak ${@:3}
fi
fi
elif [[ $command == 'sake' ]]; then
webContainer=$(getContainerName $box)
# If no container is found stop
if [[ ! $webContainer ]]; then
echo "No container found containing '$box'"
exit 1;
else
folder=$(getRootDir $webContainer)
if [[ $folder == '/var/www/public' ]];then
sake='framework/sake'
else
sake='vendor/bin/sake'
fi
if [[ $var == 'task' ]]; then
docker exec -it -u 1000 -w $folder $webContainer $sake dev/tasks/${@:4}
elif [[ $var == 'build' ]]; then
docker exec -it -u 1000 -w $folder $webContainer $sake dev/build "flush=1"
else
docker exec -it -u 1000 -w $folder $webContainer $sake ${@:3}
fi
fi
# Update default host
elif [[ $command == 'host' ]]; then
host=$box
sed -i "s/DEFAULT_HOST=.*/DEFAULT_HOST=$host/" docker-compose.yml
docker-compose up -d
# Create a new docker-compose
elif [[ $command == 'create' ]] || [[ $command == 'new' ]]; then
if [[ $box == '' ]]; then
echo 'No folder name was specified'
exit 1;
fi
boxName=$(getBoxName $box)
url=$var
# if url wasn't passed use box instead
if [[ $url = "" ]]; then
url=$(getBoxURL $box)
fi
# creates the folder
mkdir $box
# copies the template file
cp docker-compose-default.yml $box/docker-compose.yml
cd $box
mkdir public
chown $USER:www-data -R public/
chmod 775 -R public/
mkdir html
chown $USER:www-data -R html/
chmod 775 -R html/
# replaces placeholders
sed -i "s/NAME/$boxName/g" docker-compose.yml
sed -i "s/URL/$url/g" docker-compose.yml
sed -i "s/TOP_DOMAIN/$topDomain/g" docker-compose.yml
# add to hosts
# echo "creating host entry"
# sudo bash -c "echo '127.0.0.1 $url.$topDomain www.$url.$topDomain' >> /etc/hosts"
# echo out results
echo "Created a new docker instance in folder: $box"
echo "With containers prefixed with $boxName"
echo "With the url of: $url.$topDomain"
elif [[ $command == 'help' ]]; then
echo "${0##*/} up <box> [options for docker]"
echo "${0##*/} halt [box]"
echo "${0##*/} stop [box]"
echo "${0##*/} ssh <box> [user]"
echo "${0##*/} test <box> [options for phpunit]"
echo "${0##*/} sake <box> build"
echo "${0##*/} sake <box> task <task>"
echo "${0##*/} sake <box> <sake command>"
echo "${0##*/} host <box>"
echo "${0##*/} create <box> [url]"
echo "${0##*/} new <box> [url]"
echo "${0##*/} sspak <box> upgrade"
echo "${0##*/} sspak <box> install"
echo "${0##*/} sspak <box> load <sspak> [options]"
echo "${0##*/} sspak <box> save <sspak> [options]"
echo "${0##*/} sspak <box> <sspak command>"
else
echo 'Command not recognized';
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment