Skip to content

Instantly share code, notes, and snippets.

@xenozauros
Last active August 29, 2015 14:24
Show Gist options
  • Save xenozauros/686a711a9ffb3fcf6091 to your computer and use it in GitHub Desktop.
Save xenozauros/686a711a9ffb3fcf6091 to your computer and use it in GitHub Desktop.
Puppet: Gitlab WebHook processing with nginx and r10k. Integration with RequestTracker
#!/usr/bin/python
import sys
import json
import smtplib
import gitlab
import re
from subprocess import call,check_output,STDOUT
gitlab_server=''
gitlab_token=''
deploy_command=''
rt_command=''
slack_channel='puppet_deploy'
def send_slack(channel, result):
slack_command=['/usr/local/bin/slack.sh', channel, result]
call(slack_command)
def send_notice(result, subject):
SERVER = "localhost"
FROM = "Puppet <noreply@example.com>"
TO = ['me@sexample.com']
SUBJECT = subject
TEXT = result
#### Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
#### Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message.encode('utf-8'))
server.quit()
###### Logic start
try:
my_json=json.loads(sys.argv[1])
### Debug output
with open('/tmp/hook/hook.json', 'w') as outfile:
json.dump(my_json, outfile)
except:
sys.exit(1)
try:
module_name=my_json["repository"]["name"]
git_branch=my_json["ref"].split('/')[-1]
deploy_author=my_json["user_name"]
deploy_success="Deploy proccess for %s branch of %s module, initiated by %s is successful. All done withouth issues." % (git_branch, module_name, deploy_author)
except:
sys.exit(2)
if module_name=='puppet_manifests':
project_id=my_json["project_id"]
full_deploy=False
git_before=my_json["before"]
git_after=my_json["after"]
git_branch=my_json["ref"].split('/')[-1]
git=gitlab.Gitlab(gitlab_server, token=gitlab_token, verify_ssl=False)
git_diffs=git.compare_branches_tags_commits(project_id, git_before, git_after)
deploy_success="Deploy proccess, initiated by %s for %s environment of %s is successful. All done withouth issues." % (deploy_author, git_branch, module_name)
deploy_command=['/usr/local/bin/r10k', 'deploy', 'environment', git_branch]
for git_diff in git_diffs['diffs']:
if git_diff['new_path'] == 'Puppetfile':
deploy_command=['/usr/local/bin/r10k', 'deploy', 'environment', git_branch, '-p']
else:
deploy_command=['/usr/local/bin/r10k', 'deploy', 'module', module_name]
if deploy_command:
result=check_output(deploy_command,stderr=STDOUT)
if result:
subject="Something gone wrong while deploying %s, edited by %s" % (module_name, deploy_author)
send_notice(result, subject)
send_slack(slack_channel, "%s: %s" % (subject, result))
else:
subject="%s deployed without issues" % module_name
send_notice(deploy_success, subject)
send_slack(slack_channel, "%s: %s" % (subject, deploy_success))
try:
rt_ticket=''
rt_search_branch=re.search('RT_([0-9][0-9][0-9][0-9]+)',git_branch)
for commit in my_json["commits"]:
rt_search_message=re.search('RT_([0-9][0-9][0-9][0-9]+)',commit["message"])
if rt_search_branch:
rt_ticket=rt_search_branch.group(1)
elif rt_search_message:
rt_ticket=rt_search_message.group(1)
if rt_ticket:
commit_url=commit["url"]
commit_author=commit["author"]["name"]
commit_message=commit["message"]
rt_message="Gitlab commit, made by %s to branch %s of module %s is found regarding this ticket.\n Comment is: %s\n Link to commit: %s" % (commit_author, git_branch, module_name, commit_message, commit_url)
rt_command=['/usr/local/bin/hook_comment_rt.py',rt_ticket, rt_message]
call(rt_command)
except:
pass
post_deploy_command=['/usr/local/bin/r10k_post_deploy', git_branch]
#!/usr/bin/python
from rtkit.resource import RTResource
from rtkit.authenticators import CookieAuthenticator
from rtkit.errors import RTResourceError
from rtkit import set_logging
import logging
import sys
# creates a cookie for the rtserver with the credentials given at initialization.
# define your credentials here
rt_user = ''
rt_password = ''
# here is the RequestTracker URI we try to access
rt_uri = 'http://rt.example.com/REST/1.0/'
set_logging('debug')
logger = logging.getLogger('rtkit')
resource = RTResource(rt_uri, rt_user, rt_password, CookieAuthenticator)
try:
ticket_num=sys.argv[1]
ticket_comment=sys.argv[2]
except:
sys.exit(1)
try:
params = {
'content': {
'Action': 'comment',
'Text': '%s' % ticket_comment
}
}
response = resource.post(path='ticket/%s/comment' % ticket_num, payload=params,)
for r in response.parsed:
for t in r:
logger.info(t)
except RTResourceError as e:
logger.error(e.response.status_int)
logger.error(e.response.status)
logger.error(e.response.parsed)
server {
listen 8000;
server_name puppet puppet.example.com;
access_log /var/log/nginx/hook_access.log;
error_log /var/log/nginx/hook_error.log;
location / {
lua_need_request_body on;
client_max_body_size 50k;
client_body_buffer_size 50k;
content_by_lua '
os.execute("/usr/local/bin/hook.py \'"..ngx.var.request_body.."\'")
os.execute("rm -rf /data/cache/nginx && killall -HUP nginx")
';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment