From ba23154aefed45bb606259bd15d9fe598ff9d8a4 Mon Sep 17 00:00:00 2001 From: Bruno Goncalves Date: Mar 16 2018 13:17:59 +0000 Subject: fixed standard-inventory-vagrant code using inspekt --- diff --git a/inventory/standard-inventory-vagrant b/inventory/standard-inventory-vagrant index 4e63a63..ea97111 100755 --- a/inventory/standard-inventory-vagrant +++ b/inventory/standard-inventory-vagrant @@ -47,9 +47,9 @@ def main(argv): try: if opts.host: - data = host(opts.host) + data = inv_host(opts.host) else: - data = list(opts.subjects) + data = inv_list(opts.subjects) sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': '))) except RuntimeError as ex: sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex))) @@ -58,41 +58,44 @@ def main(argv): return 0 -def list(subjects): +def inv_list(subjects): hosts = [] variables = {} for subject in subjects: if subject.endswith(".box"): if not subject.endswith((".vagrant-libvirt.box", ".LibVirt.box")): - sys.stderr.write("WARNING: skipping {0}: only libvirt provider supported for vagrant boxes\n".format(subject)) + sys.stderr.write("WARNING: skipping {0}: ".format(subject) + + "only libvirt provider supported for vagrant boxes\n") continue hostname = os.path.basename(subject) - vars = host(subject) - if vars: + host_vars = inv_host(subject) + if host_vars: hosts.append(hostname) - variables[hostname] = vars - return { "localhost": { "hosts": hosts, "vars": {} }, "subjects": { "hosts": hosts, "vars": {} }, "_meta": { "hostvars": variables } } + variables[hostname] = host_vars + return {"localhost": {"hosts": hosts, "vars": {}}, + "subjects": {"hosts": hosts, "vars": {}}, + "_meta": {"hostvars": variables}} -def host(box): +def inv_host(box): # directory-separators in hostnames could be bad hostname = os.path.basename(box) null = open(os.devnull, 'w') try: lsmod = subprocess.check_output(["lsmod"], stderr=null) - except subprocess.CalledProcessError as ex: + except subprocess.CalledProcessError: raise RuntimeError("failed to run lsmod\n") if "kvm" not in lsmod.decode('utf-8'): raise RuntimeError("CPU must support KVM hardware virtualization to run vagrant\n") - needed_pkgs = [ "vagrant", "vagrant-libvirt" ] + needed_pkgs = ["vagrant", "vagrant-libvirt"] pkg_error = None try: subprocess.check_call(["rpm", "--quiet", "-q"] + needed_pkgs, stdout=sys.stderr.fileno()) - except subprocess.CalledProcessError as ex: + except subprocess.CalledProcessError: sys.stderr.write("INFO: installing packages needed to run vagrant: {0}\n".format(" ".join(needed_pkgs))) if os.path.isfile("/usr/bin/dnf"): pkgmgr = "/usr/bin/dnf" @@ -102,7 +105,7 @@ def host(box): try: subprocess.check_call([pkgmgr, "install", "-y", "-q"] + needed_pkgs, stdout=sys.stderr.fileno()) - except subprocess.CalledProcessError as ex: + except subprocess.CalledProcessError: pkg_error = "Unable to install packages needed to run vagrant: {0}\n".format(" ".join(needed_pkgs)) raise RuntimeError(pkg_error) @@ -144,7 +147,7 @@ def host(box): try: subprocess.check_call(["/usr/sbin/service", "libvirtd", "start"], stdout=sys.stderr.fileno()) - except subprocess.CalledProcessError as ex: + except subprocess.CalledProcessError: raise RuntimeError("Could not start libvirtd service") # set vagrant's working directory to our temporary directory @@ -165,7 +168,7 @@ def host(box): try: ssh_config = subprocess.check_output(["/usr/bin/vagrant", "ssh-config"], env=vagrant_env, stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as ex: + except subprocess.CalledProcessError: raise RuntimeError("failed to retrieve vagrant SSH configuration\n") debuglog.write("{0}".format(ssh_config).encode('utf-8')) @@ -177,10 +180,10 @@ def host(box): } for line in ssh_config.decode('utf-8').splitlines(): - l = line.split(None, 2) - if len(l) != 2: + ln = line.split(None, 2) + if len(ln) != 2: continue - key, val = l + key, val = ln if key == "HostName": variables["ansible_ssh_host"] = val @@ -190,12 +193,12 @@ def host(box): variables["ansible_ssh_private_key_file"] = val # verify we have all critical SSH configuration values - for key in [ "ansible_ssh_host", "ansible_ssh_port", "ansible_ssh_private_key_file" ]: - if not key in variables: + for key in ["ansible_ssh_host", "ansible_ssh_port", "ansible_ssh_private_key_file"]: + if key not in variables: raise RuntimeError("failed to retrieve vagrant SSH configuration value {0}\n".format(key)) # wait for ssh to come up - args = " ".join([ "{0}='{1}'".format(*item) for item in variables.items() ]) + args = " ".join(["{0}='{1}'".format(*item) for item in variables.items()]) inventory = os.path.join(directory, "inventory") with open(inventory, "w") as f: f.write("{0} {1}\n".format(hostname, args)) @@ -211,7 +214,7 @@ def host(box): "ping" ] - for tries in range(0, 10): + for _ in range(0, 10): try: subprocess.check_call(ping, stdout=null, stderr=null) break @@ -248,19 +251,23 @@ def host(box): try: os.kill(ppid, 0) except OSError: - break # the process no longer exists + break # the process no longer exists debuglog.write("### {0} no longer exists\n".format(ppid).encode('utf-8')) if diagnose: sys.stderr.write("\n") - sys.stderr.write("DIAGNOSE: ssh -p {0} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@{1} # password: {2}\n".format(variables["ansible_ssh_port"], variables["ansible_ssh_host"], ROOT_PASSWORD)) + sys.stderr.write("DIAGNOSE: ssh -p {0} -o StrictHostKeyChecking=no" + " -o UserKnownHostsFile=/dev/null" + "root@{1} # password: {2}\n".format(variables["ansible_ssh_port"], + variables["ansible_ssh_host"], + ROOT_PASSWORD)) sys.stderr.write("DIAGNOSE: kill {0} # when finished\n".format(os.getpid())) def _signal_handler(*args): sys.stderr.write("\nDIAGNOSE ending...\n") - debuglog.write("### DIAGNOSE: waiting for signal before cleaning up\n".format(ppid).encode('utf-8')) + debuglog.write("### DIAGNOSE: waiting for signal from {0} before cleaning up\n".format(ppid).encode('utf-8')) signal.signal(signal.SIGTERM, _signal_handler) signal.pause()