Example #1
0
    def create(self, file_path):
        """Create TAR file."""
        path = os.path.dirname(file_path)

        if path == '':
            path = '.'

        folder_name = os.path.basename(file_path)

        tar_filename = "%s.tar.gz" % folder_name
        tar_filename = os.path.join(path, tar_filename)

        extra_tar_options = ""
        if self.verbose:
            extra_tar_options += "v"

        cmd = "tar -%sczf %s %s" % (extra_tar_options, tar_filename,
                                    folder_name)

        if not self.dry_run:
            exit_code = shell.run_shell_cmd(cmd, path, self.verbose)

            if exit_code != 0:
                raise RuntimeError("ERROR: Create TAR returned exit code: %d" %
                                   exit_code)

        return tar_filename
Example #2
0
    def dump(self, databases_names, database_dump_file):
        """Dump Database."""
        cmd = "mysqldump" \
              + " --host=%s" % self.host \
              + " --user=%s" % self.username \
              + " --password=%s" % self.password \
              + " --opt" \
              + " --quote-names" \
              + " --single-transaction" \
              + " --quick" \
              + " --databases %s" % ' '.join(databases_names) \
              + " > %s" % database_dump_file

        if self.verbose:
            log_cmd = re.sub(r" --password=.*? ", " --password=******** ", cmd)
            log.verbose("%s" % log_cmd)

        if not self.dry_run:
            path = '.'

            exit_code = shell.run_shell_cmd(cmd, path, self.verbose)

            return exit_code

        return 0
Example #3
0
    def restore_repo(self, repo):
        """Restoring Repo."""
        repo_path = os.path.dirname(self._get_repo_path(repo))

        tar_ext = '.git.tar.gz'
        repo_name = os.path.basename(repo)
        tar_filename = repo_name + tar_ext
        tar_file_path = os.path.join(repo_path, tar_filename)

        tar_extracted_path = os.path.join(repo_path, repo_name) + '.git'

        path = '.'

        if not self.dry_run:
            if os.path.exists(tar_extracted_path):
                shutil.rmtree(tar_extracted_path)

            if not os.path.exists(tar_extracted_path):
                log.print_log("Deleting Pervious Repo: %s" %
                              tar_extracted_path)
                os.makedirs(tar_extracted_path)
                os.rmdir(tar_extracted_path)

            log.print_log("Downloading File...")

            for backup_class in self.backup_classes:
                backup_path = backup_class.get_backup_repo_path(repo)
                backup_class.download_file(backup_path, tar_file_path)

            chown_cmd = "sudo chown %s:%s %s" % (
                self.config.get('gerrit', 'gerrit_username'),
                self.config.get('gerrit', 'gerrit_group'), tar_file_path)
            shell.run_shell_cmd(chown_cmd, path, self.verbose)

            log.print_log("Extracting TAR File...")

            tar = Tar(self.dry_run, self.verbose)
            tar.extract(tar_file_path)

            chown_cmd = "sudo chown -R %s:%s %s" % (
                self.config.get('gerrit', 'gerrit_username'),
                self.config.get('gerrit', 'gerrit_group'), repo_path)
            shell.run_shell_cmd(chown_cmd, path, self.verbose)

            if os.path.exists(tar_file_path):
                os.remove(os.path.abspath(tar_file_path))
Example #4
0
    def restore_database(self):
        """Restoring Database."""
        # database_folder = self.config.get('backup_structure', 'database_folder')
        database_dump_file = self.config.get('database', 'database_dump_file')

        tar_file = database_dump_file + '.tar.gz'

        if not self.dry_run:
            path = '.'

            tar = Tar(self.dry_run, self.verbose)

            log.print_log("Getting File...")
            for backup_class in self.backup_classes:
                backup_path = backup_class.get_backup_repo_list_path()
                backup_class.download_file(backup_path, tar_file)

            chown_cmd = "sudo chown %s:%s %s" % (
                self.config.get('restore', 'ssh_username'),
                self.config.get('restore', 'ssh_username'), tar_file)
            shell.run_shell_cmd(chown_cmd, path, self.verbose)

            log.print_log("Extracting TAR File...")
            tar.extract(tar_file)

            chown_cmd = "sudo chown %s:%s %s" % (
                self.config.get('restore', 'ssh_username'),
                self.config.get('restore', 'ssh_username'), database_dump_file)
            shell.run_shell_cmd(chown_cmd, path, self.verbose)

            log.print_log("Restoring Database...")

            database = Database(
                self.config.get('database', 'database_host'),
                self.config.get('database', 'database_username'),
                self.config.get('database', 'database_password'))

            database.run_file_cmd(
                self.config.get('database', 'database_dump_file'))

            del database

        return 0  # TODO: Error handling
Example #5
0
    def get_repo_disk_usage(self, repo):
        """Get Repos Disk Uages."""
        repo_path = self._get_repo_path(repo)

        cmd = "du -sh %s" % repo_path

        if not self.dry_run:
            return shell.run_shell_cmd(cmd, '.' '', self.verbose)

        return 0
Example #6
0
    def run_file_cmd(self, sql_file):
        """Run File CMD."""
        cmd = "mysql" \
              + " --host=%s" % self.host \
              + " --user=%s" % self.username \
              + " --password=%s" % self.password \
              + " < %s" % sql_file

        if self.verbose:
            log_cmd = re.sub(r" --password=.*? ", " --password=******** ", cmd)
            log.verbose("%s" % log_cmd)

        if not self.dry_run:
            exit_code = shell.run_shell_cmd(cmd, '.', False)

            return exit_code

        return 0
Example #7
0
    def extract(self, file_path):
        """Extract TAR file."""
        path = os.path.dirname(file_path)

        if path == '':
            path = '.'

        extra_tar_options = ""
        if self.verbose:
            extra_tar_options += "v"

        cmd = "tar -%sxzf %s" % (extra_tar_options, file_path)

        if not self.dry_run:
            exit_code = shell.run_shell_cmd(cmd, path, self.verbose)

            if exit_code != 0:
                raise RuntimeError(
                    "ERROR: Extract TAR returned exit code: %d" % exit_code)
Example #8
0
def run_tasks(config, is_remote, taskPrefix, dry_run=False, verbose=False):
    """Run tasks."""
    for section in config.sections():
        if not section.startswith("%s:" % taskPrefix):
            continue

        taskParts = section.split(':')

        if len(taskParts) < 2:
            raise RuntimeError("ERROR: Task not defined correctly")

        if skip_task(config, section, is_remote):
            continue

        taskName = taskParts[1].lower()
        if taskName == 'stop_jenkins_job':
            jenkinsUrl = config.get(section, 'jenkins_url')
            jobName = taskParts[2]

            if not dry_run:
                _sleep(config, section)
                jenkins = Jenkins(jenkinsUrl)
                jenkins.verbose = verbose
                jenkins.dry_run = dry_run

                jenkins_job_status = jenkins.disable_job_and_wait(jobName)
                if jenkins_job_status != 0:
                    raise RuntimeError("ERROR: Disabling Jenkins Job: %d" % jenkins_job_status)

        if taskName == 'start_jenkins_job':
            jenkinsUrl = config.get(section, 'jenkins_url')
            jobName = taskParts[2]

            if not dry_run:
                _sleep(config, section)
                jenkins = Jenkins(jenkinsUrl)
                jenkins.enable_job(jobName)

        elif taskName == 'stop_services':
            serviceNames = config.get(section, 'services').split(',')

            for serviceName in serviceNames:
                log.info("Stopping service: %s" % serviceName)
                if not dry_run:
                    _sleep(config, section)
                    services.stop_service(serviceName)

        elif taskName == 'start_services':
            serviceNames = config.get(section, 'services').split(',')

            for serviceName in serviceNames:
                log.info("Starting service: %s" % serviceName)
                if not dry_run:
                    _sleep(config, section)
                    services.start_service(serviceName)

        elif taskName == 'command':
            command = config.get(section, 'command')
            pwd = config.get(section, 'pwd')

            if not dry_run:
                _sleep(config, section)
                shell.run_shell_cmd(command, pwd)

        elif taskName == 'database_command':
            if not dry_run:
                _sleep(config, section)
                database = Database(config.get('database', 'database_host'),
                                    config.get('database', 'database_username'),
                                    config.get('database', 'database_password'))
                database.verbose = verbose
                database.dry_run = dry_run
                database.run_sql_cmd(config.get(section, 'command'))
Example #9
0
def run_service_command(serviceName, command='status'):
    """Run Service Command."""
    command = "sudo service %s %s" % (serviceName, command)
    shell.run_shell_cmd(command)