From cc712fee6d1837da589ff17f751171ec7258bd84 Mon Sep 17 00:00:00 2001 From: Wilmer Uruchi Ticona Date: Wed, 17 Jul 2019 11:35:37 +0200 Subject: [PATCH 1/3] Fixed Issue #386. Now eadmin user can delete any experiment regardless of owner, iff -f is included. --- autosubmit/autosubmit.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/autosubmit/autosubmit.py b/autosubmit/autosubmit.py index 1983dbce5..ac15d7f02 100644 --- a/autosubmit/autosubmit.py +++ b/autosubmit/autosubmit.py @@ -489,22 +489,35 @@ class Autosubmit: return False @staticmethod - def _delete_expid(expid_delete): + def _delete_expid(expid_delete, force): """ Removes an experiment from path and database + If current user is eadmin and -f has been sent, it deletes regardless + of experiment owner :type expid_delete: str :param expid_delete: identifier of the experiment to delete + :type force: boolean + :param force: True if the force flag has been sent + :return: True if succesfully deleted, False otherwise + :rtype: boolean """ + # Read current user uid + my_user = os.getuid() + # Read eadmin user uid + id_eadmin = os.popen('id -u eadmin').read() + if expid_delete == '' or expid_delete is None and not os.path.exists(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid_delete)): Log.info("Experiment directory does not exist.") else: - Log.info("Removing experiment directory...") ret = False - if pwd.getpwuid(os.stat(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid_delete)).st_uid).pw_name == os.getlogin(): + currentOwner = pwd.getpwuid(os.stat(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid_delete)).st_uid).pw_name + if currentOwner == os.getlogin() or (force and my_user == id_eadmin): + if (force and my_user == id_eadmin): + Log.info("Preparing deletion of experiment %s from owner: %s, as eadmin." % (expid_delete,currentOwner)) try: - + Log.info("Removing experiment directory...") shutil.rmtree(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid_delete)) except OSError as e: Log.warning('Can not delete experiment folder: {0}', e) @@ -513,9 +526,10 @@ class Autosubmit: ret = delete_experiment(expid_delete) if ret: Log.result("Experiment {0} deleted".format(expid_delete)) - else: + else: Log.warning("Current User is not the Owner {0} can not be deleted!",expid_delete) return ret + @staticmethod def expid(hpc, description, copy_id='', dummy=False, test=False, operational=False): """ @@ -654,6 +668,7 @@ class Autosubmit: :returns: True if succesful, False if not :rtype: bool """ + log_path = os.path.join(BasicConfig.LOCAL_ROOT_DIR, "ASlogs", 'delete.log'.format(os.getuid())) try: Log.set_file(log_path) @@ -662,7 +677,8 @@ class Autosubmit: if os.path.exists(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid)): if force or Autosubmit._user_yes_no_query("Do you want to delete " + expid + " ?"): - return Autosubmit._delete_expid(expid) + print('Enter Autosubmit._delete_expid %s' % expid) + return Autosubmit._delete_expid(expid, force) else: Log.info("Quitting...") return False -- GitLab From 20dae0ab24999aebc3fd8749fe08c8989f82e0f8 Mon Sep 17 00:00:00 2001 From: Wilmer Uruchi Ticona Date: Wed, 17 Jul 2019 12:09:36 +0200 Subject: [PATCH 2/3] Added handling of failue of retrieval of user data. --- autosubmit/autosubmit.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/autosubmit/autosubmit.py b/autosubmit/autosubmit.py index ac15d7f02..86d660d9e 100644 --- a/autosubmit/autosubmit.py +++ b/autosubmit/autosubmit.py @@ -512,8 +512,21 @@ class Autosubmit: Log.info("Experiment directory does not exist.") else: ret = False - currentOwner = pwd.getpwuid(os.stat(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid_delete)).st_uid).pw_name - if currentOwner == os.getlogin() or (force and my_user == id_eadmin): + + # Handling possible failure of retrieval of current owner data + currentOwner_id = 0 + currentOwner = "empty" + try: + currentOwner = os.stat(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid_delete)).st_uid + currentOwner_id = pwd.getpwuid(os.stat(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid_delete)).st_uid).pw_name + except: + pass + finally: + Log.info("Current owner '%s' of experiment %s does not exist anymore." % (currentOwner, expid_delete)) + + # Deletion workflow continues as usual, a disjunction is included for the case when + # force is sent, and user is eadmin + if currentOwner_id == os.getlogin() or (force and my_user == id_eadmin): if (force and my_user == id_eadmin): Log.info("Preparing deletion of experiment %s from owner: %s, as eadmin." % (expid_delete,currentOwner)) try: @@ -529,7 +542,7 @@ class Autosubmit: else: Log.warning("Current User is not the Owner {0} can not be deleted!",expid_delete) return ret - + @staticmethod def expid(hpc, description, copy_id='', dummy=False, test=False, operational=False): """ -- GitLab From 8dcdaa7ade9567b6d6193fb80e587549537bffe0 Mon Sep 17 00:00:00 2001 From: Wilmer Uruchi Ticona Date: Wed, 17 Jul 2019 15:32:06 +0200 Subject: [PATCH 3/3] Log.critical and Log.warning input improved. Corrected a bug in exception handling. Log.debug included according to suggestions. --- autosubmit/autosubmit.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/autosubmit/autosubmit.py b/autosubmit/autosubmit.py index 86d660d9e..d5595cae8 100644 --- a/autosubmit/autosubmit.py +++ b/autosubmit/autosubmit.py @@ -522,13 +522,14 @@ class Autosubmit: except: pass finally: - Log.info("Current owner '%s' of experiment %s does not exist anymore." % (currentOwner, expid_delete)) + if currentOwner_id == 0: + Log.info("Current owner '{0}' of experiment {1} does not exist anymore.", currentOwner, expid_delete) # Deletion workflow continues as usual, a disjunction is included for the case when # force is sent, and user is eadmin if currentOwner_id == os.getlogin() or (force and my_user == id_eadmin): if (force and my_user == id_eadmin): - Log.info("Preparing deletion of experiment %s from owner: %s, as eadmin." % (expid_delete,currentOwner)) + Log.info("Preparing deletion of experiment {0} from owner: {1}, as eadmin.", expid_delete, currentOwner) try: Log.info("Removing experiment directory...") shutil.rmtree(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid_delete)) @@ -540,7 +541,7 @@ class Autosubmit: if ret: Log.result("Experiment {0} deleted".format(expid_delete)) else: - Log.warning("Current User is not the Owner {0} can not be deleted!",expid_delete) + Log.critical("Current user is not the owner of the experiment. {0} can not be deleted!",expid_delete) return ret @staticmethod @@ -690,7 +691,7 @@ class Autosubmit: if os.path.exists(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid)): if force or Autosubmit._user_yes_no_query("Do you want to delete " + expid + " ?"): - print('Enter Autosubmit._delete_expid %s' % expid) + Log.debug('Enter Autosubmit._delete_expid {0}', expid) return Autosubmit._delete_expid(expid, force) else: Log.info("Quitting...") -- GitLab