def virtual_ip_address(self, ips): ips = util.flatten(ips) ips = (sorted(set([ip for ip in ips if isinstance(ip, ipaddr.IPv4Address)])) + sorted(set([ip for ip in ips if isinstance(ip, ipaddr.IPv6Address)]))) if len(ips) > 0: self._virtual_ipaddress = ips[0] else: logger.error("No VRRP virtual_ipaddress available at all!") self._virtual_ipaddress = None self._virtual_ipaddress_excluded = ips[1:]
def _figure_out_modules(modules_to_load, modules_to_run): if modules_to_load is None or len(modules_to_load) == 0: modules_to_load = known_modules else: modules_to_load = util.flatten(modules_to_load) filtered_to_load = [] for module_name in modules_to_load: if module_name not in known_modules: raise Exception("Unknown module %s!" % module_name) else: filtered_to_load.append(module_name) if modules_to_run is None or len(modules_to_run) == 0: modules_to_run = filtered_to_load else: modules_to_run = util.flatten(modules_to_run) filtered_to_run = [] for module_name in modules_to_run: if module_name not in known_modules: logger.error("Ignoring unknown module %s!" % module_name) elif module_name not in modules_to_load: logger.error("Skipping module run for %s, it's not being loaded!" % module_name) else: filtered_to_run.append(module_name) return filtered_to_load, filtered_to_run
def write_file(outdir, filename, lines, dry_run, diff=False, remove_empty=False): did_write_changes = False target_filename = os.path.join(outdir, filename) if len(lines) > 0: lines.insert(0 if not lines[0].startswith('#!') else 1, "# Managed by CARETAKR") new_content = "%s\n" % '\n'.join(lines) try: with open(target_filename) as f: current_content = f.read() except: current_content = "\n" if diff is True: if new_content != current_content: diff_lines = difflib.unified_diff(current_content.splitlines(), lines, fromfile='a/%s' % filename, tofile='b/%s' % filename, lineterm='') if sys.stdout.isatty(): print('\n'.join(map(colorize, diff_lines))) else: map(logger.info, diff_lines) if dry_run is True: if os.path.isfile(target_filename) and len(lines) == 0 and remove_empty is True: logger.info("Would attempt to remove %s, because there is no new content" % target_filename) elif ((os.path.isfile(target_filename) and os.access(target_filename, os.W_OK) is True) or (os.path.exists(target_filename) is False and os.access(os.path.dirname(target_filename), os.W_OK) is True)): if new_content != current_content: if ((os.path.isfile(target_filename) and os.stat(target_filename).st_uid != os.geteuid())): logger.error("Would refuse to overwrite %s, as it's not owned by us!" % target_filename) else: logger.info("Would write %d lines to %s" % (len(lines), target_filename)) did_write_changes = True elif not (os.path.isfile(target_filename) is False and remove_empty is True): logger.info("No changes for %s" % target_filename) else: logger.error("Would not be able to write file at %s" % target_filename) else: if len(lines) == 0 and remove_empty is True: if ((os.path.isfile(target_filename) and os.stat(target_filename).st_uid != os.geteuid())): logger.error("Refusing to remove %s, as it's not owned by us!" % target_filename) else: if os.path.isfile(target_filename): logger.info("Removing %s" % target_filename) os.remove(target_filename) did_write_changes = True else: if new_content != current_content: if ((os.path.isfile(target_filename) and os.stat(target_filename).st_uid != os.geteuid())): logger.error("Refusing to overwrite %s, as it's not owned by us!" % target_filename) else: logger.info("Writing %s (%d lines)" % (target_filename, len(lines))) try: os.makedirs(os.path.dirname(target_filename)) except: pass with open(target_filename, 'w') as f: f.write(new_content) did_write_changes = True else: logger.info("No changes for %s" % target_filename) return did_write_changes