Archived
Settings file, deploy script, cleanups.
This commit is contained in:
1 parent
81ffafad5f
commit
d4e1e13453
4 files changed
+64
-30
No files matched your search
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
send_to_web.sh
|
|
||||||
*.swp
|
*.swp
|
||||||
*.pyc
|
*.pyc
|
||||||
pings.log
|
pings.log
|
||||||
|
settings.py
|
||||||
Executable
+3
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Checkout from GIT_DIR (--bare) into GIT_WORK_TREE
|
||||||
|
GIT_DIR=/var/cache/git/repositories/$2 GIT_WORK_TREE=$1 git checkout -f
|
||||||
@@ -5,10 +5,20 @@ import web
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
from socket import socket, AF_INET, SOCK_DGRAM
|
from socket import socket, AF_INET, SOCK_DGRAM
|
||||||
|
|
||||||
|
import settings
|
||||||
"""
|
"""
|
||||||
Recieve pings from Github and:
|
Recieve pings from Github Webhook API and perform actions.
|
||||||
- Sync local repo and web.
|
|
||||||
- Send a message to snurr (udp2irc-bot).
|
Examples of actions:
|
||||||
|
- Run a script to deploy on your webserver. Yup.
|
||||||
|
- Send a message to IRC (via an ircbot listening for UDP-messages). Yup.
|
||||||
|
- Send an email.
|
||||||
|
- Send a signal to an Arduino.
|
||||||
|
|
||||||
|
TODO:
|
||||||
|
- Provide deploy log
|
||||||
|
- Send mail to commiter on error.
|
||||||
|
|
||||||
Written in web.py
|
Written in web.py
|
||||||
"""
|
"""
|
||||||
@@ -18,52 +28,64 @@ urls = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
class ping:
|
class ping:
|
||||||
# Configuration
|
|
||||||
SCRIPT_PATH = '/opt/github-hooks-recieve/send_to_web.sh'
|
|
||||||
WEB_PATH = '/var/www/neuf.no/new/'
|
|
||||||
ALLOWED_REPO_URLS = ['https://github.com/neuf/main',
|
|
||||||
'https://github.com/neuf/DNSapp',
|
|
||||||
'http://git.neuf.no/?p=webpy-ping.git']
|
|
||||||
|
|
||||||
def script_path(self):
|
def script_path(self):
|
||||||
return os.path.dirname(os.path.abspath(__file__))
|
return os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
def url_allowed(self,url):
|
||||||
|
for allowed_url, deploy_to in settings.REPOS.iteritems():
|
||||||
|
if url == allowed_url:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def POST(self):
|
def POST(self):
|
||||||
post_data = web.input()
|
post_data = web.input()
|
||||||
try:
|
try:
|
||||||
data = json.loads(post_data['payload'])
|
data = json.loads(post_data['payload'])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self.log("Got ping with non-JSON POST-data" + post_data)
|
error = "Got ping with non-JSON POST-data: \'" + str(post_data) + "\'"
|
||||||
exit()
|
self.log(error)
|
||||||
|
return json.dumps({'result': error})
|
||||||
|
|
||||||
self.log("post_data: " + str(post_data))
|
repo_url = data['repository']['url']
|
||||||
if data['repository']['url'] not in self.ALLOWED_REPO_URLS:
|
repo_name = data['repository']['name']
|
||||||
self.log("Repository URL " + data['repository']['url'] + "not allowed, exiting.")
|
self.log("Got ping. Repo: " + repo_name + " Commit by: " + self.format_who(data['commits']) + ".")
|
||||||
exit()
|
if not self.url_allowed(repo_url):
|
||||||
|
not_allowed = "Repository URL \'" + repo_url + "\' not allowed."
|
||||||
|
self.log(not_allowed)
|
||||||
|
return not_allowed
|
||||||
|
|
||||||
#self.update_repo(data) # temp disabled
|
# TODO: Make optional in settings
|
||||||
|
self.update_repo(data['repository']['name'], repo_url)
|
||||||
self.send_to_irc(data)
|
self.send_to_irc(data)
|
||||||
|
|
||||||
return "thank you :-)"
|
return json.dumps({'result' : "Thank you :-)"})
|
||||||
|
|
||||||
def update_repo(self, data):
|
def update_repo(self, name, url):
|
||||||
# Update repository locally and push to web
|
if not settings.REPOS[url]:
|
||||||
args = ['/bin/sh', self.SCRIPT_PATH, self.WEB_PATH, data['repository']['url']]
|
self.log("No deploy path defined.")
|
||||||
self.log("Updating repo with command: \'" + " ".join(args) + "\'")
|
return
|
||||||
|
# run the deployscript
|
||||||
|
args = ['/bin/sh', settings.DEPLOY_SCRIPT_PATH,
|
||||||
|
settings.REPOS[url], name]
|
||||||
|
self.log("Updating repo: \'" + " ".join(args) + "\'")
|
||||||
p = Popen(args)
|
p = Popen(args)
|
||||||
|
|
||||||
def format_for_irc(self, data):
|
def format_who(self, commits):
|
||||||
# String to be sent to irc.
|
|
||||||
|
|
||||||
who = ""
|
who = ""
|
||||||
if len(data['commits']) == 1:
|
if len(commits) == 1:
|
||||||
who = data['commits'][0]['author']['name']
|
who = commits[0]['author']['name']
|
||||||
else:
|
else:
|
||||||
who = set() # unique entries
|
who = set() # unique entries
|
||||||
for commit in data['commits']:
|
for commit in commits:
|
||||||
who.add(commit['author']['name'])
|
who.add(commit['author']['name'])
|
||||||
who = ", ".join(who)
|
who = ", ".join(who)
|
||||||
|
|
||||||
|
return who
|
||||||
|
|
||||||
|
def format_for_irc(self, data):
|
||||||
|
# String to be sent to irc.
|
||||||
|
who = self.format_who(data['commits'])
|
||||||
return "Git commit: \'{0}\': {1} by {2}".format(data['repository']['name'],
|
return "Git commit: \'{0}\': {1} by {2}".format(data['repository']['name'],
|
||||||
data['compare'],
|
data['compare'],
|
||||||
who)
|
who)
|
||||||
@@ -72,7 +94,7 @@ class ping:
|
|||||||
# Send the message to snurr via UDP.
|
# Send the message to snurr via UDP.
|
||||||
data = self.format_for_irc(data)
|
data = self.format_for_irc(data)
|
||||||
udp = socket(AF_INET, SOCK_DGRAM)
|
udp = socket(AF_INET, SOCK_DGRAM)
|
||||||
udp.sendto(data, ('nintendo.neuf.no', 55666))
|
udp.sendto(data, settings.IRC_BOT_HOST)
|
||||||
|
|
||||||
def log(self, msg):
|
def log(self, msg):
|
||||||
# Log pings.
|
# Log pings.
|
||||||
@@ -80,7 +102,7 @@ class ping:
|
|||||||
log.write(datetime.now().isoformat() + ": " + msg + "\n")
|
log.write(datetime.now().isoformat() + ": " + msg + "\n")
|
||||||
log.close()
|
log.close()
|
||||||
|
|
||||||
app = web.application(urls, globals(), autoreload=False)
|
app = web.application(urls, globals(), autoreload=True)
|
||||||
application = app.wsgifunc()
|
application = app.wsgifunc()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# Configuration
|
||||||
|
|
||||||
|
DEPLOY_SCRIPT_PATH = '/opt/git-deploy.sh'
|
||||||
|
# Mappings from allowed url to deploy path.
|
||||||
|
REPOS = {
|
||||||
|
'http://example.com': '/var/www/site',
|
||||||
|
}
|
||||||
|
# Tuple with hostname and port
|
||||||
|
IRC_BOT_HOST = ('localhost',55666)
|
||||||
Reference in new issue
Block a user