@ -19,7 +19,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import json
import json
import urllib2
import base64
import base64
DOCUMENTATION = '''
DOCUMENTATION = '''
@ -51,6 +50,14 @@ options:
- This tells the githooks module what you want it to do.
- This tells the githooks module what you want it to do.
required: true
required: true
choices: [ "create", "cleanall" ]
choices: [ "create", "cleanall" ]
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'
choices: ['yes', 'no']
author: Phillip Gentry, CX Inc
author: Phillip Gentry, CX Inc
'''
'''
@ -62,16 +69,19 @@ EXAMPLES = '''
- local_action: github_hooks action=cleanall user={{ gituser }} oauthkey={{ oauthkey }} repo={{ repo }}
- local_action: github_hooks action=cleanall user={{ gituser }} oauthkey={{ oauthkey }} repo={{ repo }}
'''
'''
def list(hookurl, oauthkey, repo, user):
def list(module, hookurl, oauthkey, repo, user):
url = "%s/hooks" % repo
url = "%s/hooks" % repo
auth = base64.encodestring('%s:%s' % (user, oauthkey)).replace('\n', '')
auth = base64.encodestring('%s:%s' % (user, oauthkey)).replace('\n', '')
req = urllib2.Request(url)
headers = {
req.add_header("Authorization", "Basic %s" % auth)
'Authorization': 'Basic %s' % auth,
res = urllib2.urlopen(req)
}
out = res.read()
response, info = fetch_url(module, url, headers=headers, validate_certs=module.params['validate_certs'])
return False, out
if info['status'] != 200:
return False, ''
def clean504(hookurl, oauthkey, repo, user):
else:
return False, response.read()
def clean504(module, hookurl, oauthkey, repo, user):
current_hooks = list(hookurl, oauthkey, repo, user)[1]
current_hooks = list(hookurl, oauthkey, repo, user)[1]
decoded = json.loads(current_hooks)
decoded = json.loads(current_hooks)
@ -79,11 +89,11 @@ def clean504(hookurl, oauthkey, repo, user):
if hook['last_response']['code'] == 504:
if hook['last_response']['code'] == 504:
# print "Last response was an ERROR for hook:"
# print "Last response was an ERROR for hook:"
# print hook['id']
# print hook['id']
delete(hookurl, oauthkey, repo, user, hook['id'])
delete(module, hookurl, oauthkey, repo, user, hook['id'])
return 0, current_hooks
return 0, current_hooks
def cleanall(hookurl, oauthkey, repo, user):
def cleanall(module, hookurl, oauthkey, repo, user):
current_hooks = list(hookurl, oauthkey, repo, user)[1]
current_hooks = list(hookurl, oauthkey, repo, user)[1]
decoded = json.loads(current_hooks)
decoded = json.loads(current_hooks)
@ -91,11 +101,11 @@ def cleanall(hookurl, oauthkey, repo, user):
if hook['last_response']['code'] != 200:
if hook['last_response']['code'] != 200:
# print "Last response was an ERROR for hook:"
# print "Last response was an ERROR for hook:"
# print hook['id']
# print hook['id']
delete(hookurl, oauthkey, repo, user, hook['id'])
delete(module, hookurl, oauthkey, repo, user, hook['id'])
return 0, current_hooks
return 0, current_hooks
def create(hookurl, oauthkey, repo, user):
def create(module, hookurl, oauthkey, repo, user):
url = "%s/hooks" % repo
url = "%s/hooks" % repo
values = {
values = {
"active": True,
"active": True,
@ -107,29 +117,23 @@ def create(hookurl, oauthkey, repo, user):
}
}
data = json.dumps(values)
data = json.dumps(values)
auth = base64.encodestring('%s:%s' % (user, oauthkey)).replace('\n', '')
auth = base64.encodestring('%s:%s' % (user, oauthkey)).replace('\n', '')
out='[]'
headers = {
try :
'Authorization': 'Basic %s' % auth,
req = urllib2.Request(url)
}
req.add_data(data)
response, info = fetch_url(module, url, data=data, headers=headers, validate_certs=module.params['validate_certs'])
req.add_header("Authorization", "Basic %s" % auth)
if info['status'] != 200:
res = urllib2.urlopen(req)
return 0, '[]'
out = res.read()
else:
return 0, out
return 0, response.read()
except urllib2.HTTPError, e :
if e.code == 422 :
def delete(module, hookurl, oauthkey, repo, user, hookid):
return 0, out
def delete(hookurl, oauthkey, repo, user, hookid):
url = "%s/hooks/%s" % (repo, hookid)
url = "%s/hooks/%s" % (repo, hookid)
auth = base64.encodestring('%s:%s' % (user, oauthkey)).replace('\n', '')
auth = base64.encodestring('%s:%s' % (user, oauthkey)).replace('\n', '')
req = urllib2.Request(url)
headers = {
req.get_method = lambda: 'DELETE'
'Authorization': 'Basic %s' % auth,
req.add_header("Authorization", "Basic %s" % auth)
}
# req.add_header('Content-Type', 'application/xml')
response, info = fetch_url(module, url, data=data, headers=headers, method='DELETE', validate_certs=module.params['validate_certs'])
# req.add_header('Accept', 'application/xml')
return response.read()
res = urllib2.urlopen(req)
out = res.read()
return out
def main():
def main():
module = AnsibleModule(
module = AnsibleModule(
@ -139,6 +143,7 @@ def main():
oauthkey=dict(required=True),
oauthkey=dict(required=True),
repo=dict(required=True),
repo=dict(required=True),
user=dict(required=True),
user=dict(required=True),
validate_certs=dict(default='yes', type='bool'),
)
)
)
)
@ -149,16 +154,16 @@ def main():
user = module.params['user']
user = module.params['user']
if action == "list":
if action == "list":
(rc, out) = list(hookurl, oauthkey, repo, user)
(rc, out) = list(module, hookurl, oauthkey, repo, user)
if action == "clean504":
if action == "clean504":
(rc, out) = clean504(hookurl, oauthkey, repo, user)
(rc, out) = clean504(module, hookurl, oauthkey, repo, user)
if action == "cleanall":
if action == "cleanall":
(rc, out) = cleanall(hookurl, oauthkey, repo, user)
(rc, out) = cleanall(module, hookurl, oauthkey, repo, user)
if action == "create":
if action == "create":
(rc, out) = create(hookurl, oauthkey, repo, user)
(rc, out) = create(module, hookurl, oauthkey, repo, user)
if rc != 0:
if rc != 0:
module.fail_json(msg="failed", result=out)
module.fail_json(msg="failed", result=out)
@ -168,4 +173,6 @@ def main():
# import module snippets
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
main()
main()