forked from michaelshobbs/dokku-logspout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands
executable file
·193 lines (167 loc) · 5.47 KB
/
commands
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
[[ " logspout:info logspout:destroy logspout:port logspout:server logspout:start logspout:stop logspout:stream logspout:update help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
PLUGIN_BASE_PATH="$PLUGIN_PATH"
if [[ -n $DOKKU_API_VERSION ]]; then
PLUGIN_BASE_PATH="$PLUGIN_ENABLED_PATH"
fi
source "$PLUGIN_BASE_PATH/common/functions"
DOKKU_LOGSPOUT_ROOT="$DOKKU_ROOT/.logspout"
[[ -f "$DOKKU_LOGSPOUT_ROOT/OPTS" ]] && source "$DOKKU_LOGSPOUT_ROOT/OPTS"
do_run() {
local DOKKU_LOGSPOUT_SYSLOG_SERVER=${DOKKU_LOGSPOUT_SYSLOG_SERVER:-"syslog://"}
# shellcheck disable=SC2086
local CID=$(docker run -d --name="logspout" \
--volume=/var/run/docker.sock:/tmp/docker.sock \
--publish=127.0.0.1:${DOKKU_LOGSPOUT_PORT}:80 \
--restart=unless-stopped \
--env-file=${DOKKU_LOGSPOUT_ROOT}/ENV \
gliderlabs/logspout:${DOKKU_LOGSPOUT_IMAGE_VERSION} \
$DOKKU_LOGSPOUT_SYSLOG_SERVER)
echo "$CID" > "$DOKKU_LOGSPOUT_ROOT/CONTAINER"
}
logspout_info_cmd() {
local desc="shows info about logspout config"
local cmd="logspout:info"
if [[ -f "$DOKKU_LOGSPOUT_ROOT/CONTAINER" ]]; then
local CID=$(< "$DOKKU_LOGSPOUT_ROOT/CONTAINER")
dokku_log_info2_quiet "logspout status"
if (is_container_running "$CID"); then
dokku_log_verbose "running"
else
dokku_log_verbose "not running"
fi
else
dokku_log_warn "logspout not deployed"
fi
if [[ -f "$DOKKU_LOGSPOUT_ROOT/OPTS" ]]; then
dokku_log_info2_quiet "dokku-logspout config"
while read line; do
[[ $line = *"="* ]] && dokku_log_verbose "$line" | sed -e "s:export ::g"
done < "$DOKKU_LOGSPOUT_ROOT/OPTS"
fi
}
logspout_destroy_cmd() {
local desc="destroys logspout container"
local cmd="logspout:destroy"
if [[ -f "$DOKKU_LOGSPOUT_ROOT/CONTAINER" ]]; then
local CID=$(< "$DOKKU_LOGSPOUT_ROOT/CONTAINER")
docker stop "$CID" || true
docker rm -f "$CID" || true
rm -f "$DOKKU_LOGSPOUT_ROOT/CONTAINER"
else
dokku_log_warn "logspout not deployed"
fi
}
logspout_port_cmd() {
local desc="sets local logspout port for local output streaming"
local cmd="logspout:port"
[[ -z $2 ]] && echo "Please specify a port" && exit 1
local DOKKU_LOGSPOUT_PORT="$2"
if (grep -q DOKKU_LOGSPOUT_PORT "$DOKKU_LOGSPOUT_ROOT/OPTS"); then
grep -v DOKKU_LOGSPOUT_PORT "$DOKKU_LOGSPOUT_ROOT/OPTS" > "$DOKKU_LOGSPOUT_ROOT/.opts.tmp"
mv "$DOKKU_LOGSPOUT_ROOT/.opts.tmp" "$DOKKU_LOGSPOUT_ROOT/OPTS"
echo "export DOKKU_LOGSPOUT_PORT=$DOKKU_LOGSPOUT_PORT" >> "$DOKKU_LOGSPOUT_ROOT/OPTS"
else
echo "export DOKKU_LOGSPOUT_PORT=$DOKKU_LOGSPOUT_PORT" >> "$DOKKU_LOGSPOUT_ROOT/OPTS"
fi
}
logspout_server_cmd() {
local desc="sets external syslogs server url"
local cmd="logspout:server"
[[ -z $2 ]] && echo "Please specify a syslog uri" && exit 1
local DOKKU_LOGSPOUT_SYSLOG_SERVER="$2"
if (grep -q DOKKU_LOGSPOUT_SYSLOG_SERVER "$DOKKU_LOGSPOUT_ROOT/OPTS"); then
grep -v DOKKU_LOGSPOUT_SYSLOG_SERVER "$DOKKU_LOGSPOUT_ROOT/OPTS" > "$DOKKU_LOGSPOUT_ROOT/.opts.tmp"
mv "$DOKKU_LOGSPOUT_ROOT/.opts.tmp" "$DOKKU_LOGSPOUT_ROOT/OPTS"
echo "export DOKKU_LOGSPOUT_SYSLOG_SERVER=$DOKKU_LOGSPOUT_SYSLOG_SERVER" >> "$DOKKU_LOGSPOUT_ROOT/OPTS"
else
echo "export DOKKU_LOGSPOUT_SYSLOG_SERVER=$DOKKU_LOGSPOUT_SYSLOG_SERVER" >> "$DOKKU_LOGSPOUT_ROOT/OPTS"
fi
}
logspout_start_cmd() {
local desc="starts logspout container"
local cmd="logspout:start"
if [[ ! -f "$DOKKU_LOGSPOUT_ROOT/CONTAINER" ]]; then
if (docker inspect logspout &> /dev/null); then
docker rm -f logspout &>/dev/null || true
fi
do_run
else
local CID=$(< "$DOKKU_LOGSPOUT_ROOT/CONTAINER")
if (! is_container_running "$CID"); then
docker rm -f logspout &>/dev/null || true
do_run
fi
fi
}
logspout_stop_cmd() {
local desc="stops logspout container"
local cmd="logspout:stop"
if [[ -f "$DOKKU_LOGSPOUT_ROOT/CONTAINER" ]]; then
local CID=$(< "$DOKKU_LOGSPOUT_ROOT/CONTAINER")
docker stop "$CID" || true
else
dokku_log_warn "logspout not deployed"
fi
}
logspout_stream_cmd() {
local desc="streams logspout log stream to stdout"
local cmd="logspout:stream"
curl "http://127.0.0.1:${DOKKU_LOGSPOUT_PORT}/logs"
}
logspout_update_cmd() {
local desc="updates logspout docker images"
local cmd="logspout:update"
logspout_stop_cmd
logspout_destroy_cmd
docker pull "gliderlabs/logspout:${DOKKU_LOGSPOUT_IMAGE_VERSION}"
logspout_start_cmd
}
case "$1" in
logspout:info)
logspout_info_cmd "$@"
;;
logspout:destroy)
logspout_destroy_cmd "$@"
;;
logspout:port)
logspout_port_cmd "$@"
;;
logspout:server)
logspout_server_cmd "$@"
;;
logspout:start)
logspout_start_cmd "$@"
;;
logspout:stop)
logspout_stop_cmd "$@"
;;
logspout:stream)
logspout_stream_cmd "$@"
;;
logspout:update)
logspout_update_cmd "$@"
;;
help)
HELP=$(cat<<EOF
logspout:info, show status of running container
logspout:destroy, destroy logspout container
logspout:port <port>, set local logspout port
logspout:server <server-url>, set remote syslog server
logspout:start, start logspout container
logspout:stop, stop logspout container
logspout:stream, print log stream to stdout
logspout:update, updates the logspout docker image
EOF
)
if [[ -n $DOKKU_API_VERSION ]]; then
echo "$HELP"
else
cat && echo "$HELP"
fi
;;
*)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;;
esac