-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·130 lines (107 loc) · 3.96 KB
/
run.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
#!/usr/bin/env python
import os
import itertools
import threading
import time
import sys
from modules import message, host, user
from modules.constants import (
BASE_PATH,
NGINX_CONF_FILE,
NGINX_SITES_CONF_FILE,
NGINX_SITES_ENABLED,
HOSTS_FILE
)
# if __name__ == "__main__":
# if os.getenv("SUDO_USER") == None:
# message.error('Please start the script as root!', False, True)
# else:
# message.error('Script exit!', False, True)
if len(sys.argv) == 3:
args = sys.argv
if args[1].lower() == '--delete':
hostname = args[2]
answer = str(input(message.info('Remove virtual host?', True)+'[y/N]:'))
if answer.lower() == 'y':
if os.path.exists(NGINX_SITES_CONF_FILE % (hostname)):
os.system('sudo rm /etc/nginx/sites-*/'+hostname+'.conf')
host.delete(HOSTS_FILE, hostname)
host.restart()
message.success('Virtual host successfully removed', False, True)
else:
message.info('bye',False,True)
else:
message.error('Parameters are missing')
message.info('What do you mean?')
message.success('\t --delete %s' % (args[2]), False, True)
if os.path.exists(NGINX_CONF_FILE):
with open(NGINX_CONF_FILE, 'r') as file:
nginx_conf_content = file.read()
else:
message.error('%s not found' % (NGINX_CONF_FILE), False, True)
message.title()
try:
ROOT = user.root_dir()
PROJECT_TYPE = user.project_type()
PHPFPM_VERSION = user.phpfpm_version()
HOSTNAME = user.hostname()
except KeyboardInterrupt:
message.info('Bye', False, True)
except:
message.error('\nError! Something went wrong :(', False, True)
nginx_conf_content = nginx_conf_content.replace('__ROOT__', ROOT)\
.replace('__HOST__', HOSTNAME)\
.replace('__TYPE__', PROJECT_TYPE)\
.replace('__PHP__', PHPFPM_VERSION)
CHANGE_CONTENT = input(message.info(
'Do you want to modify the .conf file?', True)+' [y/N]:')
if CHANGE_CONTENT.lower() == 'y':
try:
editable_conf_file = os.path.join(BASE_PATH, HOSTNAME)
fruntime_conf = open(editable_conf_file, "w")
fruntime_conf.write(nginx_conf_content)
fruntime_conf.close()
os.system('sudo nano %s' % editable_conf_file)
if os.path.exists(editable_conf_file):
with open(editable_conf_file, 'r') as file:
nginx_conf_content = file.read()
os.system('sudo rm %s' % editable_conf_file)
except:
message.error('Error! Something went wrong :(')
fconf = open(NGINX_SITES_CONF_FILE % (HOSTNAME), "w")
symbolic_link = NGINX_SITES_ENABLED % (NGINX_SITES_CONF_FILE % (HOSTNAME))
creating = False
def creating_animate():
for c in itertools.cycle(['|', '/', '-', '\\']):
if creating:
break
sys.stdout.write('\r Creating virtual host ' + c)
sys.stdout.flush()
time.sleep(0.1)
t = threading.Thread(target=creating_animate)
t.start()
if fconf:
write = fconf.write(nginx_conf_content)
if write:
fconf.close()
try:
if os.path.exists('/etc/nginx/sites-enabled/'+HOSTNAME+'.conf'):
os.system('sudo rm /etc/nginx/sites-enabled/'+HOSTNAME+'.conf')
os.system('sudo ln -s %s' % symbolic_link)
if not host.check(HOSTS_FILE, HOSTNAME):
fhosts = open(HOSTS_FILE, "a")
fhosts.write('127.0.0.1\t %s \n' % HOSTNAME)
fhosts.close()
host.restart()
creating = True
message.success(
'\nVirtual host successfully created.Hostname: http://%s\n' % HOSTNAME)
message.info('Restart nginx before running')
except:
message.error('Error! Something went wrong :(', False, True)
else:
message.error(
'The .conf file could not be written. Something went wrong :(', False, True)
else:
message.error(
'The .conf file was not openning. Something went wrong :(', False, True)