Created
September 15, 2015 15:09
-
-
Save nwjsmith/8959c621658e9e3b212a to your computer and use it in GitHub Desktop.
An Ansible Library for Bugsnag deploys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
DOCUMENTATION = ''' | |
--- | |
module: bugsnag_deployment | |
short_description: Notifies Bugsnag of deploys. | |
options: | |
api_key: | |
description: | |
- The API Key associated with the project. Informs Bugsnag which project | |
has been deployed | |
required: true | |
default: null | |
release_stage: | |
description: | |
- The release stage (eg, production, staging) currently being deployed. | |
required: false | |
default: "production" | |
repository: | |
description: | |
- The url to the repository containing the source code being deployed. We | |
can use this to link directly to your source code from the Bugsnag | |
dashboard. | |
required: false | |
default: null | |
branch: | |
description: | |
- The source control branch from which you are deploying the code. | |
required: false | |
default: null | |
revision: | |
description: | |
- The source control (git, subversion, etc) revision id for the code you | |
are deploying. | |
required: false | |
default: null | |
app_version: | |
description: | |
- The app version of the code you are currently deploying. Only set this | |
if you tag your releases with semantic version numbers and deploy | |
infrequently. | |
required: false | |
default: null | |
requirements: [] | |
author: Nate Smith | |
''' | |
EXAMPLES = ''' | |
- bugsnag_deployment: api_key=API-KEY release_stage=staging repository=https://github.com/octocat/example branch=master | |
''' | |
import httplib | |
import urllib | |
class BugsnagDeploy(object): | |
def __init__(self, module): | |
self.module = module | |
self.args = self.module.params | |
self.changed = True | |
self.response = None | |
self.process() | |
def process(self): | |
if not self.module.check_mode: | |
self.make_request() | |
def make_request(self): | |
encoded_parameters = urllib.urlencode(self.parameters()) | |
headers = {"Content-Type": "application/x-www-form-urlencoded", | |
"Accept": "text/plain"} | |
connection = httplib.HTTPSConnection("notify.bugsnag.com") | |
connection.request("POST", "/deploy", encoded_parameters, headers) | |
response = connection.getresponse() | |
if not response.status == 200: | |
self.module.fail_json(msg="Response failed with status %d" % response.status) | |
self.changed = False | |
else: | |
self.response = response.read() | |
def parameters(self): | |
for arg in self.args: | |
if self.args[arg]: | |
self.args[arg] = self.args[arg].strip() | |
params = {} | |
if self.args['api_key']: | |
params['apiKey'] = self.args['api_key'] | |
if self.args['release_stage']: | |
params['releaseStage'] = self.args['release_stage'] | |
if self.args['repository']: | |
params['repository'] = self.args['repository'] | |
if self.args['branch']: | |
params['branch'] = self.args['branch'] | |
if self.args['revision']: | |
params['revision'] = self.args['revision'] | |
if self.args['app_version']: | |
params['appVersion'] = self.args['app_version'] | |
return params | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
api_key = dict(required=True, type='str'), | |
release_stage = dict(default='production', required=False, type='str'), | |
repository = dict(required=False, type='str'), | |
branch = dict(required=False, type='str'), | |
revision = dict(required=False, type='str'), | |
app_version = dict(required=False, type='str') | |
), | |
supports_check_mode=True | |
) | |
result = BugsnagDeploy(module) | |
module.exit_json(changed=result.changed, response=result.response) | |
sys.exit(0) | |
from ansible.module_utils.basic import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment