-
-
Notifications
You must be signed in to change notification settings - Fork 780
Expand file tree
/
Copy pathst2ctl
More file actions
executable file
·307 lines (275 loc) · 9.96 KB
/
st2ctl
File metadata and controls
executable file
·307 lines (275 loc) · 9.96 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
COMPONENTS="st2actionrunner st2api st2stream st2auth st2garbagecollector st2notifier st2rulesengine st2sensorcontainer st2chatops st2timersengine st2workflowengine st2scheduler"
ST2_CONF="${ST2_CONF:-/etc/st2/st2.conf}"
SYSTEMD_RELOADED=""
# Ensure global environment is sourced if exists and if not executed in the context of Github Actions
# Does not happen consistently with all OSes we support.
[ -z "${GITHUB_ACTIONS}" ] && [ -r /etc/environment ] && source /etc/environment
# load in environment to allow override of COMPONENTS and ST2_CONF above
# Ubuntu/Debian
[ -r /etc/default/st2ctl ] && source /etc/default/st2ctl
# RHEL/CentOS/Rocky
[ -r /etc/sysconfig/st2ctl ] && source /etc/sysconfig/st2ctl
function print_usage() {
echo "Usage: st2ctl {start, stop, restart, status}"
echo
echo "Usage: st2ctl {restart-component}"
echo "positional arguments:"
echo " component Name of the st2 service to restart."
echo " ${COMPONENTS}"
echo
echo "Usage: st2ctl {reopen-log-files}"
echo "positional arguments:"
echo " component Name of the st2 service to reopen the log files for."
echo " ${COMPONENTS}"
echo
echo "Usage: st2ctl {reload, clean}"
echo "optional arguments:"
echo " --register-all Register all."
echo " --register-triggers Register all triggers."
echo " --register-sensors Register all sensors."
echo " --register-rules Register all rules."
echo " --register-runners Register all runners."
echo " --register-actions Register all actions."
echo " --register-aliases Register all aliases."
echo " --register-policies Register all policies."
echo " --register-configs Register all configuration files."
echo " --register-setup-virtualenvs Create Python virtual environments for all the registered packs."
echo " --register-recreate-virtualenvs (Delete and re-)create Python virtual environments for all the registered packs."
echo " --register-fail-on-failure Exit with non-zero if some resource registration fails. Deprecated. This is now a default behavior."
echo " --register-no-fail-on-failure Don't exit with non-zero if some resource registration fails."
echo " --verbose Output additional debug and informational messages."
echo ""
echo "Most commands require elevated privileges."
}
function must_be_root() {
if [ $(id -u) -ne 0 ]; then
echo "Please run this command with root privileges"
exit 1
fi
}
# -- Detect whether running inside a Docker container --
function running_in_docker() {
[ -f /.dockerenv ] && return 0
return 1
}
# Tell the user to use Docker commands instead of st2ctl
function suggest_docker_usage() {
echo -e "\e[31mError: “st2ctl $1” is not supported inside Docker.\e[0m"
echo ""
echo "Please manage StackStorm services with Docker commands, e.g.:"
echo " docker ps"
echo " docker-compose restart st2api"
echo " docker-compose logs -f st2stream"
exit 1
}
function not_running_in_k8s() {
if [ -n "$KUBERNETES_SERVICE_HOST" ]; then
echo -e "\e[31mError: \"st2ctl status\" is not supported under Kubernetes, please use Kubernetes tools such as \"kubectl\" to view the StackStorm services in this cluster. \e[0m\n"
exit 1
fi
}
function validate_in_components() {
COM=${1}
if [ -z ${COM} ]; then
echo -e "\e[31mError: Component is required! \e[0m\n"
print_usage
exit 1
fi
if [[ " ${COMPONENTS} " != *" ${COM} "* ]]; then
echo -e "\e[31mError: Invalid component provided: ${COM} \e[0m\n"
print_usage
exit 1
fi
}
function check_python_version() {
PYTHON_VERSION=$(/opt/stackstorm/st2/bin/python --version 2>&1 | awk -F"[. ]" '{print $2}')
if [ "${PYTHON_VERSION}" = "2" ]; then
echo -e "\e[33mDeprecation warning: Support for python 2 will be removed in future StackStorm releases. Please ensure that all packs used are python 3 compatible. Your StackStorm installation may be upgraded from python 2 to python 3 in future platform releases. It is recommended to plan the manual migration to a python 3 native platform, e.g. Ubuntu 18.04 LTS or CentOS/RHEL 8. \e[0m\n"
fi
}
function st2start() {
for COM in ${COMPONENTS}; do
service_manager ${COM} start
done
check_python_version
}
function st2stop() {
for COM in ${COMPONENTS}; do
service_manager ${COM} stop
done
}
function service_manager() {
local svcname=$1 action=$2
if [ -d /run/systemd/system ]; then
# systemd is running
if [ -z $SYSTEMD_RELOADED ]; then
#Reload systemd to regenerate socket files from st2.conf
systemctl daemon-reload
SYSTEMD_RELOADED="yes"
fi
systemctl $action $svcname
elif [ $(cat /proc/1/comm) = init ] && (/sbin/initctl version 2>/dev/null | grep -q upstart) &&
[ -f /etc/init/${svcname}.conf ]; then
# init is running, and is upstart and service config is available
# ! upstart running detection is a MUST, since upstart can be just
# ! installed on a platform but not used (ex: docker container, centos).
/sbin/initctl $action $svcname
elif command -v service > /dev/null 2>&1; then
service $svcname $action
else
echo -e "\e[31mError: Unknown service manager, we ONLY support systemd and upstart! \e[0m\n"
exit 1
fi
}
# Next candidate for removal
# after we implement native `service st2component reopen` (nginx example) for each component
function reopen_component_log_files() {
COM=${1}
PID=`ps axww | grep -v grep | grep -v st2ctl | grep -E "(${COM}\.wsgi)|(bin/${COM})|(hubot .*${COM})" | awk '{print $1}'`
if [[ ! -z ${PID} ]]; then
for p in ${PID}; do
echo "Sending SIGUSR1 to ${COM} PID: ${p}"
kill -USR1 ${p}
done
else
echo "${COM} is not running"
return 1
fi
}
function register_content() {
ALLOWED_REGISTER_FLAGS='--register-all --register-actions --register-aliases --register-runners --register-policies --register-rules --register-sensors --register-triggers --register-configs --register-setup-virtualenvs --register-recreate-virtualenvs --register-fail-on-failure --register-no-fail-on-failure --verbose'
DEFAULT_REGISTER_FLAGS='--register-runners --register-actions --register-aliases --register-sensors --register-triggers --register-configs --register-rules'
SUDO_FLAGS='--register-setup-virtualenvs --register-recreate-virtualenvs'
flags="${@}"
if [ ! -z ${1} ]; then
for flag in ${flags}; do
if [[ " ${SUDO_FLAGS} " == *" $flag "* && "$(id -u)" != "0" ]]; then
echo -e "\e[31mError: \"sudo\" required for: ${flag} \e[0m\n"
exit 1
fi
if [[ " ${ALLOWED_REGISTER_FLAGS} " != *" $flag "* ]]; then # argument not allowed
echo -e "\e[31mError: Invalid flag provided: ${flag} \e[0m\n"
print_usage
exit 1
fi
done
fi
if [ -z ${1} ]; then
REGISTER_FLAGS=${DEFAULT_REGISTER_FLAGS}
elif [ ${1} == '--verbose' ] && [ -z ${2} ]; then
REGISTER_FLAGS="$DEFAULT_REGISTER_FLAGS ${1}"
else
REGISTER_FLAGS=${flags}
fi
echo "Registering content...[flags = --config-file ${ST2_CONF} ${REGISTER_FLAGS}]"
st2-register-content --config-file ${ST2_CONF} ${REGISTER_FLAGS}
}
function clean_db() {
echo "Dropping st2 Database..."
/opt/stackstorm/st2/bin/st2-cleanup-db --config-file ${ST2_CONF}
}
function clean_logs() {
echo "Cleaning st2 Logs..."
rm -Rf /var/log/st2/*
}
function getpids() {
echo "##### st2 components status #####"
COMPONENTS=${COMPONENTS}
for COM in ${COMPONENTS}; do
PID=`ps axww | grep -v grep | grep -v st2ctl | grep -E "(${COM}\.wsgi)|(bin/${COM})|(hubot .*${COM})" | awk '{print $1}'`
if [[ ! -z ${PID} ]]; then
for p in ${PID}; do
echo "${COM} PID: ${p}"
done
else
echo "${COM} is not running."
fi
done
}
# If in Docker, refuse any of the VM‐style commands
if running_in_docker; then
case "$1" in
status)
suggest_docker_usage "$1"
;;
esac
fi
case ${1} in
start)
must_be_root
st2start
getpids
;;
stop)
must_be_root
st2stop
;;
restart)
must_be_root
st2stop
sleep 1
st2start
getpids
;;
restart-component)
must_be_root
validate_in_components ${2}
service_manager ${2} restart
check_python_version
;;
reopen-log-files)
must_be_root
validate_in_components ${2}
if reopen_component_log_files ${2}; then
sleep 1
getpids
fi
;;
reload)
register_content ${@:2}
exit_code=$?
check_python_version
getpids
# Note: We want to preserve st2-register-content "fail on failure" behavior
# and propagate the correct exit code and exit with non zero on failure
exit ${exit_code}
;;
clean)
must_be_root
echo "This will drop the database and delete all logs. Are you sure [y/n]?"
read verify
if [[ "$verify" == "y" ]]; then
st2stop
clean_db
clean_logs
register_content ${@:2}
st2start
getpids
else
exit
fi
;;
status)
not_running_in_k8s
getpids
;;
*)
print_usage
exit 1
;;
esac