Connect to exists VM if already runned

This commit is contained in:
Mikhail Klementev 2018-07-15 19:32:28 +00:00
parent 5bb305a0ec
commit 36267ac5ea

View File

@ -199,24 +199,21 @@ func generateVM(name string) (realpath, reginfo, qcow2 string, err error) {
return return
} }
func start(l *libvirt.Libvirt, name string) { func isRunning(l *libvirt.Libvirt, name string) bool {
// Currently binary-only installation is not supported, because we need *.nix configurations _, err := l.DomainLookupByName("appvm_" + name) // yep, there is no libvirt error handling
gopath := os.Getenv("GOPATH") // VM is destroyed when stop so NO VM means STOPPED
appvmPath := gopath + "/src/github.com/jollheef/appvm" return err == nil
err := os.Chdir(appvmPath) }
if err != nil {
log.Fatal(err)
}
// Copy templates func generateAppVM(l *libvirt.Libvirt, appvmPath, name string) (err error) {
err = prepareTemplates(appvmPath) err = os.Chdir(appvmPath)
if err != nil { if err != nil {
log.Fatal(err) return
} }
realpath, reginfo, qcow2, err := generateVM(name) realpath, reginfo, qcow2, err := generateVM(name)
if err != nil { if err != nil {
log.Fatal(err) return
} }
sharedDir := fmt.Sprintf(os.Getenv("HOME") + "/appvm/" + name) sharedDir := fmt.Sprintf(os.Getenv("HOME") + "/appvm/" + name)
@ -224,10 +221,26 @@ func start(l *libvirt.Libvirt, name string) {
xml := generateXML(name, realpath, reginfo, qcow2, sharedDir) xml := generateXML(name, realpath, reginfo, qcow2, sharedDir)
_, err = l.DomainCreateXML(xml, libvirt.DomainStartValidate) _, err = l.DomainCreateXML(xml, libvirt.DomainStartValidate)
return
}
func start(l *libvirt.Libvirt, name string) {
// Currently binary-only installation is not supported, because we need *.nix configurations
appvmPath := os.Getenv("GOPATH") + "/src/github.com/jollheef/appvm"
// Copy templates
err := prepareTemplates(appvmPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if !isRunning(l, name) {
err = generateAppVM(l, appvmPath, name)
if err != nil {
log.Fatal(err)
}
}
cmd := exec.Command("virt-viewer", "appvm_"+name) cmd := exec.Command("virt-viewer", "appvm_"+name)
cmd.Start() cmd.Start()
} }