Skip to content

Commit fbfe0d5

Browse files
author
Stephen Hoogendijk
committed
Added ini config
improved checking for valid configuration
1 parent 8c77344 commit fbfe0d5

File tree

3 files changed

+129
-68
lines changed

3 files changed

+129
-68
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
#editor files
12
.idea
3+
.gitignore

create-vhost.ini

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
# Create vhost configuration file
4+
5+
6+
[apache]
7+
# apache runs on port 80 by default
8+
port = 80
9+
10+
# full path to the directory containing the vhosts
11+
vhost_path = /etc/apache2/sites-enabled
12+
13+
# full path to the apache executable
14+
apache_exec = /etc/init.d/apache2
15+
16+
[git]
17+
18+
# optional: path to your local git installation
19+
exec = /usr/bin/git

create-vhost.py

Lines changed: 108 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,148 @@
11
#!/usr/bin/env python
2+
from _socket import getservbyport
23

3-
import optparse, re, os, time, urllib2
4+
import optparse, re, os, time, urllib2, ConfigParser
45

5-
apache_port = 80
6+
domain = None
7+
www_dir = None
8+
git_url = None
69

7-
class vHost:
10+
cfg = ConfigParser.ConfigParser()
11+
configfile = 'create-vhost.ini'
812

13+
print '--- Create vhost script for Apache2 ---'
14+
print '--- Copyright Stephen Hoogendijk 2013, Licensed under the GPL V2 License ---'
15+
print
916

10-
domain = None
11-
www_dir = None
12-
use_pub = False
13-
git_url = None
17+
if not os.path.exists(configfile):
18+
exit('Cannot find config file %s' % configfile)
1419

15-
def __init__(self):
20+
try:
21+
cfg.read(configfile)
22+
except ConfigParser.ParsingError as exc:
23+
exit('Parse error: %s' % exc)
1624

25+
try:
26+
apache_port = int(cfg.get('apache', 'port'))
27+
vhost_path = cfg.get('apache', 'vhost_path')
28+
apache_exec = cfg.get('apache', 'vhost_path')
29+
git_exec = cfg.get('git', 'exec')
30+
except Exception as exc:
31+
exit('Config file invalid, please check %s' %configfile)
1732

18-
if domain is None:
19-
raise BaseException('Domain not set!')
20-
21-
if www_dir is None:
22-
raise BaseException('WWW root directory not set!')
23-
24-
# check if the www_dir already exists, i
25-
if os.path.exists(www_dir):
26-
27-
28-
if not str(apache_port).isdigit():
29-
raise BaseException('The apache port is not valid!')
30-
31-
# check the git repo if it has been set
32-
if git_url is not None:
33-
self._check_git(git_url)
34-
35-
36-
def create(self):
37-
38-
print 'Creating new vhost...'
39-
time.sleep(1)
40-
41-
return True
42-
43-
# this method checks if the given git repository is valid
44-
def _check_git(self, url):
45-
46-
try:
47-
data = urllib2.urlopen(url)
48-
except Exception as e:
49-
raise BaseException('GIT repo \'%s\' could not be read' % url)
5033

5134

5235
def main():
5336
global use_pub, apache_port, domain, www_dir, git_url
5437

5538
p = optparse.OptionParser()
56-
p.add_option('--interactive', '-i', default=False, help="Run this script interactively")
57-
p.add_option('--use_public_dir', '-p', default='yes', help="Use the /public directory as the document root")
39+
p.add_option('--interactive', '-i',action="store_true", help="Run this script interactively")
5840
p.add_option('--domain', '-d', default=None, help="Set the domain of this vhost")
5941
p.add_option('--www_dir', '-w', default=None, help="Set the root directory for this vhost")
6042
p.add_option('--git_url', '-g', default=None, help="Set the GIT repository URL")
61-
p.add_option('--apache_port', '-P', default=80, help="Override the apache port")
6243
options, arguments = p.parse_args()
6344

64-
if options.use_public_dir != 'yes' and options.use_public_dir != 'no':
65-
print 'Invalid option --use_public_dir', options.use_public_dir
45+
if (options.domain is None or options.www_dir is None) and options.interactive is not True:
6646
p.print_help()
6747
exit()
68-
else:
69-
use_pub = True if options.use_public_dir == 'yes' else False
7048

71-
# validate the given domain name
72-
if not re.match("^(([a-zA-Z0-9]+([\-])?[a-zA-Z0-9]+)+(\.)?)+[a-zA-Z]{2,6}$", str(options.domain)):
73-
print 'Invalid option --domain', options.domain
74-
p.print_help()
75-
exit()
49+
if options.interactive:
50+
interactive()
7651
else:
52+
# set the options
7753
domain = options.domain
54+
www_dir = options.www_dir
55+
git_url = options.git_url
7856

57+
# create the vhost (from options)
58+
create_vhost()
7959

80-
# set the options
81-
www_dir = options.www_dir
82-
git_url = options.git_url
83-
apache_port = options.apache_port
8460

85-
if options.interactive:
8661

87-
interactive()
62+
# run interactive mode
63+
def interactive():
64+
global domain, www_dir, git_url
65+
66+
domain = raw_input('Please enter the root domain for this vhost: ')
67+
www_dir = raw_input('Please enter the www directory: ')
68+
git_url = raw_input('(optional) Clone from GIT url: ')
69+
70+
#create the vhost
71+
create_vhost()
72+
73+
# first check the requirements, then create the vhost
74+
def create_vhost():
75+
_check_vhost()
76+
_check_apache()
77+
78+
print 'Creating new vhost...'
79+
time.sleep(1)
8880

81+
return True
82+
83+
# check the vhost requirements
84+
def _check_vhost():
85+
if domain is None or domain == '':
86+
exit('Domain not set!')
87+
88+
if www_dir is None or www_dir == '':
89+
exit('WWW root directory not set!')
90+
91+
if os.path.exists(www_dir):
92+
# check if the www_dir already exists, check if the
93+
if os.listdir(www_dir) != "" and git_url is not None and git_url != '':
94+
exit('Cannot clone git repository in a non-empty directory!')
95+
96+
if not str(apache_port).isdigit():
97+
exit('The apache port is not valid!')
98+
99+
# validate the given domain name
100+
if not re.match("^(([a-zA-Z0-9]+([\-])?[a-zA-Z0-9]+)+(\.)?)+[a-zA-Z]{2,6}$", str(domain)):
101+
exit('Domain not valid: %s' % domain)
102+
103+
# check the git repo if it has been set
104+
if git_url is not None and git_url != '':
105+
_check_git(git_url)
106+
107+
# check the apache requirements
108+
def _check_apache():
109+
global apache_port
89110

90-
# try and create the vhost
91111
try:
92-
vHost().create()
112+
if getservbyport(apache_port) not in ['http', 'www', 'apache2']:
113+
exit('Apache does not seem to be running on port %d' % apache_port)
114+
except Exception:
115+
exit('Error while trying to poll for apache, please check the config setting apache->port')
93116

94-
except BaseException as exc:
95-
print 'The following error occurred: %s' % exc
117+
# this method checks if the given git repository is valid
118+
def _check_git(url):
96119

97-
# run interactive mode
98-
def interactive():
99-
global domain, www_dir, use_pub
120+
if not _which(git_exec):
121+
exit('Cannot execute local git installation on: %s' % git_exec)
122+
123+
try:
124+
urllib2.urlopen(url)
125+
except Exception as e:
126+
exit('GIT repo \'%s\' could not be read' % url)
100127

101-
domain = input('Please enter the root domain for this vhost: ')
102-
www_dir = input('Please enter the www directory: ')
103-
use_pub = input('Use public dir? [y/N] ')
104128

129+
def _which(program):
130+
import os
131+
def is_exe(fpath):
132+
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
133+
134+
fpath, fname = os.path.split(program)
135+
if fpath:
136+
if is_exe(program):
137+
return program
138+
else:
139+
for path in os.environ["PATH"].split(os.pathsep):
140+
path = path.strip('"')
141+
exe_file = os.path.join(path, program)
142+
if is_exe(exe_file):
143+
return exe_file
105144

145+
return None
106146

107147
if __name__ == '__main__':
108148
main()

0 commit comments

Comments
 (0)