1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (c) 2014 Virtual Cable S.L. |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # Redistribution and use in source and binary forms, with or without modification, |
---|
7 | # are permitted provided that the following conditions are met: |
---|
8 | # |
---|
9 | # * Redistributions of source code must retain the above copyright notice, |
---|
10 | # this list of conditions and the following disclaimer. |
---|
11 | # * Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | # this list of conditions and the following disclaimer in the documentation |
---|
13 | # and/or other materials provided with the distribution. |
---|
14 | # * Neither the name of Virtual Cable S.L. nor the names of its contributors |
---|
15 | # may be used to endorse or promote products derived from this software |
---|
16 | # without specific prior written permission. |
---|
17 | # |
---|
18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
---|
22 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
23 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
24 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
---|
25 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
26 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | |
---|
29 | """ |
---|
30 | @author: Adolfo Gómez, dkmaster at dkmon dot com |
---|
31 | """ |
---|
32 | |
---|
33 | |
---|
34 | from opengnsys.service import CommonService |
---|
35 | from opengnsys.service import IPC_PORT |
---|
36 | from opengnsys import ipc |
---|
37 | |
---|
38 | from opengnsys.log import logger |
---|
39 | |
---|
40 | from opengnsys.linux.daemon import Daemon |
---|
41 | |
---|
42 | import sys |
---|
43 | import signal |
---|
44 | import json |
---|
45 | |
---|
46 | try: |
---|
47 | from prctl import set_proctitle # @UnresolvedImport |
---|
48 | except ImportError: # Platform may not include prctl, so in case it's not available, we let the "name" as is |
---|
49 | def set_proctitle(_): |
---|
50 | pass |
---|
51 | |
---|
52 | |
---|
53 | class OGAgentSvc(Daemon, CommonService): |
---|
54 | def __init__(self, args=None): |
---|
55 | Daemon.__init__(self, '/var/run/opengnsys-agent.pid') |
---|
56 | CommonService.__init__(self) |
---|
57 | |
---|
58 | def run(self): |
---|
59 | logger.debug('** Running Daemon **') |
---|
60 | set_proctitle('OGAgent') |
---|
61 | |
---|
62 | self.initialize() |
---|
63 | |
---|
64 | # Call modules initialization |
---|
65 | # They are called in sequence, no threading is done at this point, so ensure modules onActivate always returns |
---|
66 | |
---|
67 | # ********************* |
---|
68 | # * Main Service loop * |
---|
69 | # ********************* |
---|
70 | # Counter used to check ip changes only once every 10 seconds, for |
---|
71 | # example |
---|
72 | try: |
---|
73 | while self.isAlive: |
---|
74 | # In milliseconds, will break |
---|
75 | self.doWait(1000) |
---|
76 | except (KeyboardInterrupt, SystemExit) as e: |
---|
77 | logger.error('Requested exit of main loop') |
---|
78 | except Exception as e: |
---|
79 | logger.exception() |
---|
80 | logger.error('Caught exception on main loop: {}'.format(e)) |
---|
81 | |
---|
82 | self.terminate() |
---|
83 | |
---|
84 | self.notifyStop() |
---|
85 | |
---|
86 | def signal_handler(self, signal, frame): |
---|
87 | self.isAlive = False |
---|
88 | sys.stderr.write("signal handler: {}".format(signal)) |
---|
89 | |
---|
90 | |
---|
91 | def usage(): |
---|
92 | sys.stderr.write("usage: {} start|stop|restart|fg|login 'username'|logout 'username'|message 'module' 'message' 'json'\n".format(sys.argv[0])) |
---|
93 | sys.exit(2) |
---|
94 | |
---|
95 | |
---|
96 | if __name__ == '__main__': |
---|
97 | logger.setLevel('INFO') |
---|
98 | |
---|
99 | if len(sys.argv) == 5 and sys.argv[1] == 'message': |
---|
100 | logger.debug('Running client opengnsys') |
---|
101 | client = None |
---|
102 | try: |
---|
103 | client = ipc.ClientIPC(IPC_PORT) |
---|
104 | client.sendMessage(sys.argv[2], sys.argv[3], json.loads(sys.argv[4])) |
---|
105 | sys.exit(0) |
---|
106 | except Exception as e: |
---|
107 | logger.error(e) |
---|
108 | |
---|
109 | if len(sys.argv) == 3 and sys.argv[1] in ('login', 'logout'): |
---|
110 | logger.debug('Running client opengnsys') |
---|
111 | client = None |
---|
112 | try: |
---|
113 | client = ipc.ClientIPC(IPC_PORT) |
---|
114 | if 'login' == sys.argv[1]: |
---|
115 | client.sendLogin(sys.argv[2]) |
---|
116 | sys.exit(0) |
---|
117 | elif 'logout' == sys.argv[1]: |
---|
118 | client.sendLogout(sys.argv[2]) |
---|
119 | sys.exit(0) |
---|
120 | else: |
---|
121 | usage() |
---|
122 | except Exception as e: |
---|
123 | logger.error(e) |
---|
124 | elif len(sys.argv) != 2: |
---|
125 | usage() |
---|
126 | |
---|
127 | logger.debug('Executing actor') |
---|
128 | daemon = OGAgentSvc() |
---|
129 | |
---|
130 | signal.signal(signal.SIGTERM, daemon.signal_handler) |
---|
131 | signal.signal(signal.SIGINT, daemon.signal_handler) |
---|
132 | |
---|
133 | if len(sys.argv) == 2: |
---|
134 | if 'start' == sys.argv[1]: |
---|
135 | daemon.start() |
---|
136 | elif 'stop' == sys.argv[1]: |
---|
137 | daemon.stop() |
---|
138 | elif 'restart' == sys.argv[1]: |
---|
139 | daemon.restart() |
---|
140 | elif 'fg' == sys.argv[1]: |
---|
141 | daemon.run() |
---|
142 | else: |
---|
143 | usage() |
---|
144 | sys.exit(0) |
---|
145 | else: |
---|
146 | usage() |
---|