initial changes for restoring initial artifacts after signup removal
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/dbd2f07f Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/dbd2f07f Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/dbd2f07f Branch: refs/heads/singup_deletion_cleanup Commit: dbd2f07f1a2395a5256e2439595242ff8d521abe Parents: 2951b41 Author: Isuru Haththotuwa <[email protected]> Authored: Tue Dec 1 11:07:56 2015 +0530 Committer: Isuru Haththotuwa <[email protected]> Committed: Tue Dec 1 11:07:56 2015 +0530 ---------------------------------------------------------------------- .../cartridge.agent/constants.py | 2 + .../modules/artifactmgt/git/agentgithandler.py | 22 ++++---- .../modules/util/cartridgeagentutils.py | 55 ++++++++++++++++++++ .../plugins/DefaultArtifactCheckout.py | 34 +++++++----- 4 files changed, 86 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/dbd2f07f/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/constants.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/constants.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/constants.py index cd2ce36..4672579 100644 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/constants.py +++ b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/constants.py @@ -140,3 +140,5 @@ MONITORING_RECEIVER_PORT = "monitoring.server.port" MONITORING_RECEIVER_SECURE_PORT = "monitoring.server.secure.port" MONITORING_SERVER_ADMIN_USERNAME = "monitoring.server.admin.username" MONITORING_SERVER_ADMIN_PASSWORD = "monitoring.server.admin.password" + +BACKUP_DIR_SUFFIX = "_backup" http://git-wip-us.apache.org/repos/asf/stratos/blob/dbd2f07f/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/artifactmgt/git/agentgithandler.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/artifactmgt/git/agentgithandler.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/artifactmgt/git/agentgithandler.py index 2cb4c38..7546619 100644 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/artifactmgt/git/agentgithandler.py +++ b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/artifactmgt/git/agentgithandler.py @@ -28,10 +28,10 @@ import time from config import Config from exception import GitRepositorySynchronizationException from git import * -import errno from ...util.asyncscheduledtask import AbstractAsyncScheduledTask, ScheduledExecutor from ...util.log import LogFactory +from ...util.cartridgeagentutils import Utils class AgentGitHandler: @@ -315,7 +315,9 @@ class AgentGitHandler: "Could not remove repository folder for tenant:%s %s" % (git_repo.tenant_id, e)) # restore default artifacts - AgentGitHandler.restore_default_artifacts("/tmp/default_artifacts", git_repo.local_repo_path) + if Utils.directory_exists(Utils.strip_trailing_slash(git_repo.local_repo_path) + + constants.BACKUP_DIR_SUFFIX): + AgentGitHandler.restore_default_artifacts(git_repo.local_repo_path) AgentGitHandler.clear_repo(tenant_id) AgentGitHandler.log.info("Git repository deleted for tenant %s" % git_repo.tenant_id) @@ -323,19 +325,13 @@ class AgentGitHandler: return True @staticmethod - def restore_default_artifacts (default_artifact_backup_location, local_repo_path): + def restore_default_artifacts(default_dir): try: - if os.path.isdir(default_artifact_backup_location): - # first remove all the artifacts in the local_repo_path - if os.listdir(local_repo_path): - # non-empty - filelist = [f for f in os.listdir(local_repo_path)] - for f in filelist: - GitUtils.delete_folder_tree(local_repo_path + '/' + f) - AgentGitHandler.log.info('Restoring default artifacts from ' + default_artifact_backup_location + ' to ' + local_repo_path) - shutil.copytree(default_artifact_backup_location, local_repo_path) + Utils.move_directory(Utils.strip_trailing_slash(default_dir) + constants.BACKUP_DIR_SUFFIX, default_dir) + AgentGitHandler.log.info('Restored contents from backup location ' +Utils.strip_trailing_slash(default_dir) + + constants.BACKUP_DIR_SUFFIX) except OSError as e: - AgentGitHandler.log.error('Contents of ' + default_artifact_backup_location + ' not restored. Error: %s' % e) + AgentGitHandler.log.error('Contents of ' + default_dir + ' not restored. Error: %s' % e) @staticmethod def execute_git_command(command, repo_path): http://git-wip-us.apache.org/repos/asf/stratos/blob/dbd2f07f/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/util/cartridgeagentutils.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/util/cartridgeagentutils.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/util/cartridgeagentutils.py index ebd6889..74c7e75 100644 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/util/cartridgeagentutils.py +++ b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/modules/util/cartridgeagentutils.py @@ -21,6 +21,8 @@ import time import socket import string import hashlib +import shutil +import os from log import LogFactory @@ -120,6 +122,59 @@ def check_ports_active(ip_address, ports): return True +class Utils (object): + + @staticmethod + def directory_exists(dir): + """ + Checks if the given directory exists + :param dir: directory to check + :return: True if the directory dir exists, else False + :rtype: bool + """ + try: + return os.path.isdir(dir) + except OSError as e: + log.error("Unable to check directory existance [%s]" % e) + return False + + @staticmethod + def copy_directory(src, destination): + """ + Copies if the directory 'src' to 'destination' + :param src: location of directory to copy + :param destination: new directory location + """ + try: + shutil.copytree(src, destination) + log.debug("Directory [%s] copied to [%s]" % (src, destination)) + except OSError as e: + log.error('Directory not copied. Error: %s' % e) + + @staticmethod + def strip_trailing_slash(string): + """ + If the string has a trailing '/', removes it + :param string: string to check + :return: string without a trailing '/' + :rtype: string + """ + if string.endswith('/'): + return string[:-1] + return string + + @staticmethod + def move_directory(src, destination): + """ + Moves if the directory 'src' to 'destination' + :param src: location of directory to move + :param destination: new directory location + """ + try: + shutil.move(src, destination) + log.debug("Directory [%s] moved to [%s]" % (src, destination)) + except OSError as e: + log.error('Directory not moved. Error: %s' % e) class IncrementalCeilingListIterator(object): """ http://git-wip-us.apache.org/repos/asf/stratos/blob/dbd2f07f/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/plugins/DefaultArtifactCheckout.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/plugins/DefaultArtifactCheckout.py b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/plugins/DefaultArtifactCheckout.py index 1bef17b..52648a1 100644 --- a/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/plugins/DefaultArtifactCheckout.py +++ b/components/org.apache.stratos.python.cartridge.agent/src/main/python/cartridge.agent/cartridge.agent/plugins/DefaultArtifactCheckout.py @@ -17,12 +17,12 @@ from plugins.contracts import IArtifactCheckoutPlugin from modules.util.log import LogFactory +from modules.util.cartridgeagentutils import Utils from modules.artifactmgt.git.agentgithandler import AgentGitHandler from config import Config import constants from exception import * import shutil -import errno import os @@ -94,8 +94,13 @@ class DefaultArtifactCheckout(IArtifactCheckoutPlugin): self.log.info("Executing git clone: [tenant-id] %s [repo-url] %s, [repo path] %s", git_repo.tenant_id, git_repo.repo_url, git_repo.local_repo_path) - # copy default artifacts (if any) to a /tmp/default_artifacts - self.backupDefaultArtifacts(git_repo.local_repo_path, "/tmp/default_artifacts") + # copy default artifacts (if any) to a a temp location + # if directory name is dir, the backup directory name would be dir_backup + if self.initial_artifacts_exists(git_repo.local_repo_path): + self.log.info("Default artifacts exist at " + git_repo.local_repo_path) + self.backup_initial_artifacts(git_repo.local_repo_path) + else: + self.log.info("No default artifacts exist at " + git_repo.local_repo_path) try: git_repo = AgentGitHandler.clone(git_repo) @@ -109,17 +114,18 @@ class DefaultArtifactCheckout(IArtifactCheckoutPlugin): AgentGitHandler.retry_clone(git_repo) AgentGitHandler.add_repo(git_repo) - def backupDefaultArtifacts(src, dest): + def initial_artifacts_exists(self, dir): try: - if not os.path.isdir(src): - self.log.info ('Direcotry ' + src + ' does not exist') - return - if os.path.isdir(dest): - self.log.info('Directory ' + dest + ' already exists, will delete') - shutil.rmtree(dest) - self.log.info('Copying default artifacts from ' + src + ' to ' + dest) - shutil.copytree(src, dest) + return os.path.exists(dir) and os.listdir(dir) except OSError as e: - self.log.error('Directory not copied. Error: %s' % e) - + self.log.error('Unable to check if directory exists | non-empty, error: %s' % e) + return False + def backup_initial_artifacts(self, src): + self.log.info('Initial artifacts exists, taking backup to ' + Utils.strip_trailing_slash(src) + + constants.BACKUP_DIR_SUFFIX + + ' directory') + try: + shutil.copytree(src, Utils.strip_trailing_slash(src) + constants.BACKUP_DIR_SUFFIX) + except OSError as e: + self.log.error('Directory not copied. Error: %s' % e) \ No newline at end of file
