def GetPools(self, options, proxy_options, locale, sender=None): """ Try to get pools installed/available/consumed at this system :param options: D-Bus object storing options of query :param proxy_options: D-Bus object with proxy configuration :param locale: String with locale (e.g. de_DE.UTF-8) :param sender: Not used argument :return: String with JSON dump """ options = dbus_utils.dbus_to_python(options, expected_type=dict) proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) on_date = options.setdefault('on_date', "") if on_date != "": options['on_date'] = self._parse_date(on_date) after_date = options.setdefault("after_date", "") if after_date != "": options["after_date"] = self._parse_date(after_date) future = options.setdefault("future", "") if future != "": options["future"] = future cp = self.build_uep(proxy_options, proxy_only=True) entitlement_service = EntitlementService(cp) pools = entitlement_service.get_pools(**options) return json.dumps(pools)
def GetStatus(self, on_date, locale, sender=None): """ Get status of entitlements :param on_date: Date :param locale: String with locale (e.g. de_DE.UTF-8) :param sender: Not used argument :return: String with JSON dump """ on_date = dbus_utils.dbus_to_python(on_date, expected_type=str) locale = dbus_utils.dbus_to_python(locale, expected_type=str) if on_date == "": on_date = None else: on_date = self._parse_date(on_date) Locale.set(locale) try: # get_status doesn't need a Candlepin connection status = EntitlementService(None).get_status(on_date) except Exception as e: log.exception(e) raise dbus.DBusException(str(e)) return json.dumps(status)
def ListInstalledProducts(self, filter_string, proxy_options, locale, sender=None): # We reinitialize dependency injection here for following reason. When new product # certificate is installed (or existing is removed), then this change is not propagated to # CertSorter and other caches. Calling installed_products.list(filter_string) without # reinitialization of dependency injection would return old (cached) list of installed # products. init_dep_injection() filter_string = dbus_utils.dbus_to_python(filter_string, expected_type=str) proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep(proxy_options, proxy_only=True) installed_products = InstalledProducts(cp) try: response = installed_products.list(filter_string) except Exception as err: raise dbus.DBusException(str(err)) return json.dumps(response)
def PoolAttach(self, pools, quantity, proxy_options, locale, sender=None): self.ensure_registered() pools = dbus_utils.dbus_to_python(pools, expected_type=list) quantity = dbus_utils.dbus_to_python(quantity, expected_type=int) proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) if quantity < 1: raise dbus.DBusException("Quantity must be a positive number.") cp = self.build_uep(proxy_options, proxy_only=True) attach_service = AttachService(cp) try: results = [] for pool in pools: resp = attach_service.attach_pool(pool, quantity) results.append(json.dumps(resp)) except Exception as e: log.exception(e) raise dbus.DBusException(str(e)) # TODO Likely should only call this if something is actually attached entcertlib.EntCertActionInvoker().update() return results
def Set(self, property_name, new_value, locale, sender=None): property_name = dbus_utils.dbus_to_python(property_name, expected_type=str) new_value = dbus_utils.dbus_to_python(new_value, expected_type=str) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) section, _dot, property_name = property_name.partition('.') if not property_name: raise DBusException("Setting an entire section is not supported. Use 'section.property' format.") self.config[section][property_name] = new_value self.config.persist()
def RemoveAllEntitlements(self, proxy_options, locale, sender=None): """ Try to remove all entitlements (subscriptions) from the system :param proxy_options: Settings of proxy :param locale: String with locale (e.g. de_DE.UTF-8) :param sender: Not used argument :return: Json string containing response """ proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) cp = self.build_uep(proxy_options, proxy_only=True) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) entitlement_service = EntitlementService(cp) result = entitlement_service.remove_all_entitlements() return json.dumps(result)
def RegisterWithActivationKeys(self, org, activation_keys, options, connection_options, locale): """ Note this method is registration ONLY. Auto-attach is a separate process. """ connection_options = dbus_utils.dbus_to_python(connection_options, expected_type=dict) options = dbus_utils.dbus_to_python(options, expected_type=dict) options['activation_keys'] = dbus_utils.dbus_to_python(activation_keys, expected_type=list) org = dbus_utils.dbus_to_python(org, expected_type=str) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep(connection_options) register_service = RegisterService(cp) consumer = register_service.register(org, **options) return json.dumps(consumer)
def AutoAttach(self, service_level, proxy_options, locale, sender=None): self.ensure_registered() service_level = dbus_utils.dbus_to_python(service_level, expected_type=str) or None proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep(proxy_options, proxy_only=True) attach_service = AttachService(cp) try: resp = attach_service.attach_auto(service_level) except Exception as e: log.exception(e) raise dbus.DBusException(str(e)) # TODO Likely should only call this if something is actually attached entcertlib.EntCertActionInvoker().update() return json.dumps(resp)
def Get(self, property_name, locale, sender=None): locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) section, _dot, property_name = property_name.partition('.') if property_name: return self.config[section][property_name] else: return dbus.Dictionary(self.config[section], signature='sv')
def Stop(self, locale, sender=None): locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) with self.lock: if self.server: self.server.shutdown() self.server = None log.debug("Stopped DomainSocketServer") return True else: raise exceptions.Failed("No domain socket server is running")
def RemoveEntitlementsBySerials(self, serials, proxy_options, locale, sender=None): """ Try to remove entitlements (subscriptions) by serials :param serials: List of serial numbers of subscriptions :param proxy_options: Settings of proxy :param locale: String with locale (e.g. de_DE.UTF-8) :param sender: Not used argument :return: Json string representing list of serial numbers """ serials = dbus_utils.dbus_to_python(serials, expected_type=list) proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep(proxy_options, proxy_only=True) entitlement_service = EntitlementService(cp) removed_serials, unremoved_serials = entitlement_service.remove_entitlements_by_serials(serials) return json.dumps(removed_serials)
def GetAll(self, locale, sender=None): locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) d = dbus.Dictionary({}, signature='sv') for k, v in six.iteritems(self.config): d[k] = dbus.Dictionary({}, signature='ss') for kk, vv in six.iteritems(v): d[k][kk] = vv return d
def Register(self, org, username, password, options, connection_options, locale): """ This method registers the system using basic auth (username and password for a given org). For any option that is required but not included the default will be used. Options is a dict of strings that modify the outcome of this method. Note this method is registration ONLY. Auto-attach is a separate process. """ if self.is_registered(): raise dbus.DBusException("This system is already registered") org = dbus_utils.dbus_to_python(org, expected_type=str) connection_options = dbus_utils.dbus_to_python(connection_options, expected_type=dict) connection_options['username'] = dbus_utils.dbus_to_python(username, expected_type=str) connection_options['password'] = dbus_utils.dbus_to_python(password, expected_type=str) options = dbus_utils.dbus_to_python(options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep(connection_options) register_service = RegisterService(cp) consumer = register_service.register(org, **options) return json.dumps(consumer)
def GetOrg(self, locale, sender=None): """ D-Bus method for getting current organization (owner) :param locale: string with locale :param sender: not used :return: """ locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) org = get_current_owner() return json.dumps(org)
def GetSyspurposeStatus(self, locale, sender=None): """ D-Bus method for getting system purpose status :param locale: string representing locale :param sender: object representing application which called this method :return: """ locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep({}) system_purpose = syspurpose.Syspurpose(cp) syspurpose_status = system_purpose.get_syspurpose_status()['status'] return system_purpose.get_overall_status(syspurpose_status)
def RemoveAllEntitlements(self, proxy_options, sender=None): """ Try to remove all entitlements (subscriptions) from the system :param proxy_options: Settings of proxy :param sender: Not used argument :return: Json string containing response """ proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) cp = self.build_uep(proxy_options, proxy_only=True) entitlement_service = EntitlementService(cp) result = entitlement_service.remove_all_entitlements() return json.dumps(result)
def PoolAttach(self, pools, quantity, proxy_options, locale, sender=None): self.ensure_registered() pools = dbus_utils.dbus_to_python(pools, expected_type=list) quantity = dbus_utils.dbus_to_python(quantity, expected_type=int) proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) if quantity < 1: raise dbus.DBusException("Quantity must be a positive number.") cp = self.build_uep(proxy_options, proxy_only=True) # TODO: Change log.info() to: # raise dbus.DBusException('Attaching of pool(s) is not allowed in simple content access mode') # in the next minor release of subscription-manager if is_simple_content_access(uep=cp) is True: log.info( "Calling D-Bus method PoolAttach() is deprecated, when Simple Content Access mode " "is used and it will be not be supported in the next minor release of " "subscription-manager") attach_service = AttachService(cp) try: results = [] for pool in pools: resp = attach_service.attach_pool(pool, quantity) results.append(json.dumps(resp)) except Exception as e: log.exception(e) raise dbus.DBusException(str(e)) # TODO Likely should only call this if something is actually attached entcertlib.EntCertActionInvoker().update() return results
def Start(self, locale, sender=None): locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) with self.lock: if self.server: return self.server.address log.debug('Attempting to create new domain socket server') self.server = server.DomainSocketServer( object_classes=[DomainSocketRegisterDBusObject], ) address = self.server.run() log.debug('DomainSocketServer created and listening on "%s"', address) return address
def Unregister(self, proxy_options, locale, sender=None): """ Definition and implementation of D-Bus method :param proxy_options: Definition of proxy settings :param locale: String with locale (e.g. de_DE.UTF-8) :param sender: Not used argument """ proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) self.ensure_registered() uep = self.build_uep(proxy_options, proxy_only=True) try: UnregisterService(uep).unregister() except Exception as err: raise dbus.DBusException(str(err)) # The system is unregistered now, restart virt-who to stop sending # host-to-guest mapping. restart_virt_who()
def RegisterWithActivationKeys(self, org, activation_keys, options, connection_options, locale): """ Note this method is registration ONLY. Auto-attach is a separate process. """ connection_options = dbus_utils.dbus_to_python(connection_options, expected_type=dict) options = dbus_utils.dbus_to_python(options, expected_type=dict) options['activation_keys'] = dbus_utils.dbus_to_python( activation_keys, expected_type=list) org = dbus_utils.dbus_to_python(org, expected_type=str) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep(connection_options) register_service = RegisterService(cp) consumer = register_service.register(org, **options) log.debug("System registered, updating entitlements if needed") entcertlib = EntCertActionInvoker() entcertlib.update() return json.dumps(consumer)
def AutoAttach(self, service_level, proxy_options, sender=None): self.ensure_registered() service_level = dbus_utils.dbus_to_python(service_level, str) cp = self.build_uep(proxy_options, proxy_only=True) attach_service = AttachService(cp) try: resp = attach_service.attach_auto(service_level) except Exception as e: log.exception(e) raise dbus.DBusException(str(e)) # TODO Likely should only call this if something is actually attached entcertlib.EntCertActionInvoker().update() return json.dumps(resp)
def GetAll(self, locale, sender=None): """ Method for getting whole configuration :param locale: string with locale :param sender: not used :return: D-bus dictionary with configuration """ locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) d = dbus.Dictionary({}, signature="sv") for k, v in self.config.items(): d[k] = dbus.Dictionary({}, signature="ss") for kk, vv in v.items(): d[k][kk] = vv return d
def Get(self, property_name, locale, sender=None): """ D-Bus method for getting one configuration property or one section :param property_name: string with name of property e.g. server.hostname or section e.g. server :param locale: string with locale :param sender: not used :return: string with value of property or dictionary with dictionary of one section """ locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) section, _dot, property_name = property_name.partition(".") if property_name: return self.config[section][property_name] else: return dbus.Dictionary(self.config[section], signature="sv")
def GetUuid(self, locale, sender=None): """ D-Bus method for getting current consumer UUID :param locale: string with locale :param sender: :return: string with UUID """ locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) consumer = Consumer() try: uuid = consumer.get_consumer_uuid() except Exception as err: raise dbus.DBusException(str(err)) return str(uuid)
def Start(self, locale, sender=None): locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) with self.lock: if self.server: return self.server.address log.debug("Attempting to create new domain socket server") cmd_line = DBusSender.get_cmd_line(sender) self.server = server.DomainSocketServer( object_classes=[DomainSocketRegisterDBusObject], sender=sender, cmd_line=cmd_line) address = self.server.run() log.debug( 'DomainSocketServer for sender %s created and listening on "%s"' % (sender, address)) return address
def Unregister(self, proxy_options, sender=None): """ Definition and implementation of D-Bus method :param proxy_options: Definition of proxy settings :param sender: Not used argument """ proxy_options = dbus_utils.dbus_to_python(proxy_options, expected_type=dict) self.ensure_registered() uep = self.build_uep(proxy_options, proxy_only=True) try: UnregisterService(uep).unregister() except Exception as err: raise dbus.DBusException(str(err)) # The system is unregistered now, restart virt-who to stop sending # host-to-guest mapping. restart_virt_who()
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 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 GetStatus(self, on_date, sender=None): """ Get status of entitlements :param on_date: Date :param sender: Not used argument :return: String with JSON dump """ try: on_date = dbus_utils.dbus_to_python(on_date) if on_date == "": on_date = None else: on_date = self._parse_date(on_date) # get_status doesn't need a Candlepin connection status = EntitlementService(None).get_status(on_date) except Exception as e: log.exception(e) raise dbus.DBusException(str(e)) return json.dumps(status)
def Register(self, org, username, password, options, connection_options, locale): """ This method registers the system using basic auth (username and password for a given org). For any option that is required but not included the default will be used. Options is a dict of strings that modify the outcome of this method. Note this method is registration ONLY. Auto-attach is a separate process. """ if self.is_registered(): raise dbus.DBusException("This system is already registered") org = dbus_utils.dbus_to_python(org, expected_type=str) connection_options = dbus_utils.dbus_to_python(connection_options, expected_type=dict) connection_options['username'] = dbus_utils.dbus_to_python( username, expected_type=str) connection_options['password'] = dbus_utils.dbus_to_python( password, expected_type=str) options = dbus_utils.dbus_to_python(options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep(connection_options) register_service = RegisterService(cp) # Try to get organization from the list available organizations, when the list contains # only one item, then register_service.determine_owner_key will return this organization if not org: org = register_service.determine_owner_key( username=connection_options['username'], get_owner_cb=self._get_owner_cb, no_owner_cb=self._no_owner_cb) # When there is more organizations, then signal was triggered in callback method # _get_owner_cb, but some exception has to be raised here to not try registration process if not org: raise OrgNotSpecifiedException( username=connection_options['username']) consumer = register_service.register(org, **options) return json.dumps(consumer)
def GetValidFields(self, locale, sender=None): """ Method for getting valid syspurpose attributes and values :param locale: string with locale :param sender: object representing application which called this method :return: string representing dictionary with valid fields """ locale = dbus_utils.dbus_to_python(locale, expected_type=str) Locale.set(locale) cp = self.build_uep({}) system_purpose = syspurpose.Syspurpose(cp) valid_fields = system_purpose.get_owner_syspurpose_valid_fields() if valid_fields is None: # When it is not possible to get valid fields, then raise exception if self.is_registered() is False: raise dbus.DBusException( "Unable to get system purpose valid fields. System is not registered.", ) else: raise dbus.DBusException( "Unable to get system purpose valid fields.", ) else: return json.dumps(valid_fields)
def Register(self, org, username, password, options, connection_options, locale): """ This method registers the system using basic auth (username and password for a given org). For any option that is required but not included the default will be used. Options is a dict of strings that modify the outcome of this method. Note this method is registration ONLY. Auto-attach is a separate process. """ if self.is_registered(): raise dbus.DBusException("This system is already registered") org = dbus_utils.dbus_to_python(org, expected_type=str) connection_options = dbus_utils.dbus_to_python(connection_options, expected_type=dict) connection_options["username"] = dbus_utils.dbus_to_python( username, expected_type=str) connection_options["password"] = dbus_utils.dbus_to_python( password, expected_type=str) options = dbus_utils.dbus_to_python(options, expected_type=dict) locale = dbus_utils.dbus_to_python(locale, expected_type=str) with DBusSender() as dbus_sender: dbus_sender.set_cmd_line(sender=self.sender, cmd_line=self.cmd_line) Locale.set(locale) cp = self.build_uep(connection_options) register_service = RegisterService(cp) # Try to get organization from the list available organizations, when the list contains # only one item, then register_service.determine_owner_key will return this organization if not org: org = register_service.determine_owner_key( username=connection_options["username"], get_owner_cb=self._get_owner_cb, no_owner_cb=self._no_owner_cb, ) # When there is more organizations, then signal was triggered in callback method # _get_owner_cb, but some exception has to be raised here to not try registration process if not org: raise OrgNotSpecifiedException( username=connection_options["username"]) # Remove 'enable_content' option, because it will not be proceed in register service enable_content = self._remove_enable_content_option(options) consumer = register_service.register(org, **options) # When consumer is created, then we can try to enabled content, when it was # requested in options. if enable_content is True: self._enable_content(cp, consumer) dbus_sender.reset_cmd_line() return json.dumps(consumer)