Example #1
0
    def check_system_purpose_set_test(self):
        """Test the check_system_purpose_set() helper function."""
        # system purpose set
        with tempfile.TemporaryDirectory() as sysroot:
            # create a dummy syspurpose file
            syspurpose_path = RHSM_SYSPURPOSE_FILE_PATH
            directory = os.path.split(syspurpose_path)[0]
            os.makedirs(util.join_paths(sysroot, directory))
            os.mknod(util.join_paths(sysroot, syspurpose_path))
            self.assertTrue(check_system_purpose_set(sysroot))

        # system purpose not set
        with tempfile.TemporaryDirectory() as sysroot:
            self.assertFalse(check_system_purpose_set(sysroot))
Example #2
0
def give_the_system_purpose(sysroot, rhsm_syspurpose_proxy, role, sla, usage, addons):
    """Set system purpose for the installed system by calling the syspurpose tool.

    The tool is called in the specified system root, so this method should only
    be called once the given system root contains the syspurpose utility.

    :param str sysroot: system root path
    :param rhsm_syspurpose_proxy: com.redhat.RHSM1.Syspurpose proxy
    :type rhsm_syspurpose_proxy: dasbus DBus proxy object
    :param role: role of the system
    :type role: str or None
    :param sla: Service Level Agreement for the system
    :type sla: str or None
    :param usage: intended usage of the system
    :type usage: str or None
    :param list addons: any additional layered products or features
    """
    # first check if system purpose data has already been set
    if check_system_purpose_set(sysroot):
        # Remove existing system purpose data.
        #
        # This is important, as otherwise it would be both not possible to
        # clear existing system purpose data if say a user sets all values
        # to "not specified" in the GUI after setting them to some values
        # previously. Also due to syspurpose setting one value at a time
        # one could end up with unwanted hybrid configuration combining
        # new and old date, if not all fields are set in the most recent
        # invocation.
        log.debug("subscription: clearing old system purpose data")
        syspurpose_path = join_paths(sysroot, RHSM_SYSPURPOSE_FILE_PATH)
        os.remove(syspurpose_path)

    if role or sla or usage or addons:
        # Construct a dictionary of values to feed to the SetSyspurpose DBus method.
        syspurpose_dict = {}
        if role:
            syspurpose_dict["role"] = get_variant(Str, role)
        if sla:
            syspurpose_dict["service_level_agreement"] = get_variant(Str, sla)
        if usage:
            syspurpose_dict["usage"] = get_variant(Str, usage)
        if addons:
            syspurpose_dict["addons"] = get_variant(List[Str], addons)
        log.debug("subscription: setting system purpose")
        try:
            locale = os.environ.get("LANG", "")
            rhsm_syspurpose_proxy.SetSyspurpose(syspurpose_dict, locale)
            log.debug("subscription: system purpose has been set")
            return True
        except DBusError as e:
            log.debug("subscription: failed to set system purpose: %s", str(e))
            return False
    else:
        log.warning("subscription: not calling syspurpose as no fields have been provided")
        # doing nothing is still not a failure
        return True
Example #3
0
    def _transfer_system_purpose(self):
        """Transfer the system purpose file if present.

        A couple notes:
         - this might be needed even if the system has not been subscribed
           during the installation and is therefore always attempted
         - this means the syspurpose tool has been called in the installation
           environment & we need to transfer the results to the target system
        """
        if check_system_purpose_set(sysroot="/"):
            log.debug("subscription: transferring syspurpose file")
            target_syspurpose_file_path = self._sysroot + RHSM_SYSPURPOSE_FILE_PATH
            self._copy_file(RHSM_SYSPURPOSE_FILE_PATH, target_syspurpose_file_path)
Example #4
0
def give_the_system_purpose(sysroot, role, sla, usage, addons):
    """Set system purpose for the installed system by calling the syspurpose tool.

    The tool is called in the specified system root, so this method should only
    be called once the given system root contains the syspurpose utility.

    :param str sysroot: system root path
    :param role: role of the system
    :type role: str or None
    :param sla: Service Level Agreement for the system
    :type sla: str or None
    :param usage: intended usage of the system
    :type usage: str or None
    :param list addons: any additional layered products or features
    """
    # first check if system purpose data has already been set
    if check_system_purpose_set(sysroot):
        # Remove existing system purpose data.
        #
        # This is important, as otherwise it would be both not possible to
        # clear existing system purpose data if say a user sets all values
        # to "not specified" in the GUI after setting them to some values
        # previously. Also due to syspurpose setting one value at a time
        # one could end up with unwanted hybrid configuration combining
        # new and old date, if not all fields are set in the most recent
        # invocation.
        log.debug("subscription: clearing old system purpose data")
        syspurpose_path = util.join_paths(sysroot, RHSM_SYSPURPOSE_FILE_PATH)
        os.remove(syspurpose_path)

    if role or sla or usage or addons:
        # using join_paths() as both paths are absolute
        syspurpose_sysroot_path = util.join_paths(sysroot,
                                                  SYSPURPOSE_UTILITY_PATH)
        if os.path.exists(syspurpose_sysroot_path):
            # The syspurpose utility can only set one value at a time,
            # so we might need to call it multiple times to set all the
            # requested values.
            #
            # Also as the values can contain white space we need to make sure the
            # values passed to arguments are all properly quoted.
            if role:
                args = ["set-role", role]
                if _call_syspurpose_tool(sysroot, args):
                    return False
            if sla:
                args = ["set-sla", sla]

                if _call_syspurpose_tool(sysroot, args):
                    return False
            if usage:
                args = ["set-usage", usage]
                if _call_syspurpose_tool(sysroot, args):
                    return False
            if addons:
                args = ["add", "addons"]
                for addon in addons:
                    args.append(addon)
                if _call_syspurpose_tool(sysroot, args):
                    return False
            log.debug("subscription: system purpose has been set")
            return True
        else:
            log.error(
                "subscription: the syspurpose tool is missing, cannot set system purpose"
            )
            return False
    else:
        log.warning(
            "subscription: not calling syspurpose as no fields have been provided"
        )
        # doing nothing is still not a failure
        return True