package cn.reghao.tnb.sb.kvm;

import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.libvirt.Connect;
import org.libvirt.Domain;
import org.libvirt.DomainInfo;
import org.libvirt.LibvirtException;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 虚拟主机
 *
 * @author reghao
 * @date 2020-10-28 18:48:52
 */
@Slf4j
@Service
public class VirtualHost {
    private final String uri;

    public VirtualHost() {
        this.uri = "qemu:///system";
    }

    public Domain getDomain(String name) throws LibvirtException {
        Connect conn = new Connect(uri, false);
        Domain domain = conn.domainLookupByName(name);
        return domain;
    }

    public void start(Domain domain) throws Exception {
        domain.create();
    }

    public void restart(Domain domain) throws Exception {
        domain.reboot(0);
    }

    public void stop(Domain domain) throws Exception {
        domain.shutdown();
    }

    public Domain createDomain() throws Exception {
        Connect conn = new Connect(uri, false);
        String xml = "/home/reghao/data/os/vhost.xml";
        Document doc = new SAXReader().read(new FileInputStream(xml));
        return conn.domainDefineXML(doc.asXML());
    }

    public List<String> listDomain() {
        List<String> list = new ArrayList<>();
        try {
            Connect conn = new Connect(uri, false);
            String[] inactiveDomains = conn.listDefinedDomains();
            for (String domainName : inactiveDomains) {
                //list.add(conn.domainLookupByName(domainName));
            }

            int[] activeDomains = conn.listDomains();
            for (int domainId : activeDomains) {
                Domain domain = conn.domainLookupByID(domainId);
                String name = domain.getName();
                boolean autoStart = domain.getAutostart();
                domain.getMaxVcpus();
                domain.getVcpusInfo();
                domain.getMaxMemory();
                domain.memoryStats(1);
                DomainInfo domainInfo = domain.getInfo();
                String state = domainInfo.state.name();
                list.add(domain.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    public static void main(String[] args) throws Exception {
        VirtualHost vhost = new VirtualHost();
        //Domain domain1 = vhost.createDomain();
        List<String> domainNames = vhost.listDomain();
        for (String name : domainNames) {
            Domain domain = vhost.getDomain(name);
            int active = domain.isActive();
            if (active == 0) {
                domain.create();
                log.info("start {}", name);
            } else {
                //DomainInfo domainInfo = domain.getInfo();
                domain.shutdown();
                log.info("shutdown {}", name);
            }
            //domain.undefine();
            //domain.free();

            /*String xmlDesc = domain.getXMLDesc(0);
            domain.detachDevice(xmlDesc);
            domain.destroy();*/
        }
    }
}
