diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a201e35 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +.vscode/ + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cca770d --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +BIN ?= dockerutil +PREFIX ?= /usr/local + +install: + cp dockeruitl $(PREFIX)/bin/$(BIN) + +uninstall: + rm -f $(PREFIX)/bin/$(BIN) \ No newline at end of file diff --git a/dockerutil b/dockerutil new file mode 100755 index 0000000..323e8e9 --- /dev/null +++ b/dockerutil @@ -0,0 +1,192 @@ +#!/usr/bin/env bash + +# CONSTANTS: +readonly DOCKERUTIL_BOLD=$(tput bold) +readonly DOCKERUTIL_UNDERLINE=$(tput sgr 0 1) +readonly DOCKERUTIL_RESET=$(tput sgr0) + +readonly DOCKERUTIL_PURPLE=$(tput setaf 171) +readonly DOCKERUTIL_RED=$(tput setaf 1) +readonly DOCKERUTIL_GREEN=$(tput setaf 76) +readonly DOCKERUTIL_TAN=$(tput setaf 3) +readonly DOCKERUTIL_BLUE=$(tput setaf 38) + +# LOGGING: +function dockerutil::print_header() { + printf "\n${DOCKERUTIL_BOLD}${DOCKERUTIL_PURPLE}========== %s ==========${DOCKERUTIL_RESET}\n" "$@" 1>&2 +} + +function dockerutil::print_arrow() { + printf "➜ $@\n" 1>&2 +} + +function dockerutil::print_success() { + printf "${DOCKERUTIL_GREEN}✔ %s${DOCKERUTIL_RESET}\n" "$@" 1>&2 +} + +function dockerutil::print_error() { + printf "${DOCKERUTIL_RED}✖ %s${DOCKERUTIL_RESET}\n" "$@" 1>&2 +} + +function dockerutil::print_warning() { + printf "${DOCKERUTIL_TAN}➜ %s${DOCKERUTIL_RESET}\n" "$@" 1>&2 +} + +function dockerutil::print_underline() { + printf "${DOCKERUTIL_UNDERLINE}${DOCKERUTIL_BOLD}%s${DOCKERUTIL_RESET}\n" "$@" 1>&2 +} +function dockerutil::print_bold() { + printf "${DOCKERUTIL_BOLD}%s${DOCKERUTIL_RESET}\n" "$@" +} +dockerutil::print_note() { + printf "${DOCKERUTIL_UNDERLINE}${DOCKERUTIL_BOLD}${DOCKERUTIL_BLUE}Note:${DOCKERUTIL_RESET} ${DOCKERUTIL_BLUE}%s${DOCKERUTIL_RESET}\n" "$@" 1>&2 +} + +function dockerutil::is_cygwin_env() { + [[ $(uname -s) == 'CYGWIN'* ]] +} + +function dockerutil::get_docker_shared_project_dir() { + local ret=$PWD + + # check for cygwin users(on windows) + if $(dockerutil::is_cygwin_env); then + # for windows users you must sets shared folder on projects + # help: https://gist.github.com/matthiasg/76dd03926d095db08745 + local shared_dir=${DOCKER_SHARED_PROJECT_DIR:-'//home/docker/projects'} + # replaced projects root dir with shared dir + ret="${shared_dir}/$(basename $PWD)" + fi + + echo $ret +} + +function dockerutil::pwd() { + local ret=$PWD + + # check for cygwin users(on windows) + if $(dockerutil::is_cygwin_env); then + ret=$(cygpath -w "$PWD") + fi + + echo $ret +} + +####################################### +# Returns true when service with given name exists +# Globals: +# None +# Arguments: +# service_name +# Returns: +# not empty when exists +####################################### +function dockerutil::service_exists { + local exists=$(docker service ls -q -f "name=${1}") + [ ! -z "$exists" ] +} + +####################################### +# Returns true when service with given name exists +# Globals: +# None +# Arguments: +# label +# Returns: +# None +####################################### +function dockerutil::clean_all_with_label() { + local label=$1 + local sleep_time=${2:-3} + + dockerutil::print_header "CLEANING ENTITIES WITH LABEL: $label" + + for type in 'service' 'secret' 'volume' 'network' 'config' + do + dockerutil::print_arrow "removing ${type}s" + entities=$(docker $type ls -qf "label=$label") + if [ -n "$entities" ]; then + docker $type ls -qf "label=$label" | xargs docker $type rm + sleep $sleep_time + fi + done +} + +function dockerutil::remove_entity() { + local type=$1 + local id=$2 +} + +####################################### +# Returns true when network with given name exists +# Globals: +# None +# Arguments: +# service_name +# Returns: +# bool +####################################### +function dockerutil::network_exists { + local exists=$(docker network ls -q -f "name=${1}\$") + [ ! -z "$exists" ] +} + +function dockerutil::secret_exists { + local exists=$(docker secret ls -q -f "name=${1}") + [ "$exists" != '' ] +} + +function dockerutil::create_secret_if_not_exists { + local name=$1 + local value=$2 + + if ! $(dockerutil::secret_exists $name); then + echo $value | docker secret create $name - + dockerutil::print_success "secret $name created" + else + dockerutil::print_warning "secret $name exists" + fi +} + +function dockerutil::exec_command_in_container { + local container=$1 + local command=$2 + + echo $(docker exec $container /bin/sh -c "$command") +} + +function dockerutil::get_container_file_contents { + local container=$1 + local filename=$2 + + if [ ! $(dockerutil::exec_command_in_container $container "[ -f $filename ] && echo "1" || echo ''") ]; then + dockerutil::print_error "file: $filename in container not exists" + return 1 + fi + + echo $(dockerutil::exec_command_in_container $container "cat $filename") +} + +function dockerutil::get_service_container { + local service_name=$1 + + local service_filter="label=com.docker.swarm.service.name=$service_name" + while [ ! "$(docker ps -f "$service_filter" -f "status=running" -q)" ]; + do + dockerutil::print_note "waiting for container of $service_name service" + sleep 2 + done + echo $(docker ps -f "$service_filter" -q) +} + +function dockerutil::composer_install { + local install_dir=$1 + local user=$2 + + docker run \ + --rm \ + --interactive --tty \ + --volume "$install_dir":/app \ + --user $(id -u $user):$(id -g $user) \ + composer install --no-dev --optimize-autoloader --prefer-dist --no-suggest --ignore-platform-reqs +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..603ffb6 --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "name": "bash_dockerutil", + "version": "1.0.0", + "description": "Docker utility functions", + "scripts": [ "term" ], + "install": "make install" +} \ No newline at end of file diff --git a/test_dockerutil.sh b/test_dockerutil.sh new file mode 100644 index 0000000..bb0462a --- /dev/null +++ b/test_dockerutil.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +. ./bin/dockerutil + + +dockerutil::print_header "print_arrow()" +dockerutil::print_arrow "text" + +dockerutil::print_header "print_success()" +dockerutil::print_success "text" + +dockerutil::print_header "print_error()" +dockerutil::print_error "text" + +dockerutil::print_header "print_warning()" +dockerutil::print_warning "text" + +dockerutil::print_header "print_underline()" +dockerutil::print_underline "text" + +dockerutil::print_header "print_bold()" +dockerutil::print_bold "text" + +dockerutil::print_header "print_note()" +dockerutil::print_note "text" + +dockerutil::print_header "is_cygwin_env()" +$(dockerutil::is_cygwin_env) && echo 'true' || echo 'false' + +dockerutil::print_header "pwd()" +dockerutil::pwd + +dockerutil::print_header "get_docker_shared_project_dir()" +dockerutil::get_docker_shared_project_dir + +dockerutil::print_header "dockerutil::clean_all_with_label()" +test_label="dockerutil_testenv" +docker service create --name "dockerutil_test_service" --label $test_label nginx:alpine +dockerutil::clean_all_with_label $test_label 2 + +