Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dd80008

Browse files
committed
add docker-hook support
1 parent e9c09f4 commit dd80008

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

‎Dockerfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,27 @@ sed -i -e "s/^;clear_env = no$/clear_env = no/" ${fpm_conf} &&\
9797
ln -s /etc/php5/php.ini /etc/php5/conf.d/php.ini && \
9898
find /etc/php5/conf.d/ -name "*.ini" -exec sed -i -re 's/^(\s*)#(.*)/1円;2円/g' {} \;
9999

100+
# Install/setup Python deps
101+
RUN pip install requests
102+
100103
# Add Scripts
101104
ADD scripts/start.sh /start.sh
102105
ADD scripts/pull /usr/bin/pull
103106
ADD scripts/push /usr/bin/push
104107
ADD scripts/letsencrypt-setup /usr/bin/letsencrypt-setup
105108
ADD scripts/letsencrypt-renew /usr/bin/letsencrypt-renew
109+
ADD scripts/docker-hook /usr/bin/docker-hook
110+
ADD scripts/hook-listener /usr/bin/hook-listener
111+
112+
# Setup permissions
106113
RUN chmod 755 /usr/bin/pull && chmod 755 /usr/bin/push && chmod 755 /usr/bin/letsencrypt-setup && chmod 755 /usr/bin/letsencrypt-renew && chmod 755 /start.sh
114+
RUN chmod +x /usr/bin/docker-hook
115+
RUN chmod +x /usr/bin/hook-listener
107116

108117
# copy in code
109118
ADD src/ /var/www/html/
110119

111-
EXPOSE 443 80
120+
EXPOSE 443 80 8555
112121

113122
#CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]
114123
CMD ["/start.sh"]

‎conf/supervisord.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ stdout_logfile=/dev/stdout
4242
stdout_logfile_maxbytes=0
4343
stderr_logfile=/dev/stderr
4444
stderr_logfile_maxbytes=0
45+
46+
[program:hook-listener]
47+
command=/usr/bin/hook-listener
48+
autostart=true
49+
autorestart=true
50+
priority=20

‎scripts/docker-hook

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""Automatic Docker Deployment via Webhooks"""
5+
6+
import json
7+
import os
8+
from subprocess import Popen
9+
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
10+
try:
11+
# For Python 3.0 and later
12+
from http.server import HTTPServer
13+
from http.server import BaseHTTPRequestHandler
14+
except ImportError:
15+
# Fall back to Python 2
16+
from BaseHTTPServer import BaseHTTPRequestHandler
17+
from BaseHTTPServer import HTTPServer as HTTPServer
18+
import sys
19+
import logging
20+
import requests
21+
22+
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
23+
level=logging.DEBUG,
24+
stream=sys.stdout)
25+
26+
27+
class RequestHandler(BaseHTTPRequestHandler):
28+
"""A POST request handler which expects a token in its path."""
29+
def do_POST(self):
30+
logging.info("Path: %s", self.path)
31+
header_length = int(self.headers.getheader('content-length', "0"))
32+
json_payload = self.rfile.read(header_length)
33+
env = dict(os.environ)
34+
json_params = {}
35+
if len(json_payload) > 0:
36+
json_params = json.loads(json_payload)
37+
env.update(('REPOSITORY_' + var.upper(), str(val))
38+
for var, val in json_params['repository'].items())
39+
40+
# Check if the secret URL was called
41+
if args.token in self.path:
42+
logging.info("Start executing '%s'" % args.cmd)
43+
try:
44+
Popen(args.cmd, env=env).wait()
45+
self.send_response(200, "OK")
46+
if 'callback_url' in json_params:
47+
# Make a callback to Docker Hub
48+
data = {'state': 'success'}
49+
headers = {'Content-type': 'application/json',
50+
'Accept': 'text/plain'}
51+
requests.post(json_params['callback_url'],
52+
data=json.dumps(data),
53+
headers=headers)
54+
except OSError as err:
55+
self.send_response(500, "OSError")
56+
logging.error("You probably didn't use 'sh ./script.sh'.")
57+
logging.error(err)
58+
if 'callback_url' in json_params:
59+
# Make a callback to Docker Hub
60+
data = {'state': 'failure',
61+
'description': str(err)}
62+
headers = {'Content-type': 'application/json',
63+
'Accept': 'text/plain'}
64+
requests.post(json_params['callback_url'],
65+
data=json.dumps(data),
66+
headers=headers)
67+
else:
68+
self.send_response(401, "Not authorized")
69+
self.end_headers()
70+
71+
72+
def get_parser():
73+
"""Get a command line parser for docker-hook."""
74+
parser = ArgumentParser(description=__doc__,
75+
formatter_class=ArgumentDefaultsHelpFormatter)
76+
77+
parser.add_argument("-t", "--token",
78+
dest="token",
79+
required=True,
80+
help=("Secure auth token (can be choosen "
81+
"arbitrarily)"))
82+
parser.add_argument("-c", "--cmd",
83+
dest="cmd",
84+
required=True,
85+
nargs="*",
86+
help="Command to execute when triggered")
87+
parser.add_argument("--addr",
88+
dest="addr",
89+
default="0.0.0.0",
90+
help="address where it listens")
91+
parser.add_argument("--port",
92+
dest="port",
93+
type=int,
94+
default=8555,
95+
metavar="PORT",
96+
help="port where it listens")
97+
return parser
98+
99+
100+
def main(addr, port):
101+
"""Start a HTTPServer which waits for requests."""
102+
httpd = HTTPServer((addr, port), RequestHandler)
103+
httpd.serve_forever()
104+
105+
if __name__ == '__main__':
106+
parser = get_parser()
107+
if len(sys.argv) == 1:
108+
parser.print_help()
109+
sys.exit(1)
110+
args = parser.parse_args()
111+
main(args.addr, args.port)

‎scripts/hook-listener

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
docker-hook -t "$GIT_HOOK_TOKEN" -c pull

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /