campfire 4.13 KB
Newer Older
1
#!/usr/bin/python
Adam committed
2 3 4 5 6
# -*- coding: utf-8 -*-

DOCUMENTATION = '''
---
module: campfire
7
version_added: "1.2"
Adam committed
8 9 10
short_description: Send a message to Campfire
description:
   - Send a message to Campfire.
Michael DeHaan committed
11
   - Messages with newlines will result in a "Paste" message being sent.
Adam committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
version_added: "1.2"
options:
  subscription:
    description:
      - The subscription name to use.
    required: true
  token:
    description:
      - API token.
    required: true
  room:
    description:
      - Room number to which the message should be sent.
    required: true
  msg:
    description:
      - The message body.
    required: true
  notify:
    description:
      - Send a notification sound before the message.
    required: false
    choices: ["56k", "bueller", "crickets", "dangerzone", "deeper",
              "drama", "greatjob", "horn", "horror" , "inconceivable",
              "live", "loggins", "noooo", "nyan", "ohmy", "ohyeah",
              "pushit", "rimshot", "sax", "secret", "tada", "tmyk",
              "trombone", "vuvuzela", "yeah", "yodel"]

# informational: requirements for nodes
requirements: [ urllib2, cgi ]
author: Adam Garside <adam.garside@gmail.com>
'''

EXAMPLES = '''
46
- campfire: subscription=foo token=12345 room=123 msg="Task completed."
Adam committed
47

48
- campfire: subscription=foo token=12345 room=123 notify=loggins
Adam committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        msg="Task completed ... with feeling."
'''


def main():

    try:
        import urllib2
    except ImportError:
        module.fail_json(msg="urllib2 is required")

    try:
        import cgi
    except ImportError:
        module.fail_json(msg="cgi is required")

    module = AnsibleModule(
        argument_spec=dict(
            subscription=dict(required=True),
            token=dict(required=True),
            room=dict(required=True),
            msg=dict(required=True),
            notify=dict(required=False,
                        choices=["56k", "bueller", "crickets",
                                 "dangerzone", "deeper", "drama",
                                 "greatjob", "horn", "horror",
                                 "inconceivable", "live", "loggins",
                                 "noooo", "nyan", "ohmy", "ohyeah",
                                 "pushit", "rimshot", "sax", "secret",
                                 "tada", "tmyk", "trombone", "vuvuzela",
                                 "yeah", "yodel"]),
        ),
81
        supports_check_mode=False
Adam committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    )

    subscription = module.params["subscription"]
    token = module.params["token"]
    room = module.params["room"]
    msg = module.params["msg"]
    notify = module.params["notify"]

    URI = "https://%s.campfirenow.com" % subscription
    NSTR = "<message><type>SoundMessage</type><body>%s</body></message>"
    MSTR = "<message><body>%s</body></message>"
    AGENT = "Ansible/1.2"

    try:

        # Setup basic auth using token as the username
        pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
        pm.add_password(None, URI, token, 'X')

        # Setup Handler and define the opener for the request
        handler = urllib2.HTTPBasicAuthHandler(pm)
        opener = urllib2.build_opener(handler)

        target_url = '%s/room/%s/speak.xml' % (URI, room)

        # Send some audible notification if requested
        if notify:
            req = urllib2.Request(target_url, NSTR % cgi.escape(notify))
            req.add_header('Content-Type', 'application/xml')
            req.add_header('User-agent', AGENT)
            response = opener.open(req)

        # Send the message
        req = urllib2.Request(target_url, MSTR % cgi.escape(msg))
        req.add_header('Content-Type', 'application/xml')
        req.add_header('User-agent', AGENT)
        response = opener.open(req)

120 121 122 123 124 125
    except urllib2.HTTPError, e:
        if not (200 <= e.code < 300):
            module.fail_json(msg="unable to send msg: '%s', campfire api"
                                 " returned error code: '%s'" %
                                 (msg, e.code))

Adam committed
126
    except Exception, e:
127
        module.fail_json(msg="unable to send msg: %s" % msg)
Adam committed
128 129 130

    module.exit_json(changed=True, room=room, msg=msg, notify=notify)

131
# import module snippets
132
from ansible.module_utils.basic import *
Adam committed
133
main()