2014-09-26 01:01:01 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Phillip Gentry <phillip@cx.com>
|
2017-07-29 15:05:38 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: github_hooks
|
2017-06-07 05:52:03 +00:00
|
|
|
short_description: Manages GitHub service hooks.
|
2014-09-26 01:01:01 +00:00
|
|
|
description:
|
|
|
|
- Adds service hooks and removes service hooks that have an error status.
|
|
|
|
version_added: "1.4"
|
|
|
|
options:
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- Github username.
|
|
|
|
required: true
|
|
|
|
oauthkey:
|
|
|
|
description:
|
2017-06-07 05:52:03 +00:00
|
|
|
- The oauth key provided by GitHub. It can be found/generated on GitHub under "Edit Your Profile" >> "Developer settings" >> "Personal Access Tokens"
|
2014-09-26 01:01:01 +00:00
|
|
|
required: true
|
|
|
|
repo:
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- >
|
|
|
|
This is the API url for the repository you want to manage hooks for. It should be in the form of: https://api.github.com/repos/user:/repo:.
|
|
|
|
Note this is different than the normal repo url.
|
2014-09-26 01:01:01 +00:00
|
|
|
required: true
|
|
|
|
hookurl:
|
|
|
|
description:
|
2017-06-07 05:52:03 +00:00
|
|
|
- When creating a new hook, this is the url that you want GitHub to post to. It is only required when creating a new hook.
|
2014-09-26 01:01:01 +00:00
|
|
|
required: false
|
|
|
|
action:
|
|
|
|
description:
|
|
|
|
- This tells the githooks module what you want it to do.
|
|
|
|
required: true
|
2016-05-02 17:21:02 +00:00
|
|
|
choices: [ "create", "cleanall", "list", "clean504" ]
|
2014-09-26 01:01:01 +00:00
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates for the target repo will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
2018-04-24 17:05:50 +00:00
|
|
|
type: bool
|
2014-09-29 13:35:04 +00:00
|
|
|
content_type:
|
|
|
|
description:
|
|
|
|
- Content type to use for requests made to the webhook
|
|
|
|
required: false
|
|
|
|
default: 'json'
|
|
|
|
choices: ['json', 'form']
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2015-06-16 18:32:39 +00:00
|
|
|
author: "Phillip Gentry, CX Inc (@pcgentry)"
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Example creating a new service hook. It ignores duplicates.
|
2016-12-01 12:30:04 +00:00
|
|
|
- github_hooks:
|
|
|
|
action: create
|
2016-12-10 14:23:19 +00:00
|
|
|
hookurl: http://11.111.111.111:2222
|
2016-12-01 12:30:04 +00:00
|
|
|
user: '{{ gituser }}'
|
|
|
|
oauthkey: '{{ oauthkey }}'
|
2016-12-10 14:23:19 +00:00
|
|
|
repo: https://api.github.com/repos/pcgentry/Github-Auto-Deploy
|
2014-09-26 01:01:01 +00:00
|
|
|
|
2017-03-23 01:50:28 +00:00
|
|
|
# Cleaning all hooks for this repo that had an error on the last update. Since this works for all hooks in a repo it is probably best that this would
|
|
|
|
# be called from a handler.
|
2016-12-01 12:30:04 +00:00
|
|
|
- github_hooks:
|
|
|
|
action: cleanall
|
|
|
|
user: '{{ gituser }}'
|
|
|
|
oauthkey: '{{ oauthkey }}'
|
|
|
|
repo: '{{ repo }}'
|
|
|
|
delegate_to: localhost
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
2017-07-29 15:05:38 +00:00
|
|
|
import json
|
2018-04-06 09:01:42 +00:00
|
|
|
import base64
|
2017-07-29 15:05:38 +00:00
|
|
|
|
2017-06-07 05:52:03 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
2018-04-06 09:01:42 +00:00
|
|
|
from ansible.module_utils._text import to_bytes
|
2017-02-02 19:45:22 +00:00
|
|
|
|
|
|
|
|
2017-06-07 05:52:03 +00:00
|
|
|
def request(module, url, user, oauthkey, data='', method='GET'):
|
2018-04-06 09:01:42 +00:00
|
|
|
auth = base64.b64encode(to_bytes('%s:%s' % (user, oauthkey)).replace('\n', ''))
|
2014-09-26 01:01:01 +00:00
|
|
|
headers = {
|
|
|
|
'Authorization': 'Basic %s' % auth,
|
|
|
|
}
|
2017-06-07 05:52:03 +00:00
|
|
|
response, info = fetch_url(module, url, headers=headers, data=data, method=method)
|
|
|
|
return response, info
|
|
|
|
|
|
|
|
|
|
|
|
def _list(module, oauthkey, repo, user):
|
|
|
|
url = "%s/hooks" % repo
|
|
|
|
response, info = request(module, url, user, oauthkey)
|
2014-09-26 01:01:01 +00:00
|
|
|
if info['status'] != 200:
|
|
|
|
return False, ''
|
|
|
|
else:
|
|
|
|
return False, response.read()
|
|
|
|
|
2017-06-07 05:52:03 +00:00
|
|
|
|
|
|
|
def _clean504(module, oauthkey, repo, user):
|
|
|
|
current_hooks = _list(module, oauthkey, repo, user)[1]
|
2014-09-26 01:01:01 +00:00
|
|
|
decoded = json.loads(current_hooks)
|
|
|
|
|
|
|
|
for hook in decoded:
|
|
|
|
if hook['last_response']['code'] == 504:
|
2017-06-07 05:52:03 +00:00
|
|
|
_delete(module, oauthkey, repo, user, hook['id'])
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
return 0, current_hooks
|
|
|
|
|
2017-06-07 05:52:03 +00:00
|
|
|
|
|
|
|
def _cleanall(module, oauthkey, repo, user):
|
|
|
|
current_hooks = _list(module, oauthkey, repo, user)[1]
|
2014-09-26 01:01:01 +00:00
|
|
|
decoded = json.loads(current_hooks)
|
|
|
|
|
|
|
|
for hook in decoded:
|
|
|
|
if hook['last_response']['code'] != 200:
|
2017-06-07 05:52:03 +00:00
|
|
|
_delete(module, oauthkey, repo, user, hook['id'])
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
return 0, current_hooks
|
|
|
|
|
2017-06-07 05:52:03 +00:00
|
|
|
|
2014-09-30 07:12:10 +00:00
|
|
|
def _create(module, hookurl, oauthkey, repo, user, content_type):
|
2014-09-26 01:01:01 +00:00
|
|
|
url = "%s/hooks" % repo
|
|
|
|
values = {
|
|
|
|
"active": True,
|
|
|
|
"name": "web",
|
|
|
|
"config": {
|
|
|
|
"url": "%s" % hookurl,
|
2014-09-29 13:35:04 +00:00
|
|
|
"content_type": "%s" % content_type
|
2014-09-26 01:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 05:52:03 +00:00
|
|
|
data = json.dumps(values)
|
|
|
|
response, info = request(module, url, user, oauthkey, data=data, method='POST')
|
2014-09-26 01:01:01 +00:00
|
|
|
if info['status'] != 200:
|
|
|
|
return 0, '[]'
|
|
|
|
else:
|
|
|
|
return 0, response.read()
|
|
|
|
|
2017-06-07 05:52:03 +00:00
|
|
|
|
|
|
|
def _delete(module, oauthkey, repo, user, hookid):
|
2014-09-26 01:01:01 +00:00
|
|
|
url = "%s/hooks/%s" % (repo, hookid)
|
2017-06-07 05:52:03 +00:00
|
|
|
response, info = request(module, url, user, oauthkey, method='DELETE')
|
2014-09-26 01:01:01 +00:00
|
|
|
return response.read()
|
|
|
|
|
2017-06-07 05:52:03 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2017-06-07 05:52:03 +00:00
|
|
|
action=dict(required=True, choices=['list', 'clean504', 'cleanall', 'create']),
|
2017-01-29 07:28:53 +00:00
|
|
|
hookurl=dict(required=False),
|
|
|
|
oauthkey=dict(required=True, no_log=True),
|
|
|
|
repo=dict(required=True),
|
|
|
|
user=dict(required=True),
|
|
|
|
validate_certs=dict(default='yes', type='bool'),
|
|
|
|
content_type=dict(default='json', choices=['json', 'form']),
|
2014-09-26 01:01:01 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
action = module.params['action']
|
|
|
|
hookurl = module.params['hookurl']
|
|
|
|
oauthkey = module.params['oauthkey']
|
|
|
|
repo = module.params['repo']
|
|
|
|
user = module.params['user']
|
2014-09-29 13:35:04 +00:00
|
|
|
content_type = module.params['content_type']
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if action == "list":
|
2017-06-07 05:52:03 +00:00
|
|
|
(rc, out) = _list(module, oauthkey, repo, user)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if action == "clean504":
|
2017-06-07 05:52:03 +00:00
|
|
|
(rc, out) = _clean504(module, oauthkey, repo, user)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if action == "cleanall":
|
2017-06-07 05:52:03 +00:00
|
|
|
(rc, out) = _cleanall(module, oauthkey, repo, user)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if action == "create":
|
2014-09-30 07:12:10 +00:00
|
|
|
(rc, out) = _create(module, hookurl, oauthkey, repo, user, content_type)
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="failed", result=out)
|
|
|
|
|
|
|
|
module.exit_json(msg="success", result=out)
|
|
|
|
|
|
|
|
|
2016-12-05 16:23:23 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|