-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathadd-vhost-ngphfpmypg.py
executable file
·215 lines (183 loc) · 9.02 KB
/
add-vhost-ngphfpmypg.py
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
#!/usr/bin/env python2.7
import sys
import os
import jinja2
from fabric.api import *
from fabric.tasks import execute
import getpass
templateLoader = jinja2.FileSystemLoader( searchpath="/" )
templateEnv = jinja2.Environment( loader=templateLoader )
TEMPNVFILE = os.getcwd()+'/jinja2temps/c7ngvhost.conf'
TEMPPFILE = os.getcwd()+'/jinja2temps/c7index.php'
TEMPHFILE = os.getcwd()+'/jinja2temps/c7index.html'
TEMPPGPFILE = os.getcwd()+'/jinja2temps/cfpginsert.php'
tempht = templateEnv.get_template( TEMPHFILE )
tempphp = templateEnv.get_template( TEMPPFILE )
tempnv = templateEnv.get_template( TEMPNVFILE )
temppgp = templateEnv.get_template( TEMPPGPFILE )
env.host_string = raw_input('Please enter WEB server IP address: ')
env.user = raw_input('Please enter username for UNIX/Linux server: ')
env.password = getpass.getpass()
sitename = raw_input('Please enter site name: ')
tempnvVars = { "sname" : sitename, "domain" : sitename, }
outputngvhText = tempnv.render( tempnvVars )
outputnghText = tempht.render( tempnvVars)
def vhhtmlwriter():
with open(os.getcwd()+'/output/'+sitename+'.conf', 'wb') as ngvhfile:
ngvhfile.write(outputngvhText)
with open(os.getcwd()+'/output/index.html', 'wb') as indhtml:
indhtml.write(outputnghText)
def sqlservicecheck(pidfile, pid):
if pidfile == pid:
print('SQL service already running...')
pass
else:
print('SQL service is not running...')
sys.exit()
def prandwainput():
print('Virtual host '+sitename+' already configured...')
print(' 1. To add MySQL database for this virtual host write 1 and press "Enter"!!!')
print(' 2. To add PostgreSQL database for this virtual host write 2 and press "Enter"!!!')
print(' 3. If you want to exit from script just press "Enter" button. ')
global inst
inst = raw_input('Please select: ')
def dbcreds():
global sitedb
sitedb = raw_input('Enter name for new database: ')
global sitedbuser
sitedbuser = raw_input('Enter user name for database: ')
global sitedbpasswd
sitedbpasswd = getpass.getpass('Enter pass for '+sitedbuser+': ')
global sitedbpasswd1
sitedbpasswd1 = getpass.getpass('Repeat pass for '+sitedbuser+': ')
while sitedbpasswd != sitedbpasswd1:
print('Entered passwords must be the same. Please enter passwords again. ')
sitedbpasswd = getpass.getpass('Please enter password: ')
sitedbpasswd1 = getpass.getpass('Please repeat password: ')
if sitedbpasswd == sitedbpasswd1:
print('The password set successfully!')
break
print('Entered passwords must be the same. Please enter passwords again. ')
def inphpcreater():
tempphVars = { "sitedb" : sitedb, "sitedbuser" : sitedbuser, "sitedbpasswd" : sitedbpasswd}
outputphpText = tempphp.render( tempphVars )
with open(os.getcwd()+'/output/index.php', 'wb') as nginpfile:
nginpfile.write(outputphpText)
put(os.getcwd()+'/output/index.php', '/var/www/'+sitename+'/html/index.php')
def pgphpcreater():
tempphVars = { "sitedb" : sitedb, "sitedbuser" : sitedbuser, "sitedbpasswd" : sitedbpasswd }
outputpgpText = temppgp.render( tempphVars )
with open(os.getcwd()+'/output/cfpgpinsert.php', 'wb') as pgphpfile:
pgphpfile.write(outputpgpText)
put(os.getcwd()+'/output/cfpgpinsert.php', '/var/www/'+sitename+'/html/insert.php')
put(os.getcwd()+'/jinja2temps/cfindex.html', '/var/www/'+sitename+'/html/index.html')
def c7vhostcreate():
run('mkdir -p /var/www/'+sitename+'/html')
put(os.getcwd()+'/output/'+sitename+'.conf', '/etc/nginx/sites-available/')
put(os.getcwd()+'/output/index.html', '/var/www/'+sitename+'/html/index.html')
run('ln -s /etc/nginx/sites-available/* /etc/nginx/sites-enabled/')
run('systemctl restart nginx')
def f10vhostcreate():
run('mkdir -p /var/www/'+sitename+'/html')
put(os.getcwd()+'/output/'+sitename+'.conf', '/usr/local/etc/nginx/sites-available/')
put(os.getcwd()+'/output/index.html', '/var/www/'+sitename+'/html/')
run('ln -s /usr/local/etc/nginx/sites-available/* /usr/local/etc/nginx/sites-enabled/ ; service nginx restart')
def checkvhexists():
if sitename == domex:
print(' Entered domain name '+sitename+' is already exists on the '+env.host_string+' server!!!')
print(' Please enter different name than "'+sitename+'" !!!')
sys.exit()
else:
pass
def createmysqldb():
run('mysql -u root -p\'freebsd\' -e "CREATE DATABASE '+sitedb+';"')
run('mysql -u root -p\'freebsd\' -e "GRANT ALL PRIVILEGES ON '+sitedb+'.* TO '+sitedbuser+'@localhost IDENTIFIED BY \''+sitedbpasswd+'\';"')
run('mysql -u root -p\'freebsd\' -e "FLUSH PRIVILEGES;"')
cuser = 'postgres'
fuser = 'pgsql'
def createpgsqldb(username):
oversitepass = "'\\'%s\\''" % sitedbpasswd
run('su - '+username+' -c "psql -c \'CREATE DATABASE '+sitedb+';\'"')
run('su - '+username+' -c "psql -c \'CREATE USER '+sitedbuser+' WITH PASSWORD '+oversitepass+';\'"')
run('su - '+username+' -c "psql -c \'GRANT ALL PRIVILEGES ON DATABASE '+sitedb+' TO '+sitedbuser+';\'"')
run('su - '+username+' -c "psql -c \'CREATE TABLE book( bookid CHAR(255), bookname CHAR(255), author CHAR(255), publisher CHAR(255), dop CHAR(255), price CHAR(255) );\' '+sitedb+'"')
run('su - '+username+' -c "psql -c \'GRANT ALL PRIVILEGES ON TABLE book TO '+sitedbuser+';\' '+sitedb+'"')
def dbornotselect():
if inst == "1":
print(' You have chose MySQL with PHP-FPM!')
if osver == 'FreeBSD' and ftype >= 10:
mysqlpidf = run('cat /var/db/mysql/*.pid')
mysqlpid = run('ps waux|grep /usr/local/libexec/mysqld | grep -v grep | awk \'{ print $2 }\'')
sqlservicecheck(mysqlpid, mysqlpidf)
dbcreds()
createmysqldb()
inphpcreater()
run('service php-fpm restart ; service nginx restart')
elif osver == 'Linux' and lintype == 'CentOS':
msqlpid = run('cat /var/run/mariadb/mariadb.pid')
msqlpidfile = run('ps waux|grep mysql | grep -v grep| grep -v safe | awk \'{ print $2 }\'')
sqlservicecheck(msqlpid, msqlpidfile)
dbcreds()
createmysqldb()
inphpcreater()
run('systemctl restart nginx ; systemctl restart php-fpm')
print('MySQL database and Nginx configured for your site: '+sitename+'')
elif inst == "2":
print(' You have chose PostgreSQL with PHP-FPM!')
if osver == 'FreeBSD' and ftype >= 10:
psqlpidf = run('cat /usr/local/pgsql/data/postmaster.pid | head -1')
psqlpid = run('ps waux|grep /usr/local/bin/postgres | grep -v grep | awk \'{ print $2 }\'')
sqlservicecheck(psqlpid, psqlpidf)
dbcreds()
createpgsqldb(fuser)
elif osver == 'Linux' and lintype == 'CentOS':
pgsqlpidf = run('cat /var/run/postgresql/.s.PGSQL.5432.lock | head -1')
pgsqlpid = run('ps waux|grep /usr/bin/postgres | grep -v grep | awk \'{ print $2 }\'')
sqlservicecheck(pgsqlpid, pgsqlpidf)
dbcreds()
createpgsqldb(cuser)
else:
print(' Server type is not detected!!!')
pgphpcreater()
print('PostgreSQL database and Nginx configured for your site: '+sitename+'')
else:
sys.exit()
with settings(
hide('warnings', 'running', 'stdout', 'stderr'),
warn_only=True
):
osver = run('uname -s')
lintype = run('cat /etc/redhat-release | awk \'{ print $1 }\'')
ftype = run('uname -v | awk \'{ print $2 }\' | cut -f1 -d \'.\'')
if osver == 'FreeBSD' and ftype >= 10:
print(' This is FreeBSD server...')
domex = run('ls -la /usr/local/etc/nginx/sites-enabled/ | grep '+sitename+' | awk \'{ print $9 }\' | cut -f1,2 -d \'.\'')
checkvhexists()
nginxpidf = run('cat /var/run/nginx.pid')
nginxpid = run('ps waux | grep nginx | grep root | grep -v grep | awk \'{ print $2 }\'')
if nginxpid == nginxpidf:
print(' You have already running Nginx web server...')
vhhtmlwriter()
f10vhostcreate()
else:
print(' Nginx server is not running. For install Nginx web server please use ./ngphfpmypg.py script...')
sys.exit()
prandwainput()
dbornotselect()
elif osver == 'Linux' and lintype == 'CentOS':
print(' This is CentOS server...')
domex = run('ls -la /etc/nginx/sites-enabled/ | grep '+sitename+' | awk \'{ print $9 }\' | cut -f1,2 -d \'.\'')
checkvhexists()
ngpidf = run('cat /var/run/nginx.pid')
ngpid = run('ps waux | grep nginx | grep root | grep -v grep | awk \'{ print $2 }\'')
if ngpid == ngpidf:
print(' You have already running Nginx web server...')
vhhtmlwriter()
c7vhostcreate()
else:
print(' Nginx server is not running. For install Nginx web server please use ./ngphfpmypg.py script...')
sys.exit()
prandwainput()
dbornotselect()
else:
print(' This script supports FreeBSD or CentOS7 server...')