-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcommands
executable file
·231 lines (201 loc) · 6.74 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/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 logspout:help 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"
f_gethostname() {
local desc="return hostname based on opsworks stack/layer/hostname or just system hostname"
local OPSWORKS_STACK_NAME HOSTNAME
OPSWORKS_STACK_NAME=$(grep "OpsWorks Stack:" /etc/motd 2>/dev/null || true)
if [[ -n "$OPSWORKS_STACK_NAME" ]]; then
HOSTNAME=$(echo "$OPSWORKS_STACK_NAME" "$(hostname)" | awk -F: '{ print $2 }' | xargs | tr '[:upper:]' '[:lower:]' | sed -e 's: :-:g')
else
HOSTNAME=$(hostname)
fi
echo "$HOSTNAME"
}
do_run() {
local CID HOSTNAME DOKKU_LOGSPOUT_SYSLOG_SERVER=${DOKKU_LOGSPOUT_SYSLOG_SERVER:-"syslog://"}
HOSTNAME="$(f_gethostname)"
# shellcheck disable=SC2086
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 \
--hostname=${HOSTNAME} \
--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 CID
local desc="shows info about logspout config"
local cmd="logspout:info"
if [[ -f "$DOKKU_LOGSPOUT_ROOT/CONTAINER" ]]; then
CID=$(< "$DOKKU_LOGSPOUT_ROOT/CONTAINER")
dokku_log_info2_quiet "logspout status"
if (is_container_status "$CID" "Running"); 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 -r line; do
[[ $line = *"="* ]] && dokku_log_verbose "$line" | sed -e "s:export ::g"
done < "$DOKKU_LOGSPOUT_ROOT/OPTS"
fi
if [[ -f "$DOKKU_LOGSPOUT_ROOT/ENV" ]] && [[ -s "$DOKKU_LOGSPOUT_ROOT/ENV" ]]; then
dokku_log_info2_quiet "dokku-logspout env"
while read -r line; do
[[ $line = *"="* ]] && dokku_log_verbose "$line" | sed -e "s:export ::g"
done < "$DOKKU_LOGSPOUT_ROOT/ENV"
fi
}
logspout_destroy_cmd() {
local CID
local desc="destroys logspout container"
local cmd="logspout:destroy"
if [[ -f "$DOKKU_LOGSPOUT_ROOT/CONTAINER" ]]; then
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 CID
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
CID=$(< "$DOKKU_LOGSPOUT_ROOT/CONTAINER")
if (! is_container_status "$CID" "Running"); then
docker rm -f logspout &>/dev/null || true
do_run
fi
fi
}
logspout_stop_cmd() {
local CID
local desc="stops logspout container"
local cmd="logspout:stop"
if [[ -f "$DOKKU_LOGSPOUT_ROOT/CONTAINER" ]]; then
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 | logspout:help)
help_content_func () {
declare desc="return logspout plugin help content"
cat<<help_content
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
help_content
}
if [[ -n $DOKKU_API_VERSION ]]; then
if [[ $1 = "logspout:help" ]] ; then
echo -e 'Usage: dokku logspout[:COMMAND]'
echo ''
echo 'Manage logspout integration.'
echo ''
echo 'Additional commands:'
help_content_func | sort | column -c2 -t -s,
else
help_content_func
fi
else
cat && help_content_func
fi
;;
*)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;;
esac