/
add-vhost-apmyph.py
executable file
·188 lines (147 loc) · 8.33 KB
/
add-vhost-apmyph.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
#!/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 )
TEMPAVFILE = os.getcwd()+'/jinja2temps/apvhost.conf'
TEMPHFILE = os.getcwd()+'/jinja2temps/tempindex.html'
TEMPPFILE = os.getcwd()+'/jinja2temps/tempindex.php'
tempav = templateEnv.get_template( TEMPAVFILE )
tempht = templateEnv.get_template( TEMPHFILE )
tempphp = templateEnv.get_template( TEMPPFILE )
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: ')
tempavVars = { "sname" : sitename, "domain" : sitename, }
outputavText = tempav.render( tempavVars )
outputahText = tempht.render( tempavVars )
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...')
getfhttpdpack = run('which httpd')
httpdpidfile = run('cat /var/run/httpd.pid')
httpdpid = run('ps waux | grep httpd | grep root | grep -v grep | awk \'{ print $2 }\'')
if getfhttpdpack == '/usr/local/sbin/httpd' and httpdpidfile == httpdpid:
print(' Apache web server installed and working...')
domex = run('ls -la /usr/local/domen/ | grep '+sitename+' | awk \'{ print $9 }\' | cut -f1,2 -d \'.\'')
if sitename == domex:
print('Entered domain name '+sitename+' is already exists on the '+env.host_string+' server!!!')
sys.exit()
else:
pass
with open(sitename+".conf", "wb") as apvhostfile:
apvhostfile.write(outputavText)
put(sitename+'.conf', '/usr/local/domen/'+sitename+'.conf')
with open("index.html", "wb") as aphtfile:
aphtfile.write(outputahText)
run('mkdir /var/www/'+sitename+'_public_html')
put('index.html', '/var/www/'+sitename+'_public_html')
run('/usr/local/etc/rc.d/apache24 restart')
print('Virtualhost '+sitename+' already configured...')
print('If you want to configure database for '+sitename+' virtualhost just press "Enter"!!!')
print('If you want to exit from script write "n" and press Enter button. ')
inst = raw_input('Please select: ')
if inst == "":
print('You are selected "Enter" button')
msqlpidfile = run('ps waux|grep mysql | grep -v grep| grep -v safe | awk \'{ print $2 }\'')
msqlpid = run('cat /var/db/mysql/*.pid')
if msqlpidfile == msqlpid:
print('MySQL service already running...')
pass
else:
sys.exit()
sitedb = raw_input('Enter name for new database: ')
sitedbuser = raw_input('Enter new mysql user name: ')
sitedbpasswd = getpass.getpass('Enter pass for '+sitedbuser+': ')
sitedbpasswd1 = getpass.getpass('Repeat pass for '+sitedbuser+': ')
if sitedbpasswd == sitedbpasswd1:
pass
else:
print('The password for site db username must be the same!!! ')
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;"')
tempphVars = { "sitedb" : sitedb, "sitedbuser" : sitedbuser, "sitedbpasswd" : sitedbpasswd}
outputphpText = tempphp.render( tempphVars )
with open("index.php", "wb") as aphtfile:
aphtfile.write(outputphpText)
put('index.php', '/var/www/'+sitename+'_public_html/index.php')
run('/usr/local/etc/rc.d/apache24 restart')
print('MySQL database for '+sitename+' already configured...')
elif inst == "n":
print('You entered "n" button.')
else:
print('You can only press "Enter" button or write "n" and after press enter button!!!')
elif getfhttpdpack != '/usr/local/sbin/httpd' and httpdpidfile != httpdpid:
print(' Apache web server is not working...')
elif osver == 'Linux' and lintype == 'CentOS':
print(' This is CentOS server...')
getlhttpdpack = run('which httpd')
httpdpidfile = run('cat /var/run/httpd/httpd.pid')
httpdpid = run('ps waux | grep httpd | grep root | grep -v grep | awk \'{ print $2 }\'')
if getlhttpdpack == '/usr/sbin/httpd' and httpdpidfile == httpdpid:
print(' Apache web server installed and working...')
domex = run('ls -la /usr/local/domen/ | grep '+sitename+' | awk \'{ print $9 }\' | cut -f1,2 -d \'.\'')
if sitename == domex:
print('Entered domain name '+sitename+' is already exists on the '+env.host_string+' server!!!')
sys.exit()
else:
pass
with open(sitename+".conf", "wb") as apvhostfile:
apvhostfile.write(outputavText)
put(sitename+'.conf', '/usr/local/domen/'+sitename+'.conf')
with open("index.html", "wb") as aphtfile:
aphtfile.write(outputahText)
run('mkdir /var/www/'+sitename+'_public_html')
put('index.html', '/var/www/'+sitename+'_public_html')
run('systemctl restart httpd.service')
print('Virtualhost '+sitename+' already configured...')
print('If you want to configure database for '+sitename+' virtualhost just press "Enter"!!!')
print('If you want to exit from script write "n" and press Enter button. ')
inst = raw_input('Please select: ')
if inst == "":
print('You are selected "Enter" button')
msqlpidfile = run('ps waux|grep mysql | grep -v grep| grep -v safe | awk \'{ print $2 }\'')
msqlpid = run('cat /var/run/mariadb/mariadb.pid')
if msqlpidfile == msqlpid:
print('MySQL service already configured and running...')
pass
else:
print('MySQL service is not running...')
sys.exit()
sitedb = raw_input('Enter name for new database: ')
sitedbuser = raw_input('Enter new mysql user name: ')
sitedbpasswd = getpass.getpass('Enter pass for '+sitedbuser+': ')
sitedbpasswd1 = getpass.getpass('Repeat pass for '+sitedbuser+': ')
if sitedbpasswd == sitedbpasswd1:
pass
else:
print('The repeated password for '+sitedbuser+' must be the same!!! ')
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;"')
tempphVars = { "sitedb" : sitedb, "sitedbuser" : sitedbuser, "sitedbpasswd" : sitedbpasswd}
outputphpText = tempphp.render( tempphVars )
with open("index.php", "wb") as aphtfile:
aphtfile.write(outputphpText)
put('index.php', '/var/www/'+sitename+'_public_html/index.php')
run('systemctl restart httpd.service')
print('MySQL database for '+sitename+' already configured...')
elif inst == "n":
print('You entered "n" button.')
else:
print('You can only press "Enter" button or write "n" and after press enter button!!!')
elif getlhttpdpack != '/usr/sbin/httpd' and httpdpidfile != httpdpid:
print(' Apache web server is not working...')
sys.exit()