forked from Eugeny/ajenti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajenti-panel
executable file
·139 lines (117 loc) · 3.78 KB
/
ajenti-panel
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
#!/usr/bin/env python
import daemon
import fcntl
import getopt
import logging
import os
import stat
import sys
import ajenti
import ajenti.log
from reconfigure.configs import AjentiConfig
class PidFile(object):
"""
Context manager that locks a pid file. Implemented as class
not generator because daemon.py is calling .__exit__() with no parameters
instead of the None, None, None specified by PEP-343.
"""
def __init__(self, path):
self.path = path
self.pidfile = None
def __enter__(self):
self.pidfile = open(self.path, "a+")
try:
fcntl.flock(self.pidfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
raise SystemExit("Already running according to " + self.path)
self.pidfile.seek(0)
self.pidfile.truncate()
self.pidfile.write(str(os.getpid()))
self.pidfile.flush()
self.pidfile.seek(0)
return self.pidfile
def __exit__(self, exc_type=None, exc_value=None, exc_tb=None):
try:
self.pidfile.close()
except IOError as err:
# ok if file was just closed elsewhere
if err.errno != 9:
raise
os.remove(self.path)
def usage():
print("""
Usage: %s [options]
Options:
-c, --config <file> - Use given config file instead of default
-v - Debug/verbose logging
-d, --daemon - Run in background (daemon mode)
-h, --help - This help
--set-platform <id> - Override OS detection
""")
if __name__ == '__main__':
log_level = logging.INFO
config_file = ''
try:
opts, args = getopt.getopt(sys.argv[1:], 'hc:dv', ['help', 'config=', 'daemon', 'set-platform='])
except getopt.GetoptError as e:
print(str(e))
usage()
sys.exit(2)
is_daemon = False
for o, a in opts:
if o in ('-h', '--help'):
usage()
sys.exit(0)
elif o in ('-v',):
log_level = logging.DEBUG
ajenti.debug = True
elif o in ('-c', '--config'):
config_file = a
elif o in ('-d', '--start'):
is_daemon = True
elif o == '--set-platform':
ajenti.platform = a
ajenti.log.init(log_level)
# Find default config file
if not config_file:
# Check for config file in /etc/ajenti/ajenti.conf
if os.path.isfile('/etc/ajenti/config.json'):
config_file = '/etc/ajenti/config.json'
elif os.path.isfile(os.path.join(sys.path[0], 'config.json')):
# Try local config file
config_file = os.path.join(sys.path[0], 'config.json')
if not os.path.exists(config_file):
logging.error('Config file "%s" not found' % config_file)
sys.exit(1)
else:
logging.info('Using config file %s' % config_file)
os.chmod(config_file, stat.S_IRWXU)
ajenti.config = AjentiConfig(path=config_file)
ajenti.config.load()
logging.basicConfig(level=log_level)
if is_daemon:
ajenti.log.init_log_directory()
logfile = open(ajenti.log.LOG_FILE, 'w+')
context = daemon.DaemonContext(
pidfile=PidFile('/var/run/ajenti.pid'),
stdout=logfile,
stderr=logfile,
detach_process=True
)
with context:
ajenti.log.init_log_rotation()
from ajenti import core
try:
core.run()
except Exception as e:
core.handle_crash(e)
else:
logging.info('Ajenti starting in foreground')
try:
from ajenti import core
core.run()
except KeyboardInterrupt:
pass
except Exception as e:
from ajenti import core
core.handle_crash(e)