-
Notifications
You must be signed in to change notification settings - Fork 60
/
htpasswd_migration.sh
89 lines (75 loc) · 2.14 KB
/
htpasswd_migration.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
#!/bin/bash
# This script will create htpasswd for each controlled services for migration Apache->Nginx
read -r -p "Please enter .env file location : " envi
if [[ ! -f $envi ]]
then
echo "Please enter correct file location"
exit
fi
# $1 is the file to read. Result is one setting per line, name followed by single space
# followed by value. We can't source the .env file because it's really a .ini file and
# doesn't follow shell syntax.
function _parseenv {
sed -n -e 's/#.*$//g' -e 's/^[ \t]*//' -e 's/[ \t]*=[ \t]*/=/' -e 's/^\([A-Za-z0-9_][A-Za-z0-9_]*\)=\(.*\)$/\1 \2/p' "$1"
}
TTN_DASHBOARD_DATA="$(_parseenv "$envi" | sed -ne 's/^TTN_DASHBOARD_DATA //p')"
htgroup="${TTN_DASHBOARD_DATA}docker-apache2/authdata/.htgroup"
htpasswd="${TTN_DASHBOARD_DATA}docker-apache2/authdata/.htpasswd"
PS3="Please enter your choice on the number listed above, To exit press 'ctrl+d ' : "
select var in "creating htpasswd for each controlled service manually" "creating htpasswd for each controlled service automatically"
do
case $var in
"creating htpasswd for each controlled service manually")
PS3="Please select service: "
select output in $(sed 's/:.*$//' "$htgroup")
do
true > "${output}_htpasswd"
for i in $(tr < "$htgroup" ' |,' '\n' | sed 's/.*:$//' | sort -u)
do
read -r -p "Do you want the User: $i to be added in .htpasswd (y/n) : " j
case $j in
[yY][eE][sS]|[yY])
sed -n "/$i/p" "$htpasswd" >> "${output}_htpasswd"
;;
[nN][oO]|[nN])
continue
;;
*)
echo "Please Enter yes or no"
break
;;
esac
done
done
echo ""
echo ""
echo ""
echo "It is done. Thanks!"
echo ""
echo ""
exit
;;
"creating htpasswd for each controlled service automatically")
while read -r line
do
file=$(echo "$line" | awk '{print $1}' | sed 's/://')
echo "create:" "${file}_htpasswd"
true > "${file}_htpasswd"
for k in $(echo "$line" | tr ' |,' '\n')
do
sed -n "/$k/p" "$htpasswd" >> "${file}_htpasswd"
done
done < "$htgroup"
echo ""
echo ""
echo ""
echo "It is done. Thanks!"
echo ""
echo ""
exit
;;
*)
echo "Please enter correct number"
;;
esac
done