forked from jaybeaton/traefik-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traefik-helper.sh
executable file
·74 lines (59 loc) · 1.9 KB
/
traefik-helper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/sh
if ! [ -x "$(command -v realpath)" ]; then
# If realpath isn't present, use this instead.
# Note: This will not work correctly for symlinks.
CURRENTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
else
CURRENTDIR="$( dirname $( realpath "${BASH_SOURCE[0]}" ) )";
fi
YML_TEMPLATE="$CURRENTDIR/traefik-helper.yml";
YML_TEMPLATE_DEFAULT="$CURRENTDIR/traefik-helper.default.yml";
if [ ! -e "$YML_TEMPLATE" ]; then
# Custom yml file not present, use default.
YML_TEMPLATE="$YML_TEMPLATE_DEFAULT";
fi
DOCKER_PROJECT_NAME="traefikhelper";
YML_FILE_LOCATION="/tmp/traefik-helper.yml";
TRAEFIC_COMMAND="docker-compose -p $DOCKER_PROJECT_NAME -f $YML_FILE_LOCATION";
if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "help" ]; then
echo "Manage Traefik Docker container with docker-compose.
Usage:
$0 [ARGS...]
[ARGS...] will be passed directly to docker-compose.
Examples:
$0 up -d # Bring up Traefik Docker container.
$0 start # Start Traefik Docker container.
$0 stop # Stop Traefik Docker container.
$0 down # Bring down Traefik Docker container.";
exit 0;
fi
# Get all networks.
NETWORKS=`docker network ls | grep '_default' | grep -v '${DOCKER_PROJECT_NAME}_default' | awk "{print \\$2}"`;
if [ "$NETWORKS" = "" ]; then
echo "**Error: No networks found. Start Docker containers first.";
exit 0;
fi
# Build token replacements for the yml file.
NETWORK_LIST='';
NETWORK_SECTION='';
for n in $NETWORKS
do
NETWORK_LIST="$NETWORK_LIST - $n
";
NETWORK_SECTION="$NETWORK_SECTION $n:
external:
name: $n
";
done
echo "Networks found:";
echo "---------------";
echo "$NETWORKS";
echo "";
# Read the yml and replace tokens.
YML=$(<"$YML_TEMPLATE");
YML="${YML//~NETWORK_LIST~/${NETWORK_LIST}}";
YML="${YML//~NETWORK_SECTION~/${NETWORK_SECTION}}";
# Write the yml file out.
echo "$YML" > "$YML_FILE_LOCATION";
# Run the docker-compose command with passed arguments.
$TRAEFIC_COMMAND $@;