184 lines
4.5 KiB
Go
Raw Normal View History

2024-10-18 04:28:31 +00:00
package app
import (
2024-12-27 19:59:44 +00:00
"encoding/gob"
2024-10-18 04:28:31 +00:00
"flag"
2024-12-27 19:59:44 +00:00
"fmt"
2024-10-18 04:28:31 +00:00
"log"
"net/http"
"strconv"
2025-01-15 19:36:06 +00:00
"time"
2024-10-18 04:28:31 +00:00
"github.com/gin-gonic/gin"
2024-10-18 04:28:31 +00:00
"github.com/luthermonson/go-proxmox"
)
const APIVersion string = "0.0.2"
2024-10-18 04:28:31 +00:00
2024-12-27 19:59:44 +00:00
var client ProxmoxClient
2024-10-18 04:28:31 +00:00
func Run() {
2024-12-27 19:59:44 +00:00
gob.Register(proxmox.Client{})
2025-01-15 19:36:06 +00:00
gin.SetMode(gin.ReleaseMode)
2024-12-27 19:59:44 +00:00
2024-10-18 04:28:31 +00:00
configPath := flag.String("config", "config.json", "path to config.json file")
flag.Parse()
config := GetConfig(*configPath)
log.Println("Initialized config from " + *configPath)
2024-12-27 19:59:44 +00:00
token := fmt.Sprintf(`%s@%s!%s`, config.PVE.Token.USER, config.PVE.Token.REALM, config.PVE.Token.ID)
client = NewClient(config.PVE.URL, token, config.PVE.Token.Secret)
2024-10-18 04:28:31 +00:00
router := gin.Default()
cluster := Cluster{}
cluster.Init(client)
2025-01-20 21:42:13 +00:00
start := time.Now()
log.Printf("Starting cluster sync\n")
cluster.Sync()
log.Printf("Synced cluster in %fs\n", time.Since(start).Seconds())
2025-01-15 19:36:06 +00:00
// set repeating update for full rebuilds
2025-01-20 21:42:13 +00:00
ticker := time.NewTicker(time.Duration(config.ReloadInterval) * time.Second)
log.Printf("Initialized cluster sync interval of %ds", config.ReloadInterval)
2025-01-15 19:36:06 +00:00
channel := make(chan bool)
go func() {
for {
select {
case <-channel:
return
case <-ticker.C:
2025-01-20 21:42:13 +00:00
start := time.Now()
log.Printf("Starting cluster sync\n")
cluster.Sync()
log.Printf("Synced cluster in %fs\n", time.Since(start).Seconds())
2025-01-15 19:36:06 +00:00
}
}
}()
router.GET("/version", func(c *gin.Context) {
PVEVersion, err := client.Version()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, gin.H{"api-version": APIVersion, "pve-version": PVEVersion})
}
})
router.GET("/nodes/:node", func(c *gin.Context) {
nodeid := c.Param("node")
2025-01-20 21:42:13 +00:00
node, err := cluster.GetNode(nodeid)
2025-01-20 21:42:13 +00:00
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
} else {
c.JSON(http.StatusOK, gin.H{"node": node})
return
}
})
router.GET("/nodes/:node/devices", func(c *gin.Context) {
nodeid := c.Param("node")
node, err := cluster.GetNode(nodeid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
} else {
c.JSON(http.StatusOK, gin.H{"devices": node.Devices})
return
}
})
router.GET("/nodes/:node/instances/:vmid", func(c *gin.Context) {
nodeid := c.Param("node")
vmid, err := strconv.ParseUint(c.Param("vmid"), 10, 64)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", c.Param("instance"))})
return
}
2025-01-20 21:42:13 +00:00
node, err := cluster.GetNode(nodeid)
2025-01-20 21:42:13 +00:00
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
} else {
instance, err := node.GetInstance(uint(vmid))
2025-01-20 21:42:13 +00:00
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
} else {
2025-01-20 21:42:13 +00:00
c.JSON(http.StatusOK, gin.H{"instance": instance})
return
}
}
})
router.POST("/sync", func(c *gin.Context) {
go func() {
start := time.Now()
log.Printf("Starting cluster sync\n")
cluster.Sync()
log.Printf("Synced cluster in %fs\n", time.Since(start).Seconds())
}()
})
router.POST("/nodes/:node/sync", func(c *gin.Context) {
nodeid := c.Param("node")
go func() {
start := time.Now()
log.Printf("Starting %s sync\n", nodeid)
err := cluster.RebuildHost(nodeid)
if err != nil {
log.Printf("Failed to sync %s: %s", nodeid, err.Error())
return
} else {
log.Printf("Synced %s in %fs\n", nodeid, time.Since(start).Seconds())
return
}
}()
})
router.POST("/nodes/:node/instances/:vmid/sync", func(c *gin.Context) {
nodeid := c.Param("node")
vmid, err := strconv.ParseUint(c.Param("vmid"), 10, 64)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", c.Param("instance"))})
return
}
go func() {
start := time.Now()
log.Printf("Starting %s.%d sync\n", nodeid, vmid)
node, err := cluster.GetNode(nodeid)
if err != nil {
log.Printf("Failed to sync %s.%d: %s", nodeid, vmid, err.Error())
return
}
instance, err := node.GetInstance(uint(vmid))
if err != nil {
log.Printf("Failed to sync %s.%d: %s", nodeid, vmid, err.Error())
return
}
err = node.RebuildInstance(instance.Type, uint(vmid))
if err != nil {
log.Printf("Failed to sync %s.%d: %s", nodeid, vmid, err.Error())
return
} else {
log.Printf("Synced %s.%d in %fs\n", nodeid, vmid, time.Since(start).Seconds())
return
}
}()
})
router.Run("0.0.0.0:" + strconv.Itoa(config.ListenPort))
2024-10-18 04:28:31 +00:00
}