-
Notifications
You must be signed in to change notification settings - Fork 9
/
fioservice
executable file
·200 lines (165 loc) · 5.37 KB
/
fioservice
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
#!/usr/bin/env python3
import os
import sys
# import time
# import daemon
# import daemon.pidfile
import signal
import argparse
from fiotools import __version__
from fiotools.server import FIOWebService
from fiotools.handlers import ( # NOQA: F401
OpenshiftCMDHandler,
KubernetesCMDHandler,
SSHHandler,
NativeFIOHandler,
DebugHandler,
)
from fiotools.utils import rfile, get_pid_file, port_in_use
import fiotools.configuration as configuration
# settings.init()
import logging
stream = logging.StreamHandler()
logger = logging.getLogger()
logger.addHandler(stream)
DEFAULT_DIR = os.path.expanduser('~')
DEFAULT_NAMESPACE = 'fio'
def cmd_parser():
parser = argparse.ArgumentParser(
description='Manage the fio web service daemon',
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
'--version',
action='store_true',
default=False,
help="Show fioloadgen version",
)
parser.add_argument(
'--mode',
type=str,
choices=['dev', 'prod', 'debug'],
help="mode to run the service",
)
subparsers = parser.add_subparsers(help="sub-command")
parser_start = subparsers.add_parser(
'start',
help="start the FIO web service")
parser_start.set_defaults(func=command_start)
parser_start.add_argument(
'--type',
required=False,
choices=['oc', 'native', 'kubectl'],
help="type of fioservice target",
)
parser_start.add_argument(
'--namespace',
required=False,
type=str,
help="Namespace for Openshift based tests",
)
# parser_start.add_argument(
# '--debug',
# action='store_true',
# help="run standalone without a connection to help debug",
# )
parser_stop = subparsers.add_parser(
'stop',
help="stop the FIO service")
parser_stop.set_defaults(func=command_stop)
parser_stop = subparsers.add_parser(
'restart',
help="restart the service")
parser_stop.set_defaults(func=command_restart)
parser_status = subparsers.add_parser(
'status',
help="show current state of the FIO service")
parser_status.set_defaults(func=command_status)
return parser
def pid_exists(pidfile):
return os.path.exists(pidfile)
def command_status():
pidfile = get_pid_file()
if os.path.exists(pidfile):
print("PID file : {}".format(pidfile))
pid = rfile(pidfile)
print("PID : {}".format(pid))
if os.path.exists('/proc/{}'.format(pid)):
state = "running"
else:
state = "not running"
print("State : {}".format(state))
else:
print("Not running")
return
def command_restart():
pidfile = get_pid_file()
if pid_exists(pidfile):
command_stop()
command_start()
else:
print("service not running")
def command_start():
if not os.path.isdir(DEFAULT_DIR):
os.makedirs(DEFAULT_DIR)
if args.mode in ['debug', 'dev']:
logger.setLevel(logging.DEBUG)
if os.path.exists(get_pid_file()):
logger.error("Already running")
exit(1)
configuration.init(args)
if args.mode == 'debug':
logger.info("Using debug handler")
handler = DebugHandler()
elif configuration.settings.type == 'oc':
logger.info("Using 'oc' command handler")
handler = OpenshiftCMDHandler(mgr='fiomgr')
elif configuration.settings.type == 'kubectl':
logger.info("Using 'kubectl' command handler")
handler = KubernetesCMDHandler(mgr='fiomgr')
elif configuration.settings.type == 'native':
logger.info("Using 'native' fio handler")
handler = NativeFIOHandler() # ns=args.namespace)
else:
logger.error(f"'{configuration.settings.type}' handler has not been implemented yet")
sys.exit(1)
if not handler.has_connection:
logger.error("Handler is not usable: environment or configuration problem")
sys.exit(1)
logger.info("Checking port {} is free".format(configuration.settings.port))
if port_in_use(configuration.settings.port):
logger.error("-> port in use, unable to continue")
sys.exit(1)
server = FIOWebService(handler=handler)
logger.info("Checking connection to {}".format(handler._target))
if server.ready or configuration.settings.mode == 'debug':
logger.info("Starting the fioservice daemon")
# Call the run handler to start the web service
server.run()
else:
logger.error("{} connection unavailable, or workers not ready".format(handler._target))
logger.error("stopped")
# NB. run() forks the daemon, so anything here is never reached
def command_stop():
pidfile = get_pid_file()
if not os.path.exists(pidfile):
print("nothing to do")
return
with open(pidfile) as p:
pid = p.read().strip()
try:
os.kill(int(pid), signal.SIGTERM)
except Exception:
print("kill for {} caused an exception")
raise
else:
print("engine stopped")
if __name__ == '__main__':
parser = cmd_parser()
args = parser.parse_args()
if args.version:
print('fioloadgen version: {}'.format(__version__))
elif 'func' in args:
args.func()
else:
print("Unknown option(s) provided - try -h to show available options")