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()