2019-05-12 00:48:59 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "flag"
|
|
|
|
import "log"
|
2019-06-04 18:15:55 -05:00
|
|
|
import "fmt"
|
|
|
|
import "regexp"
|
2019-05-12 00:48:59 -05:00
|
|
|
import "net/http"
|
2019-05-23 22:46:02 -05:00
|
|
|
import "encoding/json"
|
|
|
|
import "io/ioutil"
|
2019-05-12 00:48:59 -05:00
|
|
|
|
|
|
|
import pb "git.wit.com/wit/witProtobuf"
|
2019-05-23 22:46:02 -05:00
|
|
|
import "github.com/davecgh/go-spew/spew"
|
2019-05-24 01:43:42 -05:00
|
|
|
import "github.com/tidwall/gjson"
|
2019-05-23 22:46:02 -05:00
|
|
|
|
|
|
|
type network struct {
|
|
|
|
Subdomain string `json:"subdomain"`
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:29:55 -05:00
|
|
|
func fetchNetworks(ge *pb.Event) []network {
|
2019-05-23 22:46:02 -05:00
|
|
|
var networks []network
|
2019-05-27 23:29:55 -05:00
|
|
|
req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
2019-05-23 22:46:02 -05:00
|
|
|
if err != nil { log.Fatal("can't make new req") }
|
2019-05-27 23:29:55 -05:00
|
|
|
req.Header.Set("X-Wit-Auth", ge.Account.Token)
|
2019-05-23 22:46:02 -05:00
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil { log.Printf("Can't exec the req to list networks: %s", err) }
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil { log.Fatal("can't read resp") }
|
2019-05-28 20:14:07 -05:00
|
|
|
log.Printf("NETWORKS =", string(body))
|
2019-05-23 22:46:02 -05:00
|
|
|
if body == nil {
|
|
|
|
log.Println("networks is nil")
|
|
|
|
} else {
|
|
|
|
len := len(body)
|
|
|
|
err = json.Unmarshal(body, &networks)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("body = ", body)
|
|
|
|
log.Println("body len = ", len)
|
|
|
|
log.Println("can't unmarshal resp for list networks: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spew.Dump(networks)
|
|
|
|
return networks
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
"name": "vm10",
|
|
|
|
"cpu": 4,
|
|
|
|
"ram": 2048,
|
|
|
|
"created_on": "2019-03-18T19:22:10.755428Z",
|
|
|
|
"scope": "private",
|
|
|
|
"role": "k8s-node",
|
|
|
|
"stage": "Creating...",
|
|
|
|
"seconds_left": 300,
|
|
|
|
"base_image": "debcore_guest_latest",
|
|
|
|
"address_list": [
|
|
|
|
"192.168.0.1/32"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
type address struct {
|
|
|
|
Address string
|
|
|
|
}
|
|
|
|
|
|
|
|
type vm struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Role string `json:"role"`
|
|
|
|
BaseImage string `json:"base_image"`
|
|
|
|
}
|
|
|
|
|
2019-05-27 23:29:55 -05:00
|
|
|
func fetchVMs(ge *pb.Event, network string) []vm {
|
2019-05-23 22:46:02 -05:00
|
|
|
var vms []vm
|
2019-05-27 23:29:55 -05:00
|
|
|
req, err := http.NewRequest("GET", ge.Account.URL + "vms/" + network, nil)
|
2019-05-28 20:14:07 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("fetchVMs() can't make new req")
|
|
|
|
return vms
|
|
|
|
}
|
2019-05-27 23:29:55 -05:00
|
|
|
req.Header.Set("X-Wit-Auth", ge.Account.Token)
|
2019-05-23 22:46:02 -05:00
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
2019-05-28 20:14:07 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("fetchVMs() Can't exec the req to fetch vms for %s: %s", network, err)
|
|
|
|
return vms
|
|
|
|
}
|
2019-05-23 22:46:02 -05:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2019-05-28 20:14:07 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("fetchVMs() can't read resp")
|
|
|
|
return vms
|
|
|
|
}
|
2019-05-23 22:46:02 -05:00
|
|
|
err = json.Unmarshal(body, &vms)
|
2019-05-28 20:14:07 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("fetchVMs() can't unmarshal resp for fetch vms: ", err, "\n\n", network, string(body))
|
|
|
|
return vms
|
|
|
|
}
|
2019-05-23 22:46:02 -05:00
|
|
|
spew.Dump(vms)
|
|
|
|
return vms
|
|
|
|
}
|
|
|
|
|
2019-05-12 00:48:59 -05:00
|
|
|
var addr = flag.String("addr", ":9000", "http service address")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
http.HandleFunc("/", serveHome)
|
2019-05-12 00:57:19 -05:00
|
|
|
http.HandleFunc("/event", eventHandler)
|
2019-05-12 00:48:59 -05:00
|
|
|
|
|
|
|
log.Println("Starting http.ListenAndServe() on port 9000")
|
|
|
|
err := http.ListenAndServe(*addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 17:36:56 -05:00
|
|
|
|
2019-05-27 23:29:55 -05:00
|
|
|
// Gets a list of all the customer VM's
|
2019-05-24 17:36:56 -05:00
|
|
|
func processGetEvent(ge *pb.Event) *pb.Event {
|
|
|
|
log.Println("processGetEvent() START")
|
2019-05-26 14:35:13 -05:00
|
|
|
if (checkLogin(ge) == false) {
|
2019-05-26 12:53:02 -05:00
|
|
|
e := pb.MakeFailResponse()
|
2019-05-27 23:29:55 -05:00
|
|
|
e.Comment = "processGetEvent() LOGIN FAILED on checkLogin(ge)"
|
2019-05-26 15:13:50 -05:00
|
|
|
log.Println(e.Comment)
|
2019-05-26 12:53:02 -05:00
|
|
|
return e
|
|
|
|
}
|
2019-05-26 14:35:13 -05:00
|
|
|
log.Println("processGetEvent() ge.Account.Token =", ge.Account.Token)
|
2019-05-26 03:04:12 -05:00
|
|
|
log.Println("processGetEvent() ge.Account.Username =", ge.Account.Username)
|
2019-05-26 14:35:13 -05:00
|
|
|
log.Println("processGetEvent() ge.Account.Email =", ge.Account.Email)
|
2019-05-24 17:36:56 -05:00
|
|
|
|
|
|
|
e := pb.Event{}
|
|
|
|
|
2019-05-27 23:29:55 -05:00
|
|
|
networks := fetchNetworks(ge)
|
|
|
|
for _, network := range(networks) {
|
2019-05-24 17:36:56 -05:00
|
|
|
log.Println("network.Subdomain =",network.Subdomain)
|
|
|
|
log.Println("network=",network)
|
|
|
|
enetwork := pb.Event_Network{}
|
|
|
|
enetwork.Name = network.Subdomain
|
|
|
|
|
|
|
|
e.Networks = append(e.Networks, &enetwork)
|
|
|
|
|
2019-05-27 23:29:55 -05:00
|
|
|
vms := fetchVMs(ge, network.Subdomain)
|
|
|
|
for _, vm := range(vms) {
|
2019-05-24 17:36:56 -05:00
|
|
|
evm := pb.Event_VM{}
|
|
|
|
evm.Name = vm.Name
|
|
|
|
evm.Cpus = 1
|
|
|
|
evm.Memory = 512
|
|
|
|
evm.Disk = 20
|
2019-06-04 18:15:55 -05:00
|
|
|
// TODO: bmath -- let's change the stack-api to remove this hack
|
|
|
|
evm.Hostname = vm.Name + "." + enetwork.Name + ".customers.wprod.wit.com"
|
2019-05-24 17:36:56 -05:00
|
|
|
e.Vms = append(e.Vms, &evm)
|
|
|
|
|
|
|
|
log.Println("\tvm.Name =",vm.Name)
|
|
|
|
log.Println("\tvm =",vm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &e
|
|
|
|
}
|
|
|
|
|
2019-05-28 15:28:25 -05:00
|
|
|
/*
|
2019-05-27 23:29:55 -05:00
|
|
|
// should check a user can login here
|
2019-05-24 17:36:56 -05:00
|
|
|
func processLoginEvent(ge *pb.Event) *pb.Event {
|
2019-05-26 14:35:13 -05:00
|
|
|
log.Println("processLoginEvent() LOGIN START")
|
|
|
|
if (checkLogin(ge) == false) {
|
2019-05-24 17:36:56 -05:00
|
|
|
e := pb.MakeFailResponse()
|
|
|
|
e.Comment = "LOGIN FAILED"
|
2019-05-26 15:13:50 -05:00
|
|
|
log.Println(e.Comment)
|
2019-05-24 17:36:56 -05:00
|
|
|
return e
|
|
|
|
}
|
2019-05-26 03:04:12 -05:00
|
|
|
|
2019-05-26 02:27:51 -05:00
|
|
|
e := pb.MakeOkResponse()
|
2019-05-26 03:04:12 -05:00
|
|
|
e.Account = ge.Account
|
2019-05-26 15:13:50 -05:00
|
|
|
e.Comment = "processLoginEvent() LOGIN WORKED"
|
|
|
|
log.Println(e.Comment)
|
2019-05-24 17:36:56 -05:00
|
|
|
return e
|
|
|
|
}
|
2019-05-28 15:28:25 -05:00
|
|
|
*/
|
2019-05-24 17:36:56 -05:00
|
|
|
|
2019-05-28 01:18:44 -05:00
|
|
|
func JSONtoVM(ge *pb.Event, json string) {
|
|
|
|
tmp := "network_addr"
|
|
|
|
value := gjson.Get(json, tmp)
|
|
|
|
println(tmp, "=", value.String())
|
|
|
|
|
|
|
|
tmp = "public_mac"
|
|
|
|
value = gjson.Get(json, tmp)
|
|
|
|
println(tmp, "=", value.String())
|
|
|
|
|
|
|
|
tmp = "public_addr"
|
|
|
|
value = gjson.Get(json, tmp)
|
|
|
|
println(tmp, "=", value.String())
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:36:56 -05:00
|
|
|
func processAddEvent(ge *pb.Event) *pb.Event {
|
2019-05-27 23:29:55 -05:00
|
|
|
log.Println("processAddEvent() START ge.Account =", ge.Account)
|
|
|
|
if (checkLogin(ge) == false) {
|
|
|
|
e := pb.MakeFailResponse()
|
|
|
|
e.Comment = "processAddEvent() LOGIN FAILED. ge.Account = nil"
|
|
|
|
log.Println(e.Comment)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:15:55 -05:00
|
|
|
a := regexp.MustCompile(`\.`) // split subdomain on .
|
|
|
|
stuff := a.Split(ge.Account.Domain, -1)
|
|
|
|
log.Printf("processAddEvent() stuff = %q\n", stuff)
|
|
|
|
log.Println("processAddEvent() host =", stuff[0])
|
|
|
|
log.Println("processAddEvent() memory =", ge.Vms[0].Memory)
|
|
|
|
|
|
|
|
memory := fmt.Sprintf("%d", ge.Vms[0].Memory)
|
|
|
|
url := ge.Account.URL + "vms/" + stuff[0] + "?count=1&cpu=2&ram=" + memory + "&disk=25"
|
|
|
|
log.Println("processAddEvent() new URL =", url)
|
|
|
|
|
2019-05-28 15:28:25 -05:00
|
|
|
newVM := string(POSTbody(ge, url))
|
|
|
|
log.Println("processAddEvent() recieved newVM=", newVM)
|
2019-05-27 23:29:55 -05:00
|
|
|
|
|
|
|
e := pb.MakeOkResponse()
|
|
|
|
e.Account = ge.Account
|
2019-05-28 15:28:25 -05:00
|
|
|
e.Comment = newVM
|
2019-05-27 23:29:55 -05:00
|
|
|
log.Println(e.Comment)
|
|
|
|
return e
|
2019-05-26 02:27:51 -05:00
|
|
|
}
|