Move files around in the API.
Bots are not part of what we distribute, so put them in the repo root. We also updated some of the bots to use relative path names. (imported from commit 0471d863450712fd0cdb651f39f32e9041df52ba)
This commit is contained in:
parent
2a47fc4777
commit
d8f13837d9
23 changed files with 5 additions and 4 deletions
|
@ -1,63 +0,0 @@
|
|||
#### Dependencies
|
||||
|
||||
The Humbug API Python bindings require the following Python libraries:
|
||||
|
||||
* simplejson
|
||||
* requests (version >= 0.12 and << 1.0.0)
|
||||
|
||||
#### Using the API
|
||||
|
||||
For now, the only fully supported API operation is sending a message.
|
||||
The other API queries work, but are under active development, so
|
||||
please make sure we know you're using them so that we can notify you
|
||||
as we make any changes to them.
|
||||
|
||||
The easiest way to use these API bindings is to base your tools off
|
||||
of the example tools under api/examples in this distribution.
|
||||
|
||||
If you place your API key in `~/.humbugrc` the Python API bindings will
|
||||
automatically read it in. The format of the config file is as follows:
|
||||
|
||||
[api]
|
||||
key=<api key from the web interface>
|
||||
email=<your email address>
|
||||
|
||||
You can obtain your Humbug API key from the Humbug settings page.
|
||||
|
||||
A typical simple bot sending API messages will look as follows:
|
||||
|
||||
At the top of the file:
|
||||
|
||||
# Make sure the Humbug API distribution's root directory is in sys.path, then:
|
||||
import humbug
|
||||
humbug_client = humbug.Client(email="your_email@example.com")
|
||||
|
||||
When you want to send a message:
|
||||
|
||||
message = {
|
||||
"type": "stream",
|
||||
"to": ["support"],
|
||||
"subject": "your subject",
|
||||
"content": "your content",
|
||||
}
|
||||
humbug_client.send_message(message)
|
||||
|
||||
Additional examples:
|
||||
|
||||
client.send_message({'type': 'stream', 'content': 'Humbug rules!',
|
||||
'subject': 'feedback', 'to': ['support']})
|
||||
client.send_message({'type': 'private', 'content': 'Humbug rules!',
|
||||
'to': ['user1@example.com', 'user2@example.com']})
|
||||
|
||||
send_message() returns a dict guaranteed to contain the following
|
||||
keys: msg, result. For successful calls, result will be "success" and
|
||||
msg will be the empty string. On error, result will be "error" and
|
||||
msg will describe what went wrong.
|
||||
|
||||
#### Sending messages
|
||||
|
||||
You can use the included `api/bin/humbug-send` script to send messages via the
|
||||
API directly from existing scripts.
|
||||
|
||||
humbug-send hamlet@example.com cordelia@example.com -m \
|
||||
"Conscience doth make cowards of us all."
|
|
@ -1,123 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# humbug-send -- Sends a message to the specified recipients.
|
||||
|
||||
# Copyright © 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
import os
|
||||
import optparse
|
||||
import logging
|
||||
|
||||
|
||||
logging.basicConfig()
|
||||
|
||||
log = logging.getLogger('humbug-send')
|
||||
|
||||
def do_send_message(client, message_data ):
|
||||
'''Sends a message and optionally prints status about the same.'''
|
||||
|
||||
if message_data['type'] == 'stream':
|
||||
log.info('Sending message to stream "%s", subject "%s"... ' % \
|
||||
(message_data['to'], message_data['subject']))
|
||||
else:
|
||||
log.info('Sending message to %s... ' % message_data['to'])
|
||||
response = client.send_message(message_data)
|
||||
if response['result'] == 'success':
|
||||
log.info('Message sent.')
|
||||
return True
|
||||
else:
|
||||
log.error(response['msg'])
|
||||
return False
|
||||
|
||||
def main(argv=None):
|
||||
if argv is None:
|
||||
argv = sys.argv
|
||||
|
||||
usage = """%prog [options] [recipient...]
|
||||
|
||||
Sends a message specified recipients.
|
||||
|
||||
Examples: %prog --stream denmark --subject castle -m "Something is rotten in the state of Denmark."
|
||||
%prog hamlet@example.com cordelia@example.com -m "Conscience doth make cowards of us all."
|
||||
"""
|
||||
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
||||
|
||||
import humbug
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
|
||||
# Grab parser options from the API common set
|
||||
parser.add_option_group(humbug.generate_option_group(parser))
|
||||
|
||||
parser.add_option('-m', '--message',
|
||||
help='Specifies the message to send, prevents interactive prompting.')
|
||||
|
||||
group = optparse.OptionGroup(parser, 'Stream parameters')
|
||||
group.add_option('-s', '--stream',
|
||||
dest='stream',
|
||||
action='store',
|
||||
help='Allows the user to specify a stream for the message.')
|
||||
group.add_option('-S', '--subject',
|
||||
dest='subject',
|
||||
action='store',
|
||||
help='Allows the user to specify a subject for the message.')
|
||||
parser.add_option_group(group)
|
||||
|
||||
|
||||
(options, recipients) = parser.parse_args(argv[1:])
|
||||
|
||||
if options.verbose:
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
# Sanity check user data
|
||||
if len(recipients) != 0 and (options.stream or options.subject):
|
||||
parser.error('You cannot specify both a username and a stream/subject.')
|
||||
if len(recipients) == 0 and (bool(options.stream) != bool(options.subject)):
|
||||
parser.error('Stream messages must have a subject')
|
||||
if len(recipients) == 0 and not (options.stream and options.subject):
|
||||
parser.error('You must specify a stream/subject or at least one recipient.')
|
||||
|
||||
client = humbug.init_from_options(options)
|
||||
|
||||
if not options.message:
|
||||
options.message = sys.stdin.read()
|
||||
|
||||
if options.stream:
|
||||
message_data = {
|
||||
'type': 'stream',
|
||||
'content': options.message,
|
||||
'subject': options.subject,
|
||||
'to': options.stream,
|
||||
}
|
||||
else:
|
||||
message_data = {
|
||||
'type': 'private',
|
||||
'content': options.message,
|
||||
'to': recipients,
|
||||
}
|
||||
|
||||
if not do_send_message(client, message_data):
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
|
@ -1,348 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
import sys
|
||||
import time
|
||||
import optparse
|
||||
import os
|
||||
import random
|
||||
import logging
|
||||
import subprocess
|
||||
import hashlib
|
||||
from os import path
|
||||
|
||||
parser = optparse.OptionParser()
|
||||
parser.add_option('--verbose',
|
||||
dest='verbose',
|
||||
default=False,
|
||||
action='store_true')
|
||||
parser.add_option('--site',
|
||||
dest='site',
|
||||
default="https://humbughq.com",
|
||||
action='store')
|
||||
parser.add_option('--sharded',
|
||||
default=False,
|
||||
action='store_true')
|
||||
parser.add_option('--root-path',
|
||||
dest='root_path',
|
||||
default="/home/humbug",
|
||||
action='store')
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
# The 'api' directory needs to go first, so that 'import humbug' won't pick up
|
||||
# some other directory named 'humbug'.
|
||||
sys.path[:0] = [os.path.join(options.root_path, "api/"),
|
||||
os.path.join(options.root_path, "python-zephyr"),
|
||||
os.path.join(options.root_path, "python-zephyr/build/lib.linux-x86_64-2.6/"),
|
||||
options.root_path]
|
||||
|
||||
mit_user = 'tabbott/extra@ATHENA.MIT.EDU'
|
||||
humbug_user = 'tabbott/extra@mit.edu'
|
||||
|
||||
sys.path.append(".")
|
||||
import humbug
|
||||
humbug_client = humbug.Client(
|
||||
email=humbug_user,
|
||||
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
verbose=True,
|
||||
client="test: Humbug API",
|
||||
site=options.site)
|
||||
|
||||
# Configure logging
|
||||
log_file = os.path.join(options.root_path, "check-mirroring-log")
|
||||
log_format = "%(asctime)s: %(message)s"
|
||||
logging.basicConfig(format=log_format)
|
||||
|
||||
formatter = logging.Formatter(log_format)
|
||||
file_handler = logging.FileHandler(log_file)
|
||||
file_handler.setFormatter(formatter)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.addHandler(file_handler)
|
||||
|
||||
# Initialize list of streams to test
|
||||
if options.sharded:
|
||||
# NOTE: Streams in this list must be in humbug_user's Humbug
|
||||
# subscriptions, or we won't receive messages via Humbug.
|
||||
|
||||
# The sharded stream list has a bunch of pairs
|
||||
# (stream, shard_name), where sha1sum(stream).startswith(shard_name)
|
||||
test_streams = [
|
||||
("message", "p"),
|
||||
("tabbott-nagios-test-32", "0"),
|
||||
("tabbott-nagios-test-33", "1"),
|
||||
("tabbott-nagios-test-2", "2"),
|
||||
("tabbott-nagios-test-5", "3"),
|
||||
("tabbott-nagios-test-13", "4"),
|
||||
("tabbott-nagios-test-7", "5"),
|
||||
("tabbott-nagios-test-22", "6"),
|
||||
("tabbott-nagios-test-35", "7"),
|
||||
("tabbott-nagios-test-4", "8"),
|
||||
("tabbott-nagios-test-3", "9"),
|
||||
("tabbott-nagios-test-1", "a"),
|
||||
("tabbott-nagios-test-49", "b"),
|
||||
("tabbott-nagios-test-34", "c"),
|
||||
("tabbott-nagios-test-12", "d"),
|
||||
("tabbott-nagios-test-11", "e"),
|
||||
("tabbott-nagios-test-9", "f"),
|
||||
]
|
||||
for (stream, test) in test_streams:
|
||||
if stream == "message":
|
||||
continue
|
||||
assert(hashlib.sha1(stream).hexdigest().startswith(test))
|
||||
else:
|
||||
test_streams = [
|
||||
("message", "p"),
|
||||
("tabbott-nagios-test", "a"),
|
||||
]
|
||||
|
||||
def print_status_and_exit(status):
|
||||
# The output of this script is used by Nagios. Various outputs,
|
||||
# e.g. true success and punting due to a SERVNAK, result in a
|
||||
# non-alert case, so to give us something unambiguous to check in
|
||||
# Nagios, print the exit status.
|
||||
print status
|
||||
sys.exit(status)
|
||||
|
||||
def send_humbug(message):
|
||||
result = humbug_client.send_message(message)
|
||||
if result["result"] != "success":
|
||||
logger.error("Error sending humbug, args were:")
|
||||
logger.error(message)
|
||||
logger.error(result)
|
||||
print_status_and_exit(1)
|
||||
|
||||
# Returns True if and only if we "Detected server failure" sending the zephyr.
|
||||
def send_zephyr(zwrite_args, content):
|
||||
p = subprocess.Popen(zwrite_args, stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate(input=content.encode("utf-8"))
|
||||
if p.returncode != 0:
|
||||
if "Detected server failure while receiving acknowledgement for" in stdout:
|
||||
logger.warning("Got server failure error sending zephyr; retrying")
|
||||
logger.warning(stderr)
|
||||
return True
|
||||
logger.error("Error sending zephyr:")
|
||||
logger.info(stdout)
|
||||
logger.error(stderr)
|
||||
print_status_and_exit(1)
|
||||
return False
|
||||
|
||||
# Subscribe to Humbugs
|
||||
try:
|
||||
res = humbug_client.get_profile()
|
||||
max_message_id = res.get('max_message_id')
|
||||
if max_message_id is None:
|
||||
logging.error("Error subscribing to Humbugs!")
|
||||
logging.error(res)
|
||||
print_status_and_exit(1)
|
||||
except Exception:
|
||||
logger.exception("Unexpected error subscribing to Humbugs")
|
||||
print_status_and_exit(1)
|
||||
|
||||
# Subscribe to Zephyrs
|
||||
import zephyr
|
||||
zephyr_subs_to_add = []
|
||||
for (stream, test) in test_streams:
|
||||
if stream == "message":
|
||||
zephyr_subs_to_add.append((stream, 'personal', mit_user))
|
||||
else:
|
||||
zephyr_subs_to_add.append((stream, '*', '*'))
|
||||
|
||||
actually_subscribed = False
|
||||
for tries in xrange(10):
|
||||
try:
|
||||
zephyr.init()
|
||||
zephyr._z.subAll(zephyr_subs_to_add)
|
||||
zephyr_subs = zephyr._z.getSubscriptions()
|
||||
|
||||
missing = 0
|
||||
for elt in zephyr_subs_to_add:
|
||||
if elt not in zephyr_subs:
|
||||
logging.error("Failed to subscribe to %s" % (elt,))
|
||||
missing += 1
|
||||
if missing == 0:
|
||||
actually_subscribed = True
|
||||
break
|
||||
except IOError, e:
|
||||
if "SERVNAK received" in e:
|
||||
logger.error("SERVNAK repeatedly received, punting rest of test")
|
||||
else:
|
||||
logger.exception("Exception subscribing to zephyrs")
|
||||
|
||||
if not actually_subscribed:
|
||||
logger.error("Failed to subscribe to zephyrs")
|
||||
print_status_and_exit(1)
|
||||
|
||||
# Prepare keys
|
||||
zhkeys = {}
|
||||
hzkeys = {}
|
||||
def gen_key(key_dict):
|
||||
bits = str(random.getrandbits(32))
|
||||
while bits in key_dict:
|
||||
# Avoid the unlikely event that we get the same bits twice
|
||||
bits = str(random.getrandbits(32))
|
||||
return bits
|
||||
|
||||
def gen_keys(key_dict):
|
||||
for (stream, test) in test_streams:
|
||||
key_dict[gen_key(key_dict)] = (stream, test)
|
||||
|
||||
gen_keys(zhkeys)
|
||||
gen_keys(hzkeys)
|
||||
|
||||
notices = []
|
||||
|
||||
# We check for new zephyrs multiple times, to avoid filling the zephyr
|
||||
# receive queue with 30+ messages, which might result in messages
|
||||
# being dropped.
|
||||
def receive_zephyrs():
|
||||
while True:
|
||||
try:
|
||||
notice = zephyr.receive(block=False)
|
||||
except Exception:
|
||||
logging.exception("Exception receiving zephyrs:")
|
||||
notice = None
|
||||
if notice is None:
|
||||
break
|
||||
if notice.opcode != "":
|
||||
continue
|
||||
notices.append(notice)
|
||||
|
||||
logger.info("Starting sending messages!")
|
||||
# Send zephyrs
|
||||
zsig = "Timothy Good Abbott"
|
||||
for key, (stream, test) in zhkeys.items():
|
||||
if stream == "message":
|
||||
zwrite_args = ["zwrite", "-n", "-s", zsig, mit_user]
|
||||
else:
|
||||
zwrite_args = ["zwrite", "-n", "-s", zsig, "-c", stream, "-i", "test"]
|
||||
server_failure = send_zephyr(zwrite_args, str(key))
|
||||
if server_failure:
|
||||
# Replace the key we're not sure was delivered with a new key
|
||||
value = zhkeys.pop(key)
|
||||
new_key = gen_key(zhkeys)
|
||||
zhkeys[new_key] = value
|
||||
server_failure_again = send_zephyr(zwrite_args, str(new_key))
|
||||
if server_failure_again:
|
||||
logging.error("Zephyr server failure twice in a row on keys %s and %s! Aborting." %
|
||||
(key, new_key))
|
||||
print_status_and_exit(1)
|
||||
else:
|
||||
logging.warning("Replaced key %s with %s due to Zephyr server failure." %
|
||||
(key, new_key))
|
||||
receive_zephyrs()
|
||||
|
||||
receive_zephyrs()
|
||||
logger.info("Sent Zephyr messages!")
|
||||
|
||||
# Send Humbugs
|
||||
for key, (stream, test) in hzkeys.items():
|
||||
if stream == "message":
|
||||
send_humbug({
|
||||
"type": "private",
|
||||
"content": str(key),
|
||||
"to": humbug_user,
|
||||
})
|
||||
else:
|
||||
send_humbug({
|
||||
"type": "stream",
|
||||
"subject": "test",
|
||||
"content": str(key),
|
||||
"to": stream,
|
||||
})
|
||||
receive_zephyrs()
|
||||
|
||||
logger.info("Sent Humbug messages!")
|
||||
|
||||
# Normally messages manage to forward through in under 3 seconds, but
|
||||
# sleep 10 to give a safe margin since the messages do need to do 2
|
||||
# round trips. This alert is for correctness, not performance, and so
|
||||
# we want it to reliably alert only when messages aren't being
|
||||
# delivered at all.
|
||||
time.sleep(10)
|
||||
receive_zephyrs()
|
||||
|
||||
logger.info("Starting receiving messages!")
|
||||
|
||||
# receive humbugs
|
||||
messages = humbug_client.get_messages({'last': str(max_message_id)})['messages']
|
||||
logger.info("Finished receiving Humbug messages!")
|
||||
|
||||
receive_zephyrs()
|
||||
logger.info("Finished receiving Zephyr messages!")
|
||||
|
||||
all_keys = set(zhkeys.keys() + hzkeys.keys())
|
||||
def process_keys(content_list):
|
||||
# Start by filtering out any keys that might have come from
|
||||
# concurrent check-mirroring processes
|
||||
content_keys = [key for key in content_list if key in all_keys]
|
||||
key_counts = {}
|
||||
for key in all_keys:
|
||||
key_counts[key] = 0
|
||||
for key in content_keys:
|
||||
key_counts[key] += 1
|
||||
z_missing = set(key for key in zhkeys.keys() if key_counts[key] == 0)
|
||||
h_missing = set(key for key in hzkeys.keys() if key_counts[key] == 0)
|
||||
duplicates = any(val > 1 for val in key_counts.values())
|
||||
success = all(val == 1 for val in key_counts.values())
|
||||
return key_counts, z_missing, h_missing, duplicates, success
|
||||
|
||||
# The h_foo variables are about the messages we _received_ in Humbug
|
||||
# The z_foo variables are about the messages we _received_ in Zephyr
|
||||
h_contents = [message["content"] for message in messages]
|
||||
z_contents = [notice.message.split('\0')[1] for notice in notices]
|
||||
(h_key_counts, h_missing_z, h_missing_h, h_duplicates, h_success) = process_keys(h_contents)
|
||||
(z_key_counts, z_missing_z, z_missing_h, z_duplicates, z_success) = process_keys(z_contents)
|
||||
|
||||
if z_success and h_success:
|
||||
logger.info("Success!")
|
||||
print_status_and_exit(0)
|
||||
elif z_success:
|
||||
logger.info("Received everything correctly in Zephyr!")
|
||||
elif h_success:
|
||||
logger.info("Received everything correctly in Humbug!")
|
||||
|
||||
logger.error("Messages received the wrong number of times:")
|
||||
for key in all_keys:
|
||||
if z_key_counts[key] == 1 and h_key_counts[key] == 1:
|
||||
continue
|
||||
if key in zhkeys:
|
||||
(stream, test) = zhkeys[key]
|
||||
logger.warning("%10s: z got %s, h got %s. Sent via Zephyr(%s): class %s" % \
|
||||
(key, z_key_counts[key], h_key_counts[key], test, stream))
|
||||
if key in hzkeys:
|
||||
(stream, test) = hzkeys[key]
|
||||
logger.warning("%10s: z got %s. h got %s. Sent via Humbug(%s): class %s" % \
|
||||
(key, z_key_counts[key], h_key_counts[key], test, stream))
|
||||
logger.error("")
|
||||
logger.error("Summary of specific problems:")
|
||||
|
||||
if h_duplicates:
|
||||
logger.error("humbug: Received duplicate messages!")
|
||||
logger.error("humbug: This is probably a bug in our message loop detection.")
|
||||
logger.error("humbug: where Humbugs go humbug=>zephyr=>humbug")
|
||||
if z_duplicates:
|
||||
logger.error("zephyr: Received duplicate messages!")
|
||||
logger.error("zephyr: This is probably a bug in our message loop detection.")
|
||||
logger.error("zephyr: where Zephyrs go zephyr=>humbug=>zephyr")
|
||||
|
||||
if z_missing_z:
|
||||
logger.error("zephyr: Didn't receive all the Zephyrs we sent on the Zephyr end!")
|
||||
logger.error("zephyr: This is probably an issue with check-mirroring sending or receiving Zephyrs.")
|
||||
if h_missing_h:
|
||||
logger.error("humbug: Didn't receive all the Humbugs we sent on the Humbug end!")
|
||||
logger.error("humbug: This is probably an issue with check-mirroring sending or receiving Humbugs.")
|
||||
if z_missing_h:
|
||||
logger.error("zephyr: Didn't receive all the Humbugs we sent on the Zephyr end!")
|
||||
if z_missing_h == h_missing_h:
|
||||
logger.error("zephyr: Including some Humbugs that we did receive on the Humbug end.")
|
||||
logger.error("zephyr: This suggests we have a humbug=>zephyr mirroring problem.")
|
||||
logger.error("zephyr: aka the personals mirroring script has issues.")
|
||||
if h_missing_z:
|
||||
logger.error("humbug: Didn't receive all the Zephyrs we sent on the Humbug end!")
|
||||
if h_missing_z == z_missing_z:
|
||||
logger.error("humbug: Including some Zephyrs that we did receive on the Zephyr end.")
|
||||
logger.error("humbug: This suggests we have a zephyr=>humbug mirroring problem.")
|
||||
logger.error("humbug: aka the global class mirroring script has issues.")
|
||||
|
||||
print_status_and_exit(1)
|
|
@ -1,137 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
Script to provide information about send-receive times.
|
||||
|
||||
It supports both munin and nagios outputs
|
||||
|
||||
It must be run on a machine that is using the live database for the
|
||||
Django ORM.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import os
|
||||
import sys
|
||||
import optparse
|
||||
import random
|
||||
|
||||
|
||||
def total_seconds(timedelta):
|
||||
return (timedelta.microseconds + (timedelta.seconds + timedelta.days * 24 * 3600) * 10**6) / 10.**6
|
||||
|
||||
usage = """Usage: send-receive.py [options] [config]
|
||||
|
||||
'config' is optional, if present will return config info.
|
||||
Otherwise, returns the output data."""
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option('--site',
|
||||
dest='site',
|
||||
default="https://humbughq.com",
|
||||
action='store')
|
||||
|
||||
parser.add_option('--nagios',
|
||||
dest='nagios',
|
||||
action='store_true')
|
||||
|
||||
parser.add_option('--munin',
|
||||
dest='munin',
|
||||
action='store_true')
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
if not options.nagios and not options.munin:
|
||||
print 'No output options specified! Please provide --munin or --nagios'
|
||||
sys.exit(0)
|
||||
|
||||
if len(args) > 2:
|
||||
print usage
|
||||
sys.exit(0)
|
||||
|
||||
if options.munin:
|
||||
if len(args) and args[0] == 'config':
|
||||
print \
|
||||
"""graph_title Send-Receive times
|
||||
graph_info The number of seconds it takes to send and receive a message from the server
|
||||
graph_args -u 5 -l 0
|
||||
graph_vlabel RTT (seconds)
|
||||
sendreceive.label Send-receive round trip time
|
||||
sendreceive.warning 3
|
||||
sendreceive.critical 5"""
|
||||
sys.exit(0)
|
||||
|
||||
sys.path.append('/home/humbug/humbug/api')
|
||||
import humbug
|
||||
|
||||
states = {
|
||||
"OK": 0,
|
||||
"WARNING": 1,
|
||||
"CRITICAL": 2,
|
||||
"UNKNOWN": 3
|
||||
}
|
||||
|
||||
def report(state, time, msg=None):
|
||||
if msg:
|
||||
print "%s: %s" % (state, msg)
|
||||
else:
|
||||
print "%s: send time was %s" % (state, time)
|
||||
exit(states[state])
|
||||
|
||||
def send_humbug(sender, message, nagios):
|
||||
result = sender.send_message(message)
|
||||
if result["result"] != "success" and nagios:
|
||||
report("CRITICAL", "Error sending Humbug, args were: %s, %s" % (message, result))
|
||||
|
||||
def get_humbug(recipient, max_message_id):
|
||||
return recipient.get_messages({'last': str(max_message_id)})['messages']
|
||||
|
||||
# hamlet and othello are default users
|
||||
sender = "hamlet@humbughq.com"
|
||||
recipient = "othello@humbughq.com"
|
||||
|
||||
humbug_sender = humbug.Client(
|
||||
email=sender,
|
||||
api_key="dfe1c934d555f4b9538d0d4cfd3069c2",
|
||||
verbose=True,
|
||||
client="test: Humbug API",
|
||||
site=options.site)
|
||||
|
||||
humbug_recipient = humbug.Client(
|
||||
email=recipient,
|
||||
api_key="4e5d97591bec64bf57d2698ffbb563e3",
|
||||
verbose=True,
|
||||
client="test: Humbug API",
|
||||
site=options.site)
|
||||
|
||||
|
||||
max_message_id = humbug_recipient.get_profile().get('max_message_id')
|
||||
msg_to_send = str(random.getrandbits(64))
|
||||
time_start = datetime.datetime.now()
|
||||
|
||||
send_humbug(humbug_sender, {
|
||||
"type": 'private',
|
||||
"content": msg_to_send,
|
||||
"subject": "time to send",
|
||||
"to": recipient,
|
||||
}, options.nagios)
|
||||
|
||||
msg_content = []
|
||||
|
||||
while msg_to_send not in msg_content:
|
||||
messages = get_humbug(humbug_recipient, max_message_id)
|
||||
time_diff = datetime.datetime.now() - time_start
|
||||
|
||||
# Prevents checking the same messages everytime in the conditional
|
||||
# statement of the while loop
|
||||
max_message_id = max([msg['id'] for msg in messages])
|
||||
msg_content = [m['content'] for m in messages]
|
||||
|
||||
if options.nagios:
|
||||
if time_diff.seconds > 3:
|
||||
report('WARNING', time_diff)
|
||||
if time_diff.seconds > 6:
|
||||
report('CRITICAL', time_diff)
|
||||
|
||||
if options.munin:
|
||||
print "sendreceive.value %s" % total_seconds(time_diff)
|
||||
elif options.nagios:
|
||||
report('OK', time_diff)
|
|
@ -1,58 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
import sys
|
||||
from os import path
|
||||
import logging
|
||||
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
class StreamLogger(object):
|
||||
"""
|
||||
Give a file-like interface to a logger instance.
|
||||
"""
|
||||
def __init__(self, logger, log_level=logging.INFO):
|
||||
self.logger = logger
|
||||
self.log_level = log_level
|
||||
self.buffer = ""
|
||||
|
||||
def write(self, message):
|
||||
self.buffer += message
|
||||
|
||||
if message[-1] == "\n":
|
||||
self.logger.log(self.log_level, self.buffer.rstrip())
|
||||
self.buffer = ""
|
||||
|
||||
logging.basicConfig(filename="/var/log/humbug/feedback-bot.log",
|
||||
level=logging.DEBUG, format="%(asctime)s %(levelname)s\t%(message)s")
|
||||
|
||||
# The API prints to stdout, so capture and format those messages as well.
|
||||
stdout_logger = StreamLogger(logging.getLogger("stdout"), logging.INFO)
|
||||
sys.stdout = stdout_logger
|
||||
|
||||
stderr_logger = StreamLogger(logging.getLogger("stderr"), logging.ERROR)
|
||||
sys.stderr = stderr_logger
|
||||
|
||||
prod_client = humbug.Client(
|
||||
email="feedback@humbughq.com",
|
||||
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
verbose=True,
|
||||
site="https://humbughq.com")
|
||||
staging_client = humbug.Client(
|
||||
email="feedback@humbughq.com",
|
||||
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
||||
verbose=True,
|
||||
site="https://staging.humbughq.com")
|
||||
|
||||
def forward_message(message):
|
||||
if message["type"] != "private" or len(message["display_recipient"]) != 2:
|
||||
return
|
||||
forwarded_message = {
|
||||
"type": "stream",
|
||||
"to": "support",
|
||||
"subject": "feedback from %s" % message["sender_email"],
|
||||
"content": message["content"],
|
||||
}
|
||||
staging_client.send_message(forwarded_message)
|
||||
logging.info("Forwarded feedback from %s" % (message["sender_email"],))
|
||||
|
||||
prod_client.call_on_each_message(forward_message)
|
|
@ -1,151 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import errno
|
||||
import datetime
|
||||
import optparse
|
||||
import urlparse
|
||||
import itertools
|
||||
import traceback
|
||||
from os import path
|
||||
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
parser = optparse.OptionParser(r"""
|
||||
|
||||
%prog \
|
||||
--user foo@humbughq.com \
|
||||
--calendar http://www.google.com/calendar/feeds/foo%40humbughq.com/private-fedcba9876543210fedcba9876543210/basic
|
||||
|
||||
Send yourself reminders on Humbug of Google Calendar events.
|
||||
|
||||
To get the calendar URL:
|
||||
- Load Google Calendar in a web browser
|
||||
- Find your calendar in the "My calendars" list on the left
|
||||
- Click the down-wedge icon that appears on mouseover, and select "Calendar settings"
|
||||
- Copy the link address for the "XML" button under "Private Address"
|
||||
|
||||
Run this on your personal machine. Your API key and calendar URL are revealed to local
|
||||
users through the command line.
|
||||
|
||||
Depends on: python-gdata
|
||||
""")
|
||||
|
||||
parser.add_option('--user',
|
||||
dest='user',
|
||||
action='store',
|
||||
help='Humbug user email address',
|
||||
metavar='EMAIL')
|
||||
parser.add_option('--api-key',
|
||||
dest='api_key',
|
||||
action='store',
|
||||
help='API key for that user [default: read ~/.humbug-api-key]')
|
||||
parser.add_option('--calendar',
|
||||
dest='calendar',
|
||||
action='store',
|
||||
help='Google Calendar XML "Private Address"',
|
||||
metavar='URL')
|
||||
parser.add_option('--site',
|
||||
dest='site',
|
||||
default="https://humbughq.com",
|
||||
action='store',
|
||||
help='Humbug site [default: https://humbughq.com]',
|
||||
metavar='URL')
|
||||
parser.add_option('--interval',
|
||||
dest='interval',
|
||||
default=10,
|
||||
type=int,
|
||||
action='store',
|
||||
help='Minutes before event for reminder [default: 10]',
|
||||
metavar='MINUTES')
|
||||
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
if not (options.user and options.calendar):
|
||||
parser.error('You must specify --user and --calendar')
|
||||
|
||||
try:
|
||||
from gdata.calendar.client import CalendarClient
|
||||
except ImportError:
|
||||
parser.error('Install python-gdata')
|
||||
|
||||
def get_calendar_url():
|
||||
parts = urlparse.urlparse(options.calendar)
|
||||
pat = path.split(parts.path)
|
||||
if pat[1] != 'basic':
|
||||
parser.error('The --calendar URL should be the XML "Private Address" ' +
|
||||
'from your calendar settings')
|
||||
return urlparse.urlunparse((parts.scheme, parts.netloc, pat[0] + '/full',
|
||||
'', 'futureevents=true&orderby=startdate', ''))
|
||||
|
||||
calendar_url = get_calendar_url()
|
||||
|
||||
client = humbug.Client(
|
||||
email=options.user,
|
||||
api_key=options.api_key,
|
||||
site=options.site,
|
||||
verbose=True)
|
||||
|
||||
def get_events():
|
||||
feed = CalendarClient().GetCalendarEventFeed(uri=calendar_url)
|
||||
|
||||
for event in feed.entry:
|
||||
start = event.when[0].start.split('.')[0]
|
||||
# All-day events can have only a date
|
||||
fmt = '%Y-%m-%dT%H:%M:%S' if 'T' in start else '%Y-%m-%d'
|
||||
start = datetime.datetime.strptime(start, fmt)
|
||||
yield (event.uid.value, start, event.title.text)
|
||||
|
||||
# Our cached view of the calendar, updated periodically.
|
||||
events = []
|
||||
|
||||
# Unique keys for events we've already sent, so we don't remind twice.
|
||||
sent = set()
|
||||
|
||||
def send_reminders():
|
||||
global sent
|
||||
|
||||
messages = []
|
||||
keys = set()
|
||||
now = datetime.datetime.now()
|
||||
|
||||
for uid, start, title in events:
|
||||
dt = start - now
|
||||
if dt.days == 0 and dt.seconds < 60*options.interval:
|
||||
# The unique key includes the start time, because of
|
||||
# repeating events.
|
||||
key = (uid, start)
|
||||
if key not in sent:
|
||||
line = '%s starts at %s' % (title, start.strftime('%H:%M'))
|
||||
print 'Sending reminder:', line
|
||||
messages.append(line)
|
||||
keys.add(key)
|
||||
|
||||
if not messages:
|
||||
return
|
||||
|
||||
if len(messages) == 1:
|
||||
message = 'Reminder: ' + messages[0]
|
||||
else:
|
||||
message = 'Reminder:\n\n' + '\n'.join('* ' + m for m in messages)
|
||||
|
||||
client.send_message(dict(
|
||||
type = 'private',
|
||||
to = options.user,
|
||||
content = message))
|
||||
|
||||
sent |= keys
|
||||
|
||||
# Loop forever
|
||||
for i in itertools.count():
|
||||
try:
|
||||
# We check reminders every minute, but only
|
||||
# download the calendar every 10 minutes.
|
||||
if not i % 10:
|
||||
events = list(get_events())
|
||||
send_reminders()
|
||||
except:
|
||||
traceback.print_exc()
|
||||
time.sleep(60)
|
|
@ -1,100 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
#
|
||||
# Humbug trac plugin -- sends humbugs when tickets change.
|
||||
#
|
||||
# Install by placing in the plugins/ subdirectory and then adding
|
||||
# "humbug_trac" to the [components] section of the conf/trac.ini file,
|
||||
# like so:
|
||||
#
|
||||
# [components]
|
||||
# humbug_trac = enabled
|
||||
#
|
||||
# You may then need to restart trac (or restart Apache) for the bot
|
||||
# (or changes to the bot) to actually be loaded by trac.
|
||||
#
|
||||
# Our install is trac.humbughq.com:/home/humbug/trac/
|
||||
|
||||
from trac.core import Component, implements
|
||||
from trac.ticket import ITicketChangeListener
|
||||
import sys
|
||||
|
||||
sys.path.append("/home/humbug/humbug/api")
|
||||
import humbug
|
||||
client = humbug.Client(
|
||||
email="humbug+trac@humbughq.com",
|
||||
site="https://staging.humbughq.com",
|
||||
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
|
||||
|
||||
def markdown_ticket_url(ticket, heading="ticket"):
|
||||
return "[%s #%s](https://trac.humbughq.com/ticket/%s)" % (heading, ticket.id, ticket.id)
|
||||
|
||||
def markdown_block(desc):
|
||||
return "\n\n>" + "\n> ".join(desc.split("\n")) + "\n"
|
||||
|
||||
def truncate(string, length):
|
||||
if len(string) <= length:
|
||||
return string
|
||||
return string[:length - 3] + "..."
|
||||
|
||||
def trac_subject(ticket):
|
||||
return truncate("#%s: %s" % (ticket.id, ticket.values.get("summary")), 60)
|
||||
|
||||
def send_update(ticket, content):
|
||||
client.send_message({
|
||||
"type": "stream",
|
||||
"to": "trac",
|
||||
"content": content,
|
||||
"subject": trac_subject(ticket)
|
||||
})
|
||||
|
||||
class HumbugPlugin(Component):
|
||||
implements(ITicketChangeListener)
|
||||
|
||||
def ticket_created(self, ticket):
|
||||
"""Called when a ticket is created."""
|
||||
content = "%s created %s in component **%s**, priority **%s**:\n" % \
|
||||
(ticket.values.get("reporter"), markdown_ticket_url(ticket),
|
||||
ticket.values.get("component"), ticket.values.get("priority"))
|
||||
if ticket.values.get("description") != "":
|
||||
content += "%s" % markdown_block(ticket.values.get("description"))
|
||||
send_update(ticket, content)
|
||||
|
||||
def ticket_changed(self, ticket, comment, author, old_values):
|
||||
"""Called when a ticket is modified.
|
||||
|
||||
`old_values` is a dictionary containing the previous values of the
|
||||
fields that have changed.
|
||||
"""
|
||||
if not comment and set(old_values.keys()) <= set(["priority", "milestone",
|
||||
"cc", "keywords",
|
||||
"component"]):
|
||||
# This is probably someone going through trac and updating
|
||||
# the priorities; this can result in a lot of messages
|
||||
# nobody wants to read, so don't send them without a comment.
|
||||
return
|
||||
|
||||
content = "%s updated %s" % (author, markdown_ticket_url(ticket))
|
||||
if comment:
|
||||
content += ' with comment: %s\n\n' % (markdown_block(comment,))
|
||||
else:
|
||||
content += ":\n\n"
|
||||
field_changes = []
|
||||
for key in old_values.keys():
|
||||
if key == "description":
|
||||
content += '- Changed %s from %s to %s' % (key, markdown_block(old_values.get(key)),
|
||||
markdown_block(ticket.values.get(key)))
|
||||
elif old_values.get(key) == "":
|
||||
field_changes.append('%s: => **%s**' % (key, ticket.values.get(key)))
|
||||
elif ticket.values.get(key) == "":
|
||||
field_changes.append('%s: **%s** => ""' % (key, old_values.get(key)))
|
||||
else:
|
||||
field_changes.append('%s: **%s** => **%s**' % (key, old_values.get(key),
|
||||
ticket.values.get(key)))
|
||||
content += ", ".join(field_changes)
|
||||
|
||||
send_update(ticket, content)
|
||||
|
||||
def ticket_deleted(self, ticket):
|
||||
"""Called when a ticket is deleted."""
|
||||
content = "%s was deleted." % markdown_ticket_url(ticket, heading="Ticket")
|
||||
send_update(ticket, content)
|
|
@ -1,50 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
import sys
|
||||
import optparse
|
||||
from os import path
|
||||
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
# Nagios passes the notification details as command line options.
|
||||
# In Nagios, "output" means "first line of output", and "long
|
||||
# output" means "other lines of output".
|
||||
parser = optparse.OptionParser()
|
||||
parser.add_option('--output', default='')
|
||||
parser.add_option('--long-output', default='')
|
||||
for opt in ('type', 'host', 'service', 'state'):
|
||||
parser.add_option('--' + opt)
|
||||
(opts, args) = parser.parse_args()
|
||||
|
||||
msg = dict(type='stream', to='nagios')
|
||||
|
||||
# Set a subject based on the host or service in question. This enables
|
||||
# threaded discussion of multiple concurrent issues, and provides useful
|
||||
# context when narrowed.
|
||||
#
|
||||
# We send PROBLEM and RECOVERY messages to the same subject.
|
||||
if opts.service is None:
|
||||
# Host notification
|
||||
thing = 'host'
|
||||
msg['subject'] = 'host %s' % (opts.host,)
|
||||
else:
|
||||
# Service notification
|
||||
thing = 'service'
|
||||
msg['subject'] = 'service %s on %s' % (opts.service, opts.host)
|
||||
|
||||
# e.g. **PROBLEM**: service is CRITICAL
|
||||
msg['content'] = '**%s**: %s is %s' % (opts.type, thing, opts.state)
|
||||
|
||||
# The "long output" can contain newlines represented by "\n" escape sequences.
|
||||
# The Nagios mail command uses /usr/bin/printf "%b" to expand these.
|
||||
# We will be more conservative and handle just this one escape sequence.
|
||||
output = (opts.output + '\n' + opts.long_output.replace(r'\n', '\n')).strip()
|
||||
if output:
|
||||
# Put any command output in a code block.
|
||||
msg['content'] += ('\n\n~~~~\n' + output + "\n~~~~\n")
|
||||
|
||||
client = humbug.Client(
|
||||
email = 'humbug+nagios@humbughq.com',
|
||||
api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
||||
site = 'https://staging.humbughq.com')
|
||||
client.send_message(msg)
|
|
@ -1,68 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# Copyright (C) 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person
|
||||
# obtaining a copy of this software and associated documentation files
|
||||
# (the "Software"), to deal in the Software without restriction,
|
||||
# including without limitation the rights to use, copy, modify, merge,
|
||||
# publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
# and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
import sys
|
||||
from os import path, environ
|
||||
|
||||
|
||||
# Configure this script as a Tddium post-build task and it will send
|
||||
# messages to Humbug when a build finishes.
|
||||
#
|
||||
# Expects Tddium environment variables plus:
|
||||
#
|
||||
# HUMBUG_USER e.g. builds@solanolabs.com
|
||||
# HUMBUG_API_KEY e.g. 00000000000000000000000000000000
|
||||
# HUMBUG_STREAM e.g. builds
|
||||
#
|
||||
# If HUMBUG_API_KEY is not specified, it will be read from
|
||||
# ~/.humbug-api-key.
|
||||
|
||||
|
||||
# Path to the directory where humbug.py lives.
|
||||
# Here we assume it's in the parent of the directory
|
||||
# where this script lives.
|
||||
|
||||
humbug_directory = path.join(path.dirname(__file__), '..')
|
||||
|
||||
|
||||
sys.path.append(humbug_directory)
|
||||
import humbug
|
||||
|
||||
client = humbug.Client(
|
||||
email = environ['HUMBUG_USER'],
|
||||
api_key = environ.get('HUMBUG_API_KEY'))
|
||||
|
||||
tddium_server = environ.get('TDDIUM_API_SERVER', 'api.tddium.com')
|
||||
report_url = 'https://%s/1/reports/%s' % (tddium_server, environ['TDDIUM_SESSION_ID'])
|
||||
repo_name = path.basename(environ['TDDIUM_REPO_ROOT'])
|
||||
|
||||
result = client.send_message(dict(
|
||||
type = 'stream',
|
||||
to = environ['HUMBUG_STREAM'],
|
||||
subject = 'build for ' + repo_name,
|
||||
content = '%s [%s](%s)' %
|
||||
(repo_name, environ['TDDIUM_BUILD_STATUS'], report_url)))
|
||||
|
||||
if result['result'] != 'success':
|
||||
sys.stderr.write('Error sending to Humbug:\n%s\n' % (result['msg'],))
|
||||
sys.exit(1)
|
|
@ -1,147 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
import optparse
|
||||
import ConfigParser
|
||||
from os import path
|
||||
|
||||
CONFIGFILE = os.path.expanduser("~/.humbug_twitterrc")
|
||||
|
||||
sys.path.append(path.join(path.dirname(__file__), '../..'))
|
||||
import humbug
|
||||
|
||||
def write_config(config, since_id, user):
|
||||
config.set('twitter', 'since_id', since_id)
|
||||
config.set('twitter', 'user_id', user)
|
||||
with open(CONFIGFILE, 'wb') as configfile:
|
||||
config.write(configfile)
|
||||
|
||||
parser = optparse.OptionParser(r"""
|
||||
|
||||
%prog --user foo@humbughq.com --twitter_id twitter_handle
|
||||
|
||||
Slurp tweets on your timeline into a specific humbug stream.
|
||||
|
||||
Run this on your personal machine. Your API key and twitter id are revealed to local
|
||||
users through the command line or config file.
|
||||
|
||||
This bot uses OAuth to authenticate with twitter. Please create a ~/.humbug_twitterrc with
|
||||
the following contents:
|
||||
|
||||
[twitter]
|
||||
consumer_key =
|
||||
consumer_secret =
|
||||
access_token_key =
|
||||
access_token_secret =
|
||||
|
||||
In order to obtain a consumer key & secret, you must register a new application under your twitter account:
|
||||
|
||||
1. Go to http://dev.twitter.com
|
||||
2. Log in
|
||||
3. In the menu under your username, click My Applications
|
||||
4. Create a new application
|
||||
|
||||
Make sure to go the application you created and click "create my access token" as well. Fill in the values displayed.
|
||||
|
||||
Depends on: twitter-python
|
||||
""")
|
||||
|
||||
parser.add_option('--user',
|
||||
help='Humbug user email address',
|
||||
metavar='EMAIL')
|
||||
parser.add_option('--api-key',
|
||||
help='API key for that user [default: read ~/.humbugrc]')
|
||||
parser.add_option('--twitter-id',
|
||||
help='Twitter username to poll for new tweets from"',
|
||||
metavar='URL')
|
||||
parser.add_option('--site',
|
||||
default="https://humbughq.com",
|
||||
help='Humbug site [default: https://humbughq.com]',
|
||||
metavar='URL')
|
||||
parser.add_option('--stream',
|
||||
help='Default humbug stream to write tweets to')
|
||||
parser.add_option('--limit-tweets',
|
||||
default=15,
|
||||
type='int',
|
||||
help='Maximum number of tweets to push at once')
|
||||
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
if not options.twitter_id:
|
||||
parser.error('You must specify --twitter-id')
|
||||
|
||||
try:
|
||||
config = ConfigParser.ConfigParser()
|
||||
config.read(CONFIGFILE)
|
||||
|
||||
consumer_key = config.get('twitter', 'consumer_key')
|
||||
consumer_secret = config.get('twitter', 'consumer_secret')
|
||||
access_token_key = config.get('twitter', 'access_token_key')
|
||||
access_token_secret = config.get('twitter', 'access_token_secret')
|
||||
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
|
||||
parser.error("Please provide a ~/.humbug_twitterrc")
|
||||
|
||||
if not consumer_key or not consumer_secret or not access_token_key or not access_token_secret:
|
||||
parser.error("Please provide a ~/.humbug_twitterrc")
|
||||
|
||||
try:
|
||||
import twitter
|
||||
except ImportError:
|
||||
parser.error("Install twitter-python")
|
||||
|
||||
api = twitter.Api(consumer_key=consumer_key,
|
||||
consumer_secret=consumer_secret,
|
||||
access_token_key=access_token_key,
|
||||
access_token_secret=access_token_secret)
|
||||
|
||||
|
||||
user = api.VerifyCredentials()
|
||||
|
||||
if not user.GetId():
|
||||
print "Unable to log in to twitter with supplied credentials. Please double-check and try again"
|
||||
sys.exit()
|
||||
|
||||
try:
|
||||
since_id = config.getint('twitter', 'since_id')
|
||||
except ConfigParser.NoOptionError:
|
||||
since_id = -1
|
||||
|
||||
try:
|
||||
user_id = config.get('twitter', 'user_id')
|
||||
except ConfigParser.NoOptionError:
|
||||
user_id = options.twitter_id
|
||||
|
||||
client = humbug.Client(
|
||||
email=options.user,
|
||||
api_key=options.api_key,
|
||||
site=options.site,
|
||||
verbose=True)
|
||||
|
||||
if since_id < 0 or options.twitter_id != user_id:
|
||||
# No since id yet, fetch the latest and then start monitoring from next time
|
||||
# Or, a different user id is being asked for, so start from scratch
|
||||
# Either way, fetch last 5 tweets to start off
|
||||
statuses = api.GetFriendsTimeline(user=options.twitter_id, count=5)
|
||||
else:
|
||||
# We have a saved last id, so insert all newer tweets into the humbug stream
|
||||
statuses = api.GetFriendsTimeline(user=options.twitter_id, since_id=since_id)
|
||||
|
||||
for status in statuses[::-1][:options.limit_tweets]:
|
||||
composed = "%s (%s)" % (status.GetUser().GetName(), status.GetUser().GetScreenName())
|
||||
message = {
|
||||
"type": "stream",
|
||||
"to": [options.stream],
|
||||
"subject": composed,
|
||||
"content": status.GetText(),
|
||||
}
|
||||
|
||||
ret = client.send_message(message)
|
||||
|
||||
if ret['result'] == 'error':
|
||||
# If sending failed (e.g. no such stream), abort and retry next time
|
||||
print "Error sending message to humbug: %s" % ret['msg']
|
||||
break
|
||||
else:
|
||||
since_id = status.GetId()
|
||||
|
||||
write_config(config, since_id, user_id)
|
|
@ -1,26 +0,0 @@
|
|||
SHELL=/bin/bash
|
||||
# Edit this file to introduce tasks to be run by cron.
|
||||
#
|
||||
# Each task to run has to be defined through a single line
|
||||
# indicating with different fields when the task will be run
|
||||
# and what command to run for the task
|
||||
#
|
||||
# To define the time you can provide concrete values for
|
||||
# minute (m), hour (h), day of month (dom), month (mon),
|
||||
# and day of week (dow) or use '*' in these fields (for 'any').
|
||||
#
|
||||
# Notice that tasks will be started based on the cron's system
|
||||
# daemon's notion of time and timezones.
|
||||
#
|
||||
# Output of the crontab jobs (including errors) is sent through
|
||||
# email to the user the crontab file belongs to (unless redirected).
|
||||
#
|
||||
# For example, you can run a backup of all your user accounts
|
||||
# at 5 a.m every week with:
|
||||
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
|
||||
#
|
||||
# For more information see the manual pages of crontab(5) and cron(8)
|
||||
#
|
||||
# m h dom mon dow command
|
||||
35 * * * * env KRB5CCNAME=/tmp/krb5cc_1000.tmp kinit -k -t /home/humbug/tabbott.extra.keytab tabbott/extra@ATHENA.MIT.EDU; mv /tmp/krb5cc_1000.tmp /tmp/krb5cc_1000
|
||||
*/2 * * * * /home/humbug/api/bots/check-mirroring &> /var/run/nagios/check-mirroring-results-tmp; mv /var/run/nagios/check-mirroring-results-tmp /var/run/nagios/check-mirroring-results
|
|
@ -1,61 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# Copyright (C) 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person
|
||||
# obtaining a copy of this software and associated documentation files
|
||||
# (the "Software"), to deal in the Software without restriction,
|
||||
# including without limitation the rights to use, copy, modify, merge,
|
||||
# publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
# and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
import sys
|
||||
import subprocess
|
||||
import time
|
||||
import optparse
|
||||
import os
|
||||
import traceback
|
||||
|
||||
from zephyr_mirror_backend import parse_args
|
||||
|
||||
(options, args) = parse_args()
|
||||
|
||||
args = [os.path.join(options.root_path, "user_root", "zephyr_mirror_backend.py")]
|
||||
args.extend(sys.argv[1:])
|
||||
|
||||
if options.sync_subscriptions:
|
||||
subprocess.call(args)
|
||||
sys.exit(0)
|
||||
|
||||
if options.forward_class_messages and not options.noshard:
|
||||
sys.path.append("/home/humbug/humbug")
|
||||
from zephyr.lib.parallel import run_parallel
|
||||
print "Starting parallel zephyr class mirroring bot"
|
||||
jobs = list("0123456789abcdef")
|
||||
def run_job(shard):
|
||||
subprocess.call(args + ["--shard=%s" % (shard,)])
|
||||
return 0
|
||||
for (status, job) in run_parallel(run_job, jobs, threads=16):
|
||||
print "A mirroring shard died!"
|
||||
pass
|
||||
sys.exit(0)
|
||||
|
||||
while True:
|
||||
print "Starting zephyr mirroring bot"
|
||||
try:
|
||||
subprocess.call(args)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
time.sleep(1)
|
|
@ -1,893 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# Copyright (C) 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person
|
||||
# obtaining a copy of this software and associated documentation files
|
||||
# (the "Software"), to deal in the Software without restriction,
|
||||
# including without limitation the rights to use, copy, modify, merge,
|
||||
# publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
# and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
import sys
|
||||
import traceback
|
||||
import simplejson
|
||||
import re
|
||||
import time
|
||||
import subprocess
|
||||
import optparse
|
||||
import os
|
||||
import datetime
|
||||
import textwrap
|
||||
import signal
|
||||
import logging
|
||||
import hashlib
|
||||
import unicodedata
|
||||
import tempfile
|
||||
|
||||
DEFAULT_SITE = "https://humbughq.com"
|
||||
|
||||
def to_humbug_username(zephyr_username):
|
||||
if "@" in zephyr_username:
|
||||
(user, realm) = zephyr_username.split("@")
|
||||
else:
|
||||
(user, realm) = (zephyr_username, "ATHENA.MIT.EDU")
|
||||
if realm.upper() == "ATHENA.MIT.EDU":
|
||||
return user.lower() + "@mit.edu"
|
||||
return user.lower() + "|" + realm.upper() + "@mit.edu"
|
||||
|
||||
def to_zephyr_username(humbug_username):
|
||||
(user, realm) = humbug_username.split("@")
|
||||
if "|" not in user:
|
||||
return user.lower() + "@ATHENA.MIT.EDU"
|
||||
match_user = re.match(r'([a-zA-Z0-9_]+)\|(.+)', user)
|
||||
if not match_user:
|
||||
raise Exception("Could not parse Zephyr realm for cross-realm user %s" % (humbug_username,))
|
||||
return match_user.group(1).lower() + "@" + match_user.group(2).upper()
|
||||
|
||||
# Checks whether the pair of adjacent lines would have been
|
||||
# linewrapped together, had they been intended to be parts of the same
|
||||
# paragraph. Our check is whether if you move the first word on the
|
||||
# 2nd line onto the first line, the resulting line is either (1)
|
||||
# significantly shorter than the following line (which, if they were
|
||||
# in the same paragraph, should have been wrapped in a way consistent
|
||||
# with how the previous line was wrapped) or (2) shorter than 60
|
||||
# characters (our assumed minimum linewrapping threshhold for Zephyr)
|
||||
# or (3) the first word of the next line is longer than this entire
|
||||
# line.
|
||||
def different_paragraph(line, next_line):
|
||||
words = next_line.split()
|
||||
return (len(line + " " + words[0]) < len(next_line) * 0.8 or
|
||||
len(line + " " + words[0]) < 50 or
|
||||
len(line) < len(words[0]))
|
||||
|
||||
# Linewrapping algorithm based on:
|
||||
# http://gcbenison.wordpress.com/2011/07/03/a-program-to-intelligently-remove-carriage-returns-so-you-can-paste-text-without-having-it-look-awful/
|
||||
def unwrap_lines(body):
|
||||
lines = body.split("\n")
|
||||
result = ""
|
||||
previous_line = lines[0]
|
||||
for line in lines[1:]:
|
||||
line = line.rstrip()
|
||||
if (re.match(r'^\W', line, flags=re.UNICODE)
|
||||
and re.match(r'^\W', previous_line, flags=re.UNICODE)):
|
||||
result += previous_line + "\n"
|
||||
elif (line == "" or
|
||||
previous_line == "" or
|
||||
re.match(r'^\W', line, flags=re.UNICODE) or
|
||||
different_paragraph(previous_line, line)):
|
||||
# Use 2 newlines to separate sections so that we
|
||||
# trigger proper Markdown processing on things like
|
||||
# bulleted lists
|
||||
result += previous_line + "\n\n"
|
||||
else:
|
||||
result += previous_line + " "
|
||||
previous_line = line
|
||||
result += previous_line
|
||||
return result
|
||||
|
||||
def send_humbug(zeph):
|
||||
message = {}
|
||||
if options.forward_class_messages:
|
||||
message["forged"] = "yes"
|
||||
message['type'] = zeph['type']
|
||||
message['time'] = zeph['time']
|
||||
message['sender'] = to_humbug_username(zeph['sender'])
|
||||
if "subject" in zeph:
|
||||
# Truncate the subject to the current limit in Humbug. No
|
||||
# need to do this for stream names, since we're only
|
||||
# subscribed to valid stream names.
|
||||
message["subject"] = zeph["subject"][:60]
|
||||
if zeph['type'] == 'stream':
|
||||
# Forward messages sent to -c foo -i bar to stream bar subject "instance"
|
||||
if zeph["stream"] == "message":
|
||||
message['to'] = zeph['subject'].lower()
|
||||
message['subject'] = "instance %s" % (zeph['subject'],)
|
||||
elif zeph["stream"] == "tabbott-test5":
|
||||
message['to'] = zeph['subject'].lower()
|
||||
message['subject'] = "test instance %s" % (zeph['subject'],)
|
||||
else:
|
||||
message["to"] = zeph["stream"]
|
||||
else:
|
||||
message["to"] = zeph["recipient"]
|
||||
message['content'] = unwrap_lines(zeph['content'])
|
||||
|
||||
if options.test_mode and options.site == DEFAULT_SITE:
|
||||
logger.debug("Message is: %s" % (str(message),))
|
||||
return {'result': "success"}
|
||||
|
||||
return humbug_client.send_message(message)
|
||||
|
||||
def send_error_humbug(error_msg):
|
||||
message = {"type": "private",
|
||||
"sender": humbug_account_email,
|
||||
"to": humbug_account_email,
|
||||
"content": error_msg,
|
||||
}
|
||||
humbug_client.send_message(message)
|
||||
|
||||
current_zephyr_subs = set()
|
||||
def zephyr_bulk_subscribe(subs):
|
||||
try:
|
||||
zephyr._z.subAll(subs)
|
||||
except IOError:
|
||||
# Since we haven't added the subscription to
|
||||
# current_zephyr_subs yet, we can just return (so that we'll
|
||||
# continue processing normal messages) and we'll end up
|
||||
# retrying the next time the bot checks its subscriptions are
|
||||
# up to date.
|
||||
logger.exception("Error subscribing to streams (will retry automatically):")
|
||||
logging.warning("Streams were: %s" % ([cls for cls, instance, recipient in subs],))
|
||||
return
|
||||
try:
|
||||
actual_zephyr_subs = [cls for (cls, _, _) in zephyr._z.getSubscriptions()]
|
||||
except IOError:
|
||||
logging.exception("Error getting current Zephyr subscriptions")
|
||||
# Don't add anything to current_zephyr_subs so that we'll
|
||||
# retry the next time we check for streams to subscribe to
|
||||
# (within 15 seconds).
|
||||
return
|
||||
for (cls, instance, recipient) in subs:
|
||||
if cls not in actual_zephyr_subs:
|
||||
logging.error("Zephyr failed to subscribe us to %s; will retry" % (cls,))
|
||||
try:
|
||||
# We'll retry automatically when we next check for
|
||||
# streams to subscribe to (within 15 seconds), but
|
||||
# it's worth doing 1 retry immediately to avoid
|
||||
# missing 15 seconds of messages on the affected
|
||||
# classes
|
||||
zephyr._z.sub(cls, instance, recipient)
|
||||
except IOError:
|
||||
pass
|
||||
else:
|
||||
current_zephyr_subs.add(cls)
|
||||
|
||||
def update_subscriptions_from_humbug():
|
||||
try:
|
||||
res = humbug_client.get_public_streams()
|
||||
if res.get("result") == "success":
|
||||
streams = res["streams"]
|
||||
else:
|
||||
logger.error("Error getting public streams:\n%s" % res)
|
||||
return
|
||||
except Exception:
|
||||
logger.exception("Error getting public streams:")
|
||||
return
|
||||
classes_to_subscribe = set()
|
||||
for stream in streams:
|
||||
# Zephyr class names are canonicalized by first applying NFKC
|
||||
# normalization and then lower-casing server-side
|
||||
canonical_cls = unicodedata.normalize("NFKC", stream).lower().encode("utf-8")
|
||||
if canonical_cls in current_zephyr_subs:
|
||||
continue
|
||||
if canonical_cls in ['security', 'login', 'network', 'ops', 'user_locate',
|
||||
'mit',
|
||||
'hm_ctl', 'hm_stat', 'zephyr_admin', 'zephyr_ctl']:
|
||||
# These zephyr classes cannot be subscribed to by us, due
|
||||
# to MIT's Zephyr access control settings
|
||||
continue
|
||||
if (options.shard is not None and
|
||||
not hashlib.sha1(canonical_cls).hexdigest().startswith(options.shard)):
|
||||
# This stream is being handled by a different zephyr_mirror job.
|
||||
continue
|
||||
|
||||
classes_to_subscribe.add((canonical_cls, "*", "*"))
|
||||
if len(classes_to_subscribe) > 0:
|
||||
zephyr_bulk_subscribe(list(classes_to_subscribe))
|
||||
|
||||
def maybe_restart_mirroring_script():
|
||||
if os.stat(os.path.join(options.root_path, "stamps", "restart_stamp")).st_mtime > start_time or \
|
||||
((options.user == "tabbott" or options.user == "tabbott/extra") and
|
||||
os.stat(os.path.join(options.root_path, "stamps", "tabbott_stamp")).st_mtime > start_time):
|
||||
logger.warning("")
|
||||
logger.warning("zephyr mirroring script has been updated; restarting...")
|
||||
try:
|
||||
if child_pid is not None:
|
||||
os.kill(child_pid, signal.SIGTERM)
|
||||
except OSError:
|
||||
# We don't care if the child process no longer exists, so just print the error
|
||||
logging.exception("")
|
||||
try:
|
||||
zephyr._z.cancelSubs()
|
||||
except IOError:
|
||||
# We don't care whether we failed to cancel subs properly, but we should log it
|
||||
logging.exception("")
|
||||
while True:
|
||||
try:
|
||||
os.execvp(os.path.join(options.root_path, "user_root", "zephyr_mirror_backend.py"), sys.argv)
|
||||
except Exception:
|
||||
logger.exception("Error restarting mirroring script; trying again... Traceback:")
|
||||
time.sleep(1)
|
||||
|
||||
def process_loop(log):
|
||||
sleep_count = 0
|
||||
sleep_time = 0.1
|
||||
while True:
|
||||
try:
|
||||
notice = zephyr.receive(block=False)
|
||||
except Exception:
|
||||
logger.exception("Error checking for new zephyrs:")
|
||||
time.sleep(1)
|
||||
continue
|
||||
if notice is not None:
|
||||
try:
|
||||
process_notice(notice, log)
|
||||
except Exception:
|
||||
logger.exception("Error relaying zephyr:")
|
||||
time.sleep(2)
|
||||
|
||||
try:
|
||||
maybe_restart_mirroring_script()
|
||||
except Exception:
|
||||
logging.exception("Error checking whether restart is required:")
|
||||
|
||||
time.sleep(sleep_time)
|
||||
sleep_count += sleep_time
|
||||
if sleep_count > 15:
|
||||
sleep_count = 0
|
||||
if options.forward_class_messages:
|
||||
# Ask the Humbug server about any new classes to subscribe to
|
||||
try:
|
||||
update_subscriptions_from_humbug()
|
||||
except Exception:
|
||||
logging.exception("Error updating subscriptions from Humbug:")
|
||||
|
||||
def parse_zephyr_body(zephyr_data):
|
||||
try:
|
||||
(zsig, body) = zephyr_data.split("\x00", 1)
|
||||
except ValueError:
|
||||
(zsig, body) = ("", zephyr_data)
|
||||
return (zsig, body)
|
||||
|
||||
def process_notice(notice, log):
|
||||
(zsig, body) = parse_zephyr_body(notice.message)
|
||||
is_personal = False
|
||||
is_huddle = False
|
||||
|
||||
if notice.opcode == "PING":
|
||||
# skip PING messages
|
||||
return
|
||||
|
||||
zephyr_class = notice.cls.lower()
|
||||
|
||||
if notice.recipient != "":
|
||||
is_personal = True
|
||||
# Drop messages not to the listed subscriptions
|
||||
if is_personal and not options.forward_personals:
|
||||
return
|
||||
if (zephyr_class not in current_zephyr_subs) and not is_personal:
|
||||
logger.debug("Skipping ... %s/%s/%s" %
|
||||
(zephyr_class, notice.instance, is_personal))
|
||||
return
|
||||
if notice.format.endswith("@(@color(blue))"):
|
||||
logger.debug("Skipping message we got from Humbug!")
|
||||
return
|
||||
|
||||
if is_personal:
|
||||
if body.startswith("CC:"):
|
||||
is_huddle = True
|
||||
# Map "CC: sipbtest espuser" => "starnine@mit.edu,espuser@mit.edu"
|
||||
huddle_recipients = [to_humbug_username(x.strip()) for x in
|
||||
body.split("\n")[0][4:].split()]
|
||||
if notice.sender not in huddle_recipients:
|
||||
huddle_recipients.append(to_humbug_username(notice.sender))
|
||||
body = body.split("\n", 1)[1]
|
||||
|
||||
zeph = { 'time' : str(notice.time),
|
||||
'sender' : notice.sender,
|
||||
'zsig' : zsig, # logged here but not used by app
|
||||
'content' : body }
|
||||
if is_huddle:
|
||||
zeph['type'] = 'private'
|
||||
zeph['recipient'] = huddle_recipients
|
||||
elif is_personal:
|
||||
zeph['type'] = 'private'
|
||||
zeph['recipient'] = to_humbug_username(notice.recipient)
|
||||
else:
|
||||
zeph['type'] = 'stream'
|
||||
zeph['stream'] = zephyr_class
|
||||
if notice.instance.strip() != "":
|
||||
zeph['subject'] = notice.instance
|
||||
else:
|
||||
zeph["subject"] = '(instance "%s")' % (notice.instance,)
|
||||
|
||||
# Add instances in for instanced personals
|
||||
if is_personal:
|
||||
if notice.cls.lower() != "message" and notice.instance.lower != "personal":
|
||||
heading = "[-c %s -i %s]\n" % (notice.cls, notice.instance)
|
||||
elif notice.cls.lower() != "message":
|
||||
heading = "[-c %s]\n" % (notice.cls,)
|
||||
elif notice.instance.lower() != "personal":
|
||||
heading = "[-i %s]\n" % (notice.instance,)
|
||||
else:
|
||||
heading = ""
|
||||
zeph["content"] = heading + zeph["content"]
|
||||
|
||||
zeph = decode_unicode_byte_strings(zeph)
|
||||
|
||||
logger.info("Received a message on %s/%s from %s..." %
|
||||
(zephyr_class, notice.instance, notice.sender))
|
||||
if log is not None:
|
||||
log.write(simplejson.dumps(zeph) + '\n')
|
||||
log.flush()
|
||||
|
||||
if os.fork() == 0:
|
||||
# Actually send the message in a child process, to avoid blocking.
|
||||
try:
|
||||
res = send_humbug(zeph)
|
||||
if res.get("result") != "success":
|
||||
logger.error("Error relaying zephyr:\n%s\n%s" % (zeph, res))
|
||||
except Exception:
|
||||
logging.exception("Error relaying zephyr:")
|
||||
finally:
|
||||
os._exit(0)
|
||||
|
||||
def decode_unicode_byte_strings(zeph):
|
||||
for field in zeph.keys():
|
||||
if isinstance(zeph[field], str):
|
||||
try:
|
||||
decoded = zeph[field].decode("utf-8")
|
||||
except Exception:
|
||||
decoded = zeph[field].decode("iso-8859-1")
|
||||
zeph[field] = decoded
|
||||
return zeph
|
||||
|
||||
def zephyr_subscribe_autoretry(sub):
|
||||
while True:
|
||||
try:
|
||||
zephyr.Subscriptions().add(sub)
|
||||
return
|
||||
except IOError:
|
||||
# Probably a SERVNAK from the zephyr server, but print the
|
||||
# traceback just in case it's something else
|
||||
logger.exception("Error subscribing to personals (retrying). Traceback:")
|
||||
time.sleep(1)
|
||||
|
||||
def zephyr_to_humbug(options):
|
||||
if options.forward_class_messages:
|
||||
update_subscriptions_from_humbug()
|
||||
if options.forward_personals:
|
||||
# Subscribe to personals; we really can't operate without
|
||||
# those subscriptions, so just retry until it works.
|
||||
zephyr_subscribe_autoretry(("message", "*", "%me%"))
|
||||
if subscribed_to_mail_messages():
|
||||
zephyr_subscribe_autoretry(("mail", "inbox", "%me%"))
|
||||
|
||||
if options.resend_log_path is not None:
|
||||
with open(options.resend_log_path, 'r') as log:
|
||||
for ln in log:
|
||||
try:
|
||||
zeph = simplejson.loads(ln)
|
||||
# New messages added to the log shouldn't have any
|
||||
# elements of type str (they should already all be
|
||||
# unicode), but older messages in the log are
|
||||
# still of type str, so convert them before we
|
||||
# send the message
|
||||
zeph = decode_unicode_byte_strings(zeph)
|
||||
# Handle importing older zephyrs in the logs
|
||||
# where it isn't called a "stream" yet
|
||||
if "class" in zeph:
|
||||
zeph["stream"] = zeph["class"]
|
||||
if "instance" in zeph:
|
||||
zeph["subject"] = zeph["instance"]
|
||||
logger.info("sending saved message to %s from %s..." %
|
||||
(zeph.get('stream', zeph.get('recipient')),
|
||||
zeph['sender']))
|
||||
send_humbug(zeph)
|
||||
except Exception:
|
||||
logger.exception("Could not send saved zephyr:")
|
||||
time.sleep(2)
|
||||
|
||||
logger.info("Starting receive loop.")
|
||||
|
||||
if options.log_path is not None:
|
||||
with open(options.log_path, 'a') as log:
|
||||
process_loop(log)
|
||||
else:
|
||||
process_loop(None)
|
||||
|
||||
def send_zephyr(zwrite_args, content):
|
||||
p = subprocess.Popen(zwrite_args, stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate(input=content.encode("utf-8"))
|
||||
if p.returncode:
|
||||
logging.error("zwrite command '%s' failed with return code %d:" % (
|
||||
" ".join(zwrite_args), p.returncode,))
|
||||
if stdout:
|
||||
logging.info("stdout: " + stdout)
|
||||
elif stderr:
|
||||
logging.warning("zwrite command '%s' printed the following warning:" % (
|
||||
" ".join(zwrite_args),))
|
||||
if stderr:
|
||||
logging.warning("stderr: " + stderr)
|
||||
return (p.returncode, stderr)
|
||||
|
||||
def send_authed_zephyr(zwrite_args, content):
|
||||
return send_zephyr(zwrite_args, content)
|
||||
|
||||
def send_unauthed_zephyr(zwrite_args, content):
|
||||
return send_zephyr(zwrite_args + ["-d"], content)
|
||||
|
||||
def forward_to_zephyr(message):
|
||||
wrapper = textwrap.TextWrapper(break_long_words=False, break_on_hyphens=False)
|
||||
wrapped_content = "\n".join("\n".join(wrapper.wrap(line))
|
||||
for line in message["content"].split("\n"))
|
||||
|
||||
zwrite_args = ["zwrite", "-n", "-s", zsig_fullname, "-F", "http://zephyr.1ts.org/wiki/df @(@color(blue))"]
|
||||
if message['type'] == "stream":
|
||||
zephyr_class = message["display_recipient"]
|
||||
instance = message["subject"]
|
||||
|
||||
match_whitespace_instance = re.match(r'^\(instance "(\s*)"\)$', instance)
|
||||
if match_whitespace_instance:
|
||||
# Forward messages sent to '(instance "WHITESPACE")' back to the
|
||||
# appropriate WHITESPACE instance for bidirectional mirroring
|
||||
instance = match_whitespace_instance.group(1)
|
||||
elif (instance == "instance %s" % (zephyr_class,) or
|
||||
instance == "test instance %s" % (zephyr_class,)):
|
||||
# Forward messages to e.g. -c -i white-magic back from the
|
||||
# place we forward them to
|
||||
if instance.startswith("test"):
|
||||
instance = zephyr_class
|
||||
zephyr_class = "tabbott-test5"
|
||||
else:
|
||||
instance = zephyr_class
|
||||
zephyr_class = "message"
|
||||
zwrite_args.extend(["-c", zephyr_class, "-i", instance])
|
||||
logger.info("Forwarding message to class %s, instance %s" % (zephyr_class, instance))
|
||||
elif message['type'] == "private":
|
||||
if len(message['display_recipient']) == 1:
|
||||
recipient = to_zephyr_username(message["display_recipient"][0]["email"])
|
||||
recipients = [recipient]
|
||||
elif len(message['display_recipient']) == 2:
|
||||
recipient = ""
|
||||
for r in message["display_recipient"]:
|
||||
if r["email"].lower() != humbug_account_email.lower():
|
||||
recipient = to_zephyr_username(r["email"])
|
||||
break
|
||||
recipients = [recipient]
|
||||
else:
|
||||
zwrite_args.extend(["-C"])
|
||||
# We drop the @ATHENA.MIT.EDU here because otherwise the
|
||||
# "CC: user1 user2 ..." output will be unnecessarily verbose.
|
||||
recipients = [to_zephyr_username(user["email"]).replace("@ATHENA.MIT.EDU", "")
|
||||
for user in message["display_recipient"]]
|
||||
logger.info("Forwarding message to %s" % (recipients,))
|
||||
zwrite_args.extend(recipients)
|
||||
|
||||
if options.test_mode:
|
||||
logger.debug("Would have forwarded: %s\n%s" %
|
||||
(zwrite_args, wrapped_content.encode("utf-8")))
|
||||
return
|
||||
|
||||
heading = "Hi there! This is an automated message from Humbug."
|
||||
support_closing = """If you have any questions, please be in touch through the \
|
||||
Feedback tab or at support@humbughq.com."""
|
||||
|
||||
(code, stderr) = send_authed_zephyr(zwrite_args, wrapped_content)
|
||||
if code == 0 and stderr == "":
|
||||
return
|
||||
elif code == 0:
|
||||
return send_error_humbug("""%s
|
||||
|
||||
Your last message was successfully mirrored to zephyr, but zwrite \
|
||||
returned the following warning:
|
||||
|
||||
%s
|
||||
|
||||
%s""" % (heading, stderr, support_closing))
|
||||
elif code != 0 and (stderr.startswith("zwrite: Ticket expired while sending notice to ") or
|
||||
stderr.startswith("zwrite: No credentials cache found while sending notice to ")):
|
||||
# Retry sending the message unauthenticated; if that works,
|
||||
# just notify the user that they need to renew their tickets
|
||||
(code, stderr) = send_unauthed_zephyr(zwrite_args, wrapped_content)
|
||||
if code == 0:
|
||||
return send_error_humbug("""%s
|
||||
|
||||
Your last message was forwarded from Humbug to Zephyr unauthenticated, \
|
||||
because your Kerberos tickets have expired. It was sent successfully, \
|
||||
but please renew your Kerberos tickets in the screen session where you \
|
||||
are running the Humbug-Zephyr mirroring bot, so we can send \
|
||||
authenticated Zephyr messages for you again.
|
||||
|
||||
%s""" % (heading, support_closing))
|
||||
|
||||
# zwrite failed and it wasn't because of expired tickets: This is
|
||||
# probably because the recipient isn't subscribed to personals,
|
||||
# but regardless, we should just notify the user.
|
||||
return send_error_humbug("""%s
|
||||
|
||||
Your Humbug-Zephyr mirror bot was unable to forward that last message \
|
||||
from Humbug to Zephyr. That means that while Humbug users (like you) \
|
||||
received it, Zephyr users did not. The error message from zwrite was:
|
||||
|
||||
%s
|
||||
|
||||
%s""" % (heading, stderr, support_closing))
|
||||
|
||||
def maybe_forward_to_zephyr(message):
|
||||
if (message["sender_email"] == humbug_account_email):
|
||||
if not ((message["type"] == "stream") or
|
||||
(message["type"] == "private" and
|
||||
False not in [u["email"].lower().endswith("mit.edu") for u in
|
||||
message["display_recipient"]])):
|
||||
# Don't try forward private messages with non-MIT users
|
||||
# to MIT Zephyr.
|
||||
return
|
||||
timestamp_now = datetime.datetime.now().strftime("%s")
|
||||
if float(message["timestamp"]) < float(timestamp_now) - 15:
|
||||
logger.warning("Skipping out of order message: %s < %s" %
|
||||
(message["timestamp"], timestamp_now))
|
||||
return
|
||||
try:
|
||||
forward_to_zephyr(message)
|
||||
except Exception:
|
||||
# Don't let an exception forwarding one message crash the
|
||||
# whole process
|
||||
logger.exception("Error forwarding message:")
|
||||
|
||||
def humbug_to_zephyr(options):
|
||||
# Sync messages from zephyr to humbug
|
||||
logger.info("Starting syncing messages.")
|
||||
while True:
|
||||
try:
|
||||
humbug_client.call_on_each_message(maybe_forward_to_zephyr)
|
||||
except Exception:
|
||||
logger.exception("Error syncing messages:")
|
||||
time.sleep(1)
|
||||
|
||||
def subscribed_to_mail_messages():
|
||||
# In case we have lost our AFS tokens and those won't be able to
|
||||
# parse the Zephyr subs file, first try reading in result of this
|
||||
# query from the environment so we can avoid the filesystem read.
|
||||
stored_result = os.environ.get("HUMBUG_FORWARD_MAIL_ZEPHYRS")
|
||||
if stored_result is not None:
|
||||
return stored_result == "True"
|
||||
for (cls, instance, recipient) in parse_zephyr_subs(verbose=False):
|
||||
if (cls.lower() == "mail" and instance.lower() == "inbox"):
|
||||
os.environ["HUMBUG_FORWARD_MAIL_ZEPHYRS"] = "True"
|
||||
return True
|
||||
os.environ["HUMBUG_FORWARD_MAIL_ZEPHYRS"] = "False"
|
||||
return False
|
||||
|
||||
def add_humbug_subscriptions(verbose):
|
||||
zephyr_subscriptions = set()
|
||||
skipped = set()
|
||||
for (cls, instance, recipient) in parse_zephyr_subs(verbose=verbose):
|
||||
if cls.lower() == "message":
|
||||
if recipient != "*":
|
||||
# We already have a (message, *, you) subscription, so
|
||||
# these are redundant
|
||||
continue
|
||||
# We don't support subscribing to (message, *)
|
||||
if instance == "*":
|
||||
if recipient == "*":
|
||||
skipped.add((cls, instance, recipient, "subscribing to all of class message is not supported."))
|
||||
continue
|
||||
# If you're on -i white-magic on zephyr, get on stream white-magic on humbug
|
||||
# instead of subscribing to stream "message" on humbug
|
||||
zephyr_subscriptions.add(instance)
|
||||
continue
|
||||
elif cls.lower() == "mail" and instance.lower() == "inbox":
|
||||
# We forward mail zephyrs, so no need to print a warning.
|
||||
continue
|
||||
elif len(cls) > 30:
|
||||
skipped.add((cls, instance, recipient, "Class longer than 30 characters"))
|
||||
continue
|
||||
elif instance != "*":
|
||||
skipped.add((cls, instance, recipient, "Unsupported non-* instance"))
|
||||
continue
|
||||
elif recipient != "*":
|
||||
skipped.add((cls, instance, recipient, "Unsupported non-* recipient."))
|
||||
continue
|
||||
zephyr_subscriptions.add(cls)
|
||||
|
||||
if len(zephyr_subscriptions) != 0:
|
||||
res = humbug_client.add_subscriptions(list(zephyr_subscriptions))
|
||||
if res.get("result") != "success":
|
||||
print "Error subscribing to streams:"
|
||||
print res["msg"]
|
||||
return
|
||||
|
||||
already = res.get("already_subscribed")
|
||||
new = res.get("subscribed")
|
||||
if verbose:
|
||||
if already is not None and len(already) > 0:
|
||||
print
|
||||
print "Already subscribed to:", ", ".join(already)
|
||||
if new is not None and len(new) > 0:
|
||||
print
|
||||
print "Successfully subscribed to:", ", ".join(new)
|
||||
|
||||
if len(skipped) > 0:
|
||||
if verbose:
|
||||
print
|
||||
print "\n".join(textwrap.wrap("""\
|
||||
You have some lines in ~/.zephyr.subs that could not be
|
||||
synced to your Humbug subscriptions because they do not
|
||||
use "*" as both the instance and recipient and not one of
|
||||
the special cases (e.g. personals and mail zephyrs) that
|
||||
Humbug has a mechanism for forwarding. Humbug does not
|
||||
allow subscribing to only some subjects on a Humbug
|
||||
stream, so this tool has not created a corresponding
|
||||
Humbug subscription to these lines in ~/.zephyr.subs:
|
||||
"""))
|
||||
print
|
||||
|
||||
for (cls, instance, recipient, reason) in skipped:
|
||||
if verbose:
|
||||
if reason != "":
|
||||
print " [%s,%s,%s] (%s)" % (cls, instance, recipient, reason)
|
||||
else:
|
||||
print " [%s,%s,%s]" % (cls, instance, recipient, reason)
|
||||
if len(skipped) > 0:
|
||||
if verbose:
|
||||
print
|
||||
print "\n".join(textwrap.wrap("""\
|
||||
If you wish to be subscribed to any Humbug streams related
|
||||
to these .zephyrs.subs lines, please do so via the Humbug
|
||||
web interface.
|
||||
"""))
|
||||
print
|
||||
if verbose:
|
||||
print
|
||||
print "IMPORTANT: Please reload the Humbug app for these changes to take effect."
|
||||
|
||||
def valid_stream_name(name):
|
||||
return name != ""
|
||||
|
||||
def parse_zephyr_subs(verbose=False):
|
||||
zephyr_subscriptions = set()
|
||||
subs_file = os.path.join(os.environ["HOME"], ".zephyr.subs")
|
||||
if not os.path.exists(subs_file):
|
||||
if verbose:
|
||||
print >>sys.stderr, "Couldn't find ~/.zephyr.subs!"
|
||||
return []
|
||||
|
||||
for line in file(subs_file, "r").readlines():
|
||||
line = line.strip()
|
||||
if len(line) == 0:
|
||||
continue
|
||||
try:
|
||||
(cls, instance, recipient) = line.split(",")
|
||||
cls = cls.replace("%me%", options.user)
|
||||
instance = instance.replace("%me%", options.user)
|
||||
recipient = recipient.replace("%me%", options.user)
|
||||
if not valid_stream_name(cls):
|
||||
if verbose:
|
||||
print >>sys.stderr, "Skipping subscription to unsupported class name: [%s]" % (line,)
|
||||
continue
|
||||
except Exception:
|
||||
if verbose:
|
||||
print >>sys.stderr, "Couldn't parse ~/.zephyr.subs line: [%s]" % (line,)
|
||||
continue
|
||||
zephyr_subscriptions.add((cls.strip(), instance.strip(), recipient.strip()))
|
||||
return zephyr_subscriptions
|
||||
|
||||
def fetch_fullname(username):
|
||||
try:
|
||||
proc = subprocess.Popen(['hesinfo', username, 'passwd'],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
out, _err_unused = proc.communicate()
|
||||
if proc.returncode == 0:
|
||||
return out.split(':')[4].split(',')[0]
|
||||
except Exception:
|
||||
logger.exception("Error getting fullname for %s:" % (username,))
|
||||
|
||||
return username
|
||||
|
||||
def configure_logger(direction_name):
|
||||
if options.forward_class_messages:
|
||||
if options.test_mode:
|
||||
log_file = "/home/humbug/test-mirror-log"
|
||||
else:
|
||||
log_file = "/home/humbug/mirror-log"
|
||||
else:
|
||||
f = tempfile.NamedTemporaryFile(prefix="humbug-log.%s." % (options.user,),
|
||||
delete=False)
|
||||
log_file = f.name
|
||||
# Close the file descriptor, since the logging system will
|
||||
# reopen it anyway.
|
||||
f.close()
|
||||
logger = logging.getLogger(__name__)
|
||||
log_format = "%(asctime)s " + direction_name + ": %(message)s"
|
||||
formatter = logging.Formatter(log_format)
|
||||
logging.basicConfig(format=log_format)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
file_handler = logging.FileHandler(log_file)
|
||||
file_handler.setFormatter(formatter)
|
||||
logger.addHandler(file_handler)
|
||||
return logger
|
||||
|
||||
def parse_args():
|
||||
parser = optparse.OptionParser()
|
||||
parser.add_option('--forward-class-messages',
|
||||
default=False,
|
||||
help=optparse.SUPPRESS_HELP,
|
||||
action='store_true')
|
||||
parser.add_option('--shard',
|
||||
help=optparse.SUPPRESS_HELP)
|
||||
parser.add_option('--noshard',
|
||||
default=False,
|
||||
help=optparse.SUPPRESS_HELP,
|
||||
action='store_true')
|
||||
parser.add_option('--resend-log',
|
||||
dest='resend_log_path',
|
||||
help=optparse.SUPPRESS_HELP)
|
||||
parser.add_option('--enable-log',
|
||||
dest='log_path',
|
||||
help=optparse.SUPPRESS_HELP)
|
||||
parser.add_option('--no-forward-personals',
|
||||
dest='forward_personals',
|
||||
help=optparse.SUPPRESS_HELP,
|
||||
default=True,
|
||||
action='store_false')
|
||||
parser.add_option('--no-forward-from-humbug',
|
||||
default=True,
|
||||
dest='forward_from_humbug',
|
||||
help=optparse.SUPPRESS_HELP,
|
||||
action='store_false')
|
||||
parser.add_option('--verbose',
|
||||
default=False,
|
||||
help=optparse.SUPPRESS_HELP,
|
||||
action='store_true')
|
||||
parser.add_option('--sync-subscriptions',
|
||||
default=False,
|
||||
action='store_true')
|
||||
parser.add_option('--site',
|
||||
default=DEFAULT_SITE,
|
||||
help=optparse.SUPPRESS_HELP)
|
||||
parser.add_option('--user',
|
||||
default=os.environ["USER"],
|
||||
help=optparse.SUPPRESS_HELP)
|
||||
parser.add_option('--root-path',
|
||||
default="/afs/athena.mit.edu/user/t/a/tabbott/for_friends",
|
||||
help=optparse.SUPPRESS_HELP)
|
||||
parser.add_option('--test-mode',
|
||||
default=False,
|
||||
help=optparse.SUPPRESS_HELP,
|
||||
action='store_true')
|
||||
parser.add_option('--api-key-file',
|
||||
default=os.path.join(os.environ["HOME"], "Private", ".humbug-api-key"))
|
||||
return parser.parse_args()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Set the SIGCHLD handler back to SIG_DFL to prevent these errors
|
||||
# when importing the "requests" module after being restarted using
|
||||
# the restart_stamp functionality:
|
||||
#
|
||||
# close failed in file object destructor:
|
||||
# IOError: [Errno 10] No child processes
|
||||
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
|
||||
|
||||
(options, args) = parse_args()
|
||||
|
||||
# The 'api' directory needs to go first, so that 'import humbug' won't pick
|
||||
# up some other directory named 'humbug'.
|
||||
pyzephyr_lib_path = "python-zephyr/build/lib.linux-" + os.uname()[4] + "-2.6/"
|
||||
sys.path[:0] = [os.path.join(options.root_path, 'api'),
|
||||
options.root_path,
|
||||
os.path.join(options.root_path, "python-zephyr"),
|
||||
os.path.join(options.root_path, pyzephyr_lib_path)]
|
||||
|
||||
# In case this is an automated restart of the mirroring script,
|
||||
# and we have lost AFS tokens, first try reading the API key from
|
||||
# the environment so that we can skip doing a filesystem read.
|
||||
if os.environ.get("HUMBUG_API_KEY") is not None:
|
||||
api_key = os.environ.get("HUMBUG_API_KEY")
|
||||
else:
|
||||
if not os.path.exists(options.api_key_file):
|
||||
print "\n".join(textwrap.wrap("""\
|
||||
Could not find API key file.
|
||||
You need to either place your api key file at %s,
|
||||
or specify the --api-key-file option.""" % (options.api_key_file,)))
|
||||
sys.exit(1)
|
||||
api_key = file(options.api_key_file).read().strip()
|
||||
# Store the API key in the environment so that our children
|
||||
# don't need to read it in
|
||||
os.environ["HUMBUG_API_KEY"] = api_key
|
||||
|
||||
humbug_account_email = options.user + "@mit.edu"
|
||||
import humbug
|
||||
humbug_client = humbug.Client(
|
||||
email=humbug_account_email,
|
||||
api_key=api_key,
|
||||
verbose=True,
|
||||
client="zephyr_mirror",
|
||||
site=options.site)
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
if options.sync_subscriptions:
|
||||
print "Syncing your ~/.zephyr.subs to your Humbug Subscriptions!"
|
||||
add_humbug_subscriptions(True)
|
||||
sys.exit(0)
|
||||
|
||||
# Kill all zephyr_mirror processes other than this one and its parent.
|
||||
if not options.test_mode:
|
||||
pgrep_query = "/usr/bin/python.*zephyr_mirror"
|
||||
if options.shard is not None:
|
||||
pgrep_query = "%s.*--shard=%s" % (pgrep_query, options.shard)
|
||||
proc = subprocess.Popen(['pgrep', '-U', os.environ["USER"], "-f", pgrep_query],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
out, _err_unused = proc.communicate()
|
||||
for pid in map(int, out.split()):
|
||||
if pid == os.getpid() or pid == os.getppid():
|
||||
continue
|
||||
|
||||
# Another copy of zephyr_mirror.py! Kill it.
|
||||
print "Killing duplicate zephyr_mirror process %s" % (pid,)
|
||||
try:
|
||||
os.kill(pid, signal.SIGKILL)
|
||||
except OSError:
|
||||
# We don't care if the target process no longer exists, so just print the error
|
||||
traceback.print_exc()
|
||||
|
||||
if options.shard is not None and set(options.shard) != set("a"):
|
||||
# The shard that is all "a"s is the one that handles personals
|
||||
# forwarding and humbug => zephyr forwarding
|
||||
options.forward_personals = False
|
||||
options.forward_from_humbug = False
|
||||
|
||||
if options.forward_from_humbug:
|
||||
child_pid = os.fork()
|
||||
if child_pid == 0:
|
||||
# Run the humbug => zephyr mirror in the child
|
||||
logger = configure_logger("humbug=>zephyr")
|
||||
zsig_fullname = fetch_fullname(options.user)
|
||||
humbug_to_zephyr(options)
|
||||
sys.exit(0)
|
||||
else:
|
||||
child_pid = None
|
||||
|
||||
import zephyr
|
||||
while True:
|
||||
try:
|
||||
# zephyr.init() tries to clear old subscriptions, and thus
|
||||
# sometimes gets a SERVNAK from the server
|
||||
zephyr.init()
|
||||
break
|
||||
except IOError:
|
||||
traceback.print_exc()
|
||||
time.sleep(1)
|
||||
logger_name = "zephyr=>humbug"
|
||||
if options.shard is not None:
|
||||
logger_name += "(%s)" % (options.shard,)
|
||||
logger = configure_logger(logger_name)
|
||||
# Have the kernel reap children for when we fork off processes to send Humbugs
|
||||
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
|
||||
zephyr_to_humbug(options)
|
|
@ -1,14 +0,0 @@
|
|||
#!/bin/sh
|
||||
# Two quick API tests using curl
|
||||
|
||||
curl https://humbughq.com/api/v1/send_message \
|
||||
-d "api-key=YOUR_API_KEY" \
|
||||
-d "email=YOUR_EMAIL" \
|
||||
-d "type=private" -d "content=test" \
|
||||
-d "to=RECIPIENT_EMAIL"
|
||||
|
||||
curl https://humbughq.com/api/v1/get_messages \
|
||||
-d "api-key=YOUR_API_KEY" \
|
||||
-d "email=YOUR_EMAIL"
|
||||
|
||||
# Or replace https://humbughq.com with your local test instance
|
|
@ -1,44 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright © 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
from os import path
|
||||
import optparse
|
||||
|
||||
usage = """get-public-streams --user=<email address> [options]
|
||||
|
||||
Prints out all the public streams in the realm.
|
||||
|
||||
Example: get-public-streams --user=tabbott@humbughq.com --site=https://zephyr.humbughq.com
|
||||
"""
|
||||
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option_group(humbug.generate_option_group(parser))
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
client = humbug.init_from_options(options)
|
||||
|
||||
print client.get_public_streams()
|
|
@ -1,4 +0,0 @@
|
|||
; put this file in ~/.humbugrc
|
||||
[api]
|
||||
key=<api key from the web interface>
|
||||
email=<your email address>
|
|
@ -1,43 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright © 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
from os import path
|
||||
import optparse
|
||||
|
||||
usage = """list-subscriptions --user=<email address> [options]
|
||||
|
||||
Prints out a list of the user's subscriptions.
|
||||
|
||||
Example: list-subscriptions --user=tabbott@humbughq.com --site=https://zephyr.humbughq.com
|
||||
"""
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option_group(humbug.generate_option_group(parser))
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
client = humbug.init_from_options(options)
|
||||
|
||||
print client.list_subscriptions()
|
|
@ -1,50 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright © 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
from os import path
|
||||
import optparse
|
||||
|
||||
usage = """print-messages --user=<email address> [--stream=<stream>] [options]
|
||||
|
||||
Prints out each message received by the indicated user, or on the indicated public stream.
|
||||
|
||||
Example: print-messages --user=tabbott@humbughq.com --site=https://zephyr.humbughq.com
|
||||
"""
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option_group(humbug.generate_option_group(parser))
|
||||
parser.add_option('--stream', default=None)
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
client = humbug.init_from_options(options)
|
||||
|
||||
def print_message(message):
|
||||
print message
|
||||
|
||||
get_messages_options = {}
|
||||
if options.stream is not None:
|
||||
get_messages_options['stream_name'] = options.stream
|
||||
client.call_on_each_message(print_message, get_messages_options)
|
|
@ -1,47 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright © 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
from os import path
|
||||
import optparse
|
||||
|
||||
usage = """print-next-message --user=<email address> [--stream=<stream>] [options]
|
||||
|
||||
Prints out the next message received by the indicated user, or on the indicated public stream.
|
||||
|
||||
Example: print-next-messages --user=tabbott@humbughq.com --site=https://zephyr.humbughq.com
|
||||
"""
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option_group(humbug.generate_option_group(parser))
|
||||
parser.add_option('--stream', default=None)
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
client = humbug.init_from_options(options)
|
||||
|
||||
get_messages_options = {}
|
||||
if options.stream is not None:
|
||||
get_messages_options['stream_name'] = options.stream
|
||||
print client.get_messages(get_messages_options)
|
|
@ -1,61 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright © 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
from os import path
|
||||
import optparse
|
||||
|
||||
usage = """send-message [options] <recipients>
|
||||
|
||||
Sends a test message to the specified recipients.
|
||||
|
||||
Example: send-message --sender=you@example.com --type=stream commits --subject="my subject" --message="test message"
|
||||
Example: send-message --sender=you@example.com --site=https://zephyr.humbughq.com user1@example.com user2@example.com
|
||||
"""
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option('--api-key')
|
||||
parser.add_option('--sender')
|
||||
parser.add_option('--subject', default="test")
|
||||
parser.add_option('--message', default="test message")
|
||||
parser.add_option('--site', default='https://humbughq.com')
|
||||
parser.add_option('--type', default='private')
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
if len(args) == 0:
|
||||
parser.error("You must specify recipients")
|
||||
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
client = humbug.Client(
|
||||
email=options.sender,
|
||||
api_key=options.api_key,
|
||||
verbose=True,
|
||||
site=options.site)
|
||||
|
||||
message_data = {
|
||||
"type": options.type,
|
||||
"content": options.message,
|
||||
"subject": options.subject,
|
||||
"to": args,
|
||||
}
|
||||
print client.send_message(message_data)
|
|
@ -1,48 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright © 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
from os import path
|
||||
import optparse
|
||||
|
||||
usage = """subscribe --user=<email address> [options] --streams=<streams>
|
||||
|
||||
Ensures the user is subscribed to the listed streams.
|
||||
|
||||
Example: subscribe --user=tabbott@humbughq.com --site=https://zephyr.humbughq.com
|
||||
"""
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option_group(humbug.generate_option_group(parser))
|
||||
parser.add_option('--streams', default='')
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
client = humbug.init_from_options(options)
|
||||
|
||||
if options.streams == "":
|
||||
print >>sys.stderr, "Usage:", parser.usage
|
||||
sys.exit(1)
|
||||
|
||||
print client.add_subscriptions(options.streams.split())
|
|
@ -1,48 +0,0 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright © 2012 Humbug, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
from os import path
|
||||
import optparse
|
||||
|
||||
usage = """unsubscribe --user=<email address> [options] --streams=<streams>
|
||||
|
||||
Ensures the user is not subscribed to the listed streams.
|
||||
|
||||
Example: unsubscribe --user=tabbott@humbughq.com --site=https://zephyr.humbughq.com
|
||||
"""
|
||||
sys.path.append(path.join(path.dirname(__file__), '..'))
|
||||
import humbug
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option_group(humbug.generate_option_group(parser))
|
||||
parser.add_option('--streams', default='')
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
client = humbug.init_from_options(options)
|
||||
|
||||
if options.streams == "":
|
||||
print >>sys.stderr, "Usage:", parser.usage
|
||||
sys.exit(1)
|
||||
|
||||
print client.remove_subscriptions(options.streams.split())
|
Loading…
Add table
Add a link
Reference in a new issue