@@ 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)