forked from siemens/mtda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtda-service
More file actions
executable file
·153 lines (128 loc) · 4.27 KB
/
mtda-service
File metadata and controls
executable file
·153 lines (128 loc) · 4.27 KB
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
#!/usr/bin/env python3
# ---------------------------------------------------------------------------
# MTDA Service
# ---------------------------------------------------------------------------
#
# This software is a part of MTDA.
# Copyright (C) 2023 Siemens Digital Industries Software
#
# ---------------------------------------------------------------------------
# SPDX-License-Identifier: MIT
# ---------------------------------------------------------------------------
# System imports
import argparse
import daemon
import lockfile
import os
import os.path
import signal
import sys
import zerorpc
import socket
import zeroconf
from systemd import daemon as sd
# Local imports
from mtda.main import MultiTenantDeviceAccess
import mtda.constants as CONSTS
class Application:
def __init__(self):
self.agent = None
self.remote = None
self.logfile = "/var/log/mtda.log"
self.pidfile = "/var/run/mtda.pid"
def daemonize(self):
context = daemon.DaemonContext(
working_directory=os.getcwd(),
stdout=open(self.logfile, 'w+'),
stderr=open(self.logfile, 'w+'),
umask=0o002,
pidfile=lockfile.FileLock(self.pidfile)
)
context.signal_map = {
signal.SIGTERM: 'terminate',
signal.SIGHUP: 'terminate',
}
with context:
status = self.server()
return status
def _ip(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('192.0.2.0', 0))
ip = s.getsockname()[0]
s.close()
return ip
def server(self):
# Start our agent
status = self.agent.start()
if status is False:
return False
# Start our RPC server
uri = "tcp://*:%d" % (self.agent.ctrlport)
s = zerorpc.Server(self.agent, heartbeat=20)
s.bind(uri)
# Initialize ZeroConf
interfaces = zeroconf.InterfaceChoice.All
zc = zeroconf.Zeroconf(interfaces=interfaces)
props = {
'description': 'Multi-Tenant Device Access'
}
deviceType = CONSTS.MDNS.TYPE
name = self.agent.name
info = zeroconf.ServiceInfo(
type_=deviceType,
name='{}.{}'.format(name, deviceType),
addresses=[socket.inet_aton(self._ip())],
port=int(self.agent.ctrlport),
properties=props,
server='{}.local.'.format(name))
try:
zc.register_service(info)
except zeroconf.NonUniqueNameException:
info = None
try:
sd.notify('READY=1')
s.run()
except KeyboardInterrupt:
s.stop()
self.agent.stop()
finally:
if info is not None:
zc.unregister_service(info)
return True
def print_version(self):
agent = MultiTenantDeviceAccess()
print("MTDA version: %s" % agent.version)
def main(self):
config = None
parser = argparse.ArgumentParser(
description='service process for MTDA')
parser.add_argument('-c', '--config',
nargs=1,
help='alternate configuration file')
parser.add_argument('-n', '--no-detach',
action='store_true',
help='do not detach from the calling process')
parser.add_argument('-v', '--version',
action='store_true',
help='print version of this program and exit')
args = parser.parse_args()
if args.version is True:
self.print_version()
sys.exit(0)
if args.config is not None:
config = args.config[0]
# Start our server
self.agent = MultiTenantDeviceAccess()
self.agent.load_config(None, True, config)
self.remote = self.agent.remote
if args.no_detach is False:
status = self.daemonize()
else:
status = self.server()
if status is False:
print('Failed to start the MTDA server!', file=sys.stderr)
return False
return True
if __name__ == '__main__':
app = Application()
app.main()