def add(self, repo_urls):
        self.__response['status'] = "failure"
        for url in repo_urls:
            self.__repo_number += 1
            repo_name = manage_config.flex_repo_name + str(self.__repo_number)
            self.__log_helper.logger.debug(str(repo_name) + " " + str(url))
            command = "smart channel --add '" + repo_name + "' type=rpm-md baseurl=" + url + " -y"
            result = shell_ops.run_cmd_chk(command)
            if result['returncode']:
                if "error" in result['cmd_output']:
                    self.action(repo_name, 'remove', 'WR')
                    self.__error = "Failed to add repository: " + result['cmd_output'][result['cmd_output'].index("error:") + 7:].replace("\n", "")
                else:
                    self.__error = "Error adding update repository"
                self.__response['message'] = self.__error
                self.__log_helper.logger.error("Failed to add repository. Error output: '%s'" % self.__response['message'])
                return self.__response
            else:
                command = "smart update '" + repo_name + "'"
                result = shell_ops.run_cmd_chk(command)  # Attempt to connect to new repo
                if result['returncode']:  # If attempt fails determine error and remove repo

                    # Sometimes, some special repo URL will not be removed from the cache file when the repo is removed. (repo add and then repo remove)
                    # If this happens, then "smart update this repo" will fail. So we want to manually delete the cache file.
                    if "OSError: [Errno 2] No such file or directory" in result['cmd_output']:
                        self.__log_helper.logger.debug("trying to remove cache file and retry...")
                        if os.path.isfile(manage_config.smart_cache_file):
                            self.__log_helper.logger.debug(manage_config.smart_cache_file + ' exists, so we will try to remove it.')
                            try:
                                os.remove(manage_config.smart_cache_file)
                                self.__log_helper.logger.debug("Try updating again............")
                                result = shell_ops.run_cmd_chk(command)  # Attempt to connect to new repo
                                if not result['returncode']:  # If it works
                                    continue  # go to the next loop
                            except Exception as e:
                                self.__log_helper.logger.error(manage_config.smart_cache_file + ' cannot be removed. ' + str(e))

                    if "error" in result['cmd_output']:
                        if "Invalid URL" in result['cmd_output']:
                            self.__response['message'] = "Failed to add repository: Invalid URL."
                        elif "Failed to connect" in result['cmd_output'] or 'URL returned error: 503' in result['cmd_output']:
                            self.__response['message'] = "Failed to add repository: Unable to connect to repository."
                        elif "Invalid XML" in result['cmd_output']:
                            self.__response['message'] = "Failed to add repository: Repository XML file invalid. "
                        else:
                            self.__response['message'] = result['cmd_output'][result['cmd_output'].index("error:") + 7:].replace("\n", "")
                    else:
                        self.__response['message'] = 'Error adding repository: ' + str(result['cmd_output'])

                    self.action(repo_name, 'remove', 'WR')
                    self.__log_helper.logger.error("Failed to add repository. Error output: '%s'" % self.__response['message'])
                    return self.__response
        self.__response['status'] = "success"
        self.__log_helper.logger.debug(str(self.__response))
        return self.__response
 def action(self, m_repo, action, type):
     cmd_output = dict()
     for repo in m_repo:
         if type == 'not_WR' and manage_config.flex_repo_name not in repo:
             self.__log_helper.logger.debug(str(action) + " " + str(repo))
             cmd_output = shell_ops.run_cmd_chk("smart channel --" + action + " " + repo + " -y")
             if cmd_output['returncode']:
                 return cmd_output
         if type == 'WR' and manage_config.flex_repo_name in repo:
             self.__log_helper.logger.debug(str(action) + " " + str(repo))
             cmd_output = shell_ops.run_cmd_chk("smart channel --" + action + " " + repo + " -y")
             if cmd_output['returncode']:
                 return cmd_output
     if action == 'remove':
         cmd_output = shell_ops.run_cmd_chk("smart update")
     cmd_output['returncode'] = 0
     return cmd_output
Ejemplo n.º 3
0
 def action(self, m_repo, action, type):
     cmd_output = dict()
     for repo in m_repo:
         if type == 'not_WR' and manage_config.flex_repo_name not in repo:
             self.__log_helper.logger.debug(str(action) + " " + str(repo))
             cmd_output = shell_ops.run_cmd_chk("smart channel --" +
                                                action + " " + repo + " -y")
             if cmd_output['returncode']:
                 return cmd_output
         if type == 'WR' and manage_config.flex_repo_name in repo:
             self.__log_helper.logger.debug(str(action) + " " + str(repo))
             cmd_output = shell_ops.run_cmd_chk("smart channel --" +
                                                action + " " + repo + " -y")
             if cmd_output['returncode']:
                 return cmd_output
     if action == 'remove':
         cmd_output = shell_ops.run_cmd_chk("smart update")
     cmd_output['returncode'] = 0
     return cmd_output
 def getMcafeeStatus(self):
     """ Return status of MEC service
     Returns:
         active / inactive
     """
     chk_out = shell_ops.run_cmd_chk('systemctl is-active scsrvc.service')
     if chk_out['returncode']:
         result = 'Inactive'
     else:
         result = 'Active'
     return result
def update_channels(CheckNetworkAgain=True):
    """ Update the channel cache.
    Args:
        CheckNetworkAgain (bool):  True if we want to check network again.
    Returns:
        str: Json string with keys
                'status' = 'success' or 'failure', and
                'message' = '', and
                'p_list' = new packages list
    """
    log_helper = logging_helper.logging_helper.Logger()
    log_path = "/tmp/uc_log"
    response = ({
        'status': 'failure',
        'message': ''
    })

    do_run = False
    network_checker = network_ops.NetworkCheck()
    if CheckNetworkAgain:
        network_checker.test_network_connection(check_http=manage_config.network_check_http)
    if network_checker.get_stored_https_status() and network_checker.get_stored_http_status():
        do_run = True

    if do_run:
        # We need this "smart update"... Add repo is relying on this.
        up_result = shell_ops.run_cmd_chk("smart update")
        if up_result['returncode']:
            if "server certificate verification failed." in up_result['cmd_output']:
                response['message'] = "Failed to update repository: Repository server certificate verification failed. "
            else:
                response['message'] = up_result['cmd_output'][up_result['cmd_output'].index("error:") + 7:].replace(
                    "\n", "")
            # Create log for proxy test to display error.
            try:
                uc_log = file(log_path, 'w+')
                uc_log.write(response['message'])
                uc_log.close()
            except:
                pass
            log_helper.logger.error("Failed to update repository. Error output: '%s'" % response['message'])
        else:
            # Clean existing error if one exists.
            if os.path.isfile(log_path):
                try:
                    os.remove(log_path)
                except:
                    pass
            response['status'] = 'success'
            log_helper.logger.debug("Successfully updated channel.")
        manage_package.update_package_list()

    response['p_list'] = manage_package.get_data()
    return json.dumps(response)
    def os_upgrade(self, user_name, password):
        self.__result_dict[self.__str_status_key] = self.__str_status_failure
        self.__result_dict[self.__str_error_key] = ''

        self.__config.upgrade_status('True')

        updater = manage_os_update.OS_UPDATER(sysinfo_ops.rcpl_version, sysinfo_ops.arch,
                                              user_name=user_name, password=password)

        # Add pro repositories
        if not self.__has_failure:
            repo_result = updater.add_os_repos(do_rcpl_update=False, user_name=user_name, password=password)
            if repo_result['status'] == 'fail':
                self.__str_error += repo_result['error']
                self.__has_failure = True

        # Install Pro Packages
        build_list = ProPackageList()
        self.__install_list = build_list.get_packages()
        if not self.__has_failure:
            for package in self.__install_list:
                self.__log_helper.logger.debug('Installing ' + str(package))
                try:
                    command = "smart install -y " + package
                    result = shell_ops.run_cmd_chk(command)
                    response = manage_package.parse_package_installation_result(pkg_name=package, result_dict=result)
                    if 'status' in response:
                        if (response['status'] != 'success') and ('error' in response):
                            self.__has_failure = True
                            self.__str_error += response['error']
                            self.__str_error += ' '
                except Exception as e:
                    self.__has_failure = True
                    self.__str_error += ("For " + package + ", " + str(e))
                    self.__str_error += ' '
                    pass

        if self.__has_failure:
            self.__log_helper.logger.error("Package installation failed:  " + self.__str_error)
            self.__result_dict[self.__str_status_key] = self.__str_status_failure
            self.__result_dict[self.__str_error_key] = self.__str_error
            self.__config.upgrade_status('False')
            updater.remove_os_repos(do_update=True, do_pro=True)
        else:
            self.__result_dict[self.__str_status_key] = self.__str_status_success
            self.__log_helper.logger.debug(str(self.__result_dict))
            # Remove old repositories
            updater.remove_os_repos(do_update=True, do_pro=False)

        # Rebuild package database, now that we have pro repos.
        manage_package.build_package_database()

        # Update GUI
        return json.dumps(self.__result_dict)
Ejemplo n.º 7
0
    def remove_os_repos(self, do_update=True, do_pro=False):
        """ Remove OS repos
        We do not re-build the package database in this function though.

        Args:
            do_update (bool): True if we do Smart Update at the end.
            do_pro (bool): True if we remove pro.

        Returns:
            dict: {'status': 'fail', 'error': ''} or {'status': 'success', 'error': ''}
        """
        result = {'status': 'fail', 'error': ''}
        if self.__error is None:  # no error yet
            if do_pro:
                repo_name = manage_config.pro_repo_name
            else:
                repo_name = manage_config.flex_repo_name

            # build to-be-removed list
            list_repos = manage_repo.list_repos()
            for repo in list_repos:
                if repo_name in repo:
                    command = "smart channel --remove '" + repo + "' -y"
                    remove_result = shell_ops.run_cmd_chk(command)
                    if remove_result['returncode']:  # fail
                        result['error'] = "remove failed: " + remove_result['cmd_output']
                        return result

            if do_update:
                update_result = shell_ops.run_cmd_chk("smart update")
                if update_result['returncode']:  # fail
                    result['error'] = "update failed: " + update_result['cmd_output']
                    return result

            result['status'] = 'success'
            result['error'] = ''
        else:  # fail
            result['error'] = str(self.__error)
        return result
 def getCPUType(self):
     """ Return cpu arch.
     Returns:
         str: cpu arch
     """
     global arch
     global display_arch
     if arch == "Not Set":
         self.__log_helper.logger.debug('Setting Arch')
         command = '''rpm -q --queryformat %{ARCH} bash'''
         arch = shell_ops.run_command(command)
         if arch == 'corei7_64':
             arch = "baytrail"
         elif arch == 'quark':
             arch = "quark"
         else:
             arch = "haswell"
     cpu_name = shell_ops.run_cmd_chk(
         "cat /proc/cpuinfo | grep 'model name' | uniq")
     display_arch = cpu_name['cmd_output'].split(':')[1]
     return display_arch
Ejemplo n.º 9
0
    def osUpdate(self, user_name='', password=''):
        """ Do OS Update
        Logic:
            If Flex:
                Force to replace existing flex repos with highest flex rcpl repos.
            Else (Pro):
                Force to replace existing pro repos with highest pro rcpl repos.
            Enable only the OS repos.
            Do Smart Upgrade.
            Enable the previous disabled repos (non-OS repos).
            Check default repo and update channel and build package database.

        Args:
            user_name (str): user name for Pro repo.
            password (str): password for Pro repo.

        Returns:
            dict: {'status': 'failure', 'message': ''} or {'status': 'success', 'message': ''}
        """
        self.__result['status'] = 'failure'
        if self.__error is None:  # no error yet

            # Flex: force to replace existing flex repos with highest rcpl repos.
            # Pro: force to replace existing pro repos with highest rcpl repos.
            add_result = self.add_os_repos(do_rcpl_update=True, user_name=user_name, password=password)
            if add_result['status'] == 'fail':
                self.__result['message'] = 'It failed to add OS repos. ' + add_result['error']
                return self.__result

            # enable only the OS repos
            response_repos = manage_repo.enable_only_os_repos()
            if response_repos['status'] is False:
                self.__result['message'] = 'It failed to disable non-OS repos.'
                return self.__result

            # do smart upgrade
            cmd_output = shell_ops.run_cmd_chk("smart upgrade -y")
            if "Cannot allocate memory" in cmd_output['cmd_output']:
                cmd_output['returncode'] = "memerror"

            if cmd_output['returncode'] == "memerror":
                # re-enable the disabled repo
                manage_repo.enable_repo(response_repos['disabled_repos'])
                self.__result['message'] = 'Some packages were not updated. Not enough memory during update process. Please run OS update again.'
            elif cmd_output['returncode']:  # fail
                # re-enable the disabled repo
                manage_repo.enable_repo(response_repos['disabled_repos'])
                self.__result['message'] = "Error during upgrade process. Error: " + cmd_output['cmd_output']
                return self.__result

            # re-enable the disabled repo
            manage_repo.enable_repo(response_repos['disabled_repos'])

            # check default repo and update channel
            manage_repo.configure_default_repo()

            self.__result['status'] = 'success'
        else:
            self.__result['status'] = 'failure'

        return self.__result
Ejemplo n.º 10
0
    def add_os_repos(self, do_rcpl_update=False, user_name='', password=''):
        """ Add OS repos
        We do not re-build the package database in this function though.
        This logic does not work for flex to pro upgrade.
        Logic:
            If do_rcpl_update,
                remove existing one
                add the highest rcpl's repos.
            Else,
                If OS repos do not exist
                    add the current rcpl's repos.

        Args:
            do_rcpl_update (bool): True if we want to update to the highest RCPL.
            user_name (str): user name for Pro repo.
            password (str): password for Pro repo.

        Returns:
            dict: {'status': 'fail', 'error': ''} or {'status': 'success', 'error': ''}
        """
        result = {'status': 'fail', 'error': ''}
        if self.__error is None:  # no error yet
            target_repos_list = []
            if do_rcpl_update and self.higher_version:
                # remove existing one first
                remove_result = self.remove_os_repos(do_update=False, do_pro=self.__pro_enabled)
                if remove_result['status'] == 'fail':
                    result['error'] = 'Cannot remove old OS repos. ' + remove_result['error']
                    return result
                # get the highest RCPL's repos.
                target_repos_list = self.__repoUrls_highest
            else:
                if not self.check_os_repos_existed(check_pro=self.__pro_enabled):  # no OS repo yet
                    # get the current RCPL's repos.
                    target_repos_list = self.__repoUrls

            if target_repos_list:  # not empty
                # add
                repo_number = 0
                for url in target_repos_list:
                    repo_number += 1
                    if self.__pro_enabled:
                        repo_pre_name = manage_config.pro_repo_name
                        repo_name = repo_pre_name + str(repo_number)
                        user_name = user_name.replace("@", "%40")
                        url = url.replace("://", "://%s:%s@" % (user_name, password))
                        command = "smart channel --add '" + repo_name + "' type=rpm-md baseurl='" + url + "' -y"
                    else:
                        repo_pre_name = manage_config.flex_repo_name
                        repo_name = repo_pre_name + str(repo_number)
                        command = "smart channel --add '" + repo_name + "' type=rpm-md baseurl='" + url + "' -y"
                    self.__log_helper.logger.debug("About to add OS repo: " + str(repo_name) + " " + str(url))
                    # add
                    add_result = shell_ops.run_cmd_chk(command)
                    if add_result['returncode']:  # fail
                        if "error" in add_result['cmd_output']:
                            result['error'] = "Failed to add repository: " + add_result['cmd_output'][add_result['cmd_output'].index("error:") + 7:].replace("\n", "")
                        else:
                            result['error'] = "Error adding update repository: " + add_result['cmd_output']
                        self.__log_helper.logger.error("Failed to add repository. Error output: '%s'" % result['error'])
                        # remove OS repos upon failure
                        self.remove_os_repos(do_update=True, do_pro=self.__pro_enabled)
                        # TODO: check result['error'] to remove account and password. ://[account]:[password]@ to ://
                        return result
                    # update
                    command = "smart update '" + repo_name + "'"
                    update_result = shell_ops.run_cmd_chk(command)  # Attempt to connect to new repo
                    if update_result['returncode']:  # If attempt fails determine error and remove repo
                        # Sometimes, some special repo URL will not be removed from the cache file when the repo is removed. (repo add and then repo remove)
                        # If this happens, then "smart update this repo" will fail. So we want to manually delete the cache file.
                        if "OSError: [Errno 2] No such file or directory" in update_result['cmd_output']:
                            self.__log_helper.logger.debug("trying to remove cache file and retry...")
                            if os.path.isfile(manage_config.smart_cache_file):
                                self.__log_helper.logger.debug(manage_config.smart_cache_file + ' exists, so we will try to remove it.')
                                try:
                                    os.remove(manage_config.smart_cache_file)
                                    self.__log_helper.logger.debug("Try updating again............")
                                    update_result = shell_ops.run_cmd_chk(command)  # Attempt to connect to new repo
                                    if not update_result['returncode']:  # If it works
                                        continue  # go to the next loop item
                                except Exception as e:
                                    self.__log_helper.logger.error(manage_config.smart_cache_file + ' cannot be removed. ' + str(e))
                        if "error" in update_result['cmd_output']:
                            if "Invalid URL" in update_result['cmd_output']:
                                result['error'] = "Failed to add repository: Invalid URL."
                            elif "Failed to connect" in update_result['cmd_output'] or 'URL returned error: 503' in update_result['cmd_output']:
                                result['error'] = "Failed to add repository: Unable to connect to repository."
                            elif "Invalid XML" in update_result['cmd_output']:
                                result['error'] = "Failed to add repository: Repository XML file invalid. "
                            elif "server certificate verification failed." in update_result['cmd_output']:
                                result['error'] = "SSL certificate error: Server certificate verification failed. "
                            else:
                                result['error'] = update_result['cmd_output'][update_result['cmd_output'].index("error:") + 7:].replace("\n", "")
                        else:
                            result['error'] = 'Error adding repository: ' + update_result['cmd_output']
                        self.__log_helper.logger.error("Failed to add repository. Error output: '%s'" % result['error'])
                        # remove OS repos upon failure
                        self.remove_os_repos(do_update=True, do_pro=self.__pro_enabled)
                        # TODO: check result['error'] to remove account and password. ://[account]:[password]@ to ://
                        return result
                result['status'] = 'success'
                result['error'] = ''
            else:  # else of if target_repos_list
                result['status'] = 'success'
                result['error'] = ''
        else:  # else of if self.__error is None
            result['error'] = str(self.__error)
            # TODO: check result['error'] to remove account and password. ://[account]:[password]@ to ://
        return result
    def os_upgrade(self, user_name, password):
        self.__result_dict[self.__str_status_key] = self.__str_status_failure
        self.__result_dict[self.__str_error_key] = ''

        self.__config.upgrade_status('True')

        updater = manage_os_update.OS_UPDATER(sysinfo_ops.rcpl_version,
                                              sysinfo_ops.arch,
                                              user_name=user_name,
                                              password=password)

        # Add pro repositories
        if not self.__has_failure:
            repo_result = updater.add_os_repos(do_rcpl_update=False,
                                               user_name=user_name,
                                               password=password)
            if repo_result['status'] == 'fail':
                self.__str_error += repo_result['error']
                self.__has_failure = True

        # Install Pro Packages
        build_list = ProPackageList()
        self.__install_list = build_list.get_packages()
        if not self.__has_failure:
            for package in self.__install_list:
                self.__log_helper.logger.debug('Installing ' + str(package))
                try:
                    command = "smart install -y " + package
                    result = shell_ops.run_cmd_chk(command)
                    response = manage_package.parse_package_installation_result(
                        pkg_name=package, result_dict=result)
                    if 'status' in response:
                        if (response['status'] != 'success') and ('error'
                                                                  in response):
                            self.__has_failure = True
                            self.__str_error += response['error']
                            self.__str_error += ' '
                except Exception as e:
                    self.__has_failure = True
                    self.__str_error += ("For " + package + ", " + str(e))
                    self.__str_error += ' '
                    pass

        if self.__has_failure:
            self.__log_helper.logger.error("Package installation failed:  " +
                                           self.__str_error)
            self.__result_dict[
                self.__str_status_key] = self.__str_status_failure
            self.__result_dict[self.__str_error_key] = self.__str_error
            self.__config.upgrade_status('False')
            updater.remove_os_repos(do_update=True, do_pro=True)
        else:
            self.__result_dict[
                self.__str_status_key] = self.__str_status_success
            self.__log_helper.logger.debug(str(self.__result_dict))
            # Remove old repositories
            updater.remove_os_repos(do_update=True, do_pro=False)

        # Rebuild package database, now that we have pro repos.
        manage_package.build_package_database()

        # Update GUI
        return json.dumps(self.__result_dict)
def add_repo(url, user_name, password, name, from_startup=False, from_GUI=False, check_network_again=True):
    """ Add a repository to the channel cache.

    Args:
        url (str): Location of the repository
        user_name (str): Login protected repository username
        password (str): Login protected repository password
        name (str): Desired name of the repository
        from_startup (bool): True if this is called by server.py start-up
        from_GUI (bool): True if this is called by front-end
        check_network_again (bool): True if need to check network again

    Returns:
        str: Json string with keys
                'status' = 'success' or 'failure', and
                'error' = '', and
                'p_list' = new packages list
    """
    log_helper = logging_helper.logging_helper.Logger()
    response = {'status': 'failure', 'error': '', 'p_list': []}

    if ' ' in url:
        response['error'] = "Failed to add repository: URL cannot contain spaces"
        log_helper.logger.error("Error: URL cannot contain spaces")
        return json.dumps(response)

    if 'http://' in url or 'https://' in url:
        pass
    else:
        response['error'] = "Failed to add repository: URL must contain protocol (example: http://)"
        log_helper.logger.error("Error: URL must contain protocol")
        return json.dumps(response)

    # use the original list_repos to get all repos so that we can check name.
    repo_list = list_repos()
    if name in repo_list:  # Confrim repo does not exist
        response['error'] = "Duplicate name"
        log_helper.logger.error("Error: Duplicate repository name '%s'" % name)
        return json.dumps(response)

    if user_name != "None":  # Build URL with unique user identification 
        user_name = user_name.replace("@", "%40")
        if password == "None":
            url = url.replace("://", "://%s@" % user_name)
        else:
            url = url.replace("://", "://%s:%s@" % (user_name, password))

    command = "smart channel --add '" + name + "' type=rpm-md baseurl='" + url + "' -y"
    result = shell_ops.run_cmd_chk(command)
    if result['returncode']:
        if "error" in result['cmd_output']:
            remove_repo(name)
            error = "Failed to add repository: " + result['cmd_output'][result['cmd_output'].index("error:") + 7:].replace("\n", "")
            response['error'] = error
            log_helper.logger.error("Failed to add repository. Error output: '%s'" % response['error'])
    else:
        # If this is run by server.py startup, we do not do update if we do not have network.
        do_update = True
        if from_startup:
            # run by startup, need to see if the network is good or not.
            network_checker = network_ops.NetworkCheck()
            if network_checker.get_stored_https_status() and network_checker.get_stored_http_status():  # yes network
                do_update = True
            else:
                do_update = False
        else:
            pass

        if do_update:
            command = "smart update '" + name + "'"
            result = shell_ops.run_cmd_chk(command)  # Attempt to connect to new repo
        else:
            result = {'returncode': None, 'cmd_output': None}
        if result['returncode']:  # If attempt fails determine error and remove repo
            if "error" in result['cmd_output']:
                if "Invalid URL" in result['cmd_output']:
                    response['error'] = "Failed to add repository: Invalid URL."
                elif "Failed to connect" in result['cmd_output'] or 'URL returned error: 503' in result['cmd_output']:
                    response['error'] = "Failed to add repository: Unable to connect to repository."
                elif "Invalid XML" in result['cmd_output']:
                    response['error'] = "Failed to add repository: Repository XML file invalid. "
                elif "server certificate verification failed.":
                    response['error'] = "Failed to add repository: SSL certificate error. Server certificate verification failed. "
                else:
                    response['error'] = result['cmd_output'][result['cmd_output'].index("error:") + 7:].replace("\n", "")
            else:
                response['error'] = 'Error adding repository'
            remove_repo(name)
            log_helper.logger.error("Failed to add repository. Error output: '%s'" % response['error'])
        else:  # Update channels and package list
            # Need to track the repos first, before update_channels().
            if from_GUI:
                log_helper.logger.debug("Adding repo " + name + " to tracking list.")
                tracking_result = RepoTracking.add_to_tracking(name)
                js_data = json.loads(tracking_result)
                if js_data['status'] != "success":
                    remove_repo(name)
                    log_helper.logger.error("Failed to add repo into tracking. Error: " + str(tracking_result))
                else:
                    response['status'] = "success"
            else:
                response['status'] = "success"

            update_channels(CheckNetworkAgain=check_network_again)

            log_helper.logger.debug("Successfully added repository '%s'" % name)

    response['p_list'] = manage_package.get_data()
    return json.dumps(response)
Ejemplo n.º 13
0
    def add(self, repo_urls):
        self.__response['status'] = "failure"
        for url in repo_urls:
            self.__repo_number += 1
            repo_name = manage_config.flex_repo_name + str(self.__repo_number)
            self.__log_helper.logger.debug(str(repo_name) + " " + str(url))
            command = "smart channel --add '" + repo_name + "' type=rpm-md baseurl=" + url + " -y"
            result = shell_ops.run_cmd_chk(command)
            if result['returncode']:
                if "error" in result['cmd_output']:
                    self.action(repo_name, 'remove', 'WR')
                    self.__error = "Failed to add repository: " + result[
                        'cmd_output'][result['cmd_output'].index("error:") +
                                      7:].replace("\n", "")
                else:
                    self.__error = "Error adding update repository"
                self.__response['message'] = self.__error
                self.__log_helper.logger.error(
                    "Failed to add repository. Error output: '%s'" %
                    self.__response['message'])
                return self.__response
            else:
                command = "smart update '" + repo_name + "'"
                result = shell_ops.run_cmd_chk(
                    command)  # Attempt to connect to new repo
                if result[
                        'returncode']:  # If attempt fails determine error and remove repo

                    # Sometimes, some special repo URL will not be removed from the cache file when the repo is removed. (repo add and then repo remove)
                    # If this happens, then "smart update this repo" will fail. So we want to manually delete the cache file.
                    if "OSError: [Errno 2] No such file or directory" in result[
                            'cmd_output']:
                        self.__log_helper.logger.debug(
                            "trying to remove cache file and retry...")
                        if os.path.isfile(manage_config.smart_cache_file):
                            self.__log_helper.logger.debug(
                                manage_config.smart_cache_file +
                                ' exists, so we will try to remove it.')
                            try:
                                os.remove(manage_config.smart_cache_file)
                                self.__log_helper.logger.debug(
                                    "Try updating again............")
                                result = shell_ops.run_cmd_chk(
                                    command)  # Attempt to connect to new repo
                                if not result['returncode']:  # If it works
                                    continue  # go to the next loop
                            except Exception as e:
                                self.__log_helper.logger.error(
                                    manage_config.smart_cache_file +
                                    ' cannot be removed. ' + str(e))

                    if "error" in result['cmd_output']:
                        if "Invalid URL" in result['cmd_output']:
                            self.__response[
                                'message'] = "Failed to add repository: Invalid URL."
                        elif "Failed to connect" in result[
                                'cmd_output'] or 'URL returned error: 503' in result[
                                    'cmd_output']:
                            self.__response[
                                'message'] = "Failed to add repository: Unable to connect to repository."
                        elif "Invalid XML" in result['cmd_output']:
                            self.__response[
                                'message'] = "Failed to add repository: Repository XML file invalid. "
                        else:
                            self.__response['message'] = result['cmd_output'][
                                result['cmd_output'].index("error:") +
                                7:].replace("\n", "")
                    else:
                        self.__response[
                            'message'] = 'Error adding repository: ' + str(
                                result['cmd_output'])

                    self.action(repo_name, 'remove', 'WR')
                    self.__log_helper.logger.error(
                        "Failed to add repository. Error output: '%s'" %
                        self.__response['message'])
                    return self.__response
        self.__response['status'] = "success"
        self.__log_helper.logger.debug(str(self.__response))
        return self.__response