-
Notifications
You must be signed in to change notification settings - Fork 6
/
server-manager.sh
executable file
·139 lines (110 loc) · 2.27 KB
/
server-manager.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
#!/usr/bin/env bash
htdocs=$PWD
nginxConfDir="/usr/local/etc/nginx/sites-enabled"
hostsFile="/etc/hosts"
function getColor
{
case $1 in
"red" )
echo "1";;
"green" )
echo "2";;
esac
}
function createDirectory
{
if [[ ! -e $1 ]]; then
mkdir -p $1
writeMessage "Directory '$1' was created"
else
writeMessage "Directory '$1' exists"
fi
}
function remove
{
if [[ $1 != "" || $1 != "." ]]; then
if [[ -e $1 ]]; then
rm -r $1
fi
writeMessage 'Item '$1' was removed'
else
writeMessage 'Access denied'
fi
}
function writeMessage
{
if [[ $2 ]]; then
color=$(getColor "$2")
else
color=$(getColor "green")
fi
echo "$(tput setaf $color) $1 $(tput sgr0)"
}
function setChmod
{
chmod -R $1 $2
}
function getConfig
{
echo -ne "server { \\r
listen 80; \\r
server_name $1.lc; \\r
root $htdocs/$1/www/; \\r
\\r
error_log $htdocs/$1/log/$1_errors.log; \\r
access_log $htdocs/$1/log/$1_access.log; \\r
\\r
include common/common.conf; \\r
include common/php.conf; \\r
include common/nette.conf; \\r
}"
}
function isRunning
{
if ps ax | grep -v grep | grep $1 > /dev/null
then
echo "1"
else
echo "0"
fi
}
function cloneRepo
{
git clone $1 $2
}
#stop nginx server, if nginx is running
if [ $(isRunning "nginx") = "1" ]; then
writeMessage "Stop nginx server"
nginx -s stop
fi
#set name of Host name
echo -n 'Write name of host:'
read hostName
if [ -z "$hostName" ]; then
writeMessage "Host name may not be empty!" "red"
else
if [[ $1 = "-r" ]]; then
writeMessage "Removing server files"
remove "$htdocs/$hostName"
writeMessage "Removing server host"
remove "$nginxConfDir/$hostName.conf"
writeMessage "Removing host from $hostsFile"
string="127.0.0.1 $hostName.lc"
sed "/$string/d" $hostsFile > 'hosts.temp'
mv "hosts.temp" $hostsFile
else
if [[ $1 = '-c' && $2 != '' ]]; then
writeMessage "Cloning github repository"
cloneRepo $2 "$htdocs/$hostName"
fi
createDirectory "$htdocs/$hostName/log"
createDirectory "$htdocs/$hostName/www"
setChmod "777" "$htdocs/$hostName"
writeMessage "Write config to file"
echo $(getConfig $hostName) > "$nginxConfDir/$hostName.conf"
writeMessage "Adding host into $hostsFile"
echo "127.0.0.1 $hostName.lc" >> $hostsFile
fi
fi
writeMessage "Start nginx server"
nginx