2016-06-12 00:22:17 +02:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
from pelican_deploy import DeploymentRunner
|
2016-06-13 10:27:18 +02:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
|
from importlib.machinery import SourceFileLoader
|
2016-06-13 16:05:22 +02:00
|
|
|
from operator import methodcaller
|
|
|
|
|
from bottle import run, default_app
|
2016-06-13 23:10:52 +02:00
|
|
|
from wsgiref.simple_server import make_server
|
2016-06-12 04:17:54 +02:00
|
|
|
import pelican_deploy.webhookbottle
|
2016-06-12 00:22:17 +02:00
|
|
|
import logging
|
2016-06-13 23:10:52 +02:00
|
|
|
import atexit
|
|
|
|
|
import sys
|
2016-06-12 00:22:17 +02:00
|
|
|
|
2016-06-13 23:27:06 +02:00
|
|
|
def init_app(configpath):
|
2016-06-13 10:27:18 +02:00
|
|
|
|
2016-06-13 23:27:06 +02:00
|
|
|
config = SourceFileLoader("config", configpath).load_module()
|
2016-06-13 10:27:18 +02:00
|
|
|
|
|
|
|
|
runners = {name: DeploymentRunner(name, conf)
|
2016-06-13 23:10:52 +02:00
|
|
|
for name, conf in config.RUNNERS.items()}
|
2016-06-13 10:27:18 +02:00
|
|
|
|
2016-06-13 23:10:52 +02:00
|
|
|
for r in runners.values():
|
|
|
|
|
atexit.register(r.shutdown) # finally, wait for builds to finish
|
|
|
|
|
|
|
|
|
|
for r in runners.values():
|
|
|
|
|
atexit.register(r.try_abort_build) # then try to abort running builds
|
|
|
|
|
|
|
|
|
|
schedulers = {r: BackgroundScheduler(daemon=True) for r in runners}
|
2016-06-13 16:05:22 +02:00
|
|
|
for s in schedulers.values():
|
|
|
|
|
s.start()
|
2016-06-14 15:11:36 +02:00
|
|
|
atexit.register(s.shutdown, wait=False) # first stop the schedulers
|
2016-06-13 10:27:18 +02:00
|
|
|
|
2016-06-13 23:10:52 +02:00
|
|
|
atexit.register(print,
|
|
|
|
|
"<><><><><><><><><><><><><><><><><><><><><><><><><>\n",
|
|
|
|
|
">>>>> Shutting down gracefully, please wait! <<<<<\n",
|
|
|
|
|
"<><><><><><><><><><><><><><><><><><><><><><><><><>",
|
|
|
|
|
file=sys.stderr, sep="")
|
2016-06-13 10:27:18 +02:00
|
|
|
|
2016-06-13 23:10:52 +02:00
|
|
|
for i, (rname, trigger) in enumerate(config.SCHEDULED_BUILD_JOBS):
|
|
|
|
|
schedulers[rname].add_job(runners[rname].build,
|
|
|
|
|
trigger=trigger,
|
|
|
|
|
name="{} ({})".format(rname, i),
|
|
|
|
|
id="{}_{}".format(rname, i),
|
|
|
|
|
max_instances=1,
|
2016-06-14 15:03:30 +02:00
|
|
|
kwargs={"wait": True,
|
|
|
|
|
"ignore_pull_error": True})
|
2016-06-13 10:27:18 +02:00
|
|
|
|
2016-06-12 04:17:54 +02:00
|
|
|
pelican_deploy.webhookbottle.set_runners(**runners)
|
2016-06-13 10:27:18 +02:00
|
|
|
pelican_deploy.webhookbottle.set_github_secret(config.GITHUB_SECRET)
|
2016-06-13 16:05:22 +02:00
|
|
|
default_app().mount("/hooks/", pelican_deploy.webhookbottle.app)
|
|
|
|
|
|
2016-06-13 23:27:06 +02:00
|
|
|
return default_app()
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
if (len(sys.argv) != 4):
|
|
|
|
|
print("Usage: {} <configfile> <host> <port>".format(sys.argv[0]))
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
_, configpath, host, port = sys.argv
|
|
|
|
|
|
|
|
|
|
app = init_app(configpath)
|
|
|
|
|
run(app=app, host=host, port=port, debug=True)
|