def check(order): order.set_description("Export to a directory or package") hosts = get_hosts_from_etree(order.xml) if not hosts: return False for host in hosts: if len(host.get("path")) == 0: return False return True
def check(order): order.set_description('Export to a directory or package') hosts = get_hosts_from_etree(order.xml) if not hosts: return False for host in hosts: if len(host.get('path')) == 0: return False return True
def run(self, logger, order): tmp_dir = mkdtemp() for host in get_hosts_from_etree(order.xml): seedhost = self.get_seedhost_from_name(host.get_name()) address = seedhost.get_address() hostname = seedhost.get_name() dst_path = host.get('path')[0] dst_dir = os.path.join(tmp_dir, dst_path) vars = { 'hostname': hostname, 'address': address, 'os': seedhost.get('os') } profile = None for profile in self.profiles: if not profile.test_condition(vars): continue logger.info(hostname + ': Selected profile is ' + profile.name) for name, from_name in profile.files: logger.info('%s: %s -> %s' % (hostname, from_name, name)) src = self.store.get_alias(seedhost, from_name) if src is None: logger.info(hostname + ': %s not found' % from_name) continue dst = os.path.join(dst_dir, name) dst = dst.replace('{address}', address) dst = dst.replace('{hostname}', hostname) if os.path.exists(src): if not os.path.exists(dst_dir): os.makedirs(dst_dir) os.symlink(src, dst) else: logger.info(hostname + ': %s does not exist.' % src) break if profile is None: logger.info(hostname + ': No profile! ' + repr(vars)) path = os.path.join(self.out_dir, self.out_name) if self.format == 'directory': if not os.path.exists(path): os.makedirs(path) if self.overwrite: for file in os.listdir(path): file = os.path.join(path, file) rmtree(file) for file in os.listdir(tmp_dir): file = os.path.join(tmp_dir, file) move(file, path) else: self._mktar(tmp_dir) rmtree(tmp_dir) for handler in self.send_to: handler.send(path)
def run(self, logger, order): tmp_dir = mkdtemp() for host in get_hosts_from_etree(order.xml): seedhost = self.get_seedhost_from_name(host.get_name()) address = seedhost.get_address() hostname = seedhost.get_name() dst_path = host.get('path')[0] dst_dir = os.path.join(tmp_dir, dst_path) vars = {'hostname': hostname, 'address': address, 'os': seedhost.get('os')} profile = None for profile in self.profiles: if not profile.test_condition(vars): continue logger.info(hostname + ': Selected profile is ' + profile.name) for name, from_name in profile.files: logger.info('%s: %s -> %s' % (hostname, from_name, name)) src = self.store.get_alias(seedhost, from_name) if src is None: logger.info(hostname + ': %s not found' % from_name) continue dst = os.path.join(dst_dir, name) dst = dst.replace('{address}', address) dst = dst.replace('{hostname}', hostname) if os.path.exists(src): if not os.path.exists(dst_dir): os.makedirs(dst_dir) os.symlink(src, dst) else: logger.info(hostname + ': %s does not exist.' % src) break if profile is None: logger.info(hostname + ': No profile! ' + repr(vars)) path = os.path.join(self.out_dir, self.out_name) if self.format == 'directory': if not os.path.exists(path): os.makedirs(path) if self.overwrite: for file in os.listdir(path): file = os.path.join(path, file) rmtree(file) for file in os.listdir(tmp_dir): file = os.path.join(tmp_dir, file) move(file, path) else: self._mktar(tmp_dir) rmtree(tmp_dir) for handler in self.send_to: handler.send(path)
def check(order): order.set_description('Update the host database') hosts = get_hosts_from_etree(order.xml) if not hosts: return False for host in hosts: if len(host.get('path')) == 0: return False if len(host.get('country')) == 0: return False if len(host.get('city')) == 0: return False return True
def run(order, job): sec = timedelta(seconds=1) start = datetime.utcnow().replace(microsecond=0) - sec try: # Import the hosts, while preserving the values in other columns. hosts = get_hosts_from_etree(order.xml) fields = ('address', 'name', 'protocol', 'tcp_port', 'path', 'country', 'city') seeddb.save_host(hosts, fields) # Mark all hosts that are no longer known as 'deleted'. seeddb.mark_old_hosts(start) except Exception: logger.error(traceback.format_exc()) raise
def enter(order): logdir = __exscriptd__.get_order_logdir(order) logger = __exscriptd__.get_logger(order, 'command.log') decor = log_to_file(logdir, delete=True) run_cb = decor(partial(run, logger)) for host in get_hosts_from_etree(order.xml): # Track the status of the update per-host. hostname = host.get_name() task = __exscriptd__.create_task(order, 'Update !%s' % hostname) task.set_logfile(logdir, hostname + '.log') # Since the order only contains a list of hostnames without any # other info (such as the address or path), we need to load the # additional attributes from the database. seedhost = grabber.get_seedhost_from_name(hostname) if not seedhost: logger.info('%s: Error: Address for host not found.' % hostname) task.close('address-not-found') continue # Enqueue the host. qtask = queue.run_or_ignore(seedhost, run_cb) if qtask is None: logger.info('%s: Already queued, so request ignored.' % hostname) task.close('duplicate') continue # Associate the queued job with the task such that Exscriptd can # update the status of the task. job_id = qtask.job_ids.pop() task.set_job_id(job_id) logger.info('%s: Queued with job id %s.' % (hostname, job_id)) # If the order contains a <flush/> tag, delete the data of all unknown # hosts. if order.xml.find('flush') is not None: task = __exscriptd__.create_task(order, 'Delete obsolete hosts') task.set_logfile(logdir, 'flush.log') qtask = queue.enqueue(partial(flush, logger), 'flush') task.set_job_id(qtask.job_ids.pop())
def enter(order): logdir = __exscriptd__.get_order_logdir(order) logger = __exscriptd__.get_logger(order, 'command.log') decor = log_to_file(logdir, delete = True) run_cb = decor(partial(run, logger)) for host in get_hosts_from_etree(order.xml): # Track the status of the update per-host. hostname = host.get_name() task = __exscriptd__.create_task(order, 'Update !%s' % hostname) task.set_logfile(logdir, hostname + '.log') # Since the order only contains a list of hostnames without any # other info (such as the address or path), we need to load the # additional attributes from the database. seedhost = grabber.get_seedhost_from_name(hostname) if not seedhost: logger.info('%s: Error: Address for host not found.' % hostname) task.close('address-not-found') continue # Enqueue the host. qtask = queue.run_or_ignore(seedhost, run_cb) if qtask is None: logger.info('%s: Already queued, so request ignored.' % hostname) task.close('duplicate') continue # Associate the queued job with the task such that Exscriptd can # update the status of the task. job_id = qtask.job_ids.pop() task.set_job_id(job_id) logger.info('%s: Queued with job id %s.' % (hostname, job_id)) # If the order contains a <flush/> tag, delete the data of all unknown # hosts. if order.xml.find('flush') is not None: task = __exscriptd__.create_task(order, 'Delete obsolete hosts') task.set_logfile(logdir, 'flush.log') qtask = queue.enqueue(partial(flush, logger), 'flush') task.set_job_id(qtask.job_ids.pop())
def run(order, job): sec = timedelta(seconds = 1) start = datetime.utcnow().replace(microsecond = 0) - sec try: # Import the hosts, while preserving the values in other columns. hosts = get_hosts_from_etree(order.xml) fields = ('address', 'name', 'protocol', 'tcp_port', 'path', 'country', 'city') seeddb.save_host(hosts, fields) # Mark all hosts that are no longer known as 'deleted'. seeddb.mark_old_hosts(start) except Exception: logger.error(traceback.format_exc()) raise
def check(order): hosts = get_hosts_from_etree(order.xml) if not hosts: descr = '' elif len(hosts) == 1: descr = 'Update !' + hosts[0].get_name() else: descr = 'Update %d hosts' % len(hosts) if order.xml.find('flush') is not None: if descr: descr += ' and delete deactivated ones' else: descr = 'Delete deactivated hosts' if not descr: descr = 'Order did not contain any hosts' order.set_description(descr) return False order.set_description(descr) return True
def _get_hosts_from_xml(xml): host_list_node = xml.find('host-list') if host_list_node is None: return None return get_hosts_from_etree(host_list_node)