def system_info(client, with_install=False): """Run Posh-Ohai on a remote system and gather the output. :param client: :class:`smb.SMB` instance :returns: dict -- system information from PoSh-Ohai :raises: SystemInfoCommandMissing, SystemInfoCommandOld, SystemInfoNotJson SystemInfoMissingJson SystemInfoCommandMissing if `posh-ohai` is not installed. SystemInfoCommandOld if `posh-ohai` is not the latest. SystemInfoNotJson if `posh-ohai` does not return valid JSON. SystemInfoMissingJson if `posh-ohai` does not return any JSON. """ if with_install: perform_install(client) if client.is_windows(): powershell_command = 'Get-ComputerConfiguration' output = client.execute(powershell_command) unicode_output = "%s" % output try: results = json.loads(unicode_output) except ValueError: try: clean_output = get_json(unicode_output) results = json.loads(clean_output) except ValueError as err: raise errors.SystemInfoNotJson(err) return results else: raise errors.PlatformNotSupported( "PoSh-Ohai is a Windows-only sytem info provider. " "Target platform was %s", client.platform_info['dist'])
def perform_install(client, install_dir=None): """Install PoSh-Ohai on remote system. :param install_dir -- For compatibility. Ignored. """ LOG.info("Installing (or updating) PoSh-Ohai on device %s at %s:%d", client.host, client.host, client.port) # Check is it is a windows box, but fail safely to Linux is_windows = False try: is_windows = client.is_windows() except Exception: pass if is_windows: powershell_command = ('[scriptblock]::Create((New-Object -TypeName ' 'System.Net.WebClient).DownloadString(' '"http://readonly.configdiscovery.rackspace.com' '/deploy.ps1")).Invoke()') # check output to ensure that installation was successful # if not, raise SystemInfoCommandInstallFailed output = client.execute(powershell_command) return output else: raise errors.PlatformNotSupported( "PoSh-Ohai is a Windows-only sytem info provider. " "Target platform was %s", client.platform_info['dist'])
def system_info(client, with_install=False, install_dir=None): """Run Posh-Ohai on a remote system and gather the output. :param client: :class:`smb.SMB` instance :param install_dir -- this is for compatibility and is ignored :returns: dict -- system information from PoSh-Ohai :raises: SystemInfoCommandMissing, SystemInfoCommandOld, SystemInfoInvalid SystemInfoCommandMissing if `posh-ohai` is not installed. SystemInfoCommandOld if `posh-ohai` is not the latest. SystemInfoInvalid if `posh-ohai` does not return valid JSON or XML. """ if with_install: perform_install(client) if client.is_windows(): powershell_command = ('Import-Module -Name Posh-Ohai;' 'Get-ComputerConfiguration') output = client.execute(powershell_command) unicode_output = "%s" % output load_clean_json = lambda output: json.loads(get_json(output)) last_err = None for loader in json.loads, parse_xml, load_clean_json: try: return loader(unicode_output) except ValueError as err: last_err = err raise errors.SystemInfoInvalid(last_err) else: raise errors.PlatformNotSupported( "PoSh-Ohai is a Windows-only sytem info provider. " "Target platform was %s", client.platform_info['dist'])
def remove_remote(client): """Remove PoSh-Ohai from specifc remote system. Currently supports: - ubuntu [10.x, 12.x] - debian [6.x, 7.x] - redhat [5.x, 6.x] - centos [5.x, 6.x] """ if client.is_windows(): powershell_command = ('Remove-Item -Path (Join-Path -Path ' '$($env:PSModulePath.Split(";") ' '| Where-Object { $_.StartsWith(' '$env:SystemRoot)}) -ChildPath ' '"PoSh-Ohai") -Recurse -Force -ErrorAction ' 'SilentlyContinue') output = client.execute(powershell_command) return output else: raise errors.PlatformNotSupported( "PoSh-Ohai is a Windows-only sytem info provider. " "Target platform was %s", client.platform_info['dist'])