forked from EricJMarti/inventory-hunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_run.bash
executable file
·121 lines (106 loc) · 3.42 KB
/
docker_run.bash
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
set -e
usage() {
[ -n "$1" ] && echo "error: $1"
echo
echo "usage for discord, slack, or telegram:"
echo " $0 -a DISCORD|SLACK|TELEGRAM -w WEBHOOK_URL [-d TELEGRAM_CHAT_ID] -c CONFIG"
echo
echo "usage for email:"
echo " $0 -c CONFIG -e EMAIL -r RELAY"
echo
exit 1
}
[ $# -eq 0 ] && usage
alerter="email"
default_image="ericjmarti/inventory-hunter:latest"
image=$default_image
while getopts a:c:d:e:i:q:r:w:t arg
do
case "${arg}" in
a) alerter=${OPTARG};;
c) config=${OPTARG};;
d) chat_id=${OPTARG};;
e) emails+=(${OPTARG});;
i) image=${OPTARG};;
q) alerter_config=${OPTARG};;
r) relay=${OPTARG};;
w) webhook=${OPTARG};;
t) test_alert=1;;
esac
done
[ -z "$config" ] && usage "missing config argument"
[ ! -f "$config" ] && usage "$config does not exist or is not a regular file"
if [ ! -z "$alerter_config" ]; then
[ ! -f "$alerter_config" ] && usage "$alerter_config does not exist or is not a regular file"
elif [ "$alerter" = "email" ]; then
[ -z "$emails" ] && usage "missing email argument"
[ -z "$relay" ] && usage "missing relay argument"
else
[ -z "$webhook" ] && usage "missing webhook argument"
if [ "$alerter" = "telegram" ]; then
[ -z "$chat_id" ] && usage "missing telegram chat id argument"
fi
fi
if [ "$image" = "$default_image" ]; then
docker pull "$image"
else
result=$(docker images -q $image)
if [ -z "$result" ]; then
echo "the $image docker image does not exist... please build the image and try again"
echo "build command: docker build -t $image ."
exit 1
fi
fi
# docker requires absolute paths
if [ "$(uname)" = "Darwin" ]; then
if [[ ! "$config" == /* ]]; then
config="$(pwd -P)/${config#./}"
fi
if [ ! -z "$alerter_config" ] && [[ ! "$alerter_config" == /* ]]; then
alerter_config="$(pwd -P)/${alerter_config#./}"
fi
else
config=$(readlink -f $config)
if [ ! -z "$alerter_config" ]; then
alerter_config=$(readlink -f $alerter_config)
fi
fi
script_dir=$(cd "$(dirname "$0")" && pwd -P)
log_dir="$script_dir/log"
mkdir -p $log_dir
container_name=$(basename $config .yaml)
data_dir="$script_dir/data/$container_name"
log_file="$log_dir/$container_name.txt"
mkdir -p $data_dir
touch $log_file
volumes="-v $data_dir:/data -v $log_file:/log.txt -v $config:/config.yaml"
[ ! -z "$alerter_config" ] && volumes="$volumes -v $alerter_config:/alerters.yaml"
docker_run_cmd="docker run -d --rm --name $container_name --network host $volumes $image --alerter $alerter"
if [ ! -z "$alerter_config" ]; then
docker_run_cmd="$docker_run_cmd --alerter-config /alerters.yaml"
elif [ "$alerter" = "email" ]; then
docker_run_cmd="$docker_run_cmd --email ${emails[@]} --relay $relay"
else
docker_run_cmd="$docker_run_cmd --webhook $webhook"
if [ "$alerter" = "telegram" ]; then
docker_run_cmd="$docker_run_cmd --chat-id $chat_id"
fi
fi
if [ $test_alert ]; then
docker_run_cmd="$docker_run_cmd -t"
fi
docker_ps_cmd="docker ps -a -f name=$container_name"
echo "\$ $docker_run_cmd"
eval $docker_run_cmd
echo
echo "started docker container named $container_name"
echo
echo "view the status of this container using the following command:"
echo "\$ $docker_ps_cmd"
echo
eval $docker_ps_cmd
echo
echo "view logs for this container using the following command:"
echo "\$ docker logs -f $container_name"
echo