Skip to content

Commit

Permalink
Enabling rollbacks of environment builds (#93)
Browse files Browse the repository at this point in the history
* Enabling rollbacks of environments

* Black formatting
  • Loading branch information
costrouc authored Jul 25, 2021
1 parent 556f640 commit d1bf811
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
9 changes: 6 additions & 3 deletions conda-store-server/conda_store_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ def list_environments(db, namespace: str = None, search=None):
return db.query(orm.Environment).filter(*filters).all()


def get_environment(db, name, namespace: str = None):
filters = [orm.Environment.name == name]

def get_environment(db, name: str = None, namespace: str = None, id: int = None):
filters = []
if name:
filters.append(orm.Environment.name == name)
if namespace:
filters.append(orm.Environment.namespace == namespace)
if id:
filters.append(orm.Environment.id == id)

return db.query(orm.Environment).filter(*filters).first()

Expand Down
21 changes: 21 additions & 0 deletions conda-store-server/conda_store_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,24 @@ def create_build(self, specification_sha256):
).apply_async()

return build

def update_environment_build(self, name, build_id):
build = api.get_build(self.db, build_id)
if build.status != orm.BuildStatus.COMPLETED:
raise ValueError(
"cannot update environment to build id since not completed"
)

if build.specification.name != name:
raise ValueError(
"cannot update environment to build id since specification does not match environment name"
)

environment = api.get_environment(self.db, name)

self.celery_app

# must import tasks after a celery app has been initialized
from conda_store_server.worker import tasks

tasks.task_update_environment_build.si(environment.id, build.id).apply_async()
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ <h3>Builds</h3>
<a href="/build/{{ build.id }}/">Build {{ build.id }}</a>
<span>{{ build.status.value }}</span>
<div class="btn-group" role="group" aria-label="Build actions">
{% if build.id != environment.build_id and build.status.value == 'COMPLETED' %}
<button type="button" onclick="updateEnvironmentBuild('{{ build.id }}')" class="btn btn-primary mb-2">
<ion-icon name="checkmark"></ion-icon>
</button>
{% endif %}
<button type="button" onclick="buildAction('PUT', '{{ build.id }}')" class="btn btn-primary mb-2">
<ion-icon name="refresh"></ion-icon>
</button>
Expand All @@ -42,6 +47,16 @@ <h3>Builds</h3>
</ul>

<script>
function updateEnvironmentBuild(buildId) {
fetch(`/api/v1/environment/{{ environment.name }}/`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({"buildId": buildId})
})
}

function buildAction(method, buildId) {
fetch(`/api/v1/build/${buildId}/`, {
method: method,
Expand Down
8 changes: 8 additions & 0 deletions conda-store-server/conda_store_server/server/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def api_get_environment(name):
return jsonify(environment)


@app_api.route("/api/v1/environment/<name>/", methods=["PUT"])
def api_update_environment_build(name):
conda_store = get_conda_store()
build_id = request.json["buildId"]
conda_store.update_environment_build(name, build_id)
return jsonify({"status": "ok"})


@app_api.route("/api/v1/specification/", methods=["GET"])
def api_list_specification():
conda_store = get_conda_store()
Expand Down
18 changes: 17 additions & 1 deletion conda-store-server/conda_store_server/worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import yaml

from conda_store_server.worker.utils import create_worker
from conda_store_server import api, environment
from conda_store_server import api, environment, utils
from conda_store_server.build import (
build_conda_environment,
build_conda_env_export,
Expand Down Expand Up @@ -67,3 +67,19 @@ def task_build_conda_docker(build_id):
conda_store = create_worker().conda_store
build = api.get_build(conda_store.db, build_id)
build_conda_docker(conda_store, build)


@task(name="task_update_environment_build")
def task_update_environment_build(environment_id, build_id):
conda_store = create_worker().conda_store
build = api.get_build(conda_store.db, build_id)

conda_prefix = build.build_path(conda_store.store_directory)
environment_prefix = build.environment_path(conda_store.environment_directory)

utils.symlink(conda_prefix, environment_prefix)

environment = api.get_environment(conda_store.db, id=environment_id)
environment.build_id = build.id
environment.specification_id = build.specification.id
conda_store.db.commit()

0 comments on commit d1bf811

Please sign in to comment.