2017-11-03 15:06:29 +01:00
|
|
|
#!python
|
|
|
|
|
2017-11-04 14:02:56 +01:00
|
|
|
import argparse
|
2017-11-03 11:34:26 +01:00
|
|
|
import json
|
|
|
|
import os
|
2017-11-04 14:02:56 +01:00
|
|
|
import re
|
2017-11-03 11:34:26 +01:00
|
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
from pushbullet import Pushbullet
|
2017-11-03 15:06:29 +01:00
|
|
|
from runbullet import parse_config_file
|
2017-11-29 02:42:36 +01:00
|
|
|
from runbullet.backend.kafka import KafkaBackend
|
2017-11-03 11:34:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
def print_usage():
|
2017-11-04 12:28:15 +01:00
|
|
|
print ('''Usage: {} [-h|--help] <-t|--target <target name>> <-a|--action <action name>> payload
|
2017-11-03 11:34:26 +01:00
|
|
|
-h, --help:\t\tShow this help and exit
|
|
|
|
-t, --target:\tName of the target device/host
|
2017-11-04 12:28:15 +01:00
|
|
|
-a, --action\tAction to run, it includes both the package name and the method (e.g. shell.exec or music.mpd.play)
|
|
|
|
payload:\t\tArguments to the action
|
2017-11-03 11:34:26 +01:00
|
|
|
'''.format(sys.argv[0]))
|
|
|
|
|
2017-11-29 02:42:36 +01:00
|
|
|
def get_pb_device_by_name(pb, name):
|
2017-11-03 11:34:26 +01:00
|
|
|
devices = [
|
2017-11-29 02:42:36 +01:00
|
|
|
_ for _ in pb.devices if _.nickname == name
|
2017-11-03 11:34:26 +01:00
|
|
|
]
|
|
|
|
|
2017-11-29 02:42:36 +01:00
|
|
|
return devices[0] if devices else None
|
|
|
|
|
|
|
|
def send_pb_message(pb, device_name, msg):
|
|
|
|
device = get_pb_device_by_name(pb, name=device_name)
|
|
|
|
if not device:
|
2017-11-03 11:34:26 +01:00
|
|
|
print('Device {} not found - please create a virtual device on ' +
|
2017-11-09 05:04:48 +01:00
|
|
|
'your PushBullet account'.format(config['backend.pushbullet']['device']))
|
2017-11-03 11:34:26 +01:00
|
|
|
return
|
|
|
|
|
2017-11-29 02:42:36 +01:00
|
|
|
pb.push_note('', json.dumps(msg), device)
|
|
|
|
|
|
|
|
def send_kafka_message(backend, msg):
|
|
|
|
backend.send_msg(msg)
|
|
|
|
|
|
|
|
def get_backend(config):
|
|
|
|
# TODO Refactor this as something better and reuse the same
|
|
|
|
# backend classes from the runbullet consumer module
|
|
|
|
if 'backend.pushbullet' in config \
|
|
|
|
and 'pusher' in config['backend.pushbullet'] \
|
|
|
|
and config['backend.pushbullet']['pusher']:
|
|
|
|
API_KEY = config['backend.pushbullet']['token']
|
|
|
|
return Pushbullet(API_KEY)
|
|
|
|
elif 'backend.kafka' in config \
|
|
|
|
and 'pusher' in config['backend.kafka'] \
|
|
|
|
and config['backend.kafka']['pusher']:
|
|
|
|
c = config['backend.kafka']
|
2017-11-29 02:53:34 +01:00
|
|
|
return KafkaBackend({'server':c['server'], 'topic':c['topic']})
|
2017-11-29 02:42:36 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
config = parse_config_file()
|
|
|
|
|
2017-11-04 14:02:56 +01:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--target', '-t', dest='target', required=True,
|
|
|
|
help="Destination of the command")
|
2017-11-03 11:34:26 +01:00
|
|
|
|
2017-11-04 14:02:56 +01:00
|
|
|
parser.add_argument('--action', '-a', dest='action', required=True,
|
|
|
|
help="Action to execute, as package.method")
|
|
|
|
|
|
|
|
opts, args = parser.parse_known_args(sys.argv[1:])
|
2017-11-03 11:34:26 +01:00
|
|
|
payload = {}
|
|
|
|
|
2017-11-04 14:02:56 +01:00
|
|
|
if len(args) % 2 != 0:
|
|
|
|
raise RuntimeError('Odd number of key-value options passed: {}'.
|
|
|
|
format(args))
|
2017-11-03 11:34:26 +01:00
|
|
|
|
2017-11-04 14:02:56 +01:00
|
|
|
for i in range(0, len(args), 2):
|
|
|
|
payload[re.sub('^-+', '', args[i])] = args[i+1]
|
2017-11-03 11:34:26 +01:00
|
|
|
|
|
|
|
msg = {
|
2017-11-04 14:02:56 +01:00
|
|
|
'target': opts.target,
|
|
|
|
'action': opts.action,
|
2017-11-04 12:28:15 +01:00
|
|
|
**payload,
|
2017-11-03 11:34:26 +01:00
|
|
|
}
|
|
|
|
|
2017-11-04 12:28:15 +01:00
|
|
|
print('msg: {}'.format(msg))
|
2017-11-29 02:42:36 +01:00
|
|
|
|
|
|
|
backend = get_backend(config)
|
|
|
|
if isinstance(backend, Pushbullet):
|
|
|
|
send_pb_message(backend, config['backend.pushbullet']['device'], msg)
|
2017-11-29 03:18:01 +01:00
|
|
|
elif isinstance(backend, KafkaBackend):
|
2017-11-29 02:42:36 +01:00
|
|
|
send_kafka_message(backend, msg)
|
2017-11-03 11:34:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|