2013-10-30 15:03:11 +01:00
#!/usr/bin/python
2013-11-14 20:49:10 +01:00
# -*- coding: utf-8 -*-
2013-10-30 15:03:11 +01:00
2013-11-14 20:49:10 +01:00
# (c) 2013, Phillip Gentry <phillip@cx.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2013-10-30 15:03:11 +01:00
2013-11-14 20:49:10 +01:00
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
2013-10-30 15:03:11 +01:00
2013-11-14 20:49:10 +01:00
import json
import base64
2013-10-30 15:03:11 +01:00
DOCUMENTATION = '''
- - -
2013-11-19 00:28:21 +01:00
module : github_hooks
2013-10-30 15:03:11 +01:00
short_description : Manages github service hooks .
description :
- Adds service hooks and removes service hooks that have an error status .
2013-11-14 20:49:10 +01:00
version_added : " 1.4 "
2013-10-30 15:03:11 +01:00
options :
user :
description :
- Github username .
required : true
oauthkey :
description :
- The oauth key provided by github . It can be found / generated on github under " Edit Your Profile " >> " Applications " >> " Personal Access Tokens "
required : true
repo :
description :
2013-11-14 19:20:17 +01: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. "
2013-10-30 15:03:11 +01:00
required : true
hookurl :
description :
- 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 .
required : false
action :
description :
- This tells the githooks module what you want it to do .
required : true
choices : [ " create " , " cleanall " ]
2014-03-10 22:06:52 +01: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 '
choices : [ ' yes ' , ' no ' ]
2014-09-29 15:35:04 +02:00
content_type :
description :
- Content type to use for requests made to the webhook
required : false
default : ' json '
choices : [ ' json ' , ' form ' ]
2014-03-10 22:06:52 +01:00
2015-06-16 20:32:39 +02:00
author : " Phillip Gentry, CX Inc (@pcgentry) "
2013-10-30 15:03:11 +01:00
'''
EXAMPLES = '''
# Example creating a new service hook. It ignores duplicates.
2013-11-19 00:28:21 +01:00
- github_hooks : action = create hookurl = http : / / 11.111 .111 .111 : 2222 user = { { gituser } } oauthkey = { { oauthkey } } repo = https : / / api . github . com / repos / pcgentry / Github - Auto - Deploy
2013-10-30 15:03:11 +01: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.
2013-11-19 00:28:21 +01:00
- local_action : github_hooks action = cleanall user = { { gituser } } oauthkey = { { oauthkey } } repo = { { repo } }
2013-10-30 15:03:11 +01:00
'''
2014-09-30 09:07:40 +02:00
def _list ( module , hookurl , oauthkey , repo , user ) :
2013-10-30 15:03:11 +01:00
url = " %s /hooks " % repo
auth = base64 . encodestring ( ' %s : %s ' % ( user , oauthkey ) ) . replace ( ' \n ' , ' ' )
2014-03-10 22:06:52 +01:00
headers = {
' Authorization ' : ' Basic %s ' % auth ,
}
2014-03-12 16:31:01 +01:00
response , info = fetch_url ( module , url , headers = headers )
2014-03-10 22:06:52 +01:00
if info [ ' status ' ] != 200 :
return False , ' '
else :
return False , response . read ( )
2014-09-30 09:12:10 +02:00
def _clean504 ( module , hookurl , oauthkey , repo , user ) :
2014-09-30 09:07:40 +02:00
current_hooks = _list ( hookurl , oauthkey , repo , user ) [ 1 ]
2013-10-30 15:03:11 +01:00
decoded = json . loads ( current_hooks )
for hook in decoded :
if hook [ ' last_response ' ] [ ' code ' ] == 504 :
# print "Last response was an ERROR for hook:"
# print hook['id']
2014-09-30 09:12:10 +02:00
_delete ( module , hookurl , oauthkey , repo , user , hook [ ' id ' ] )
2013-10-30 15:03:11 +01:00
return 0 , current_hooks
2014-09-30 09:12:10 +02:00
def _cleanall ( module , hookurl , oauthkey , repo , user ) :
2014-09-30 09:07:40 +02:00
current_hooks = _list ( hookurl , oauthkey , repo , user ) [ 1 ]
2013-10-30 15:03:11 +01:00
decoded = json . loads ( current_hooks )
for hook in decoded :
if hook [ ' last_response ' ] [ ' code ' ] != 200 :
# print "Last response was an ERROR for hook:"
# print hook['id']
2014-09-30 09:12:10 +02:00
_delete ( module , hookurl , oauthkey , repo , user , hook [ ' id ' ] )
2013-10-30 15:03:11 +01:00
return 0 , current_hooks
2014-09-30 09:12:10 +02:00
def _create ( module , hookurl , oauthkey , repo , user , content_type ) :
2013-10-30 15:03:11 +01:00
url = " %s /hooks " % repo
values = {
" active " : True ,
" name " : " web " ,
" config " : {
" url " : " %s " % hookurl ,
2014-09-29 15:35:04 +02:00
" content_type " : " %s " % content_type
2013-10-30 15:03:11 +01:00
}
}
data = json . dumps ( values )
auth = base64 . encodestring ( ' %s : %s ' % ( user , oauthkey ) ) . replace ( ' \n ' , ' ' )
2014-03-10 22:06:52 +01:00
headers = {
' Authorization ' : ' Basic %s ' % auth ,
}
2014-03-12 16:31:01 +01:00
response , info = fetch_url ( module , url , data = data , headers = headers )
2014-03-10 22:06:52 +01:00
if info [ ' status ' ] != 200 :
return 0 , ' [] '
else :
return 0 , response . read ( )
2014-09-30 09:12:10 +02:00
def _delete ( module , hookurl , oauthkey , repo , user , hookid ) :
2013-10-30 15:03:11 +01:00
url = " %s /hooks/ %s " % ( repo , hookid )
auth = base64 . encodestring ( ' %s : %s ' % ( user , oauthkey ) ) . replace ( ' \n ' , ' ' )
2014-03-10 22:06:52 +01:00
headers = {
' Authorization ' : ' Basic %s ' % auth ,
}
2014-03-12 16:31:01 +01:00
response , info = fetch_url ( module , url , data = data , headers = headers , method = ' DELETE ' )
2014-03-10 22:06:52 +01:00
return response . read ( )
2013-10-30 15:03:11 +01:00
def main ( ) :
module = AnsibleModule (
argument_spec = dict (
action = dict ( required = True ) ,
hookurl = dict ( required = False ) ,
oauthkey = dict ( required = True ) ,
repo = dict ( required = True ) ,
user = dict ( required = True ) ,
2014-03-10 22:06:52 +01:00
validate_certs = dict ( default = ' yes ' , type = ' bool ' ) ,
2014-09-29 15:35:04 +02:00
content_type = dict ( default = ' json ' , choices = [ ' json ' , ' form ' ] ) ,
2013-10-30 15:03:11 +01:00
)
)
action = module . params [ ' action ' ]
hookurl = module . params [ ' hookurl ' ]
oauthkey = module . params [ ' oauthkey ' ]
repo = module . params [ ' repo ' ]
user = module . params [ ' user ' ]
2014-09-29 15:35:04 +02:00
content_type = module . params [ ' content_type ' ]
2013-10-30 15:03:11 +01:00
if action == " list " :
2014-09-30 09:07:40 +02:00
( rc , out ) = _list ( module , hookurl , oauthkey , repo , user )
2013-10-30 15:03:11 +01:00
if action == " clean504 " :
2014-09-30 09:12:10 +02:00
( rc , out ) = _clean504 ( module , hookurl , oauthkey , repo , user )
2013-10-30 15:03:11 +01:00
if action == " cleanall " :
2014-09-30 09:12:10 +02:00
( rc , out ) = _cleanall ( module , hookurl , oauthkey , repo , user )
2013-10-30 15:03:11 +01:00
if action == " create " :
2014-09-30 09:12:10 +02:00
( rc , out ) = _create ( module , hookurl , oauthkey , repo , user , content_type )
2013-10-30 15:03:11 +01:00
if rc != 0 :
module . fail_json ( msg = " failed " , result = out )
module . exit_json ( msg = " success " , result = out )
2013-12-02 21:11:23 +01:00
# import module snippets
from ansible . module_utils . basic import *
2014-03-10 22:06:52 +01:00
from ansible . module_utils . urls import *
2013-10-30 15:03:11 +01:00
main ( )