forked from yurik-maz/add-apache-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.py
77 lines (65 loc) · 2.38 KB
/
host.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
#!/usr/bin/env python3
# -*-coding: utf-8 -*-
import os
class WriteHost:
def __init__(self):
self.choice = 0
def get_choice(self):
print('''
================================
Select action:
1 - create catalog end host file
2 - create only host file
3 - exit
================================
''')
try:
self.choice = int(input('? '))
except Exception:
print('Your choice is wrong! Please, try again...')
exit()
def main(self):
self.get_choice()
if self.choice == 1:
self.create_catalog(1)
elif self.choice == 2:
self.create_catalog(0)
else:
print("Good bye!")
exit()
def create_catalog(self, cat):
catalog = str(input('Enter dir name for site (/var/www/...):'))
if catalog == '':
self.create_catalog(cat)
if cat == 1:
print('Creating directory /var/www/' + catalog + '...\n')
os.system('sudo mkdir /var/www/' + catalog)
self.create_host(catalog)
def create_host(self, catalog):
hostname = input('Enter hostname:')
if hostname == '':
self.create_host(catalog)
print('Add to host file... \n')
with open('/etc/hosts', 'rt') as f:
s = f.read() + '\n' + '127.0.0.1\t\t\t' + hostname + '\n'
with open('/tmp/etc_hosts.tmp', 'wt') as file:
file.write(s)
os.system('sudo mv /tmp/etc_hosts.tmp /etc/hosts')
print('Generate virtual host file...\n')
with open('/tmp/' + hostname + '.tmp', 'wt') as file:
conf = '<VirtualHost ' + hostname + '>\n'
conf += '\tDocumentRoot /var/www/' + catalog + '\n'
conf += '\t<Directory "/var/www/' + catalog + '">\n'
conf += '\t\tallow from all\n'
conf += '\t\tOptions All\n'
conf += '\t\tAllowOverride All\n'
conf += '\t</Directory>\n'
conf += '</VirtualHost>'
file.write(conf)
os.system('sudo mv /tmp/' + hostname + '.tmp /etc/apache2/sites-available/' + hostname + '.conf')
os.system('sudo a2ensite ' + hostname + '.conf')
os.system('sudo service apache2 restart')
if __name__ == "__main__":
print('Generate virtual host script.\n')
h = WriteHost()
h.main()