2019-05-12 00:48:59 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "flag"
|
|
|
|
import "io"
|
|
|
|
import "log"
|
|
|
|
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 "github.com/golang/protobuf/proto"
|
|
|
|
import pb "git.wit.com/wit/witProtobuf"
|
2019-05-23 22:46:02 -05:00
|
|
|
import "github.com/gorilla/websocket"
|
|
|
|
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
|
|
|
|
2019-05-27 23:29:55 -05:00
|
|
|
// const API = "http://stackapi:4000/"
|
2019-05-23 22:46:02 -05:00
|
|
|
|
|
|
|
type network struct {
|
|
|
|
Subdomain string `json:"subdomain"`
|
|
|
|
}
|
|
|
|
|
2019-05-28 15:28:25 -05:00
|
|
|
/*
|
2019-05-26 14:35:13 -05:00
|
|
|
func checkLogin(ge *pb.Event) bool {
|
|
|
|
if (ge.Account == nil) {
|
|
|
|
log.Println("checkLogin() pb.Event.Account == nil ERROR")
|
|
|
|
return false
|
|
|
|
}
|
2019-05-26 02:27:51 -05:00
|
|
|
if (origLoginCheck(ge) == true) {
|
2019-05-26 14:35:13 -05:00
|
|
|
log.Println("checkLogin() LOGIN WORKED")
|
|
|
|
return true
|
2019-05-24 17:36:56 -05:00
|
|
|
}
|
2019-05-26 14:35:13 -05:00
|
|
|
log.Println("checkLogin() LOGIN FAILED. ATTEMPT updateToken(ge)")
|
|
|
|
return updateToken(ge)
|
2019-05-24 17:36:56 -05:00
|
|
|
}
|
2019-05-28 15:28:25 -05:00
|
|
|
*/
|
2019-05-24 17:36:56 -05:00
|
|
|
|
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") }
|
|
|
|
log.Printf("NETWORKS =", body)
|
|
|
|
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-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 fetch vms for %s: %s", network, err) }
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil { log.Fatal("can't read resp") }
|
|
|
|
err = json.Unmarshal(body, &vms)
|
|
|
|
if err != nil { log.Fatal("can't unmarshal resp for fetch vms: ", err, "\n\n", network, string(body)) }
|
|
|
|
spew.Dump(vms)
|
|
|
|
return vms
|
|
|
|
}
|
|
|
|
|
2019-05-15 15:28:59 -05:00
|
|
|
// Also potentially look at:
|
|
|
|
// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/
|
|
|
|
|
2019-05-12 00:48:59 -05:00
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 4096,
|
|
|
|
WriteBufferSize: 4096,
|
|
|
|
EnableCompression: true,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-12 14:19:15 -05:00
|
|
|
func sendProtobuf(conn *websocket.Conn, msg *pb.Event) {
|
2019-05-27 23:29:55 -05:00
|
|
|
log.Println("sendProtobuf() START")
|
2019-05-12 14:19:15 -05:00
|
|
|
data, _ := proto.Marshal(msg)
|
|
|
|
err := conn.WriteMessage(websocket.BinaryMessage, data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("sendProtobuf() write:", err)
|
|
|
|
}
|
2019-05-27 23:29:55 -05:00
|
|
|
log.Println("sendProtobuf() END")
|
2019-05-12 14:19:15 -05:00
|
|
|
}
|
|
|
|
|
2019-05-12 00:57:19 -05:00
|
|
|
func eventHandler(w http.ResponseWriter, r *http.Request) {
|
2019-05-12 02:28:07 -05:00
|
|
|
log.Println("echoHandler() START")
|
2019-05-24 00:46:37 -05:00
|
|
|
log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent"))
|
|
|
|
log.Println("echoHandler() ATTEMPT r.Header", r.Header)
|
|
|
|
for key, value := range r.Header {
|
|
|
|
log.Println("echoHandler() HEADER key, value =", key, value)
|
|
|
|
}
|
2019-05-12 02:28:07 -05:00
|
|
|
|
2019-05-12 00:48:59 -05:00
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Upgrade:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
mytype, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("read:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if mytype == websocket.TextMessage {
|
2019-05-26 14:35:13 -05:00
|
|
|
// probably this should never happen since the client socket connection should have
|
|
|
|
// already been dropped. If a user is doing this, the account should probably be
|
|
|
|
// terminated as abuse
|
2019-05-26 15:13:50 -05:00
|
|
|
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. NO HANDLER FOR THIS YET)")
|
|
|
|
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. THIS SHOULD NEVER HAPPEN)")
|
|
|
|
log.Println("\teventHandler() GOT websocket.TextMessage (ERROR. SUSPEND THIS USER)")
|
2019-05-12 00:48:59 -05:00
|
|
|
}
|
|
|
|
if mytype == websocket.BinaryMessage {
|
2019-05-26 15:13:50 -05:00
|
|
|
log.Println("\teventHandler() GOT websocket.BinaryMessage")
|
2019-05-12 00:48:59 -05:00
|
|
|
pdata := new(pb.Event)
|
2019-05-26 15:13:50 -05:00
|
|
|
var e *pb.Event
|
2019-05-27 23:29:55 -05:00
|
|
|
e = new(pb.Event)
|
2019-05-12 00:48:59 -05:00
|
|
|
err = proto.Unmarshal(message, pdata)
|
|
|
|
if (err != nil) {
|
|
|
|
log.Printf("readConn() something fucked up happened in Unmarshal")
|
|
|
|
}
|
2019-05-27 23:29:55 -05:00
|
|
|
log.Printf("\teventHandler() pdata.Type =", pdata.Type)
|
|
|
|
log.Printf("\teventHandler() recv binary: %s", pdata)
|
2019-05-24 17:36:56 -05:00
|
|
|
if pdata.Type == pb.Event_GET {
|
2019-05-27 23:29:55 -05:00
|
|
|
log.Printf("\teventHandler() DOING Event_GET")
|
2019-05-26 15:13:50 -05:00
|
|
|
e = processGetEvent(pdata)
|
2019-05-26 15:20:50 -05:00
|
|
|
} else if pdata.Type == pb.Event_LOGIN {
|
2019-05-27 23:29:55 -05:00
|
|
|
log.Printf("\teventHandler() DOING Event_LOGIN")
|
2019-05-26 15:13:50 -05:00
|
|
|
e = processLoginEvent(pdata)
|
2019-05-26 15:20:50 -05:00
|
|
|
} else if pdata.Type == pb.Event_ADD {
|
2019-05-27 23:29:55 -05:00
|
|
|
log.Printf("\teventHandler() DOING Event_ADD")
|
2019-05-26 15:13:50 -05:00
|
|
|
e = processAddEvent(pdata)
|
2019-05-12 01:07:22 -05:00
|
|
|
}
|
2019-05-27 23:29:55 -05:00
|
|
|
if (e == nil) {
|
|
|
|
e = pb.MakeFailResponse()
|
|
|
|
e.Comment = "websocket.BinaryMessage LOGIN FAILED"
|
|
|
|
}
|
|
|
|
e.Account = pdata.Account
|
2019-05-28 15:28:25 -05:00
|
|
|
log.Println("\teventHandler() e.Comment =", e.Comment)
|
2019-05-26 15:13:50 -05:00
|
|
|
sendProtobuf(conn, e)
|
2019-05-12 00:48:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveHome(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.Error(w, "Not found.", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
io.WriteString(w, "<html><body>Echo Server</body></html>")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-26 01:22:37 -05:00
|
|
|
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-05-28 14:35:42 -05:00
|
|
|
url := ge.Account.URL + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
|
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
|
|
|
}
|