| |
@@ -13,7 +13,6 @@
|
| |
import sys
|
| |
import tempfile
|
| |
import time
|
| |
- import traceback
|
| |
import distutils.util
|
| |
|
| |
IDENTITY = """
|
| |
@@ -46,18 +45,30 @@
|
| |
-----END RSA PRIVATE KEY-----
|
| |
"""
|
| |
|
| |
+
|
| |
+ AUTH_KEY = ("AAAAB3NzaC1yc2EAAAADAQABAAABAQDUOtNJdBEXyKxBB898rdT54ULjMGuO6v4jLX"
|
| |
+ "mRsdRhR5Id/lKNc9hsdioPWUePgYlqML2iSV72vKQoVhkyYkpcsjr3zvBny9+5xej3"
|
| |
+ "+TBLoEMAm2hmllKPmxYJDU8jQJ7wJuRrOVOnk0iSNF+FcY/yaQ0owSF02Nphx47j2K"
|
| |
+ "Wc0IjGGlt4fl0fmHJuZBA2afN/4IYIIsEWZziDewVtaEjWV3InMRLllfdqGMllhFR+"
|
| |
+ "ed2hQz9PN2QcapmEvUR4UCy/mJXrke5htyFyHi8ECfyMMyYeHwbWLFQIve4CWix9qt"
|
| |
+ "ksvKjcetnxT+WWrutdr3c9cfIj/c0v/Zg/c4zETxtp")
|
| |
+
|
| |
+ DEF_USER = "root"
|
| |
+ DEF_PASSWD = "foobar"
|
| |
+
|
| |
USER_DATA = """#cloud-config
|
| |
users:
|
| |
- default
|
| |
- - name: root
|
| |
+ - name: {0}
|
| |
ssh_authorized_keys:
|
| |
- - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUOtNJdBEXyKxBB898rdT54ULjMGuO6v4jLXmRsdRhR5Id/lKNc9hsdioPWUePgYlqML2iSV72vKQoVhkyYkpcsjr3zvBny9+5xej3+TBLoEMAm2hmllKPmxYJDU8jQJ7wJuRrOVOnk0iSNF+FcY/yaQ0owSF02Nphx47j2KWc0IjGGlt4fl0fmHJuZBA2afN/4IYIIsEWZziDewVtaEjWV3InMRLllfdqGMllhFR+ed2hQz9PN2QcapmEvUR4UCy/mJXrke5htyFyHi8ECfyMMyYeHwbWLFQIve4CWix9qtksvKjcetnxT+WWrutdr3c9cfIj/c0v/Zg/c4zETxtp standard-test-qcow2
|
| |
+ - ssh-rsa {2} standard-test-qcow2
|
| |
ssh_pwauth: True
|
| |
chpasswd:
|
| |
list: |
|
| |
- root:foobar
|
| |
+ {0}:{1}
|
| |
expire: False
|
| |
- """
|
| |
+ """.format(DEF_USER, DEF_PASSWD, AUTH_KEY)
|
| |
+
|
| |
|
| |
def main(argv):
|
| |
parser = argparse.ArgumentParser(description="Inventory for a QCow2 test image")
|
| |
@@ -68,9 +79,9 @@
|
| |
|
| |
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)))
|
| |
@@ -78,16 +89,19 @@
|
| |
|
| |
return 0
|
| |
|
| |
- def list(subjects):
|
| |
- hosts = [ ]
|
| |
- variables = { }
|
| |
+
|
| |
+ def inv_list(subjects):
|
| |
+ hosts = []
|
| |
+ variables = {}
|
| |
for subject in subjects:
|
| |
if subject.endswith((".qcow2", ".qcow2c")):
|
| |
- vars = host(subject)
|
| |
- if vars:
|
| |
+ host_vars = inv_host(subject)
|
| |
+ if host_vars:
|
| |
hosts.append(subject)
|
| |
- variables[subject] = vars
|
| |
- return { "localhost": { "hosts": hosts, "vars": { } }, "subjects": { "hosts": hosts, "vars": { } }, "_meta": { "hostvars": variables } }
|
| |
+ variables[subject] = host_vars
|
| |
+ return {"localhost": {"hosts": hosts, "vars": {}},
|
| |
+ "subjects": {"hosts": hosts, "vars": {}},
|
| |
+ "_meta": {"hostvars": variables}}
|
| |
|
| |
|
| |
def start_qemu(image, cloudinit, log, portrange=(2222, 5555)):
|
| |
@@ -108,16 +122,15 @@
|
| |
# Use -cpu host and -smp by default.
|
| |
# virtio-rng-pci: https://wiki.qemu.org/Features/VirtIORNG
|
| |
return subprocess.Popen(["/usr/bin/qemu-system-x86_64",
|
| |
- "-cpu", "host", "-smp", "{}".format(multiprocessing.cpu_count()),
|
| |
- "-m", "1024", image,
|
| |
- "-enable-kvm", "-snapshot", "-cdrom", cloudinit,
|
| |
- "-net", "nic,model=virtio", "-net", "user,hostfwd=tcp:127.0.0.3:{0}-:22".format(port),
|
| |
- "-device", "virtio-rng-pci", "-rtc", "base=utc",
|
| |
- "-device", "isa-serial,chardev=pts2", "-chardev", "file,id=pts2,path=" + log,
|
| |
- "-display", "none"], stdout=open(log, 'a'), stderr=subprocess.STDOUT), port
|
| |
+ "-cpu", "host", "-smp", "{}".format(multiprocessing.cpu_count()),
|
| |
+ "-m", "1024", image, "-enable-kvm", "-snapshot", "-cdrom", cloudinit,
|
| |
+ "-net", "nic,model=virtio", "-net", "user,hostfwd=tcp:127.0.0.3:{0}-:22".format(port),
|
| |
+ "-device", "virtio-rng-pci", "-rtc", "base=utc",
|
| |
+ "-device", "isa-serial,chardev=pts2", "-chardev", "file,id=pts2,path=" + log,
|
| |
+ "-display", "none"], stdout=open(log, 'a'), stderr=subprocess.STDOUT), port
|
| |
|
| |
|
| |
- def host(image):
|
| |
+ def inv_host(image):
|
| |
null = open(os.devnull, 'w')
|
| |
|
| |
try:
|
| |
@@ -164,7 +177,7 @@
|
| |
|
| |
proc = None # for failure detection
|
| |
cpe = None # for exception scoping
|
| |
- for tries in xrange(0, 5):
|
| |
+ for _ in xrange(0, 5):
|
| |
try:
|
| |
proc, port = start_qemu(image, cloudinit, log)
|
| |
break
|
| |
@@ -175,7 +188,7 @@
|
| |
raise RuntimeError("Could not launch VM for qcow2 image"
|
| |
" '{0}':{1}".format(image, cpe.output))
|
| |
|
| |
- for tries in xrange(0, 30):
|
| |
+ for _ in xrange(0, 30):
|
| |
try:
|
| |
# The variables
|
| |
variables = {
|
| |
@@ -188,10 +201,10 @@
|
| |
}
|
| |
|
| |
# Write out a handy inventory file, for our use and for debugging
|
| |
- 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("[subjects]\nlocalhost {1}\n".format(image, args))
|
| |
+ f.write("[subjects]\nlocalhost {0}\n".format(args))
|
| |
|
| |
# Wait for ssh to come up
|
| |
ping = [
|
| |
@@ -205,7 +218,7 @@
|
| |
"/bin/true"
|
| |
]
|
| |
|
| |
- (pid, ret) = os.waitpid(proc.pid, os.WNOHANG)
|
| |
+ (pid, _) = os.waitpid(proc.pid, os.WNOHANG)
|
| |
if pid != 0:
|
| |
raise RuntimeError("qemu failed to launch qcow2 image: {0}".format(image))
|
| |
subprocess.check_call(ping, stdout=null, stderr=null)
|
| |
@@ -256,11 +269,12 @@
|
| |
os.kill(proc.pid, 0)
|
| |
except OSError:
|
| |
sys.stderr.write("Either parent process or VM process is gone.")
|
| |
- break # Either of the processes no longer exist
|
| |
+ break # Either of the processes no longer exist
|
| |
|
| |
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(port, "127.0.0.3", "foobar"))
|
| |
+ sys.stderr.write("DIAGNOSE: ssh -p {0} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "
|
| |
+ "root@{1} # password: {2}\n".format(port, "127.0.0.3", "foobar"))
|
| |
sys.stderr.write("DIAGNOSE: export ANSIBLE_INVENTORY={0}\n".format(inventory))
|
| |
sys.stderr.write("DIAGNOSE: kill {0} # when finished\n".format(os.getpid()))
|
| |
|
| |
@@ -279,5 +293,6 @@
|
| |
shutil.rmtree(directory)
|
| |
sys.exit(0)
|
| |
|
| |
+
|
| |
if __name__ == '__main__':
|
| |
sys.exit(main(sys.argv))
|
| |
results: