Dijit

deployhook-dev.py

Oct 3rd, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3.  
  4. ## You might be asking yourself why this is python2.x
  5. ## that's because I was trying to use centos6 RPM packages.
  6. ## This code wont translate well to py3
  7. #BaseHTTPServer became http.server
  8. #The print functions are crappy.
  9. # I am so sorry.
  10. #        -Jan
  11.  
  12. import BaseHTTPServer
  13. import sys
  14. import time
  15. import urlparse
  16. import json
  17. import ssl
  18. import git
  19.  
  20. git_dirs = ['/opt/tctd-salt']
  21. cert_file = '/home/jharasym/pki/salt_webhook.pem'
  22.  
  23.  
  24.  
  25. #HOST_NAME = sys.argv[1]
  26. #PORT_NUMBER = int(sys.argv[2])
  27. HOST_NAME = '0.0.0.0' # Bind port
  28. PORT_NUMBER = 5000
  29.  
  30. def handle_hook(payload):
  31.   ### Need to check payload type.
  32.   for git_dir in git_dirs:
  33.     g = git.cmd.Git(git_dir)
  34.     g.pull()
  35.  
  36.  
  37. class HookHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  38.   server_version = "JansHookHandler/0.1"
  39.   def do_GET(s):
  40.     s.send_response(200)
  41.     s.wfile.write('Hello!')
  42.  
  43.   def do_POST(s):
  44.     # Check that the IP is within the gitlab range
  45.     if not any(s.client_address[0].startswith(IP)
  46.            for IP in ('10.129.30.40')):
  47.       print time.asctime(), "Sending 403 - %s:%s" % (HOST_NAME, PORT_NUMBER)
  48.       s.send_error(403)
  49.  
  50.  
  51.     length = int(s.headers['Content-Length'])
  52.     post_data = urlparse.parse_qs(s.rfile.read(length).decode('utf-8'))
  53.  
  54.     handle_hook(post_data)
  55.  
  56.     s.send_response(200)
  57.  
  58.  
  59. if __name__ == '__main__':
  60.   server_class = BaseHTTPServer.HTTPServer
  61.   httpd = server_class((HOST_NAME, PORT_NUMBER), HookHandler)
  62.   #httpd.socket = ssl.wrap_socket (httpd.socket, certfile=cert_file, server_side=True)
  63.   print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  64.   try:
  65.     httpd.serve_forever()
  66.   except KeyboardInterrupt:
  67.     pass
  68.   httpd.server_close()
  69.   print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
Advertisement
Add Comment
Please, Sign In to add comment