virtigo/importDomain.go

71 lines
1.8 KiB
Go
Raw Normal View History

package main
import (
"errors"
"fmt"
"net/http"
pb "go.wit.com/lib/protobuf/virtbuf"
"go.wit.com/log"
)
// attempts to create a new virtual machine
func importDomain(w http.ResponseWriter, r *http.Request) (string, error) {
name := r.URL.Query().Get("domainName")
if name == "" {
result := "start failed. name is blank " + r.URL.Path
log.Warn(result)
fmt.Fprintln(w, result)
return "", errors.New(result)
}
log.Warn("importDomain() START name is", name)
fmt.Fprintln(w, "importDomain() START name is", name)
d := me.cluster.FindDropletByName(name)
if d == nil {
result := "libvirt domain " + name + " could not be found on any hypervisor"
log.Info(result)
fmt.Fprintln(w, result)
return result, errors.New(result)
}
start := d.SprintHeader()
if d.LocalOnly == "" {
result := start + " LocalOnly is blank. THIS SHOULD NEVER HAPPEN."
log.Log(WARN, result)
fmt.Fprintln(w, result)
return result, errors.New(result)
}
result := start + " local FOUND! LocalOnly = " + d.LocalOnly
log.Log(WARN, result)
fmt.Fprintln(w, result)
if d.Current.State != pb.DropletState_OFF {
result := "error: libvirt domain " + name + " is not off"
log.Info(result)
fmt.Fprintln(w, result)
return result, errors.New(result)
}
result = start + "about to attempt import "
result += "(" + d.LocalOnly + ")"
result += " " + d.Hostname
log.Log(WARN, result)
fmt.Fprintln(w, result)
h := findHypervisorByName(d.Current.Hypervisor)
if h == nil {
result = "unknown hypervisor = " + d.Current.Hypervisor
log.Log(WARN, result)
fmt.Fprintln(w, result)
return result, errors.New(result)
}
result = "finally ready to h.start(d)"
log.Log(WARN, result)
fmt.Fprintln(w, result)
ok, output := h.start(d)
if ok {
return result + output, nil
}
return result + output, errors.New("start " + name + " on hypervisor " + h.pb.Hostname)
}