1
1
#!/usr/bin/env python
2
+ from _socket import getservbyport
2
3
3
- import optparse , re , os , time , urllib2
4
+ import optparse , re , os , time , urllib2 , ConfigParser
4
5
5
- apache_port = 80
6
+ domain = None
7
+ www_dir = None
8
+ git_url = None
6
9
7
- class vHost :
10
+ cfg = ConfigParser .ConfigParser ()
11
+ configfile = 'create-vhost.ini'
8
12
13
+ print '--- Create vhost script for Apache2 ---'
14
+ print '--- Copyright Stephen Hoogendijk 2013, Licensed under the GPL V2 License ---'
15
+ print
9
16
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 )
14
19
15
- def __init__ (self ):
20
+ try :
21
+ cfg .read (configfile )
22
+ except ConfigParser .ParsingError as exc :
23
+ exit ('Parse error: %s' % exc )
16
24
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 )
17
32
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 )
50
33
51
34
52
35
def main ():
53
36
global use_pub , apache_port , domain , www_dir , git_url
54
37
55
38
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" )
58
40
p .add_option ('--domain' , '-d' , default = None , help = "Set the domain of this vhost" )
59
41
p .add_option ('--www_dir' , '-w' , default = None , help = "Set the root directory for this vhost" )
60
42
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" )
62
43
options , arguments = p .parse_args ()
63
44
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 :
66
46
p .print_help ()
67
47
exit ()
68
- else :
69
- use_pub = True if options .use_public_dir == 'yes' else False
70
48
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 ()
76
51
else :
52
+ # set the options
77
53
domain = options .domain
54
+ www_dir = options .www_dir
55
+ git_url = options .git_url
78
56
57
+ # create the vhost (from options)
58
+ create_vhost ()
79
59
80
- # set the options
81
- www_dir = options .www_dir
82
- git_url = options .git_url
83
- apache_port = options .apache_port
84
60
85
- if options .interactive :
86
61
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 )
88
80
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
89
110
90
- # try and create the vhost
91
111
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' )
93
116
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 ):
96
119
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 )
100
127
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] ' )
104
128
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
105
144
145
+ return None
106
146
107
147
if __name__ == '__main__' :
108
148
main ()
0 commit comments