source: ogClient-Git/ogclient @ 05f2fd4

Last change on this file since 05f2fd4 was 05f2fd4, checked in by Jose M. Guisado <jguisado@…>, 3 years ago

#1065 Add event subcommand arguments

ogClient can be invoked with additional arguments to send
event datagrams to the default event datagram socket at
127.0.0.1 port 55885.

ogclient session events invokation syntax:

ogclient event login foobar
ogclient event logout foobar

If event commands arguments are detected, ogclient sends the
datagram and closes afterwards.

Datagram syntax for session event is:

session [start | stop] [user]

  • Property mode set to 100755
File size: 2.0 KB
RevLine 
[87c2a6a]1#!/usr/bin/python3
[99f2951]2
[834f5cd]3#
[cb9edc8]4# Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
[834f5cd]5#
6# This program is free software: you can redistribute it and/or modify it under
7# the terms of the GNU Affero General Public License as published by the
[cb9edc8]8# Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
[834f5cd]10
[38b6d77]11import json
[fd1f01d]12import logging
[05f2fd4]13import argparse
[bf69d20]14import subprocess
[fd1f01d]15try:
16        from signal import SIG_DFL, SIGPIPE
17except ImportError:
18        from signal import SIG_DFL
19
20
[834f5cd]21from src.ogClient import *
[3dfe549]22from src.log import configure_logging
[fd1f01d]23
[834f5cd]24
[05f2fd4]25def send_event_dgram(msg, ip='127.0.0.1', port=55885):
26        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
27        sock.sendto(bytes(msg, "utf-8"), (ip, port))
28
29
30def create_parser():
31        events = ['login', 'logout']
32        parser = argparse.ArgumentParser()
33        parser.set_defaults(func=None)
34        subparsers = parser.add_subparsers()
35
36        parser_event = subparsers.add_parser('event')
37
38        subparsers_event = parser_event.add_subparsers()
39        parser_event_login = subparsers_event.add_parser('login')
40        parser_event_login.set_defaults(func=lambda x: send_event_dgram(f'session start {x.user}'))
41        parser_event_login.add_argument('user', type=str)
42        parser_event_logout = subparsers_event.add_parser('logout')
43        parser_event_logout.set_defaults(func=lambda x: send_event_dgram(f'session stop {x.user}'))
44        parser_event_logout.add_argument('user', type=str)
45
46        return parser
47
48
[834f5cd]49def main():
[05f2fd4]50        parser = create_parser()
51        args = parser.parse_args(sys.argv[1:])
52        if args.func:
53                args.func(args)
54                return
[2dbcd18]55        config_path = f'{ogClient.OG_PATH}cfg/ogclient.json'
[38b6d77]56        try:
57                with open(config_path, 'r') as f:
58                        CONFIG = json.load(f)
59        except:
60                print('Error: Parsing configuration file')
[834f5cd]61                return 0
62
[38b6d77]63        MODE = CONFIG['opengnsys']['mode']
64        URL = CONFIG['opengnsys']['url']
[1377ace]65        if MODE == 'live':
[38b6d77]66                proc = subprocess.Popen(["browser", "-qws", URL])
[fd1f01d]67        if MODE != 'windows':
68                signal.signal(SIGPIPE, SIG_DFL)
[bf69d20]69
[3dfe549]70        configure_logging(MODE)
71
[38b6d77]72        client = ogClient(config=CONFIG)
[834f5cd]73        client.connect()
74        client.run()
75
76if __name__ == "__main__":
77        main()
Note: See TracBrowser for help on using the repository browser.