Codex implementation of restart

This commit is contained in:
Fedor Korotkov
2025-10-29 06:36:07 -07:00
parent c744d724dc
commit a7a0dea41b
8 changed files with 204 additions and 1 deletions
+3
View File
@@ -159,6 +159,9 @@ func (controller *Controller) initAPI() *gin.Engine {
v1.DELETE("/vms/:name", func(c *gin.Context) {
controller.deleteVM(c).Respond(c)
})
v1.POST("/vms/:name/restart", func(c *gin.Context) {
controller.restartVM(c).Respond(c)
})
v1.GET("/vms/:name/events", func(c *gin.Context) {
controller.listVMEvents(c).Respond(c)
})
+74
View File
@@ -0,0 +1,74 @@
package controller
import (
"context"
"net/http"
"time"
"github.com/cirruslabs/orchard/internal/controller/lifecycle"
storepkg "github.com/cirruslabs/orchard/internal/controller/store"
"github.com/cirruslabs/orchard/internal/responder"
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
"github.com/cirruslabs/orchard/rpc"
"github.com/gin-gonic/gin"
)
func (controller *Controller) restartVM(ctx *gin.Context) responder.Responder {
if responder := controller.authorize(ctx, v1.ServiceAccountRoleComputeWrite); responder != nil {
return responder
}
name := ctx.Param("name")
var updatedVM v1.VM
var workerName string
var shouldNotify bool
response := controller.storeUpdate(func(txn storepkg.Transaction) responder.Responder {
vm, err := txn.GetVM(name)
if err != nil {
return responder.Error(err)
}
if vm.Worker == "" {
return responder.JSON(http.StatusPreconditionFailed,
NewErrorResponse("VM is not assigned to a worker, cannot restart"))
}
if !vm.RestartRequested {
vm.RestartRequested = true
vm.RestartedAt = time.Now()
vm.RestartCount++
shouldNotify = true
workerName = vm.Worker
lifecycle.Report(vm, "VM restart requested", controller.logger)
}
if err := txn.SetVM(*vm); err != nil {
controller.logger.Errorf("failed to store VM restart request in the DB: %v", err)
return responder.Code(http.StatusInternalServerError)
}
updatedVM = *vm
return responder.JSON(http.StatusAccepted, &updatedVM)
})
if shouldNotify && workerName != "" {
notifyCtx, notifyCtxCancel := context.WithTimeout(ctx, time.Second)
defer notifyCtxCancel()
if err := controller.workerNotifier.Notify(notifyCtx, workerName, &rpc.WatchInstruction{
Action: &rpc.WatchInstruction_SyncVmsAction{
SyncVmsAction: &rpc.WatchInstruction_SyncVMs{},
},
}); err != nil {
controller.logger.Warnf("failed to notify worker %s about VM restart: %v",
workerName, err)
}
}
return response
}