def _init_db(self): """ Initialize the internal Libosinfo.Db object. """ loader = Libosinfo.Loader() loader.process_default_path() self._db = loader.get_db()
def _make_default_variants(self, allvariants): # Add our custom generic variant o = Libosinfo.Os() o.set_param("short-id", "generic") o.set_param("name", _("Generic OS")) v = _OsVariant(o) allvariants[v.name] = v
def _get_os_loader(): global _os_loader if _os_loader: return _os_loader _os_loader = libosinfo.Loader() _os_loader.process_default_path() return _os_loader
def _os_loader(self): if not self.__os_loader: loader = Libosinfo.Loader() loader.process_default_path() self.__os_loader = loader return self.__os_loader
def _os_generic(self): if not self.__os_generic: # Add our custom generic variant o = Libosinfo.Os() o.set_param("short-id", "generic") o.set_param("name", _("Generic OS")) self.__os_generic = _OsVariant(o) return self.__os_generic
def default_inputbus(self): if self._os: fltr = libosinfo.Filter() fltr.add_constraint("class", "input") devs = self._os.get_all_devices(fltr) if devs.get_length(): return devs.get_nth(0).get_bus_type() return "ps2"
def _get_inputtype(self): if not self._os: return _SENTINEL fltr = libosinfo.Filter() fltr.add_constraint("class", "input") devs = self._os.get_all_devices(fltr) if devs.get_length(): return devs.get_nth(0).get_name() return _SENTINEL
def _os_generic(self): if not self.__os_generic: # Add our custom generic variant o = Libosinfo.Os() o.set_param("short-id", "generic") o.set_param("name", _("Generic or unknown OS. Usage is not recommended.")) self.__os_generic = _OsVariant(o) return self.__os_generic
def _get_all_variants(self): loader = libosinfo.Loader() loader.process_default_path() allvariants = self._make_default_variants() db = loader.get_db() oslist = db.get_os_list() for os in range(oslist.get_length()): osi = _OsVariant(oslist.get_nth(os)) allvariants[osi.name] = osi return allvariants
def supports_virtiorng(self): if self._os: fltr = libosinfo.Filter() fltr.add_constraint("class", "rng") devs = self._os.get_all_devices(fltr) for dev in range(devs.get_length()): d = devs.get_nth(dev) if d.get_name() == "virtio-rng": return True return False
def supports_usbtablet(self): if not self._os: return False fltr = libosinfo.Filter() fltr.add_constraint("class", "input") fltr.add_constraint("name", "tablet") devs = self._os.get_all_devices(fltr) for idx in range(devs.get_length()): if devs.get_nth(idx).get_bus_type() == "usb": return True return False
def _os_loader(self): if _WITHOUT_LIBOSINFO: if self.__os_loader is not None: self.__os_loader = None return self.__os_loader if not self.__os_loader: loader = libosinfo.Loader() loader.process_default_path() self.__os_loader = loader return self.__os_loader
def _get_netmodel(self): if not self._os: return _SENTINEL if self._os.get_distro() == "fedora": return _SENTINEL fltr = libosinfo.Filter() fltr.add_constraint("class", "net") devs = self._os.get_all_devices(fltr) if devs.get_length(): return devs.get_nth(0).get_name() return _SENTINEL
def lookup_os(self, key, raise_error=False): if key == self._os_generic.name: return self._os_generic flt = Libosinfo.Filter() flt.add_constraint(Libosinfo.PRODUCT_PROP_SHORT_ID, key) oslist = self._os_db.get_os_list().new_filtered(flt).get_elements() if len(oslist) == 0: if raise_error: raise ValueError( _("Unknown OS name '%s'. " "See `osinfo-query os` for valid values.") % key) return None return _OsVariant(oslist[0])
def run(self, terms, variables, **kwargs): ret = [] for term in terms: filter = osinfo.Filter() filter.add_constraint(osinfo.PRODUCT_PROP_SHORT_ID, term) oses = db.get_os_list().new_filtered(filter) if oses.get_length() > 0: os = OsInfoGObjectProxy(oses.get_nth(0), root_path="[" + term + "]") ret.append(os) else: ret.append({"name": term}) return ret
def default_netmodel(self): """ Default non-virtio net-model, since we check for that separately """ if not self._os: return None fltr = libosinfo.Filter() fltr.add_constraint("class", "net") devs = self._os.get_all_devices(fltr) for idx in range(devs.get_length()): devname = devs.get_nth(idx).get_name() if devname in ["pcnet", "ne2k_pci", "rtl8139", "e1000"]: return devname return None
def _is_virtionet(self): if not self._os: return _SENTINEL if self._os.get_distro() == "fedora": if self._os.get_version() == "unknown": return _SENTINEL return int(self._os.get_version() >= 9) or _SENTINEL fltr = libosinfo.Filter() fltr.add_constraint("class", "net") devs = self._os.get_all_devices(fltr) for dev in range(devs.get_length()): d = devs.get_nth(dev) if d.get_name() == "virtio-net": return True return _SENTINEL
def init_osinfo(path=None): """ Initialize libosinfo db. :param path: libosinfo distro data :return: an osinfo.Db instance """ if osinfo is None: return None loader = osinfo.Loader() if path is None: loader.process_default_path() else: loader.process_path(path) return loader.get_db()
def run(self, terms, variables, **kwargs): ret = [] for term in terms: filter = osinfo.Filter() if "=" in term: prop, value = term.split("=", 1) filter.add_constraint(prop, value) else: filter.add_constraint(osinfo.PRODUCT_PROP_SHORT_ID, term) oses = db.get_os_list().new_filtered(filter) if oses.get_length() > 0: for idx in range(oses.get_length()): os = OsInfoGObjectProxy(oses.get_nth(idx), root_path="[" + term + "]") ret.append(os) else: print("OS {} not found".format(term)) ret.append({"name": term}) return ret
def get_location(self, arch): if not self._os: return None treefilter = libosinfo.Filter() treefilter.add_constraint(libosinfo.TREE_PROP_ARCHITECTURE, arch) treelist = self._os.get_tree_list() if treelist.get_length() < 1: raise RuntimeError( _("%s does not have a URL location") % self.name) filtered_treelist = treelist.new_filtered(treefilter) if filtered_treelist.get_length() < 1: raise RuntimeError( _("%s does not have a URL location for the %s architecture") % (self.name, arch)) # Some distros have more than one URL for a specific architecture, # which is the case for Fedora and different variants (Server, # Workstation). Later on, we'll have to differentiate that and return # the right one. return filtered_treelist.get_nth(0).get_url()
#!/usr/bin/python from gi.repository import Libosinfo as osinfo; loader = osinfo.Loader() loader.process_path("./") db = loader.get_db() devs = db.get_device_list() print "All device IDs" for dev in devs.get_elements(): print (" Device " + dev.get_id()) names = db.unique_values_for_property_in_device("name") print "All device names" for name in names: print (" Name " + name) osnames = db.unique_values_for_property_in_os("short-id") osnames.sort() print "All OS short IDs" for name in osnames: print (" OS short id " + name) hvnames = db.unique_values_for_property_in_platform("short-id") hvnames.sort()
def _get_os_loader(self): loader = libosinfo.Loader() loader.process_default_path() return loader
import subprocess from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase try: from builtins import int except ImportError: from __builtin__ import int import gi gi.require_version('Libosinfo', '1.0') from gi.repository import Libosinfo as osinfo loader = osinfo.Loader() #loader.process_default_path() loader.process_path("osinfo-db/data") loader.process_path("osinfo-db-override") db = loader.get_db() class OsInfoGObjectProxy(object): def __str__(self): return "<%s @ %s obj=%s>" % (self.__class__.__name__, self._root_path, str(self._obj)) def __init__(self, obj, root_path=""): self._obj = obj self._root_path = root_path
def _make_installconfig(script, osobj, unattended_data, arch, hostname, url): """ Build a Libosinfo.InstallConfig instance """ def get_timezone(): TZ_FILE = "/etc/localtime" localtime = Gio.File.new_for_path(TZ_FILE) if not localtime.query_exists(): return None info = localtime.query_info(Gio.FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET, Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS) if not info: return None target = info.get_symlink_target() if not target: return None tokens = target.split("zoneinfo/") if not tokens or len(tokens) < 2: return None return tokens[1] def get_language(): names = GLib.get_language_names() if not names or len(names) < 2: return None return names[1] config = Libosinfo.InstallConfig() # Set user login and name based on the one from the system config.set_user_login(GLib.get_user_name()) config.set_user_realname(GLib.get_real_name()) # Set user-password. # In case it's required and not passed, just raise a RuntimeError. if script.requires_user_password() and not unattended_data.user_password: raise RuntimeError( _("%s requires the user-password to be set.") % osobj.name) config.set_user_password( unattended_data.user_password if unattended_data.user_password else "") # Set the admin-password: # In case it's required and not passed, just raise a RuntimeError. if script.requires_admin_password() and not unattended_data.admin_password: raise RuntimeError( _("%s requires the admin-password to be set.") % osobj.name) config.set_admin_password(unattended_data.admin_password if unattended_data.admin_password else "") # Set the target disk. # virtiodisk is the preferred way, in case it's supported, otherwise # just fallback to scsi. # # Note: this is linux specific and will require some changes whenever # support for Windows will be added. tgt = "/dev/vda" if osobj.supports_virtiodisk() else "/dev/sda" config.set_target_disk(tgt) # Set hardware architecture and hostname config.set_hardware_arch(arch) config.set_hostname(hostname) # Try to guess the timezone from '/etc/localtime', in case it's not # possible 'America/New_York' will be used. timezone = get_timezone() if timezone: config.set_l10n_timezone(timezone) else: logging.warning( _("'America/New_York' timezone will be used for this " "unattended installation.")) # Try to guess to language and keyboard layout from the system's # language. # # This method has flows as it's quite common to have language and # keyboard layout not matching. Otherwise, there's no easy way to guess # the keyboard layout without relying on a set of APIs of an specific # Desktop Environment. language = get_language() if language: config.set_l10n_language(language) config.set_l10n_keyboard(language) else: logging.warning( _("'en_US' will be used as both language and keyboard layout " "for unattended installation.")) if url: config.set_installation_url(url) # pylint: disable=no-member logging.debug("InstallScriptConfig created with the following params:") logging.debug("username: %s", config.get_user_login()) logging.debug("realname: %s", config.get_user_realname()) logging.debug("user password: %s", config.get_user_password()) logging.debug("admin password: %s", config.get_admin_password()) logging.debug("target disk: %s", config.get_target_disk()) logging.debug("hardware arch: %s", config.get_hardware_arch()) logging.debug("hostname: %s", config.get_hostname()) logging.debug("timezone: %s", config.get_l10n_timezone()) logging.debug("language: %s", config.get_l10n_language()) logging.debug("keyboard: %s", config.get_l10n_keyboard()) logging.debug("url: %s", config.get_installation_url()) # pylint: disable=no-member return config
def setUp(self): self.loader = osinfo.Loader() self.loader.process_default_path() self.db = self.loader.get_db()
#!/usr/bin/python3 import gi gi.require_version('Libosinfo', '1.0') from gi.repository import Libosinfo import sys loader = Libosinfo.Loader() loader.process_default_path() db = loader.get_db() oses = db.get_os_list() for i in range(oses.get_length()): os = oses.get_nth(i) osId = os.get_id() or "" osShortId = os.get_short_id() or "" osName = os.get_name() or "" osVersion = os.get_version() or "" osFamily = os.get_family() or "" osVendor = os.get_vendor() or "" osReleaseDate = os.get_release_date_string() or "" osEOLDate = os.get_eol_date_string() or "" osCodename = os.get_codename() or "" print("%s|%s|%s|%s|%s|%s|%s|%s|%s" % (osId, osShortId, osName, osVersion, osFamily, osVendor, osReleaseDate, osEOLDate, osCodename))
#!/usr/bin/python3 import gi gi.require_version('Libosinfo', '1.0') from gi.repository import Libosinfo import sys import json loader = Libosinfo.Loader() loader.process_default_path() db = loader.get_db() url_type_media = sys.argv[1].endswith(".iso") os = None res = {} if url_type_media: media = Libosinfo.Media().create_from_location(sys.argv[1]) db.identify_media(media) os = media.get_os() if os: res = {'os': os.get_id(), 'media': media.get_id()} else: tree = Libosinfo.Tree().create_from_location(sys.argv[1]) os, _ = db.guess_os_from_tree(tree) if os: res = {'os': os.get_id()} print(json.dumps(res))
def _make_installconfig(script, osobj, unattended_data, arch, hostname, url): """ Build a Libosinfo.InstallConfig instance """ def get_timezone(): TZ_FILE = "/etc/localtime" linkpath = os.path.realpath(TZ_FILE) tokens = linkpath.split("zoneinfo/") if len(tokens) > 1: return tokens[1] def get_language(): return locale.getlocale()[0] def is_user_login_safe(login): return login != "root" config = Libosinfo.InstallConfig() # Set user login and name # In case it's specified via command-line, use the specified one as login # and realname. Otherwise, fallback fto the one from the system login = unattended_data.user_login realname = unattended_data.user_login if not login: hostuser = getpass.getuser() if is_user_login_safe(hostuser): login = getpass.getuser() realname = pwd.getpwnam(login).pw_gecos if login: login = login.lower() if not is_user_login_safe(login): raise RuntimeError( _("%s cannot use '%s' as user-login.") % (osobj.name, login)) config.set_user_login(login) config.set_user_realname(realname) # Set user-password. # In case it's required and not passed, just raise a RuntimeError. if (script.requires_user_password() and not unattended_data.get_user_password()): raise RuntimeError( _("%s requires the user-password to be set.") % osobj.name) config.set_user_password(unattended_data.get_user_password() or "") # Set the admin-password: # In case it's required and not passed, just raise a RuntimeError. if (script.requires_admin_password() and not unattended_data.get_admin_password()): raise RuntimeError( _("%s requires the admin-password to be set.") % osobj.name) config.set_admin_password(unattended_data.get_admin_password() or "") # Set the target disk. # virtiodisk is the preferred way, in case it's supported, otherwise # just fallback to scsi. # # Note: this is linux specific and will require some changes whenever # support for Windows will be added. tgt = "/dev/vda" if osobj.supports_virtiodisk() else "/dev/sda" if osobj.is_windows(): tgt = "C" config.set_target_disk(tgt) # Set hardware architecture and hostname config.set_hardware_arch(arch) # Some installations will bail if the Computer's name contains one of the # following characters: "[{|}~[\\]^':; <=>?@!\"#$%`()+/.,*&]". # In order to take a safer path, let's ensure that we never set those, # replacing them by "-". hostname = re.sub("[{|}~[\\]^':; <=>?@!\"#$%`()+/.,*&]", "-", hostname) config.set_hostname(hostname) # Try to guess the timezone from '/etc/localtime', in case it's not # possible 'America/New_York' will be used. timezone = get_timezone() if timezone: config.set_l10n_timezone(timezone) # Try to guess to language and keyboard layout from the system's # language. # # This method has flaws as it's quite common to have language and # keyboard layout not matching. Otherwise, there's no easy way to guess # the keyboard layout without relying on a set of APIs of an specific # Desktop Environment. language = get_language() if language: config.set_l10n_language(language) config.set_l10n_keyboard(language) if url: config.set_installation_url(url) # pylint: disable=no-member if unattended_data.product_key: config.set_reg_product_key(unattended_data.product_key) log.debug("InstallScriptConfig created with the following params:") log.debug("username: %s", config.get_user_login()) log.debug("realname: %s", config.get_user_realname()) log.debug("target disk: %s", config.get_target_disk()) log.debug("hardware arch: %s", config.get_hardware_arch()) log.debug("hostname: %s", config.get_hostname()) log.debug("timezone: %s", config.get_l10n_timezone()) log.debug("language: %s", config.get_l10n_language()) log.debug("keyboard: %s", config.get_l10n_keyboard()) if hasattr(config, "get_installation_url"): log.debug("url: %s", config.get_installation_url()) # pylint: disable=no-member log.debug("product-key: %s", config.get_reg_product_key()) return config
def _make_installconfig(script, osobj, unattended_data, arch, hostname, url): """ Build a Libosinfo.InstallConfig instance """ def get_timezone(): TZ_FILE = "/etc/localtime" linkpath = os.path.realpath(TZ_FILE) tokens = linkpath.split("zoneinfo/") if len(tokens) > 1: return tokens[1] def get_language(): return locale.getlocale()[0] config = Libosinfo.InstallConfig() # Set user login and name based on the one from the system config.set_user_login(getpass.getuser()) config.set_user_realname(pwd.getpwnam(getpass.getuser()).pw_gecos) # Set user-password. # In case it's required and not passed, just raise a RuntimeError. if (script.requires_user_password() and not unattended_data.get_user_password()): raise RuntimeError( _("%s requires the user-password to be set.") % osobj.name) config.set_user_password(unattended_data.get_user_password() or "") # Set the admin-password: # In case it's required and not passed, just raise a RuntimeError. if (script.requires_admin_password() and not unattended_data.get_admin_password()): raise RuntimeError( _("%s requires the admin-password to be set.") % osobj.name) config.set_admin_password(unattended_data.get_admin_password() or "") # Set the target disk. # virtiodisk is the preferred way, in case it's supported, otherwise # just fallback to scsi. # # Note: this is linux specific and will require some changes whenever # support for Windows will be added. tgt = "/dev/vda" if osobj.supports_virtiodisk() else "/dev/sda" if osobj.is_windows(): tgt = "C" config.set_target_disk(tgt) # Set hardware architecture and hostname config.set_hardware_arch(arch) config.set_hostname(hostname) # Try to guess the timezone from '/etc/localtime', in case it's not # possible 'America/New_York' will be used. timezone = get_timezone() if timezone: config.set_l10n_timezone(timezone) # Try to guess to language and keyboard layout from the system's # language. # # This method has flaws as it's quite common to have language and # keyboard layout not matching. Otherwise, there's no easy way to guess # the keyboard layout without relying on a set of APIs of an specific # Desktop Environment. language = get_language() if language: config.set_l10n_language(language) config.set_l10n_keyboard(language) if url: config.set_installation_url(url) # pylint: disable=no-member if unattended_data.product_key: config.set_reg_product_key(unattended_data.product_key) log.debug("InstallScriptConfig created with the following params:") log.debug("username: %s", config.get_user_login()) log.debug("realname: %s", config.get_user_realname()) log.debug("target disk: %s", config.get_target_disk()) log.debug("hardware arch: %s", config.get_hardware_arch()) log.debug("hostname: %s", config.get_hostname()) log.debug("timezone: %s", config.get_l10n_timezone()) log.debug("language: %s", config.get_l10n_language()) log.debug("keyboard: %s", config.get_l10n_keyboard()) log.debug("url: %s", config.get_installation_url()) # pylint: disable=no-member log.debug("product-key: %s", config.get_reg_product_key()) return config