feat(controller): added in update server channel and refactors for more actions
This commit is contained in:
@@ -1,11 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
socketio "github.com/googollee/go-socket.io"
|
||||
)
|
||||
|
||||
type UpdatePayload struct {
|
||||
Action string `json:"action"`
|
||||
Target string `json:"target"`
|
||||
}
|
||||
|
||||
func registerUpdateChannel(server *socketio.Server) {
|
||||
|
||||
server.OnEvent("/", "subscribe:update", func(s socketio.Conn) {
|
||||
@@ -15,21 +28,91 @@ func registerUpdateChannel(server *socketio.Server) {
|
||||
})
|
||||
|
||||
// copy files based on the target passed over
|
||||
server.OnEvent("/", "update", func(s socketio.Conn, target string) {
|
||||
server.BroadcastToRoom("/", "update", "updateLogs",
|
||||
fmt.Sprintf("🚀 Copying latest build to %v", target))
|
||||
copyBuild(server, target)
|
||||
server.OnEvent("/", "update", func(s socketio.Conn, payload UpdatePayload) {
|
||||
switch strings.ToLower(payload.Action) {
|
||||
case "copy":
|
||||
server.BroadcastToRoom("/", "update", "updateLogs",
|
||||
fmt.Sprintf("🚀 Copying latest build to %v", payload.Target))
|
||||
copyLatestBuild(server, payload.Target)
|
||||
|
||||
case "update":
|
||||
updateServer(server, payload.Target)
|
||||
|
||||
default:
|
||||
server.BroadcastToRoom("/", "update", "updateLogs",
|
||||
fmt.Sprintf("❓ Unknown action: %s", payload.Action))
|
||||
}
|
||||
})
|
||||
|
||||
// update the server
|
||||
// server.OnEvent("/", "update", func(s socketio.Conn, target string) {
|
||||
// msg := fmt.Sprintf("🔧 Running updateServer on: %s", target)
|
||||
// server.BroadcastToRoom("/update", "update", "updateLogs", msg)
|
||||
|
||||
// })
|
||||
|
||||
server.OnEvent("/", "unsubscribe:update", func(s socketio.Conn) {
|
||||
s.Leave("update")
|
||||
s.Emit("updateLogs", "👋 Unsubscribed from update logs")
|
||||
})
|
||||
}
|
||||
|
||||
func updateServer(server *socketio.Server, target string) {
|
||||
host, err := os.Hostname()
|
||||
if err != nil {
|
||||
server.BroadcastToRoom("/", "update", "updateLogs", "Could not retrieve hostname")
|
||||
return
|
||||
}
|
||||
|
||||
if strings.Contains(host, "VMS") || strings.Contains(host, "vms") {
|
||||
server.BroadcastToRoom("/", "update", "updateLogs", "Your are about to check for a new build and then update the server.")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if target == "" {
|
||||
server.BroadcastToRoom("/", "update", "updateLogs", "You seem to be on a dev server or not an actual production server, you MUST pass a server over. I.E. USMCD1VMS036")
|
||||
return
|
||||
} else {
|
||||
server.BroadcastToRoom("/", "update", "updateLogs", fmt.Sprintf("Running the update on: %v", target))
|
||||
go triggerRemoteUpdate(server, target, UpdatePayload{Action: "update", Target: target})
|
||||
}
|
||||
}
|
||||
|
||||
func copyLatestBuild(server *socketio.Server, target string) {
|
||||
server.BroadcastToRoom("/", "update", "updateLogs",
|
||||
fmt.Sprintf("🚀 Copying latest build to %v", target))
|
||||
copyBuild(server, target)
|
||||
}
|
||||
|
||||
func triggerRemoteUpdate(server *socketio.Server, remoteURL string, payload UpdatePayload) {
|
||||
|
||||
basePath := "/api/controller"
|
||||
if os.Getenv("NODE_ENV") != "production" {
|
||||
basePath = "/lst/api/controller"
|
||||
}
|
||||
|
||||
// send POST request with JSON, expect SSE / streaming text back
|
||||
body, _ := json.Marshal(payload)
|
||||
|
||||
url := fmt.Sprintf("https://%v.alpla.net%v/update", remoteURL, basePath)
|
||||
//url := fmt.Sprintf("http://localhost:8080%v/update", basePath)
|
||||
fmt.Println(url)
|
||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
log.Println("❌ Cannot connect remote:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
decoder := bufio.NewReader(resp.Body)
|
||||
for {
|
||||
line, err := decoder.ReadString('\n')
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
log.Println("❌ Error reading stream:", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
//fmt.Println(line)
|
||||
parsed := strings.TrimSpace(line)
|
||||
if parsed != "" {
|
||||
log.Println("📡 Remote log:", parsed)
|
||||
server.BroadcastToRoom("/", "update", "updateLogs", parsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user