def gremove(job_name: str): """Remove a directory from the backup system with the given job name. \b Example: brs_backup remove horel-group3 brs_backup remove horel-group4 """ # Delete the job file. try: remove_job_file(job_name) except BaseException as e: LOGGER.error(e) click.echo("failed") sys.exit(1) return # Delete the FileSet file. try: remove_file_set_file(job_name) except BaseException as e: LOGGER.error(e) click.echo("failed") sys.exit(1) return reload_bconsole() push_to_gitlab(f"Ran command: brs_backup remove {job_name}") click.echo("success") sys.exit(0)
def uremove(p: bool, users: List[str]): """Remove a users's home directory from the backup system. \b Example: brs_backup uremove u0407846 brs_backup uremove u0407846 u1234567 brs_backup uremove -p u0407846 brs_backup uremove -p u0407846 u1234567 """ success = [] failures = [] if p: directories = get_dir_from_db(users) else: directories = get_pe_dir_from_db(users) for user in users: if user not in directories: failures.append(user) continue # Remove the job file. try: remove_job_file(user) except BaseException as e: LOGGER.error(e) failures.append(user) # Remove the file set file. try: remove_file_set_file(user) except BaseException as e: LOGGER.error(e) failures.append(user) success.append(user) # Only reload/push when files were changed. if success: reload_bconsole() push_to_gitlab( f"Ran command: brs_backup uremove {'-p ' if p else ''}{' '.join(users)}" ) click.echo("success: " + ", ".join(success)) click.echo("failure: " + ", ".join(failures)) if len(failures) == 0: sys.exit(0) elif len(success) == 0: sys.exit(1) else: sys.exit(2)
def test_push_to_gitlab(self): """ Test to ensure we're working with a valid git repository and can connect to GitLab (tested with git.pull()) """ util.push_to_gitlab("this is a blank commit") # Verifying the error message we should have gotten, indicating .pull() # worked. util.LOGGER.critical.assert_called_with( "Failed to push to GitLab due to: Cmd('git') failed due to: exit code(1)\n" " cmdline: git commit -m this is a blank commit\n stdout: '# On branch " "master\nnothing to commit, working directory clean'")
def add(job_name: str, directory: str, compression: str): """Add a directory to the backup system, likely being group directories. \b Example: brs_backup add horel-group3 saltflats-vg3-1-lv1.chpc.utah.edu:/uufs/saltflats/common/saltflats-vg3-1-lv1/horel brs_backup add horel-group4 saltflats-vg6-0-lv1.chpc.utah.edu:/uufs/saltflats/common/saltflats-vg6-0-lv1/horel --compression=GZIP """ # noqa: E501 # Create the FileSet file. try: if compression: write_file_set_file( job_name, f"Group space for {job_name}", directory.split(":")[1], compression, ) else: write_file_set_file(job_name, f"Group space for {job_name}", directory.split(":")[1]) except BaseException as e: LOGGER.error(e) click.echo("failed") sys.exit(1) return # Create the Job file. try: write_job_file(job_name, job_name, client=directory.split(":")[0]) except BaseException as e: LOGGER.error(e) # Try to clean up FileSet files. try: remove_file_set_file(job_name) except BaseException: LOGGER.error( f"Failed FileSet file cleanup for {job_name} when Job file creation " f"failed.") click.echo("failed") sys.exit(1) return reload_bconsole() push_to_gitlab(f"Ran command: brs_backup add {job_name} {directory}") click.echo("success") sys.exit(0)
def test_push_to_gitlab(self, mock_repo): """ Testing the 'push_to_gitlab' method. """ mock_repo.return_value = MagicMock(autospec=git.Repo) # Test the bare exception. mock_repo.return_value.bare = True with self.assertRaises(FileNotFoundError): util.push_to_gitlab("test message") # Ensure this was logged. util.LOGGER.error.assert_called_with( f"{config.GIT_LOCATION} is not an initialized git repo.") # Reset bare flag. mock_repo.return_value.bare = False # Simulate .add throwing an exception. mock_repo.return_value.git.add.side_effect = Exception( "TEST ERROR MSG") util.push_to_gitlab("test message") # Ensure this was logged. util.LOGGER.critical.assert_called_with( "Failed to push to GitLab due to: TEST ERROR MSG") # Reset the exception side effect. mock_repo.return_value.git.add.side_effect = None # Full test. util.push_to_gitlab("test") mock_repo.return_value.git.push.assert_called_once() util.LOGGER.info.assert_called_with( "Pushing to GitLab with commit message: 'test'")
def uadd(p: bool, compression: str, users: List[str]): """Add a user's home directory to the backup system. \b Example: brs_backup uadd u0407846 brs_backup uadd u0407846 u1234567 --compression=GZIP brs_backup uadd -p u0407846 brs_backup uadd -p u0407846 u1234567 """ success = [] failures = [] if p: directories = get_pe_dir_from_db(users) else: directories = get_dir_from_db(users) print(p) print(directories) return for user in users: if user not in directories: failures.append(user) continue # Make the file set file. try: if compression: write_file_set_file( user, f"Home directory for {user}", directories[user].split(":")[1], compression, ) else: write_file_set_file(user, f"Home directory for {user}", directories[user].split(":")[1]) except BaseException as e: LOGGER.error(str(e)) failures.append(user) # Make the job file. try: write_job_file(user, user, client=directories[user].split(":")[0]) except BaseException as e: LOGGER.error(e) # If we couldn't make the job file, try cleaning up the fileset file. try: remove_file_set_file(user) except BaseException: LOGGER.error( f"Failed FileSet file cleanup for {user} when Job file creation " f"failed.") failures.append(user) success.append(user) # Only reload/push when files were changed. if success: reload_bconsole() push_to_gitlab( f"Ran command: brs_backup uadd {'-p ' if p else ''}{' '.join(users)}" ) click.echo("success: " + ", ".join(success)) click.echo("failure: " + ", ".join(failures)) if len(failures) == 0: sys.exit(0) elif len(success) == 0: sys.exit(1) else: sys.exit(2)