Add api call: delete service from repository

Delete manifest files and all service files.
Check if service files are used in other services (by parsing manifests)

Change-Id: I77af0c5aa3213c77412a09ea223f74dceddb5713
This commit is contained in:
Ekaterina Fedorova 2013-10-30 18:04:54 +04:00
parent 38a5426553
commit 999e73bbed
2 changed files with 53 additions and 2 deletions

View File

@ -18,7 +18,6 @@ workflows:
heat:
- Demo.template
- Linux.template
agent:
- Demo.template

View File

@ -66,7 +66,7 @@ def _get_archive(client, hash_sum):
existing_hash = archive_manager.get_existing_hash(cache_dir)
if existing_hash and hash_sum is None:
log.debug('transferring existing archive')
log.debug('Transferring existing archive')
return os.path.join(cache_dir, existing_hash, ARCHIVE_PKG_NAME)
if archive_manager.hashes_match(cache_dir, existing_hash, hash_sum):
@ -130,6 +130,16 @@ def _get_manifest_files(manifest):
if k in DATA_TYPES)
def _exclude_common_files(files_for_deletion, manifests):
all_manifest_files = [_get_manifest_files(manifest)
for manifest in manifests]
for data_type, files in files_for_deletion.items():
files_for_deletion[data_type] = set(files_for_deletion[data_type])
for manifest_files in all_manifest_files:
files_for_deletion[data_type] -= set(manifest_files[data_type])
return files_for_deletion
@v1_api.route('/client/<path:client_type>')
def get_archive_data(client_type):
if client_type not in CLIENTS_DICT.keys():
@ -287,3 +297,45 @@ def upload_new_service(service_name):
else:
#ToDo: Pass error msg there
return make_response('Uploading file failed.', 400)
@v1_api.route('/admin/services/<service_name>', methods=['DELETE'])
def delete_service(service_name):
#TODO: Handle situation when error occurred in the middle of deleting.
# Need to repair already deleted files
if not re.match(r'^\w+(\.\w+)*\w+$', service_name):
abort(404)
manifests = ManifestParser().parse()
manifest_for_deletion = None
# Search for manifest to delete
for manifest in manifests:
if manifest.full_service_name == service_name:
manifest_for_deletion = manifest
files_for_deletion = _get_manifest_files(manifest_for_deletion)
manifests.remove(manifest_for_deletion)
break
if not manifest_for_deletion:
abort(404)
files_for_deletion = _exclude_common_files(files_for_deletion, manifests)
path_to_manifest = os.path.join(CONF.manifests,
manifest_for_deletion.manifest_file_name)
log.debug('Deleting manifest file {0}'.format(path_to_manifest))
if os.path.exists(path_to_manifest):
os.remove(path_to_manifest)
for data_type, files in files_for_deletion.iteritems():
for file in files:
path_to_delete = os.path.join(CONF.manifests,
getattr(CONF, data_type),
file)
try:
log.debug('Removing file {0} corresponds to {1} '
'service'.format(path_to_delete, service_name))
if os.path.exists(path_to_delete):
os.remove(path_to_delete)
except Exception:
return make_response('Unable to delete file {0}'.format(file),
500)
return jsonify(result='success')