~rootmos/action

2addd23ad6bc4db76f64ee0123b0c7cf297d951e — Gustav Behm 5 months ago 32b4bd8
Add a status subcommand
2 files changed, 34 insertions(+), 4 deletions(-)

M setup.cfg
M src/action/cli.py
M setup.cfg => setup.cfg +1 -1
@@ 1,6 1,6 @@
[metadata]
name = action
version = 0.0.1
version = 0.0.2

[options]
packages = find_namespace:

M src/action/cli.py => src/action/cli.py +33 -3
@@ 42,6 42,10 @@ def parse_args():
    trigger_cmd = subparsers.add_parser("trigger")
    add_action_arg(trigger_cmd)

    status_cmd = subparsers.add_parser("status")
    add_action_arg(status_cmd)
    status_cmd.add_argument("-p", "--print-path", action="store_true")

    return parser.parse_args()

class Action:


@@ 81,9 85,18 @@ class Action:

        def datagram_received(self, data, addr):
            j = json.loads(data.decode())
            logger.info(f"action {self.action.name} triggered: {j}")
            self.action.trigger()
            logger.debug(f"received message: {j}")
            if j.get("action") != self.action.name:
                logger.warn(f"received unexpected message: {j}")
                return

            timestamp = j.get("timestamp")

            if not j.get("dry_run"):
                logger.info(f"action {self.action.name} triggered: {timestamp}")
                self.action.trigger()
            else:
                logger.info(f"action {self.action.name} dry run: {timestamp}")

def mk_socket_path(args):
    socket_path = args.socket


@@ 118,15 131,30 @@ def bind(args):
    asyncio.run(main())
    sys.exit(ec.status)

def trigger(args):
def trigger(args, dry_run=None):
    msg = {
        "action": args.action,
        "timestamp": datetime.datetime.utcnow().astimezone().isoformat(timespec='seconds'),
    }

    if dry_run is not None:
        msg["dry_run"] = dry_run

    s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    s.sendto(json.dumps(msg).encode("UTF-8"), mk_socket_path(args))
    s.close()

def status(args):
    if args.print_path:
        print(mk_socket_path(args))
        return

    try:
        trigger(args, dry_run=True)
        sys.exit(0)
    except ConnectionRefusedError:
        sys.exit(1)

def main():
    args = parse_args()
    if args.log_file is not None:


@@ 138,3 166,5 @@ def main():
        bind(args)
    elif args.cmd == "trigger":
        trigger(args)
    elif args.cmd == "status":
        status(args)