Add DELETE /compose/delete/<uuids> API route

This will delete all of the build's results and cannot be undone.
This commit is contained in:
Brian C. Lane 2018-02-01 17:35:24 -08:00
parent 3fdf3a9819
commit b99a7756de
2 changed files with 65 additions and 2 deletions

View File

@ -18,8 +18,12 @@ import logging
log = logging.getLogger("pylorax") log = logging.getLogger("pylorax")
import os import os
import grp
from glob import glob from glob import glob
import pytoml as toml import pytoml as toml
import pwd
import shutil
import subprocess
import time import time
from pykickstart.version import makeVersion, RHEL7 from pykickstart.version import makeVersion, RHEL7
from pykickstart.parser import KickstartParser from pykickstart.parser import KickstartParser
@ -112,6 +116,12 @@ def make_compose(cfg, results_dir):
log.debug("repo_url = %s, cfg = %s", repo_url, install_cfg) log.debug("repo_url = %s, cfg = %s", repo_url, install_cfg)
novirt_install(install_cfg, joinpaths(results_dir, install_cfg.image_name), None, repo_url) novirt_install(install_cfg, joinpaths(results_dir, install_cfg.image_name), None, repo_url)
# Make sure that everything under the results directory is owned by the user
user = pwd.getpwuid(cfg.uid).pw_name
group = grp.getgrgid(cfg.gid).gr_name
log.debug("Install finished, chowning results to %s:%s", user, group)
subprocess.call(["chown", "-R", "%s:%s" % (user, group), results_dir])
def compose_detail(results_dir): def compose_detail(results_dir):
""" Return details about the build.""" """ Return details about the build."""
@ -192,3 +202,19 @@ def build_status(cfg, status_filter=None):
if status in status_filter: if status in status_filter:
results.append(compose_detail(build)) results.append(compose_detail(build))
return results return results
def uuid_delete(cfg, uuid):
"""Delete all of the results from a compose
:param cfg: Configuration settings
:type cfg: ComposerConfig
:param uuid: The UUID of the build
:type uuid: str
:returns: True if it was deleted
:rtype: bool
"""
uuid_dir = joinpaths(cfg.get("composer", "lib_dir"), "results", uuid)
if not uuid_dir or len(uuid_dir) < 10:
raise RuntimeError("Directory length is too short: %s" % uuid_dir)
shutil.rmtree(uuid_dir)
return True

View File

@ -718,6 +718,24 @@ POST `/api/v0/recipes/tag/<recipe_name>`
} }
] ]
} }
DELETE `/api/v0/compose/delete/<uuids>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Delete the list of comma-separated uuids from the compose results.
Example::
{
"errors": [],
"uuids": [
{
"status": true,
"uuid": "ae1bf7e3-7f16-4c9f-b36e-3726a1093fd0"
}
]
}
""" """
import logging import logging
@ -729,7 +747,7 @@ from pylorax.api.compose import start_build, compose_types
from pylorax.api.crossdomain import crossdomain from pylorax.api.crossdomain import crossdomain
from pylorax.api.projects import projects_list, projects_info, projects_depsolve from pylorax.api.projects import projects_list, projects_info, projects_depsolve
from pylorax.api.projects import modules_list, modules_info, ProjectsError from pylorax.api.projects import modules_list, modules_info, ProjectsError
from pylorax.api.queue import queue_status, build_status, uuid_status from pylorax.api.queue import queue_status, build_status, uuid_delete, uuid_status
from pylorax.api.recipes import list_branch_files, read_recipe_commit, recipe_filename, list_commits from pylorax.api.recipes import list_branch_files, read_recipe_commit, recipe_filename, list_commits
from pylorax.api.recipes import recipe_from_dict, recipe_from_toml, commit_recipe, delete_recipe, revert_recipe from pylorax.api.recipes import recipe_from_dict, recipe_from_toml, commit_recipe, delete_recipe, revert_recipe
from pylorax.api.recipes import tag_recipe_commit, recipe_diff from pylorax.api.recipes import tag_recipe_commit, recipe_diff
@ -1257,7 +1275,7 @@ def v0_api(api):
@api.route("/api/v0/compose/status/<uuids>") @api.route("/api/v0/compose/status/<uuids>")
@crossdomain(origin="*") @crossdomain(origin="*")
def v0_compose_status(uuids=None): def v0_compose_status(uuids):
"""Return the status of the listed uuids""" """Return the status of the listed uuids"""
results = [] results = []
for uuid in [n.strip().lower() for n in uuids.split(",")]: for uuid in [n.strip().lower() for n in uuids.split(",")]:
@ -1266,3 +1284,22 @@ def v0_api(api):
results.append(details) results.append(details)
return jsonify(uuids=results) return jsonify(uuids=results)
@api.route("/api/v0/compose/delete/<uuids>", methods=["DELETE"])
@crossdomain(origin="*")
def v0_compose_delete(uuids):
"""Delete the compose results for the listed uuids"""
results = []
errors = []
for uuid in [n.strip().lower() for n in uuids.split(",")]:
status = uuid_status(api.config["COMPOSER_CFG"], uuid)
if status["status"] not in ["FINISHED", "FAILED"]:
errors.append({"uuid":uuid, "msg":"Build not in FINISHED or FAILED."})
else:
try:
uuid_delete(api.config["COMPOSER_CFG"], uuid)
except Exception as e:
errors.append({"uuid":uuid, "msg":str(e)})
else:
results.append({"uuid":uuid, "status":True})
return jsonify(uuids=results, errors=errors)