-
Notifications
You must be signed in to change notification settings - Fork 5
/
jitsi_create_user.sh
245 lines (224 loc) · 6.38 KB
/
jitsi_create_user.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
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
#!/bin/bash -
#===============================================================================
#
# FILE: jitsi_create_user.sh
#
# USAGE: ./jitsi_create_user.sh
#
# DESCRIPTION: bash-wrapper for prosody user creation and user backup
#
# OPTIONS: --USER=jack --PW=1234 --DOMAIN=my.domain.com --BAK=y
# AUTHOR: Andre Stemmann
# ORGANIZATION: AirITSystems GmbH
# CREATED: 30.04.2020 10:41
# REVISION: v0.2
#===============================================================================
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
set -o nounset # Treat unset variables as an error
# ===============================================================================
# BASE VARIABLES
# ===============================================================================
start=$(date +%s)
TODAY=$(date +%Y%m%d%H)
PROGGI=$(basename "$0")
APP="Prosody"
LOGPATH="/var/log"
LOGFILE="${LOGPATH}/syslog"
ERRORLOG="${LOGPATH}/syslog"
APPFOLDER="/var/lib/prosody"
BACKDST="/var/backups/"
BACKUPROOT="${BACKDST}/${APP}_backup_${TODAY}"
# ===============================================================================
# BASE FUNCTIONS
# ===============================================================================
function log () {
echo "$PROGGI ; $(date '+%Y%m%d %H:%M:%S') ; $*" | tee -a "${LOGFILE}"
}
function errorlog () {
echo "${PROGGI}_ERRORLOG ; $(date '+%Y%m%d %H:%M:%S') ; $*" | tee -a "${ERRORLOG}"
}
function usercheck () {
if [[ $UID -ne 0 ]]; then
errorlog "...Become user root and try again"
exit 1
fi
}
function folder () {
if [ ! -d "$1" ]; then
mkdir -p "$1"
log "...Create Backupfolder structure for $APP $1"
else
log "...Folder $1 already exists"
fi
}
function distrocheck () {
if [[ -r /etc/os-release ]]; then
. /etc/os-release
if [[ $ID = ubuntu ]]; then
log "...Running ${ID}_${VERSION_ID} "
folder "${BACKUPROOT}"
else
errorlog "...Not running an debian based distribution. ID=$ID, VERSION=$VERSION"
folder "${BACKUPROOT}"
fi
else
errorlog "...$APP is not running a distribution with /etc/os-release available"
errorlog "...Please perform backup manually"
exit 1
fi
}
function usage () {
echo "jitsi create user"
echo "-----------------"
echo "mandatory parameter:"
echo "----------------------------------------"
echo "--USER=<string> : Jitsi Username"
echo "--PW=<string> : Jitsi User Password"
echo "--DOMAIN=<string> : Jitsi Domain to use"
echo "--BAK=boolean : Backup Config y/n"
echo "----------------------------------------"
echo "-h or --help for more information"
}
function printHelp () {
echo "These are the parameters needed to call the script"
echo "------------------------------------------------------------------------------------"
echo "--USER=<string> : The free chosen username you want to provide"
echo " e.g. pangalacticthunderuser01"
echo ""
echo "--PW=<string> : User secret pass"
echo " use a simple passphrase, its more secure than your avg. password"
echo ""
echo "--DOMAIN=<string> : The plain stripped Jitsi domain"
echo " e.g. my.chattool.com"
echo ""
echo "--BAK=<boolean> : Whether to backup existing prosody accounts or not"
echo " /var/lib/prosody/ will be rsynced to $BACKDST"
}
function parseparams () {
# catch an empty call
if [ $# -eq 0 ] ; then
echo
echo "no parameters are given!"
echo
usage
exit 1
fi
# print help text
if [ "$1" == "-h" ]; then
printHelp
exit 0
fi
if [ "$1" == "--help" ]; then
printHelp
exit 0
fi
# parse parameters
until [[ ! "$*" ]]; do
if [[ ${1:0:2} = '--' ]]; then
PAIR="${1:2}"
PARAMETER=$(echo "${PAIR%=*}" | tr '[:lower:]' '[:upper:]')
eval P_"$PARAMETER"="${PAIR##*=}"
fi
shift
done
# parameter re-check
if [ -z "$P_USER" ] ; then
errorlog "ERROR: please specify the USER - parameter"
errorlog "exiting script..."
exit 1
elif [ -z "$P_PW" ] ; then
errorlog "ERROR: please specify the PW - parameter"
errorlog "exiting script..."
exit 1
elif [ -z "$P_DOMAIN" ] ; then
errorlog "ERROR: please specify the DOMAIN - parameter"
errorlog "exiting script..."
exit 1
elif [ -z "$P_BAK" ] ; then
errorlog "ERROR: please specify the BAK - parameter"
errorlog "exiting script..."
exit 1
fi
}
function service_restart () {
if systemctl is-active --quiet "$1"; then
log "...stopping $1"
systemctl stop "$1"
sleep 3
else
log "...service $1 already stopped"
if ! systemctl is-active --quiet "$1"; then
log "...service $1 stopped"
else
log "...service $1 still running, trying to SIGTERM it now"
kill -15 "$1"
sleep 3
if ! systemctl is-active --quiet "$1"; then
log "...service $1 stopped"
else
log "...service $1 still running, trying to SIGKILL it now"
kill -9 "$1"
sleep 3
fi
fi
fi
systemctl start "$1"
sleep 3
if systemctl is-active --quiet "$1"; then
log "...$1 is safe and sound"
else
errorlog "...Failed to start $1"
errorlog "...Trying it again"
systemctl start "$1"
sleep 3
if systemctl is-active --quiet "$1"; then
log "...$1 is safe and sound"
else
errorlog "...Failed to start $1"
errorlog "...CRITICAL - check manually"
fi
fi
}
function restartq () {
echo "In order to register the new user correctly, the services"
echo "jicofo, jitsi-videobridge and prosody must be restartet"
while true; do
read -rp "Do you wish to restart all related services now? [y/n]" yn
case $yn in
[Yy]* )
service_restart jicofo;
service_restart jitsi-videobridge2;
service_restart prosody;
break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
}
function createuser () {
# create prosody user
if ! prosodyctl register "${P_USER}" "${P_DOMAIN}" "${P_PW}"; then
errorlog "please re-check user account creation manually, something went wrong"
exit 1
fi
}
function backup () {
rsync -avz --relative --log-file="${LOGFILE}" "${APPFOLDER}" "${BACKUPROOT}"
}
# ===============================================================================
# MAIN RUN
# ===============================================================================
usercheck
distrocheck
parseparams "$@"
case "$P_BAK" in
[yY]*)
backup
;;
esac
createuser
restartq
end=$(date +%s)
runtime=$((end-start))
log "...Runtime $runtime Seconds"
exit 0