def _snitch(self, session): """Orchestrates the creation of the environment. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession """ env = EnvironmentEntity( account_number=self.run.environment_account_number, name=self.run.environment_name ) for hostname, filename in self._find_host_tuples(self.file_pattern): virtualenvs = [] host = HostEntity(hostname=hostname, environment=env.identity) host = HostEntity.find(session, host.identity) if host is None: logger.warning( 'Unable to locate host entity {}'.format(hostname) ) continue with open(filename, 'r') as f: pipdict = json.loads(f.read()) pipdict = pipdict.get('data', {}) for path, pkglist in pipdict.items(): virtualenv = self._update_virtualenv( session, host, path, pkglist ) virtualenvs.append(virtualenv) host.virtualenvs.update(session, virtualenvs, self.time_in_ms)
def _snitch(self, session): """Update the kernel modules subgraph. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession """ env = EnvironmentEntity(uuid=self.run.environment_uuid) for hostname, filename in self._find_host_tuples(self.file_pattern): kms = [] # Find host in graph, continue if host not found. host = HostEntity(hostname=hostname, environment=env.identity) host = HostEntity.find(session, host.identity) if host is None: logger.warning( 'Unable to locate host entity {}'.format(hostname)) continue # Read data from file km_data = self.run.get_object(filename).get('data', {}) # Iterate over package maps for km_name, km_dict in km_data.items(): km = self._update_kernel_module(session, host, km_name, km_dict) if km is not None: kms.append(km) host.kernelmodules.update(session, kms, self.time_in_ms)
def _snitch(self, session): """Update the apt part of the graph.. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession """ env = EnvironmentEntity( account_number=self.run.environment_account_number, name=self.run.environment_name) for hostname, filename in self._find_host_tuples(self.file_pattern): aptpkgs = [] # Find host in graph, continue if host not found. host = HostEntity(hostname=hostname, environment=env.identity) host = HostEntity.find(session, host.identity) if host is None: logger.warning( 'Unable to locate host entity {}'.format(hostname)) continue # Read data from file with open(filename, 'r') as f: aptdata = json.loads(f.read()) aptlist = aptdata.get('data', []) # Iterate over package maps for aptdict in aptlist: aptpkg = self._update_apt_package(session, aptdict) if aptpkg is not None: aptpkgs.append(aptpkg) host.aptpackages.update(session, aptpkgs, self.time_in_ms)
def _host_from_tuple(self, session, env, host_tuple): """Load hostdata from json file and create HostEntity instance. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param env: Environment entity hosts belong to. :type env: EnvironmentEntity :param host_tuple: (hostname, filename) :type host_tuple: tuple :returns: Host object :rtype: HostEntity """ hostname, filename = host_tuple with open(filename, 'r') as f: fulldict = json.loads(f.read()) fulldict = fulldict.get('data', {}) # Start kwargs for making the host entity hostkwargs = {} # Remove anything not prefixed with 'ansible_' ansibledict = {} for k, v in fulldict.items(): if k.startswith('ansible_'): ansibledict[k] = v # Create properties that require little intervention for ansible_key, host_key in _EASY_KEY_MAP.items(): val = ansibledict.get(ansible_key) if val is not None: hostkwargs[host_key] = val # Create properties that can be found by path for complexkey, host_key in _COMPLEX_KEY_MAP.items(): val = complex_get(complexkey, ansibledict) if val is not None: hostkwargs[host_key] = val host = HostEntity(hostname=hostname, environment=env.identity, **hostkwargs) host.update(session, self.time_in_ms) # Update nameservers subgraph self._update_nameservers(session, host, ansibledict) # Update mounts subgraph self._update_mounts(session, host, ansibledict) # Update devices self._update_devices(session, host, ansibledict) # Update interfaces self._update_interfaces(session, host, ansibledict) return host
def _update_host(self, session, hostname, filename): """Update configuration files for a host. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param hostname: Name of the host :type hostname: str :param filename: Name of file :type filename: str """ # Extract config and environment data. with open(filename, 'r') as f: configdata = json.loads(f.read()) envdict = configdata.get('environment', {}) env = EnvironmentEntity( account_number=envdict.get('account_number'), name=envdict.get('name')) configdata = configdata.get('data', {}) # Find parent host object - return early if not exists. host = HostEntity(hostname=hostname, environment=env.identity) host = HostEntity.find(session, host.identity) if host is None: logger.warning('Unable to locate host {}'.format(hostname)) return # Iterate over configration files in the host's directory configfiles = [] for filename, contents in configdata.items(): _, name = os.path.split(filename) md5 = hashlib.md5() md5.update(contents.encode('utf-8')) md5 = md5.hexdigest() # Update configfile node configfile = ConfigfileEntity(path=filename, host=host.identity, md5=md5, contents=contents, name=name) configfile.update(session, self.time_in_ms) configfiles.append(configfile) # Update host -> configfile relationships. host.configfiles.update(session, configfiles, self.time_in_ms)
def _update_host(self, session, hostname, filename): """Update configuredinterfaces for a host. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param hostname: Name of the host :type hostname: str :param filename: Name of file :type filename: str """ # Extract config and environment data. env = EnvironmentEntity(uuid=self.run.environment_uuid) with open(filename, 'r') as f: data = json.loads(f.read()) configdata = data.get('data', {}) # Find parent host object - return early if not exists. host = HostEntity(hostname=hostname, environment=env.identity) host = HostEntity.find(session, host.identity) if host is None: logger.warning('Unable to locate host {}'.format(hostname)) return # Iterate over configration files in the host's directory interfaces = [] for device, metadata in configdata.items(): interfacekwargs = { 'device': device, 'host': host.identity } for key, val in metadata.items(): if '-' in key: # neo4j properties don't allow - # dns-nameservers, offline-sg key = key.replace('-', '_') interfacekwargs[key] = val interface = ConfiguredInterfaceEntity(**interfacekwargs) interface.update(session, self.time_in_ms) interfaces.append(interface) # Update host -> configuredinterfaces relationships. host.configuredinterfaces.update(session, interfaces, self.time_in_ms)
def _update_host(self, session, hostname, filename): """Update configuration files for a host. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param hostname: Name of the host :type hostname: str :param filename: Name of file :type filename: str """ # Extract config and environment data. env = EnvironmentEntity(uuid=self.run.environment_uuid) with open(filename, 'r') as f: configdata = json.loads(f.read()) configdata = configdata.get('data', {}) # Find parent host object - return early if not exists. host = HostEntity(hostname=hostname, environment=env.identity) host = HostEntity.find(session, host.identity) if host is None: logger.warning('Unable to locate host {}'.format(hostname)) return # Iterate over configration files in the host's directory configfiles = [] for filename, metadata in configdata.items(): _, name = os.path.split(filename) # Update configfile node configfile = ConfigfileEntity(path=filename, host=host.identity, md5=metadata.get('md5'), contents=metadata.get('contents'), is_binary=metadata.get('is_binary'), name=name) configfile.update(session, self.time_in_ms) configfiles.append(configfile) # Update host -> configfile relationships. host.configfiles.update(session, configfiles, self.time_in_ms)