improve various log messages,
improve http return codes
This commit is contained in:
56
app/app.go
56
app/app.go
@@ -25,7 +25,7 @@ func Run() {
|
||||
flag.Parse()
|
||||
|
||||
config := GetConfig(*configPath)
|
||||
log.Printf("Initialized config from %s", *configPath)
|
||||
log.Printf("[INF] initialized config from %s", *configPath)
|
||||
|
||||
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)
|
||||
@@ -35,13 +35,13 @@ func Run() {
|
||||
cluster := Cluster{}
|
||||
cluster.Init(client)
|
||||
start := time.Now()
|
||||
log.Printf("Starting cluster sync\n")
|
||||
log.Printf("[INF] starting cluster sync\n")
|
||||
cluster.Sync()
|
||||
log.Printf("Synced cluster in %fs\n", time.Since(start).Seconds())
|
||||
log.Printf("[INF] synced cluster in %fs\n", time.Since(start).Seconds())
|
||||
|
||||
// set repeating update for full rebuilds
|
||||
ticker := time.NewTicker(time.Duration(config.ReloadInterval) * time.Second)
|
||||
log.Printf("Initialized cluster sync interval of %ds", config.ReloadInterval)
|
||||
log.Printf("[INF] initialized cluster sync interval of %ds", config.ReloadInterval)
|
||||
channel := make(chan bool)
|
||||
go func() {
|
||||
for {
|
||||
@@ -50,9 +50,9 @@ func Run() {
|
||||
return
|
||||
case <-ticker.C:
|
||||
start := time.Now()
|
||||
log.Printf("Starting cluster sync\n")
|
||||
log.Printf("[INF] starting cluster sync\n")
|
||||
cluster.Sync()
|
||||
log.Printf("Synced cluster in %fs\n", time.Since(start).Seconds())
|
||||
log.Printf("[INF] synced cluster in %fs\n", time.Since(start).Seconds())
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -72,7 +72,7 @@ func Run() {
|
||||
node, err := cluster.GetNode(nodeid)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"node": node})
|
||||
@@ -86,7 +86,7 @@ func Run() {
|
||||
node, err := cluster.GetNode(nodeid)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"devices": node.Devices})
|
||||
@@ -98,7 +98,7 @@ func Run() {
|
||||
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"))})
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", c.Param("instance"))})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ func Run() {
|
||||
} else {
|
||||
instance, err := node.GetInstance(uint(vmid))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"instance": instance})
|
||||
@@ -122,9 +122,16 @@ func Run() {
|
||||
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())
|
||||
log.Printf("[INF] starting cluster sync\n")
|
||||
err := cluster.Sync()
|
||||
if err != nil {
|
||||
log.Printf("[ERR] failed to sync cluster: %s", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
log.Printf("[INF] synced cluster in %fs\n", time.Since(start).Seconds())
|
||||
return
|
||||
}
|
||||
//}()
|
||||
})
|
||||
|
||||
@@ -132,13 +139,14 @@ func Run() {
|
||||
nodeid := c.Param("node")
|
||||
//go func() {
|
||||
start := time.Now()
|
||||
log.Printf("Starting %s sync\n", nodeid)
|
||||
err := cluster.RebuildHost(nodeid)
|
||||
log.Printf("[INF] starting %s sync\n", nodeid)
|
||||
err := cluster.RebuildNode(nodeid)
|
||||
if err != nil {
|
||||
log.Printf("Failed to sync %s: %s", nodeid, err.Error())
|
||||
log.Printf("[ERR] failed to sync %s: %s", nodeid, err.Error())
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
log.Printf("Synced %s in %fs\n", nodeid, time.Since(start).Seconds())
|
||||
log.Printf("[INF] synced %s in %fs\n", nodeid, time.Since(start).Seconds())
|
||||
return
|
||||
}
|
||||
//}()
|
||||
@@ -154,30 +162,34 @@ func Run() {
|
||||
|
||||
//go func() {
|
||||
start := time.Now()
|
||||
log.Printf("Starting %s.%d sync\n", nodeid, vmid)
|
||||
log.Printf("[INF] 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())
|
||||
log.Printf("[ERR] failed to sync %s.%d: %s", nodeid, vmid, err.Error())
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := node.GetInstance(uint(vmid))
|
||||
if err != nil {
|
||||
log.Printf("Failed to sync %s.%d: %s", nodeid, vmid, err.Error())
|
||||
log.Printf("[ERR] failed to sync %s.%d: %s", nodeid, vmid, err.Error())
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": 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())
|
||||
log.Printf("[ERR] failed to sync %s.%d: %s", nodeid, vmid, err.Error())
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
log.Printf("Synced %s.%d in %fs\n", nodeid, vmid, time.Since(start).Seconds())
|
||||
log.Printf("[INF] synced %s.%d in %fs\n", nodeid, vmid, time.Since(start).Seconds())
|
||||
return
|
||||
}
|
||||
//}()
|
||||
})
|
||||
|
||||
log.Printf("[INF] starting API listening on 0.0.0.0:%d", config.ListenPort)
|
||||
router.Run("0.0.0.0:" + strconv.Itoa(config.ListenPort))
|
||||
}
|
||||
|
||||
64
app/model.go
64
app/model.go
@@ -10,6 +10,7 @@ func (cluster *Cluster) Init(pve ProxmoxClient) {
|
||||
cluster.pve = pve
|
||||
}
|
||||
|
||||
// hard sync cluster
|
||||
func (cluster *Cluster) Sync() error {
|
||||
// aquire lock on cluster, release on return
|
||||
cluster.lock.Lock()
|
||||
@@ -25,10 +26,11 @@ func (cluster *Cluster) Sync() error {
|
||||
// for each node:
|
||||
for _, hostName := range nodes {
|
||||
// rebuild node
|
||||
err := cluster.RebuildHost(hostName)
|
||||
err := cluster.RebuildNode(hostName)
|
||||
if err != nil { // if an error was encountered, continue and log the error
|
||||
log.Print(err.Error())
|
||||
continue
|
||||
log.Printf("[ERR] %s", err)
|
||||
} else { // otherwise log success
|
||||
log.Printf("[INF] successfully synced node %s", hostName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,16 +67,25 @@ func (cluster *Cluster) GetNode(hostName string) (*Node, error) {
|
||||
return host, err
|
||||
}
|
||||
|
||||
func (cluster *Cluster) RebuildHost(hostName string) error {
|
||||
// hard sync node
|
||||
// returns error if the node could not be reached
|
||||
func (cluster *Cluster) RebuildNode(hostName string) error {
|
||||
host, err := cluster.pve.Node(hostName)
|
||||
if err != nil { // host is probably down or otherwise unreachable
|
||||
return fmt.Errorf("error retrieving %s: %s, possibly down?", hostName, err.Error())
|
||||
if err != nil && cluster.Nodes[hostName] == nil { // host is unreachable and did not exist previously
|
||||
// return an error because we requested to sync a node that was not already in the cluster
|
||||
return fmt.Errorf("error retrieving %s: %s", hostName, err.Error())
|
||||
}
|
||||
|
||||
// aquire lock on host, release on return
|
||||
host.lock.Lock()
|
||||
defer host.lock.Unlock()
|
||||
|
||||
if err != nil && cluster.Nodes[hostName] != nil { // host is unreachable and did exist previously
|
||||
// assume the node is down or gone and delete from cluster
|
||||
delete(cluster.Nodes, hostName)
|
||||
return nil
|
||||
}
|
||||
|
||||
cluster.Nodes[hostName] = host
|
||||
|
||||
// get node's VMs
|
||||
@@ -86,8 +97,9 @@ func (cluster *Cluster) RebuildHost(hostName string) error {
|
||||
for _, vmid := range vms {
|
||||
err := host.RebuildInstance(VM, vmid)
|
||||
if err != nil { // if an error was encountered, continue and log the error
|
||||
log.Print(err.Error())
|
||||
continue
|
||||
log.Printf("[ERR] %s", err)
|
||||
} else {
|
||||
log.Printf("[INF] successfully synced vm %s.%d", hostName, vmid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,8 +110,10 @@ func (cluster *Cluster) RebuildHost(hostName string) error {
|
||||
}
|
||||
for _, vmid := range cts {
|
||||
err := host.RebuildInstance(CT, vmid)
|
||||
if err != nil {
|
||||
return err
|
||||
if err != nil { // if an error was encountered, continue and log the error
|
||||
log.Printf("[ERR] %s", err)
|
||||
} else {
|
||||
log.Printf("[INF] successfully synced ct %s.%d", hostName, vmid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,28 +157,35 @@ func (host *Node) GetInstance(vmid uint) (*Instance, error) {
|
||||
return instance, err
|
||||
}
|
||||
|
||||
// hard sync instance
|
||||
// returns error if the instance could not be reached
|
||||
func (host *Node) RebuildInstance(instancetype InstanceType, vmid uint) error {
|
||||
instanceID := InstanceID(vmid)
|
||||
var instance *Instance
|
||||
var err error
|
||||
if instancetype == VM {
|
||||
var err error
|
||||
instance, err = host.VirtualMachine(vmid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error retrieving %d: %s, possibly down?", vmid, err.Error())
|
||||
}
|
||||
} else if instancetype == CT {
|
||||
var err error
|
||||
instance, err = host.Container(vmid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error retrieving %d: %s, possibly down?", vmid, err.Error())
|
||||
|
||||
}
|
||||
|
||||
if err != nil && host.Instances[instanceID] == nil { // instance is unreachable and did not exist previously
|
||||
// return an error because we requested to sync an instance that was not already in the cluster
|
||||
return fmt.Errorf("error retrieving %d.%d: %s", host.Name, instanceID, err.Error())
|
||||
}
|
||||
|
||||
// aquire lock on instance, release on return
|
||||
instance.lock.Lock()
|
||||
defer instance.lock.Unlock()
|
||||
|
||||
host.Instances[InstanceID(vmid)] = instance
|
||||
if err != nil && host.Instances[instanceID] != nil { // host is unreachable and did exist previously
|
||||
// assume the instance is gone and delete from cluster
|
||||
delete(host.Instances, instanceID)
|
||||
return nil
|
||||
}
|
||||
|
||||
host.Instances[instanceID] = instance
|
||||
|
||||
for volid := range instance.configDisks {
|
||||
instance.RebuildVolume(host, volid)
|
||||
@@ -215,10 +236,11 @@ func (instance *Instance) RebuildNet(netid string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (instance *Instance) RebuildDevice(host *Node, deviceid string) error {
|
||||
func (instance *Instance) RebuildDevice(host *Node, deviceid string) {
|
||||
instanceDevice, ok := instance.configHostPCIs[deviceid]
|
||||
if !ok { // if device does not exist
|
||||
return fmt.Errorf("%s not found in devices", deviceid)
|
||||
log.Printf("%s not found in devices", deviceid)
|
||||
return
|
||||
}
|
||||
|
||||
hostDeviceBusID := DeviceID(strings.Split(instanceDevice, ",")[0])
|
||||
@@ -235,8 +257,6 @@ func (instance *Instance) RebuildDevice(host *Node, deviceid string) error {
|
||||
|
||||
instance.Devices[DeviceID(instanceDeviceBusID)].Device_ID = DeviceID(deviceid)
|
||||
instance.Devices[DeviceID(instanceDeviceBusID)].Value = instanceDevice
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (instance *Instance) RebuildBoot() {
|
||||
|
||||
Reference in New Issue
Block a user