Commit 8b135df3 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added replace function to mirror CLI.

parent dcc2291a
Pipeline #67620 canceled with stages
in 2 minutes and 57 seconds
......@@ -168,6 +168,31 @@ def index(ctx, uploads):
print(' indexed %d of %d uploads, failed to index %d entries' % (i, uploads_count, failed))
def delete_upload(upload, skip_es: bool = False, skip_files: bool = False, skip_mongo: bool = False):
# delete elastic
if not skip_es:
search.delete_upload(upload_id=upload.upload_id)
# delete files
if not skip_files:
# do it twice to get the two potential versions 'public' and 'staging'
for _ in range(0, 2):
upload_files = files.UploadFiles.get(upload_id=upload.upload_id)
try:
if upload_files is not None:
upload_files.delete()
except Exception as e:
logger = utils.get_logger(__name__)
logger.error('could not delete files', exc_info=e)
break
# delete mongo
if not skip_mongo:
proc.Calc.objects(upload_id=upload.upload_id).delete()
upload.delete()
@uploads.command(help='Delete selected upload')
@click.argument('UPLOADS', nargs=-1)
@click.option('--skip-es', help='Keep the elastic index version of the data.', is_flag=True)
......@@ -177,31 +202,10 @@ def index(ctx, uploads):
def rm(ctx, uploads, skip_es, skip_mongo, skip_files):
_, uploads = query_uploads(ctx, uploads)
logger = utils.get_logger(__name__)
print('%d uploads selected, deleting ...' % uploads.count())
for upload in uploads:
# delete elastic
if not skip_es:
search.delete_upload(upload_id=upload.upload_id)
# delete files
if not skip_files:
# do it twice to get the two potential versions 'public' and 'staging'
for _ in range(0, 2):
upload_files = files.UploadFiles.get(upload_id=upload.upload_id)
try:
if upload_files is not None:
upload_files.delete()
except Exception as e:
logger.error('could not delete files', exc_info=e)
break
# delete mongo
if not skip_mongo:
proc.Calc.objects(upload_id=upload.upload_id).delete()
upload.delete()
delete_upload(upload, skip_es=skip_es, skip_mongo=skip_mongo, skip_files=skip_files)
def __run_processing(
......
......@@ -22,6 +22,7 @@ from bravado.exception import HTTPBadRequest
from nomad import utils, processing as proc, search, config, files, infrastructure
from nomad.datamodel import Dataset, User
from nomad.cli.admin.uploads import delete_upload
from .client import client
......@@ -160,9 +161,13 @@ class Mapping:
'--staging', is_flag=True, default=False,
help='Mirror non published uploads. Only works with --move or --link.'
)
@click.option(
'--replace', is_flag=True, default=False,
help='Replace existing uploads.'
)
def mirror(
query, move: bool, link: bool, dry: bool, files_only: bool, source_mapping: str,
target_mapping: str, migration: str, staging: bool):
target_mapping: str, migration: str, staging: bool, replace: bool):
if staging and not (move or link):
print('--with-staging requires either --move or --link')
......@@ -211,11 +216,16 @@ def mirror(
# In tests, we mirror from our selves, fake that the upload does not exist
raise KeyError()
if len(query) > 0:
print(
'Upload %s already exists, updating existing uploads is not implemented yet. '
'Skip upload.' % upload_id)
continue
if replace:
delete_upload(upload=upload)
else:
if len(query) > 0:
print(
'Upload %s already exists, updating existing uploads is not '
'implemented yet. Skip upload.' % upload_id)
continue
except KeyError:
pass
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment