-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.py
executable file
·280 lines (237 loc) · 8.25 KB
/
install.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/env python3
from shutil import copytree, copy, rmtree
import os
import sys
import json
import chissy
import pkg_resources
# read config from file
file = open('install.json')
install_config = json.load(file)
file.close()
# check if is root user
if os.getuid() != 0:
print("[!!] WTF!!! Are you drunk???")
print("[!!] This script need root user")
exit(-1)
# read arg if it passed
command = ""
if len(sys.argv) > 1:
command = sys.argv[1]
# path to be insert the bash completions
completions_path = '/usr/share/bash-completion/completions'
# installation paths
install_path = "{path}/{version}".format(path=install_config['installation-path'], version=chissy.__version__)
service_path = '/usr/lib/systemd/system/chissy.service'
symlink_bin = '/usr/bin/chissy'
symlink_etc = '/etc/chissy'
# path used for logs
logs_path = '/var/log/chissy'
# files to be copied
install_files = [
'chissy',
'conf',
'chissy.sh',
'chissy.py',
# 'install.py'
]
# files to be applied 'chmod +x'
exec_files = [
'chissy.sh',
'chissy.py',
# 'install.py'
]
# read need packages from requirements.txt
file = open('requirements.txt')
req_packages = file.read()
file.close()
req_packages = req_packages.split('\n')
try:
req_packages.remove('')
except ValueError:
pass
# help
helpers = """
Usage: {name} [install|remove]
"""
def show_help():
print(helpers.format(name=sys.argv[0]))
# function to install chissy
def install():
print("[*] Install starting...")
try:
# copy file on installation path
os.makedirs(install_path)
for f in install_files:
to = '/'.join([install_path, f])
if os.path.isfile(f):
copy(f, to)
else:
copytree(f, to)
# add execution permission
for f in exec_files:
os.system("chmod +x {path}/{file}".format(path=install_path, file=f))
# change log directory
path = '/'.join([install_path, 'conf', 'log.json'])
f = open(path, 'r')
conf_log = json.load(f)
f.close()
conf_log['path'] = logs_path
json_out = json.dumps(conf_log, sort_keys=True, indent=4, separators=(',', ': '))
f = open(path, 'w')
f.write(json_out)
f.close()
# create new key
while 1:
resp = input('[?] Generate new private key? (Y/n): ')
resp = resp.lower()
if resp == '' or resp == 'y' or resp == 'ye' or resp == 'yes':
# generate RSA key
exc = 'openssl req -new -x509 -nodes -keyout {path}/conf/key/rsa.key'.format(path=install_path)
os.system(exc)
exc = 'ssh-keygen -p -m PEM -f {path}/conf/key/rsa.key'.format(path=install_path)
os.system(exc)
# change key on configurations
path = '/'.join([install_path, 'conf', 'server.json'])
f = open(path, 'r')
conf_server = json.load(f)
f.close()
conf_server['host-key-filename'] = '{path}/conf/key/rsa.key'.format(path=install_path)
json_out = json.dumps(conf_server, sort_keys=True, indent=4, separators=(',', ': '))
f = open(path, 'w')
f.write(json_out)
f.close()
break
elif resp == 'n' or resp == 'no':
break
# add bash complete
if os.path.isdir(completions_path):
copy('template/completions/chissy', '/'.join([completions_path, 'chissy']))
# create logs dir /var/log/chissy
if not os.path.exists(logs_path):
os.makedirs(logs_path)
print('[*] Files copied')
# create symbolic link on /usr/bin to chissy
os.symlink('/'.join([install_path, "chissy.sh"]), symlink_bin)
# create symlink on /etc/chissy to conf
os.symlink('/'.join([install_path, 'conf']), symlink_etc)
print('[*] Symbolic links created')
# add settings for daemon
if install_config['systemd-service']:
service_file = open('template/systemd/chissy.service', 'r')
chiss_service = service_file.read()
service_file.close()
chiss_service = chiss_service.format(
workdir=install_path,
version=chissy.__version__
)
f = open(service_path, 'w')
f.write(chiss_service)
f.close()
os.system('systemctl daemon-reload')
print('[*] Systemd service created')
# install require packages
installed_pckgs = pkg_resources.working_set
installed_pckgs = sorted(["%s" % i.key for i in installed_pckgs])
for pckg in req_packages:
if pckg not in installed_pckgs:
install_package(pckg)
print('[*] Installation complete')
print()
print('[*] Usage: chissy {start|get-log|remove-log|version|help} [options]')
print('[*] Usage daemon: systemctl {start|stop|restart|status} chissy')
print()
except Exception as e:
print('[!!] Caught a exception while installing. ' + str(e))
sys.exit(-1)
# function to uninstall chessy
def uninstall():
print()
print("[*] Uninstall starting...")
try:
# remove service and symlink
if os.path.exists(service_path):
os.remove(service_path)
if os.path.islink(symlink_bin):
os.remove(symlink_bin)
if os.path.islink(symlink_etc):
os.remove(symlink_etc)
# remove autocomplete
chissy_compl = '/'.join([completions_path, 'chissy'])
if os.path.exists(chissy_compl):
os.remove(chissy_compl)
print('[*] All installation files have been deleted')
# remove log files
while 1:
resp = input('[?] Remove all log files? (y/N): ')
resp = resp.lower()
if resp == 'y' or resp == 'ye' or resp == 'yes':
if os.path.exists(logs_path):
rmtree(logs_path)
break
elif resp == "" or resp == "n" or resp == 'no':
break
# remove installation path
rmtree(install_path)
print("[*] Uninstall complete")
print()
except Exception as e:
print('[!!] Caught a exception while uninstalling. ' + str(e))
sys.exit(-1)
# function to check if chissy is already installed and install it
def check_install():
if os.path.isdir(install_path):
print('[!!] Chissy is already installed')
while 1:
resp = input('[?] Do you want to reinstall it? (y/N): ')
resp = resp.lower()
if resp == "y" or resp == "ye" or resp == "yes":
uninstall()
break
elif resp == "" or resp == "n" or resp == "no" or resp == "not":
sys.exit(0)
# remove service and symlink
if os.path.exists(service_path):
os.remove(service_path)
if os.path.islink(symlink_bin):
os.remove(symlink_bin)
if os.path.islink(symlink_etc):
os.remove(symlink_etc)
# start install
install()
# function to check if chissy is already installed
def check_uninstall():
if os.path.isdir(install_path):
while 1:
resp = input('[?] Remove Chissy? (y/N): ')
resp = resp.lower()
if resp == "y" or resp == "ye" or resp == "yes":
uninstall()
break
elif resp == "" or resp == "n" or resp == "no":
sys.exit(0)
else:
print()
def install_package(pckg):
print('[!!] Chissy application need ' + pckg)
while 1:
resp = input("[?] Install it now with pip? (Y/n): ")
resp = resp.lower()
if resp == '' or resp == 'y' or resp == 'ye' or resp == 'yes':
if os.path.exists('/usr/bin/pip3'):
os.system('pip3 install ' + pckg)
else:
print('[!!] You don\'t have pip3 installed!!!')
break
elif resp == 'n' or resp == 'no':
break
# main
if __name__ == '__main__':
switcher = {
"": check_install,
"install": check_install,
"remove": check_uninstall,
"help": show_help
}
func = switcher.get(command, show_help)
func()