Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- ## You might be asking yourself why this is python2.x
- ## that's because I was trying to use centos6 RPM packages.
- ## This code wont translate well to py3
- #BaseHTTPServer became http.server
- #The print functions are crappy.
- # I am so sorry.
- # -Jan
- import BaseHTTPServer
- import sys
- import time
- import urlparse
- import json
- import ssl
- import git
- git_dirs = ['/opt/tctd-salt']
- cert_file = '/home/jharasym/pki/salt_webhook.pem'
- #HOST_NAME = sys.argv[1]
- #PORT_NUMBER = int(sys.argv[2])
- HOST_NAME = '0.0.0.0' # Bind port
- PORT_NUMBER = 5000
- def handle_hook(payload):
- ### Need to check payload type.
- for git_dir in git_dirs:
- g = git.cmd.Git(git_dir)
- g.pull()
- class HookHandler(BaseHTTPServer.BaseHTTPRequestHandler):
- server_version = "JansHookHandler/0.1"
- def do_GET(s):
- s.send_response(200)
- s.wfile.write('Hello!')
- def do_POST(s):
- # Check that the IP is within the gitlab range
- if not any(s.client_address[0].startswith(IP)
- for IP in ('10.129.30.40')):
- print time.asctime(), "Sending 403 - %s:%s" % (HOST_NAME, PORT_NUMBER)
- s.send_error(403)
- length = int(s.headers['Content-Length'])
- post_data = urlparse.parse_qs(s.rfile.read(length).decode('utf-8'))
- handle_hook(post_data)
- s.send_response(200)
- if __name__ == '__main__':
- server_class = BaseHTTPServer.HTTPServer
- httpd = server_class((HOST_NAME, PORT_NUMBER), HookHandler)
- #httpd.socket = ssl.wrap_socket (httpd.socket, certfile=cert_file, server_side=True)
- print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
- try:
- httpd.serve_forever()
- except KeyboardInterrupt:
- pass
- httpd.server_close()
- print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
Advertisement
Add Comment
Please, Sign In to add comment