-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping-cli.py
249 lines (180 loc) · 6 KB
/
ping-cli.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
#!/usr/bin/python3
# Batteries
import os
import signal
import time
from argparse import ArgumentParser
# Third-party Imports
import sqlalchemy
# Local Imports
from config import Config, InvalidConfiguration
from shared.models import Base
def stop():
"""
Stops the ping main process.
Returns:
int: The exit code to exit with.
"""
# Check for pidfile
if not os.path.isfile(Config.getpath('pidfile')):
print('Could not find pidfile. Is the process running?')
return 0
# Read Supervisor PID from pidfile
with open(Config.getpath('pidfile'), 'r') as pidfile:
pid = int(pidfile.readline())
# Check for running pid
if not os.path.isdir(f'/proc/{pid}'):
# Remove pidfile
os.unlink(Config.getpath('pidfile'))
# Also remove socket file if existent
if os.path.isfile(Config.getpath('socket')):
os.unlink(Config.getpath('socket'))
print('Process is not running. Removed stale pidfile and socket.')
return 0
# Send SIGTERM to process
os.kill(pid, signal.SIGTERM)
# Wait for process to end
while os.path.isdir(f'/proc/{pid}'):
print('Waiting for Ping to stop...')
time.sleep(1)
return 0
def start():
"""
Starts the Ping main process.
Returns:
int: The exit code to exit with.
"""
# Check for pidfile existence
if os.path.isfile(Config.getpath('pidfile')):
# Check for stale PID file
with open(Config.getpath('pidfile'), 'r') as pidfile:
pid = pidfile.read()
# Check if process with PID is running
if os.path.isdir(f'/proc/{pid}'):
print(f'Unable to start. Process already running with pid {pid}.')
return 1
# Remove stale PID file
print(f'Found stale pid file at {Config.get("pidfile")}. Removing...')
os.unlink(Config.getpath('pidfile'))
# Remove stale socket if existent
if os.path.isfile(Config.getpath('socket')):
os.unlink(Config.getpath('socket'))
# Only import master when required
from master import PingMaster
# Create and start the Ping master process
PingMaster(Config.getconfigpath()).start()
return 0
def restart():
"""
Restarts the Ping supervisor process.
Returns:
int: The exit code to exit with.
"""
# Save exitcode from stop execution
exitcode = stop()
# Start only if stop returns successfuly
if exitcode == 0:
exitcode = start()
return exitcode
def status():
"""
Checks if the Ping process is running.
Returns:
int: The exit code to exit with.
"""
# Check for pidfile existence
if os.path.isfile(Config.getpath('pidfile')):
# Check for stale PID file
with open(Config.getpath('pidfile'), 'r') as pidfile:
pid = pidfile.read()
# Check if process with PID is running
if os.path.isdir(f'/proc/{pid}'):
print(f'Ping process is running with pid {pid}.')
return 0
# Remove stale PID file
print(f'Found stale pid file at {Config.get("pidfile")}. Removing...')
os.unlink(Config.getpath('pidfile'))
print('Ping process is not running.')
return 1
def _reload():
"""
Signal the Ping master to reload configurations.
Returns:
int: The exit code to exit with.
"""
# Check for pidfile
if not os.path.isfile(Config.getpath('pidfile')):
print('Could not find pidfile. Is the process running?')
return 1
# Read Supervisor PID from pidfile
with open(Config.getpath('pidfile'), 'r') as pidfile:
pid = int(pidfile.readline())
# Check for running pid
if not os.path.isdir(f'/proc/{pid}'):
print('Process is not running. Removing stale pidfile.')
return 1
# Send SIGTERM to process
os.kill(pid, signal.SIGHUP)
return 0
def validate(path):
"""
Validates the configuration file syntax.
Args:
path (str): The configuration file path.
Returns:
int: The exit code to exit with.
"""
Config.validate(path)
def initdb():
"""
Creates database tables.
Returns:
int: The exit code to exit with.
"""
# Create engine
engine = sqlalchemy.create_engine(Config.get('datastore'), poolclass=sqlalchemy.pool.NullPool)
# Create database schema
try:
Base.metadata.create_all(engine)
except sqlalchemy.exc.OperationalError as e:
print(f'Operational Error\nCode: {e.orig.args[0]}\nMessage: {e.orig.args[1]}')
return 1
return 0
# Available operations
OPERATIONS = {
'stop': stop,
'start': start,
'restart': restart,
'status': status,
'reload': _reload,
'initdb': initdb
}
# Main
if __name__ == '__main__':
# Create argument parser
parser = ArgumentParser(description='Ping CLI')
parser.add_argument('-t', '--test', action="store_true", help=f'Validates the configuration file for errors.')
parser.add_argument('-c', '--config', type=str, help=f'The configuration file path.',
default=Config.CONFIG_FILE_PATH)
parser.add_argument('operation', type=str, choices=OPERATIONS.keys(),
help=f'The operation to execute. Possible values: {", ".join(OPERATIONS.keys())}')
# Parse Arguments
args = parser.parse_args()
# Convert to realpath
configfile = f'{os.path.realpath(os.path.dirname(os.path.realpath(__file__)))}/{args.config}'
# Execute requested operation
try:
# If test then just validate configuration
if args.test:
exit(validate(configfile))
else:
# Load Configuration
Config.load(configfile)
# Execute Operation
exit(OPERATIONS[args.operation]())
except InvalidConfiguration as e:
print(f'Invalid Configuration: {str(e)}')
exit(1)
except Exception as e:
print(f'Unknown error occurred: {str(e)}')
exit(1)