-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-vhost.py
executable file
·230 lines (168 loc) · 6.56 KB
/
create-vhost.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
"""
Vhost creation script for Apache2 vhosts
Author: Stephen Hoogendijk
Licensed under the GPL V2 License
"""
from _socket import getservbyport
import optparse, re, os, time, urllib2, ConfigParser, subprocess
domain = None
www_dir = None
git_url = None
do_git_checkout = False
assume_yes = False
cwd = os.path.dirname(os.path.realpath(__file__))
vhost_tpl_path = cwd+'/vhost.tpl'
if not os.access('/etc/hosts', os.W_OK):
exit('Cannot write to /etc/hosts, please run this script as root or with sudo')
if not os.access(vhost_tpl_path, os.W_OK):
print vhost_tpl_path
exit('Cannot read vhost template file!')
cfg = ConfigParser.ConfigParser()
configfile = cwd+'/create-vhost.ini'
print '--- Create vhost script for Apache2 ---'
print '--- Author: Stephen Hoogendijk, Licensed under the GPL V2 License ---'
print
if not os.path.exists(configfile):
exit('Cannot find config file %s' % configfile)
try:
cfg.read(configfile)
except ConfigParser.ParsingError as exc:
exit('Parse error: %s' % exc)
try:
apache_port = int(cfg.get('apache', 'port'))
vhost_path = cfg.get('apache', 'vhost_path')
apache_exec = cfg.get('apache', 'apache_exec')
git_exec = cfg.get('git', 'exec')
except Exception as exc:
exit('Config file invalid, please check %s' %configfile)
def main():
global assume_yes, domain, www_dir, git_url
p = optparse.OptionParser()
p.add_option('--interactive', '-i', action="store_true", help="Run this script interactively")
p.add_option('--domain', '-d', default=None, help="Set the domain of this vhost")
p.add_option('--www_dir', '-w', default=None, help="Set the root directory for this vhost")
p.add_option('--git_url', '-g', default=None, help="Set the GIT repository URL")
p.add_option('-y', action="store_true", help="Assume yes to all queries and do not prompt (unless going interactive)")
options, arguments = p.parse_args()
if (options.domain is None or options.www_dir is None) and options.interactive is not True:
p.print_help()
exit()
if options.y:
assume_yes = True
if options.interactive:
interactive()
else:
# set the options
domain = options.domain
www_dir = options.www_dir
git_url = options.git_url
# create the vhost (from options)
create_vhost()
# run interactive mode
def interactive():
global domain, www_dir, git_url
domain = raw_input('Please enter the root domain for this vhost: ')
www_dir = raw_input('Please enter the www directory: ')
git_url = raw_input('(optional) Clone from GIT url: ')
#create the vhost
create_vhost()
# first check the requirements, then create the vhost
def create_vhost():
_check_vhost()
_check_apache()
print '\nCreating new vhost...\n'
time.sleep(1)
# create the www dir if it does not exist
if not os.path.exists(www_dir):
os.mkdir(www_dir, 0775)
# read the template and replace the variables
vhost_tpl = open(vhost_tpl_path, 'r').read()
vhost_tpl = vhost_tpl.replace('{APACHE_PORT}', str(apache_port))
vhost_tpl = vhost_tpl.replace('{DOMAIN}', domain)
vhost_tpl = vhost_tpl.replace('{WWW_DIR}', www_dir)
new_vhost = '%s/vhost_%s.conf' % (vhost_path, domain)
# if the vhost file does not exist, create it
if not os.path.exists(new_vhost):
vhost_file = open(new_vhost, 'w')
vhost_file.write(vhost_tpl)
vhost_file.close()
# add the host to /etc/hosts
_add_host(domain)
_add_host('www.%s' % domain)
if do_git_checkout is True:
subprocess.call('%s clone %s %s' % (git_exec, git_url, www_dir), shell=True)
# restart apache
subprocess.call('%s restart' % apache_exec, shell=True)
#done!
print '\nFinished creating new vhost %s!' % domain
return True
# check the vhost requirements
def _check_vhost():
global do_git_checkout
if not os.path.exists(www_dir) and not assume_yes:
sure_dir = raw_input('Your given www-dir does not exist, it will be created automatically ' +
'but will be owned by root, continue? [N/y]')
if str(sure_dir).lower() != 'y':
exit('Exit due to user input')
if domain is None or domain == '':
exit('Domain not set!')
if www_dir is None or www_dir == '':
exit('WWW root directory not set!')
# check if the www_dir already exists, check if the
if os.path.exists(www_dir):
if len(os.listdir(www_dir)) != 0 and git_url is not None and git_url != '':
exit('Cannot clone git repository in a non-empty directory!')
if not str(apache_port).isdigit():
exit('The apache port is not valid!')
# validate the given domain name
if not re.match("^(([a-zA-Z0-9]+([\-])?[a-zA-Z0-9]+)+(\.)?)+[a-zA-Z]{2,6}$", str(domain)) or 'www' in domain:
exit('Domain not valid: %s. Please enter a domain without www' % domain)
# check the git repo if it has been set
if git_url is not None and git_url != '':
_check_git(git_url)
do_git_checkout = True
# check the apache requirements
def _check_apache():
try:
if getservbyport(apache_port) not in ['http', 'www', 'apache2']:
exit('Apache does not seem to be running on port %d' % apache_port)
except Exception:
exit('Error while trying to poll for apache, please check the config setting apache->port')
# this method checks if the given git repository is valid
def _check_git(url):
if not _which(git_exec):
exit('Cannot execute local git installation on: %s' % git_exec)
try:
urllib2.urlopen(url)
except Exception as e:
exit('GIT repo \'%s\' could not be read' % url)
def _which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def _add_host(host):
host_line = '127.0.0.1 %s' % host
hosts_file = open('/etc/hosts', 'r')
host_list = hosts_file.readlines()
hosts_file.close()
found = False
for line in host_list:
if str(host_line) in line:
found = True
if not found:
hosts_file = open('/etc/hosts', 'a')
hosts_file.write(str(host_line)+"\n")
hosts_file.close()
if __name__ == '__main__':
main()