From d2255654445df542d7a095598881df1fc584fc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 29 Dec 2021 16:10:10 +0100 Subject: [PATCH] Add API and button to reset a build The, roughly, redeploys the kubernetes resources in initial state (i.e. 0 replicas and init-status=todo) so the controller starts a new initialization job. --- src/runboat/api.py | 7 ++++++ src/runboat/models.py | 22 +++++++++++++++++++ .../webui-templates/runboat-build-element.js | 5 +++++ 3 files changed, 34 insertions(+) diff --git a/src/runboat/api.py b/src/runboat/api.py index b42e0eb..b1e2e9f 100644 --- a/src/runboat/api.py +++ b/src/runboat/api.py @@ -177,6 +177,13 @@ async def stop_build(name: str) -> None: await build.stop() +@router.post("/builds/{name}/reset") +async def reset_build(name: str) -> None: + """Redeploy, so as to reinitialize.""" + build = await _build_by_name(name) + await build.redeploy() + + @router.delete("/builds/{name}", dependencies=[Depends(authenticated)]) async def undeploy_build(name: str) -> None: """Delete the deployment and drop the database.""" diff --git a/src/runboat/models.py b/src/runboat/models.py index d5609b0..abd57a2 100644 --- a/src/runboat/models.py +++ b/src/runboat/models.py @@ -220,6 +220,28 @@ class Build(BaseModel): # to remove the deployment. await k8s.delete_deployment(self.deployment_name) + async def redeploy(self) -> None: + """Redeploy a build, to reinitialize it.""" + _logger.info(f"Redeploying {self}.") + await k8s.kill_job(self.name, job_kind=k8s.DeploymentMode.cleanup) + await k8s.kill_job(self.name, job_kind=k8s.DeploymentMode.initialize) + deployment_vars = k8s.make_deployment_vars( + k8s.DeploymentMode.deployment, + self.name, + self.slug, + self.commit_info, + settings.get_build_settings( + self.commit_info.repo, self.commit_info.target_branch + )[0], + ) + await k8s.deploy(deployment_vars) + await github.notify_status( + self.commit_info.repo, + self.commit_info.git_commit, + GitHubStatusState.pending, + target_url=None, + ) + async def initialize(self) -> None: """Launch the initialization job.""" # Start initizalization job. on_initialize_{started,succeeded,failed} callbacks diff --git a/src/runboat/webui-templates/runboat-build-element.js b/src/runboat/webui-templates/runboat-build-element.js index a719f8b..fadeddc 100644 --- a/src/runboat/webui-templates/runboat-build-element.js +++ b/src/runboat/webui-templates/runboat-build-element.js @@ -78,6 +78,7 @@ class RunboatBuildElement extends LitElement {

+

`; @@ -90,6 +91,10 @@ class RunboatBuildElement extends LitElement { stopHandler(e) { fetch(`/api/v1/builds/${this.build.name}/stop`, {method: 'POST'}); } + + resetHandler(e) { + fetch(`/api/v1/builds/${this.build.name}/reset`, {method: 'POST'}); + } } export {RunboatBuildElement};