def get_sys_purpose_store(): """ :return: Returns a singleton instance of the syspurpose store if it was imported. Otherwise None. """ global store if store is not None: return store elif SyspurposeStore is not None: store = SyspurposeStore.read(USER_SYSPURPOSE) return store
def write_syspurpose(values): """ Write the syspurpose to the file system. :param values: :return: """ if SyspurposeStore is not None: sp = SyspurposeStore(USER_SYSPURPOSE) sp.contents = values sp.write() else: # Simple backup in case the syspurpose tooling is not installed. try: json.dump(values, open(USER_SYSPURPOSE), ensure_ascii=True, indent=2) except OSError: log.warning('Could not write syspurpose to %s' % USER_SYSPURPOSE) return False return True
def GetSyspurpose(self, locale, sender=None): """ D-Bus method for getting current system purpose :param locale: string with locale :param sender: :return: json representation of system purpose contents """ syspurpose_path = "/etc/rhsm/syspurpose/syspurpose.json" locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) syspurpose_store = SyspurposeStore.read(syspurpose_path) try: contents = syspurpose_store.contents except Exception as err: raise dbus.DBusException(str(err)) return json.dumps(contents)
def read_syspurpose(raise_on_error=False): """ Reads the system purpose from the correct location on the file system. Makes an attempt to use a SyspurposeStore if available falls back to reading the json directly. :return: A dictionary containing the total syspurpose. """ if SyspurposeStore is not None: try: syspurpose = SyspurposeStore.read(USER_SYSPURPOSE).contents except (OSError, IOError): syspurpose = {} else: try: syspurpose = json.load(open(USER_SYSPURPOSE)) except (os.error, ValueError): # In the event this file could not be read treat it as empty if raise_on_error: raise syspurpose = {} return syspurpose
def GetSyspurpose(self, locale, sender=None): """ D-Bus method for getting current system purpose :param locale: string with locale :param sender: :return: json representation of system purpose contents """ syspurpose_path = '/etc/rhsm/syspurpose/syspurpose.json' locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) syspurpose_store = SyspurposeStore.read(syspurpose_path) try: contents = syspurpose_store.contents except Exception as err: raise dbus.DBusException(str(err)) return json.dumps(contents)
def main(): """ Run the cli (Do the syspurpose tool thing!!) :return: 0 """ parser = setup_arg_parser() args = parser.parse_args() # Syspurpose is not intended to be used in containers for the time being (could change later). if in_container(): print( _("WARNING: Setting syspurpose in containers has no effect." "Please run syspurpose on the host.\n")) syspurposestore = SyspurposeStore.read(USER_SYSPURPOSE) if args.func is not None: args.func(args, syspurposestore) else: parser.print_help() if args.requires_write: syspurposestore.write() try: syspurpose_sync = SyspurposeSync() except ImportError: print( _("Warning: Unable to sync system purpose with subscription management server: rhsm module is not available." )) else: ret = syspurpose_sync.send_syspurpose_to_candlepin(syspurposestore) if ret: print( _("System purpose successfully sent to subscription management server." )) else: print( _("Unable to send system purpose to subscription management server" )) return 0
def save_role_to_syspurpose_metadata(role): """ Save provided role value to the local Syspurpose Metadata (syspurpose.json) file. :param role: The value of role to be saved in the syspurpose file. :type role: str :return: None """ if 'SyspurposeStore' in globals() and SyspurposeStore is not None: store = SyspurposeStore.read(USER_SYSPURPOSE) if role is None or role == "": store.unset("role") else: store.set("role", role) store.write() return True else: log.error( "SyspurposeStore could not be imported. Syspurpose role value not saved locally." ) return False
def save_sla_to_syspurpose_metadata(service_level): """ Saves the provided service-level value to the local Syspurpose Metadata (syspurpose.json) file. If the service level provided is null or empty, the sla value to the local syspurpose file is set to null. :param service_level: The service-level value to be saved in the syspurpose file. :type service_level: str """ if 'SyspurposeStore' in globals() and SyspurposeStore is not None: store = SyspurposeStore.read(USER_SYSPURPOSE) # if empty, set it to null if service_level is None or service_level == "": service_level = None store.set("service_level_agreement", service_level) store.write() log.info("Syspurpose SLA value successfully saved locally.") else: log.error( "SyspurposeStore could not be imported. Syspurpose SLA value not saved locally." )
def save_usage_to_syspurpose_metadata(usage): """ Saves the provided usage value to the local Syspurpose Metadata (syspurpose.json) file. If the usage setting provided is null or empty, the usage value to the local syspurpose file is set to null. :param usage: The usage value to be saved in the syspurpose file. :type usage: str """ if 'SyspurposeStore' in globals() and SyspurposeStore is not None: store = SyspurposeStore.read(USER_SYSPURPOSE) # if empty, set it to null if usage is None or usage == "": usage = None store.set("usage", usage) store.write() log.info("Syspurpose Usage value successfully saved locally.") else: log.error( "SyspurposeStore could not be imported. Syspurpose Usage value not saved locally." )