vhost cli creator

 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
#!/bin/env python
import os
from string import Template
from optparse import OptionParser
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-d", "--domain", metavar="DOMAIN", help="defines domain for vhost")
parser.add_option("-u", "--user", metavar="USER", help="defines vhost's owner")
parser.add_option("-i", "--ip", metavar="IP", default="*", help="vhost's IP, default is *")
parser.add_option("--home", metavar="HOME", default="/home", help="Home directory, default /home")
parser.add_option("-f", "--file", metavar="FILE", help="output to file")
(options, args) = parser.parse_args()
if not options.user or not options.domain:
parser.error("options user and domain are required, see help (--help)")
template = """NameVirtualHost $ip:80
<VirtualHost $ip:80>
ServerAdmin webmaster@$domain
ServerName $domain
ServerAlias www.$domain
DocumentRoot $home/$user/domains/$domain/public
ErrorLog logs/$domain-error_log
CustomLog logs/$domain-access_log combined
</VirtualHost>"""
t = Template(template)
vhost = t.substitute(domain=options.domain, user=options.user, ip=options.ip, home=options.home)
if not options.file:
print vhost
else:
if os.path.isfile(options.file):
parser.error("file %s already exists" % options.file)
else:
f = open(options.file, 'w')
f.writelines(vhost)
f.flush()
f.close()