Last active
July 9, 2018 04:53
-
-
Save KelSolaar/c6a05fc62badff75c08933e0926c4f91 to your computer and use it in GitHub Desktop.
Docker - Bash Shell Helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Container Information | |
CONTAINER_IMAGE="nginx:latest" | |
CONTAINER_NAME="nginx" | |
# Container Run Command | |
run-container() { | |
docker run \ | |
-d \ | |
--restart always \ | |
--name ${CONTAINER_NAME} \ | |
-p 80:80 \ | |
-v ~/Containers/nginx/example.com:/usr/share/nginx/html \ | |
-v ~/Containers/nginx/default.conf:/etc/nginx/conf.d/default.conf \ | |
${CONTAINER_IMAGE} 2>&1 || true | |
} | |
# Display Colours | |
COLOUR_ESC="\x1B[" | |
COLOUR_RESET=0 | |
COLOUR_RESET_UNDERLINE=24 | |
COLOUR_RESET_REVERSE=27 | |
COLOUR_BOLD=1 | |
COLOUR_BRIGHT=2 | |
COLOUR_UNDERSCORE=4 | |
COLOUR_REVERSE=7 | |
COLOUR_DEFAULT_FG=39 | |
COLOUR_BLACK_FG=30 | |
COLOUR_RED_FG=31 | |
COLOUR_GREEN_FG=32 | |
COLOUR_BROWN_FG=33 | |
COLOUR_BLUE_FG=34 | |
COLOUR_MAGENTA_FG=35 | |
COLOUR_CYAN_FG=36 | |
COLOUR_WHITE_FG=37 | |
COLOUR_DEFAULT_BG=49 | |
COLOUR_BLACK_BG=40 | |
COLOUR_RED_BG=41 | |
COLOUR_GREEN_BG=42 | |
COLOUR_BROWN_BG=43 | |
COLOUR_BLUE_BG=44 | |
COLOUR_MAGENTA_BG=45 | |
COLOUR_CYAN_BG=46 | |
COLOUR_WHITE_BG=47 | |
COLOUR_DEBUG=$COLOUR_BLUE_FG | |
COLOUR_INFO=$COLOUR_MAGENTA_FG | |
COLOUR_HIGHLIGHT=$COLOUR_CYAN_FG | |
COLOUR_WARNING=$COLOUR_BROWN_FG | |
COLOUR_ERROR=$COLOUR_RED_FG | |
COLOUR_SUCCESS=$COLOUR_GREEN_FG | |
DEBUG_STAMP="> Debug : " | |
INFO_STAMP="> Info : " | |
WARNING_STAMP="> Warning : " | |
ERROR_STAMP="> Error : " | |
OUTPUT_STAMP="> Output : " | |
SUCCESS_STAMP="> Success : " | |
display-message() { | |
local colour="$1" | |
local categorie="$2" | |
local message="$3" | |
if ! [ $colour -ge 0 -a $colour -le 47 ] 2>/dev/null; then | |
printf "\"colour\" argument is out of range!" >&2 && \ | |
return 1 | |
fi | |
if [ -z "$categorie" ]; then | |
printf "No \"categorie\" argument defined!" >&2 && \ | |
return 1 | |
fi | |
if [ -z "$message" ]; then | |
message="" | |
fi | |
local stamp="" | |
case "$categorie" in | |
"debug") | |
stamp=$DEBUG_STAMP ;; | |
"info") | |
stamp=$INFO_STAMP ;; | |
"warning") | |
stamp=$WARNING_STAMP ;; | |
"error") | |
stamp=$ERROR_STAMP ;; | |
"output") | |
stamp=$OUTPUT_STAMP ;; | |
"success") | |
stamp=$SUCCESS_STAMP ;; | |
esac | |
message="${COLOUR_ESC}${colour}m${stamp}${message}${COLOUR_ESC}${COLOUR_RESET}m" | |
printf "$message\n" | |
} | |
logger() { | |
local colour="$1" | |
local categorie="$2" | |
shift 2 | |
while read -r line; do | |
display-message "$colour" "$categorie" "$line" | |
done <<< "$@" | |
} | |
debug() { | |
logger "${COLOUR_DEBUG}" debug "$@" | |
} | |
info() { | |
logger "${COLOUR_INFO}" info "$@" | |
} | |
warning() { | |
logger "${COLOUR_WARNING}" warning "$@" | |
} | |
error() { | |
logger "${COLOUR_ERROR}" error "$@" | |
} | |
output() { | |
logger "${COLOUR_HIGHLIGHT}" output "$@" | |
} | |
success() { | |
logger "${COLOUR_SUCCESS}" success "$@" | |
} | |
# Docker Functions | |
docker-start() { | |
info "Starting '${CONTAINER_NAME}' container." && \ | |
output "$(docker start ${CONTAINER_NAME} 2>&1)" || true | |
} | |
docker-stop() { | |
info "Stopping '${CONTAINER_NAME}' container." && \ | |
output "$(docker stop ${CONTAINER_NAME} 2>&1)" || true | |
} | |
docker-remove() { | |
info "Removing '${CONTAINER_NAME}' container." && \ | |
output "$(docker rm -f ${CONTAINER_NAME} 2>&1)" || true | |
} | |
docker-pull() { | |
info "Pulling '${CONTAINER_IMAGE}' container image." && \ | |
output "$(docker pull ${CONTAINER_IMAGE} 2>&1)" || true | |
} | |
docker-run() { | |
info "Running '${CONTAINER_NAME}' container." && \ | |
output "$(run-container)" | |
} | |
docker-end() { | |
info "Ending '${CONTAINER_NAME}' container." && \ | |
docker-stop && \ | |
docker-remove | |
} | |
docker-begin() { | |
info "Begining '${CONTAINER_NAME}' container." && \ | |
docker-end && \ | |
docker-pull && \ | |
docker-run | |
} | |
# Usage | |
help() { | |
logger "${COLOUR_INFO}" "None" "Usage: invoke.sh options\n\nOptions:\n" | |
logger "${COLOUR_HIGHLIGHT}" "None" \ | |
"\thelp : Displays this message.\n" \ | |
"\tdocker-start : Starts the '${CONTAINER_NAME}' container.\n" \ | |
"\tdocker-stop : Stops the '${CONTAINER_NAME}' container.\n" \ | |
"\tdocker-remove : Removes the '${CONTAINER_NAME}' container.\n" \ | |
"\tdocker-pull : Pulls the '${CONTAINER_IMAGE}' image.\n" \ | |
"\tdocker-run : Runs the '${CONTAINER_NAME}' container.\n" \ | |
"\tdocker-end : Ends the '${CONTAINER_NAME}' container: \ | |
Stops current instance and removes it.\n" \ | |
"\tdocker-begin : Begins the '${CONTAINER_NAME}' container: \ | |
Ends prior instance, pulls '${CONTAINER_IMAGE}' image and \ | |
starts the container.\n" | |
} | |
$* | |
# Logging interface adapted from: | |
# https://github.com/swyckoff/color-logger-bash | |
# The MIT License (MIT) | |
# Copyright (c) 2014 swyckoff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment