| |
@@ -37,12 +37,14 @@
|
| |
"""
|
| |
|
| |
|
| |
- def main(argv):
|
| |
- parser = argparse.ArgumentParser(description="Inventory for a Vagrant test box")
|
| |
+ def main():
|
| |
+ parser = argparse.ArgumentParser(
|
| |
+ description="Inventory for a Vagrant test box")
|
| |
parser.add_argument("--list", action="store_true", help="Verbose output")
|
| |
parser.add_argument('--host', help="Get host variables")
|
| |
parser.add_argument("subjects", nargs="*",
|
| |
- default=shlex.split(os.environ.get("TEST_SUBJECTS", "")))
|
| |
+ default=shlex.split(
|
| |
+ os.environ.get("TEST_SUBJECTS", "")))
|
| |
opts = parser.parse_args()
|
| |
|
| |
try:
|
| |
@@ -52,9 +54,9 @@
|
| |
data = 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)))
|
| |
+ sys.stderr.write("{}: {}\n".format(os.path.basename(sys.argv[0]),
|
| |
+ str(ex)))
|
| |
return 1
|
| |
-
|
| |
return 0
|
| |
|
| |
|
| |
@@ -64,14 +66,17 @@
|
| |
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 {}: only libvirt provider "
|
| |
+ "supported for vagrant boxes\n".format(subject))
|
| |
continue
|
| |
hostname = os.path.basename(subject)
|
| |
vars = host(subject)
|
| |
if vars:
|
| |
hosts.append(hostname)
|
| |
variables[hostname] = vars
|
| |
- return { "localhost": { "hosts": hosts, "vars": {} }, "subjects": { "hosts": hosts, "vars": {} }, "_meta": { "hostvars": variables } }
|
| |
+ return {"localhost": {"hosts": hosts, "vars": {}}, "subjects":
|
| |
+ {"hosts": hosts, "vars": {}}, "_meta": {"hostvars": variables}}
|
| |
|
| |
|
| |
def host(box):
|
| |
@@ -81,19 +86,22 @@
|
| |
|
| |
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")
|
| |
+ 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:
|
| |
- sys.stderr.write("INFO: installing packages needed to run vagrant: {0}\n".format(" ".join(needed_pkgs)))
|
| |
+ except subprocess.CalledProcessError:
|
| |
+ sys.stderr.write(
|
| |
+ "INFO: installing packages needed to run vagrant: "
|
| |
+ "{}\n".format(" ".join(needed_pkgs)))
|
| |
if os.path.isfile("/usr/bin/dnf"):
|
| |
pkgmgr = "/usr/bin/dnf"
|
| |
else:
|
| |
@@ -102,11 +110,13 @@
|
| |
try:
|
| |
subprocess.check_call([pkgmgr, "install", "-y", "-q"] + needed_pkgs,
|
| |
stdout=sys.stderr.fileno())
|
| |
- except subprocess.CalledProcessError as ex:
|
| |
- pkg_error = "Unable to install packages needed to run vagrant: {0}\n".format(" ".join(needed_pkgs))
|
| |
+ except subprocess.CalledProcessError:
|
| |
+ pkg_error = "Unable to install packages needed to run vagrant: " \
|
| |
+ "{0}\n".format(" ".join(needed_pkgs))
|
| |
raise RuntimeError(pkg_error)
|
| |
|
| |
- artifacts = os.environ.get("TEST_ARTIFACTS", os.path.join(os.getcwd(), "artifacts"))
|
| |
+ artifacts = os.environ.get("TEST_ARTIFACTS", os.path.join(os.getcwd(),
|
| |
+ "artifacts"))
|
| |
try:
|
| |
os.makedirs(artifacts)
|
| |
except OSError as exc:
|
| |
@@ -114,7 +124,8 @@
|
| |
raise
|
| |
|
| |
# Open debug log. Must be binary mode to be unbuffered.
|
| |
- debuglog = open(os.path.join(artifacts, "inventory-vagrant.log"), "wb", buffering=0)
|
| |
+ debuglog = open(os.path.join(artifacts, "inventory-vagrant.log"), "wb",
|
| |
+ buffering=0)
|
| |
|
| |
try:
|
| |
tty = os.open("/dev/tty", os.O_WRONLY)
|
| |
@@ -132,7 +143,8 @@
|
| |
with open(vagrantfile, 'w') as f:
|
| |
f.write(VAGRANTFILE.format(box, boxname, ROOT_PASSWORD))
|
| |
|
| |
- # Determine if vagrant box should be kept available for diagnosis after completion
|
| |
+ # Determine if vagrant box should be kept available for diagnosis after
|
| |
+ # completion
|
| |
try:
|
| |
diagnose = distutils.util.strtobool(os.getenv("TEST_DEBUG", "0"))
|
| |
except ValueError:
|
| |
@@ -144,7 +156,7 @@
|
| |
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
|
| |
@@ -155,17 +167,20 @@
|
| |
# And launch the actual box
|
| |
debuglog.write("### vagrant up\n".encode('utf-8'))
|
| |
proc = subprocess.Popen(["/usr/bin/vagrant", "up"],
|
| |
- env=vagrant_env, stdout=debuglog, stderr=subprocess.STDOUT)
|
| |
+ env=vagrant_env, stdout=debuglog,
|
| |
+ stderr=subprocess.STDOUT)
|
| |
rc = proc.wait()
|
| |
|
| |
if rc != 0:
|
| |
- raise RuntimeError("vagrant failed to start with exit code {0}\n".format(rc))
|
| |
+ raise RuntimeError(
|
| |
+ "vagrant failed to start with exit code {}\n".format(rc))
|
| |
|
| |
debuglog.write("### vagrant ssh-config\n".encode('utf-8'))
|
| |
try:
|
| |
ssh_config = subprocess.check_output(["/usr/bin/vagrant", "ssh-config"],
|
| |
- env=vagrant_env, stderr=subprocess.STDOUT)
|
| |
- except subprocess.CalledProcessError as ex:
|
| |
+ env=vagrant_env,
|
| |
+ stderr=subprocess.STDOUT)
|
| |
+ except subprocess.CalledProcessError:
|
| |
raise RuntimeError("failed to retrieve vagrant SSH configuration\n")
|
| |
debuglog.write("{0}".format(ssh_config).encode('utf-8'))
|
| |
|
| |
@@ -173,14 +188,15 @@
|
| |
variables = {
|
| |
"ansible_ssh_user": "root",
|
| |
"ansible_ssh_pass": ROOT_PASSWORD,
|
| |
- "ansible_ssh_common_args": "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
|
| |
+ "ansible_ssh_common_args":
|
| |
+ "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
|
| |
}
|
| |
|
| |
for line in ssh_config.decode('utf-8').splitlines():
|
| |
- l = line.split(None, 2)
|
| |
- if len(l) != 2:
|
| |
+ var_line = line.split(None, 2)
|
| |
+ if len(var_line) != 2:
|
| |
continue
|
| |
- key, val = l
|
| |
+ key, val = var_line
|
| |
|
| |
if key == "HostName":
|
| |
variables["ansible_ssh_host"] = val
|
| |
@@ -190,26 +206,28 @@
|
| |
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:
|
| |
- raise RuntimeError("failed to retrieve vagrant SSH configuration value {0}\n".format(key))
|
| |
+ 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 "
|
| |
+ "{}\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))
|
| |
|
| |
- debuglog.write("### inventory: {0} {1}\n".format(hostname, args).encode('utf-8'))
|
| |
+ debuglog.write("### inventory: {} {}\n".format(
|
| |
+ hostname, args).encode('utf-8'))
|
| |
|
| |
- ping = [
|
| |
- "/usr/bin/ansible",
|
| |
- "--inventory",
|
| |
- inventory,
|
| |
- hostname,
|
| |
- "--module-name",
|
| |
- "ping"
|
| |
- ]
|
| |
+ ping = ["/usr/bin/ansible",
|
| |
+ "--inventory",
|
| |
+ inventory,
|
| |
+ hostname,
|
| |
+ "--module-name",
|
| |
+ "ping"]
|
| |
|
| |
for tries in range(0, 10):
|
| |
try:
|
| |
@@ -218,7 +236,8 @@
|
| |
except subprocess.CalledProcessError:
|
| |
time.sleep(3)
|
| |
else:
|
| |
- raise RuntimeError("could not access launched vagrant box: {0}".format(box))
|
| |
+ raise RuntimeError(
|
| |
+ "could not access launched vagrant box: {}".format(box))
|
| |
|
| |
# Process of our parent
|
| |
ppid = os.getppid()
|
| |
@@ -241,26 +260,34 @@
|
| |
os.dup2(tty, 2)
|
| |
|
| |
# Now wait for the parent process to go away, then destroy the box
|
| |
- debuglog.write("### waiting for ppid {0} to go away\n".format(ppid).encode('utf-8'))
|
| |
+ debuglog.write(
|
| |
+ "### waiting for ppid {} to go away\n".format(ppid).encode('utf-8'))
|
| |
while True:
|
| |
time.sleep(3)
|
| |
|
| |
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'))
|
| |
+ debuglog.write("### {} 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: kill {0} # when finished\n".format(os.getpid()))
|
| |
-
|
| |
- def _signal_handler(*args):
|
| |
+ sys.stderr.write(
|
| |
+ "DIAGNOSE: ssh -p {} -o StrictHostKeyChecking=no -o "
|
| |
+ "UserKnownHostsFile=/dev/null root@{} # password: {}\n".format(
|
| |
+ variables["ansible_ssh_port"], variables["ansible_ssh_host"],
|
| |
+ ROOT_PASSWORD))
|
| |
+ sys.stderr.write(
|
| |
+ "DIAGNOSE: kill {} # when finished\n".format(os.getpid()))
|
| |
+
|
| |
+ def _signal_handler():
|
| |
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 before cleaning up\n".format(
|
| |
+ ppid).encode('utf-8'))
|
| |
|
| |
signal.signal(signal.SIGTERM, _signal_handler)
|
| |
signal.pause()
|
| |
@@ -275,4 +302,4 @@
|
| |
|
| |
|
| |
if __name__ == '__main__':
|
| |
- sys.exit(main(sys.argv))
|
| |
+ sys.exit(main())
|
| |
Remove unused import.
Groome code to pep8 style.