Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-10 Added feature for global m2c docker services autostart configuration #11

Merged
merged 5 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .m2c/bin/m2c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

declare -r m2c_version="0.2.27"
declare -r m2c_version="0.3.0"
declare -r m2c_os="$(uname -s | awk '{print tolower($0)}')"
declare -r m2c_global_dir="$(echo ~/.m2c)"
declare m2c_loading_pid=
Expand Down Expand Up @@ -523,6 +523,9 @@ then
rm -rf "$m2c_global_dir"/{bin,completions,dnsmasq,docker,functions,local,traefik,www,build} >/dev/null 2>&1
m2c_install
. "$m2c_global_dir"/functions/m2c

m2c_global_load_env

m2c_sign "m2c.test"
m2c_global up

Expand All @@ -542,7 +545,8 @@ else
. "$m2c_global_dir"/functions/m2c
declare -r m2c_project_dir="$(m2c_get_project_dir)"
m2c_log="$m2c_project_dir"/.m2c/log
m2c_load_project_env

m2c_load_env

case "$1" in
global)
Expand Down
2 changes: 1 addition & 1 deletion .m2c/build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47
48
1 change: 1 addition & 0 deletions .m2c/docker/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
M2C_GLOBAL_SERVICES_AUTO_START=always
10 changes: 5 additions & 5 deletions .m2c/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
- traefik.enable=true
- traefik.frontend.rule=Host:m2c.test
- traefik.port=8080
restart: always
restart: ${M2C_GLOBAL_SERVICES_AUTO_START}

traefik:
container_name: m2c.test__traefik
Expand All @@ -36,7 +36,7 @@ services:
- traefik.enable=true
- traefik.frontend.rule=Host:traefik.m2c.test
- traefik.port=8080
restart: always
restart: ${M2C_GLOBAL_SERVICES_AUTO_START}

portainer:
container_name: m2c.test__portainer
Expand All @@ -48,7 +48,7 @@ services:
- traefik.enable=true
- traefik.frontend.rule=Host:portainer.m2c.test
- traefik.port=9000
restart: always
restart: ${M2C_GLOBAL_SERVICES_AUTO_START}

mailhog:
container_name: m2c.test__mailhog
Expand All @@ -58,7 +58,7 @@ services:
- traefik.enable=true
- traefik.frontend.rule=Host:mailhog.m2c.test
- traefik.port=8025
restart: always
restart: ${M2C_GLOBAL_SERVICES_AUTO_START}

dnsmasq:
container_name: m2c.test__dnsmasq
Expand All @@ -71,7 +71,7 @@ services:
- traefik.enable=true
- traefik.frontend.rule=Host:dnsmasq.m2c.test
- traefik.port=8080
restart: always
restart: ${M2C_GLOBAL_SERVICES_AUTO_START}

volumes:
portainer:
Expand Down
56 changes: 54 additions & 2 deletions .m2c/functions/m2c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ m2c_get_project_dir() {
[[ "$current_dir" != ~ ]] && [[ "$current_dir" != "/" ]] && echo "$current_dir" || echo "$(pwd)"
}

m2c_load_env() {
m2c_global_load_env
m2c_load_project_env
}

m2c_save_project_env() {
local config_env="$m2c_project_dir/.m2c/docker/.env"

Expand Down Expand Up @@ -360,6 +365,10 @@ m2c_global() {
m2c_in_array "--help" $@ && m2c_global_help

case "$1" in
autostart)
shift
m2c_global_autostart "$@"
;;
info|status)
m2c_global_info
;;
Expand Down Expand Up @@ -422,6 +431,20 @@ m2c_global_error() {
exit 1
}

m2c_global_load_env() {
local config_env="$m2c_global_dir/docker/.env"

[[ -f "$config_env" ]] && . "$config_env"
}

m2c_global_save_env() {
local config_env="$m2c_global_dir/docker/.env"

[[ -f "$config_env" ]] || mkdir -p "$(dirname "$config_env")" && touch "$config_env"

set | grep '^M2C_GLOBAL_' > "$config_env"
}

m2c_global_print_service() {
case "$1" in
portainer)
Expand All @@ -447,12 +470,14 @@ m2c_global_up() {

m2c_info_bold "\n[Creating docker container$( (("${#service[@]}" > 1)) && echo "s" )]\n\n"

cd "$m2c_global_dir/docker/"

for i in "${!service[@]}"
do
m2c_global_print_service ${service[$i]}
m2c_loading
docker-compose --project-name m2c \
--file "$m2c_global_dir/docker/docker-compose.yml" up \
--file docker-compose.yml up \
--detach --force-recreate --no-color --no-deps ${service[$i]} >"$m2c_log" 2>&1
m2c_result $?
done
Expand Down Expand Up @@ -555,6 +580,29 @@ m2c_global_projects() {
exit 0
}

m2c_global_autostart() {
case "$@" in
1|enable|enabled|on|true)
M2C_GLOBAL_SERVICES_AUTO_START="always"
m2c_global_save_env
m2c_global up
m2c_msg "\nToolset related global docker services will be started automatically with docker startup.\n"
;;
0|disable|disabled|off|false)
M2C_GLOBAL_SERVICES_AUTO_START="no"
m2c_global_save_env
m2c_global up
m2c_msg "\nToolset related global docker services won't be started automatically with docker startup.\n"
;;
status)
m2c_msg "Toolset related global docker services autostart is $([[ "$M2C_GLOBAL_SERVICES_AUTO_START" == "always" ]] && echo "ON" || echo "OFF").\n"
;;
*)
m2c_global_error "$@"
;;
esac
}

m2c_global_info() {
local project="m2c"
local spaces="$(printf "%-${#project}s" " ")"
Expand Down Expand Up @@ -685,9 +733,13 @@ m2c_global_help() {
Control Mage2click toolset installation and its docker services.

\033[1;33mUsage:\033[0m
m2c global <command> [service...] [--help]
m2c global <command> [--help]

m2c global <restart|start|stop|up> [service...]
m2c global autostart [on|off]

\033[1;33mCommands:\033[0m
autostart Configure autostart of toolset docker services.
projects Show existing Mage2click-backed projects.
restart Restart Mage2click toolset docker services.
start Start stopped Mage2click toolset docker services.
Expand Down
Loading