@@ -0,0 +1,2021 @@
+'''%0AControl virtual machines via Salt%0A'''%0A%0A# Import Salt libs%0Aimport salt.client%0Aimport salt.output%0A%0A%0Adef _determine_hyper(data):%0A    '''%0A    Determine what the most resource free hypervisor is based on the given%0A    data%0A    '''%0A    # This is just checking for the hyper with the most free ram, this needs%0A    # to be much more complicated.%0A    hyper = ''%0A    bestmem = 0%0A    bestcpu = 0%0A    for hv_, comps in data.items():%0A        if comps.get('freemem', 0) %3E bestmem:%0A            bestmem = comps%5B'freemem'%5D%0A            hyper = hv_%0A    return hyper%0A%0Adef query(hyper=None, quiet=False):%0A    '''%0A    Query the virtual machines%0A    '''%0A    ret = %7B%7D%0A    client = salt.client.LocalClient(__opts__%5B'conf_file'%5D)%0A    for info in client.cmd_iter('virtual:physical', 'virt.full_info', expr_form='grain'):%0A        if not info:%0A            continue%0A        if not isinstance(info, dict):%0A            continue%0A        chunk = %7B%7D%0A        id_ = info.keys()%5B0%5D%0A        if hyper:%0A            if hyper != id_:%0A                continue%0A        if not isinstance(info%5Bid_%5D, dict):%0A            continue%0A        if not 'ret' in info%5Bid_%5D:%0A            continue%0A        chunk%5Bid_%5D = info%5Bid_%5D%5B'ret'%5D%0A        ret.update(chunk)%0A        if not quiet:%0A            salt.output.display_output(chunk, 'virt_query', __opts__)%0A%0A    return ret%0A%0A%0Adef next_hyper():%0A    '''%0A    Return the hypervisor to use for the next autodeployed vm%0A    '''%0A    hyper = _determine_hyper(query(quiet=True))%0A    print(hyper)%0A    return hyper%0A%0A%0Adef init(name, cpu, mem, image, hyper=None):%0A    '''%0A    Initialize a new vm%0A    '''%0A    data = query(hyper)%0A    if hyper:%0A        if not hyper in data:%0A            return%0A    else:%0A        hyper = _determine_hyper(data)%0A    %0A    client = salt.client.LocalClient(__opts__%5B'conf_file'%5D)%0A%0A    cmd_ret = client.cmd_iter(%0A            hyper,%0A            'virt.init',%0A            %5Bname, cpu, mem, image%5D,%0A            timeout=600)%0A%0A    ret = %7B%7D%0A%0A    for info in cmd_ret:%0A        print(info)%0A        ret.update(info)%0A%0A    return ret%0A%0A

Add the start of the virt runner