#!/usr/bin/env python
USAGE = """
SYNOPSIS
    stop_clone_guests.py config.cfg
DESCRIPTION
    This script shuts down the clones created by id_clone_hosts.py, using the
    same .cfg file.

    Please refer to the id_clone_hosts.py help for more information."""
import sys
from common import parse_cfg, is_defined_guest, is_active_guest


if not len(sys.argv) == 2:
    print USAGE
    sys.exit(1)

conn, machines_to_clone, cfg, xmlfilepath = parse_cfg(sys.argv[1])

clone_names = [cfg.get(orig_name, "clone_name")
               for orig_name in machines_to_clone]

defined_clones_names = [clone_name
                        for clone_name in clone_names
                        if is_defined_guest(conn, clone_name)]

for clone_name in defined_clones_names:
    guest = conn.lookupByName(clone_name)
    if guest.isActive():
        guest.destroy()
import sys
from xml.etree import ElementTree

from common import parse_cfg, is_defined_guest, remove_all_disks, disk_path_from_guest_name


if __name__ == "__main__":
    if not len(sys.argv) == 2:
        print USAGE
        sys.exit(1)

    conn, machines_to_clone, cfg, xmlfilepath = parse_cfg(sys.argv[1])

    clone_names = [cfg.get(orig_name, "clone_name") for orig_name in machines_to_clone]

    # For some reason, some clones may not have been defined
    defined_clone_names = [clone_name for clone_name in clone_names if is_defined_guest(conn, clone_name)]

    # Processing data and removing
    disks_to_remove = [disk_path_from_guest_name(conn, clone_name) for clone_name in defined_clone_names]
    # The disks will be removed after we destroy the machines.

    for clone_name in defined_clone_names:
        clone_guest = conn.lookupByName(clone_name)

        if clone_guest.isActive():
            clone_guest.destroy()
        clone_guest.undefine()

    remove_all_disks(conn, disks_to_remove)
    #configfilepath = "config.cfg"
    conn, machines_to_clone, cfg, xmlfilepath = parse_cfg(configfilepath)

    server = cfg.get("virt_host", "server")
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_host_keys(expanduser(join("~", ".ssh", "known_hosts")))
    ssh.connect(server)

    # Undefine the masters
    disks_to_remove = {}
    defined_clones_names = []
    for master_name in machines_to_clone:
        clone_name = cfg.get(master_name, "clone_name")
        # Check that we got a replacement before removing
        if not is_defined_guest(conn, clone_name):
            continue

        defined_clones_names.append(clone_name)
        # Make sure master and clone are inactive
        clone_guest = conn.lookupByName(clone_name)
        master_guest = conn.lookupByName(master_name)
        if clone_guest.isActive():
            clone_guest.destroy()

        if master_guest.isActive():
            master_guest.destroy()

        # Copy clone disk onto master disk
        disk_to_replace = disk_path_from_guest_name(conn, master_name)
        disk_to_copy = disk_path_from_guest_name(conn, clone_name)
if __name__ == "__main__":
    if not len(sys.argv) == 2:
        print USAGE
        sys.exit(1)

    configfilepath = sys.argv[1]
    conn, machines_to_clone, cfg, xmlfilepath = parse_cfg(configfilepath)

    clone_names = dict([(orig, cfg.get(orig, "clone_name"))
                        for orig in machines_to_clone])

    # Ignore the undefined clone guests
    mac_addresses = [(orig, first_mac_address(conn, clone_names[orig]))
                     for orig in machines_to_clone
                     if is_defined_guest(conn, clone_names[orig])]

    tree = ElementTree.ElementTree()
    tree.parse(xmlfilepath)

    dhcp_section = tree.find("ip/dhcp")
    for orig_name, mac in mac_addresses:
        hostname = cfg.get(orig_name, "hostname")
        ip = cfg.get(orig_name, "ip")
        element = ElementTree.Element("host", {"mac": mac,
                                               "name": hostname,
                                               "ip": ip})
        dhcp_section.insert(0, element)

    netxml = ElementTree.tostring(tree.getroot())
    conn.networkCreateXML(netxml)