This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual.py
More file actions
executable file
·45 lines (31 loc) · 1.38 KB
/
virtual.py
File metadata and controls
executable file
·45 lines (31 loc) · 1.38 KB
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
#!/usr/bin/python
from optparse import OptionParser
import os
parser = OptionParser()
path_apache_vhost = '/etc/apache2/sites-available/{0}'
path_sys_hosts = '/etc/hosts'
parser.add_option( "-d", "--directory", dest="directory", help="The path to the directory", metavar="DIRECTORY" )
parser.add_option( "-n", "--name", dest="name", help="Name of the virtual host", metavar="NAME" )
(options, args) = parser.parse_args()
template_dir_path = os.path.join(os.path.dirname(__file__), 'template')
template = open( template_dir_path, 'r' )
virtualHostText = template.read()
template.close()
# leer el archivo host
fileHosts = open(path_sys_hosts, 'r')
text_host = fileHosts.read();
fileHosts.close()
# comprueba que no exista la entrada en el archivo host
if text_host.find("{0}".format(options.name)) < 0:
fileHosts = open(path_sys_hosts, 'a')
fileHosts.write("127.0.0.1 {0}\n".format(options.name))
fileHosts.close()
virtualHostText = virtualHostText.format( name = options.name, directory = options.directory )
fileNameVirtualHost = path_apache_vhost.format(options.name)
fileNameVirtualHost = '/etc/apache2/sites-available/{0}'.format(options.name)
fileVirtualHost = open( fileNameVirtualHost, 'w' )
fileVirtualHost.write( virtualHostText )
fileVirtualHost.close()
# activa el nuevo stio y reinicia apache2
os.system('a2ensite {0}'.format(options.name))
os.system('service apache2 restart')