import sys import getopt import os import re base constants apache bin

  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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import sys
import getopt
import os
import re
#
# base constants
#
# apache binary file dir
APACHE_BIN_DIR = "c:\\xampp\\apache\\bin\\"
# apache virtual hosts dir
APACHE_VHOSTS_DIR = "c:\\xampp\\vhosts\\"
# apache virtual hosts config file
APACHE_VHOSTS_CONFIG_FILE = "c:\\xampp\\apache\\conf\\extra\\httpd-vhosts.conf"
# system ip to dns file
HOSTS_IP_FILE = "c:\\WINDOWS\\system32\\drivers\\etc\\hosts"
#
# function writes data to the file
#
def write_file( filename,mode = "wb",data = "" ) :
fp = open( filename,mode )
fp.write( data )
fp.close()
#
# function reads data from the file
#
def read_file( filename ) :
fp = open( filename,"rb" )
contents = fp.read()
fp.close()
return contents
#
# function removes non empty dir
#
def rm_nonempty_dir( dir_name ) :
for root, dirs, files in os.walk( dir_name,topdown = False ) :
for file in files :
os.unlink( os.path.join( root,file ) )
for dir in dirs :
os.rmdir( os.path.join( root,dir ) )
os.rmdir( dir_name )
#
# function creates vitrual host dir, doc root in it,log dir and access and error log files
#
def create_domain_dir( domain ) :
full_path = APACHE_VHOSTS_DIR + domain
try :
if not os.path.exists( full_path ) :
os.mkdir( full_path )
doc_root = os.path.join( full_path,"htdocs" )
if not os.path.exists( doc_root ) :
os.mkdir( doc_root )
logs_dir = os.path.join( full_path,"logs" )
if not os.path.exists( logs_dir ) :
os.mkdir( logs_dir )
access_log = os.path.join( logs_dir,"access.log" )
if not os.path.exists( access_log ) :
write_file( access_log )
error_log = os.path.join( logs_dir,"error.log" )
if not os.path.exists( error_log ) :
write_file( error_log )
print "Domain dir %s created" % domain
except StandardError,err :
print "Error during creating dir for domain %s" % domain
print str( err )
sys.exit( 1 )
#
# function removes vitrual host dir, doc root in it,log dir and access and error log files
#
def remove_domain_dir( domain ) :
full_path = APACHE_VHOSTS_DIR + domain
try :
rm_nonempty_dir( full_path )
print "Domain dir %s removed" % domain
except StandardError,err :
print "Error during removing dir for domain %s" % domain
print str( err )
sys.exit( 1 )
#
# function adds info about vitrual host in the apache virtual hosts config and system ip to dns file
#
def add_info_to_configs( domain ) :
try :
host_data = read_file( "host.tpl" ).replace( "{$domain}",domain )
write_file( APACHE_VHOSTS_CONFIG_FILE,"ab",host_data )
ip_data = read_file( "ip.tpl" ).replace( "{$domain}",domain )
write_file( HOSTS_IP_FILE,"ab",ip_data )
print "Info about domain %s added to configs" % domain
except StandardError,err :
print "Error during adding info to configs for domain %s" % domain
print str( err )
sys.exit( 1 )
#
# function removes info about vitrual host in the apache virtual hosts config and system ip to dns file
#
def remove_info_from_configs( domain ) :
try :
domain_re = re.compile( "#.?" + domain + r".*?" + "#.?" + domain,
re.IGNORECASE | re.DOTALL )
host_text = read_file( APACHE_VHOSTS_CONFIG_FILE )
m = domain_re.search( host_text )
if m != None :
write_file( APACHE_VHOSTS_CONFIG_FILE,"wb",
host_text[ :m.start() ] + host_text[ m.end(): ] )
ip_text = read_file( HOSTS_IP_FILE )
m = domain_re.search( ip_text )
if m != None :
write_file( HOSTS_IP_FILE,"wb",
ip_text[ :m.start() ] + ip_text[ m.end(): ] )
print "Info about domain %s removed from configs" % domain
except :
print "Error during removing info from configs for domain %s" % domain
print str( err )
sys.exit( 1 )
#
# function creates domain
#
def create_domain( domain ) :
create_domain_dir( domain )
add_info_to_configs( domain )
#
# function removes domain
#
def remove_domain( domain ) :
remove_domain_dir( domain )
remove_info_from_configs( domain )
#
# function tests apache web server configs
#
def test_apache() :
return os.system( APACHE_BIN_DIR + "apache -t" )
#
# function stops apache web server
#
def stop_apache() :
stop_status = os.system( APACHE_BIN_DIR + "apache -k stop" )
if stop_status != 0 :
print "The Apache stop error: %s" % stop_status
sys.exit( 1 )
#
# function starts apache web server
#
def start_apache() :
start_status = os.system( APACHE_BIN_DIR + "apache -k start" )
if start_status == 0 :
print "The Apache service has started"
else :
print "The Apache start error,status: %s" % start_status
sys.exit( 1 )
#
# function prints help info about script keys
#
def print_help() :
print "Keys:"
print "-d - domain name"
print "-a - action: create or delete"
print "-h - this help"
#
# main function
#
def main() :
if len( sys.argv ) == 1 :
print "Options must be given"
print_help()
sys.exit( 1 )
try :
opts, args = getopt.getopt( sys.argv[1:],"hd:a:",["help","domain","action"] )
except getopt.GetoptError,err :
print "Options parsing error: %s" % str( err )
print_help()
sys.exit( 1 )
domain = ''
action = ''
for o, a in opts :
if o in ( "-h","--help" ) :
print_help()
sys.exit()
elif o in ( "-d","--domain" ) :
domain = a
elif o in ( "-a","--action" ) :
action = a
if action not in ( "create","delete" ) :
print "Action must be create or delete, %s not allowed" % action
sys.exit( 1 )
else :
print "Unknown option %s" % o
sys.exit( 1 )
if action != '' and domain == '' :
print "If action has choosed domain must be given"
sys.exit( 1 )
stop_apache()
if action == "create" :
create_domain( domain )
elif action == "delete" :
remove_domain( domain )
test_status = test_apache()
if test_status == 0 :
start_apache()
else :
print "The Apache config syntax error,status: %s" % test_status
sys.exit( 1 )
return 0
#
# run the script
#
if __name__ == "__main__" :
main()