1
0
mirror of https://github.com/IEEE-SB-Passau/pelican-deployment-system.git synced 2017-09-06 16:35:38 +02:00

add way to remove the working dir contents of a runner

This commit is contained in:
2016-06-20 02:12:08 +02:00
parent df8b8e915b
commit e461106ea2
2 changed files with 58 additions and 7 deletions

View File

@@ -19,8 +19,9 @@ from functools import partial
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from pelican_deploy.util import exception_logged from pelican_deploy.util import exception_logged
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from threading import RLock from threading import RLock, Thread
from datetime import datetime from datetime import datetime
from shutil import rmtree
import pytz import pytz
import sys import sys
import logging import logging
@@ -63,6 +64,7 @@ class DeploymentRunner:
output=outdir, toxresult=toxresult) output=outdir, toxresult=toxresult)
self.final_install_command = runner_config["final_install_command"]\ self.final_install_command = runner_config["final_install_command"]\
.format(output=outdir) .format(output=outdir)
self._output_dir = outdir
self._build_proc_env = dict(os.environ, self._build_proc_env = dict(os.environ,
**runner_config.get("build_env", {})) **runner_config.get("build_env", {}))
@@ -76,6 +78,41 @@ class DeploymentRunner:
self.build_status = deque(maxlen=STATUS_LEN) self.build_status = deque(maxlen=STATUS_LEN)
def clean_working_dir(self, abort_running=True):
Thread(target=self.clean_working_dir_blocking).start()
def clean_working_dir_blocking(self, abort_running=True):
def clean_fn():
rmtree(str(self.build_repo_path))
rmtree(str(self._output_dir))
with self._build_lock:
if abort_running:
self.try_abort_build()
# cancel everything, so we are next
for fut in self._futures.copy():
fut.cancel()
if fut.done():
self._futures.remove(fut)
def build_job():
log.info("Starting cleaning of working dir!")
self.update_status(True, "Starting cleaning of working dir!",
running=False)
try:
exception_logged(clean_fn, log.error)()
except Exception as e:
self.update_status(False, "Cleaning failed!",
running=False, payload={"exception": e})
raise
future = self._executor.submit(build_job)
self._futures.add(future)
future.result()
log.info("Working dir cleand!")
self.update_status(True, "Working dir cleand!", running=False)
def update_status(self, ok, msg, payload=None, running=True): def update_status(self, ok, msg, payload=None, running=True):
date = pytz.utc.localize(datetime.utcnow()) date = pytz.utc.localize(datetime.utcnow())
self.build_status.append(BuildStatus(date, ok, msg, payload, running)) self.build_status.append(BuildStatus(date, ok, msg, payload, running))
@@ -148,7 +185,8 @@ class DeploymentRunner:
for r in results: for r in results:
log_git(r) log_git(r)
def build(self, abort_running=False, wait=False, ignore_pull_error=False): def build(self, abort_running=False, wait=False, ignore_pull_error=False,
build_fn=None):
with self._build_lock: with self._build_lock:
if abort_running: if abort_running:
self.try_abort_build() self.try_abort_build()
@@ -159,10 +197,12 @@ class DeploymentRunner:
if fut.done(): if fut.done():
self._futures.remove(fut) self._futures.remove(fut)
def build_job(): build_bl = partial(self._build_blocking, ignore_pull_error=
build_bl = partial(self._build_blocking, ignore_pull_error=
ignore_pull_error) ignore_pull_error)
build_func = exception_logged(build_bl, log.error) build_fn = build_fn if build_fn else build_bl
def build_job():
build_func = exception_logged(build_fn, log.error)
try: try:
build_func() build_func()
except Exception as e: except Exception as e:

View File

@@ -100,8 +100,12 @@ def runnerstatus(name):
end = end if end >= 0 else 0 end = end if end >= 0 else 0
tpl = """ tpl = """
<html> <html>
<h1>{{runner.name}} status events ({{start}} - {{end}}) -- <h1>{{runner.name}} status events ({{start}} - {{end}})</h1>
<a href={{runner.name}}/rerun>(re)start build</a></h1> <p>
<a href={{runner.name}}/rerun>(re)start build</a> --
<a href={{runner.name}}/clean_working_dir>clean working dir (use e.g. if
repository is somehow in a broken state)</a>
</p>
<ul> <ul>
% for bs in islice(reversed(bss),start,end): % for bs in islice(reversed(bss),start,end):
<% <%
@@ -129,3 +133,10 @@ def rerun(name):
runner = _get_runner(name) runner = _get_runner(name)
runner.build(abort_running=True, ignore_pull_error=True) runner.build(abort_running=True, ignore_pull_error=True)
return "Restarted the build" return "Restarted the build"
@_auth_basic
@app.route('/<name>/clean_working_dir')
def rerun(name):
runner = _get_runner(name)
runner.clean_working_dir()
return "Invoked cleaning of working dir"