2024-10-31 15:43:25 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2024-10-31 22:14:11 -05:00
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
2024-10-31 15:43:25 -05:00
|
|
|
"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)
|
|
|
|
}
|
2024-11-01 02:00:46 -05:00
|
|
|
log.Warn("importDomain() START name is", name)
|
|
|
|
fmt.Fprintln(w, "importDomain() START name is", name)
|
2024-10-31 15:43:25 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2024-11-01 02:00:46 -05:00
|
|
|
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)
|
2024-10-31 22:14:11 -05:00
|
|
|
if d.Current.State != pb.DropletState_OFF {
|
2024-11-01 02:00:46 -05:00
|
|
|
result := "error: libvirt domain " + name + " is not off"
|
2024-10-31 22:14:11 -05:00
|
|
|
log.Info(result)
|
|
|
|
fmt.Fprintln(w, result)
|
2024-11-01 02:00:46 -05:00
|
|
|
return result, errors.New(result)
|
2024-10-31 22:14:11 -05:00
|
|
|
}
|
2024-11-01 02:00:46 -05:00
|
|
|
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
|
2024-10-31 22:14:11 -05:00
|
|
|
log.Log(WARN, result)
|
|
|
|
fmt.Fprintln(w, result)
|
2024-11-01 02:00:46 -05:00
|
|
|
return result, errors.New(result)
|
2024-10-31 22:14:11 -05:00
|
|
|
}
|
2024-11-01 02:00:46 -05:00
|
|
|
result = "finally ready to h.start(d)"
|
2024-10-31 22:14:11 -05:00
|
|
|
log.Log(WARN, result)
|
2024-10-31 15:43:25 -05:00
|
|
|
fmt.Fprintln(w, result)
|
2024-11-01 02:00:46 -05:00
|
|
|
|
|
|
|
ok, output := h.start(d)
|
|
|
|
if ok {
|
|
|
|
return result + output, nil
|
|
|
|
}
|
|
|
|
return result + output, errors.New("start " + name + " on hypervisor " + h.pb.Hostname)
|
2024-10-31 15:43:25 -05:00
|
|
|
}
|