sidn-lxd-ansible-demo/lib/dynamic_inventory/lxd_status.py

38 lines
975 B
Python

import json
import subprocess
def _lxd_list_all(hosts):
return map(_lxd_list, hosts)
def _lxd_list(host):
output = subprocess.check_output([
"ssh", host, "--",
"lxc", "list", "--fast", "--format", "json"])
def add_host(g):
g["host"] = host
return g
return map(add_host, json.loads(output))
def _create_container_info(g):
data = {
"name": g["name"],
"status": g["status"],
"host": g["host"],
}
if 'homedir' in g["expanded_devices"]:
data["homedir"] = g["expanded_devices"]['homedir']
return data
def list_lxd_containers(hosts):
"""create a list of all the LXD containers that are running on the LXD hosts."""
containers_per_host = _lxd_list_all(hosts)
all_containers = {}
for containers in containers_per_host:
for container in containers:
all_containers[container["name"]] = _create_container_info(container)
return all_containers