adds some code

This commit is contained in:
2025-11-23 13:37:10 +07:00
parent 66591c1b71
commit b506a64ed1
6 changed files with 127 additions and 2 deletions

View File

@@ -140,6 +140,29 @@ func handleDeleteVM(cfg config.Config, svc Services) http.HandlerFunc {
func lifecycleVM(cfg config.Config, svc Services, action string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
// Ensure VM exists either in store or runtime
spec, specErr := svc.Store.LoadVM(id)
runtimeExists := false
if specErr != nil {
if vms, err := svc.Libvirt.ListVMs(); err == nil {
for _, vm := range vms {
if vm.ID == id || vm.Name == id {
runtimeExists = true
break
}
}
}
}
if specErr != nil && !runtimeExists {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "vm not found"})
return
}
if specErr == nil {
if err := validators.CheckStoragePoolsVM(spec.Disks, cfg); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
}
taskID := enqueueWork(svc.Tasks, "vm."+action, func(ctx context.Context) (interface{}, error) {
unlock := svc.StoreLock(id)
defer unlock()
@@ -259,6 +282,28 @@ func handleDeleteCT(cfg config.Config, svc Services) http.HandlerFunc {
func lifecycleCT(cfg config.Config, svc Services, action string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
spec, specErr := svc.Store.LoadCT(id)
runtimeExists := false
if specErr != nil {
if cts, err := svc.LXC.List(); err == nil {
for _, ct := range cts {
if ct.ID == id || ct.Name == id {
runtimeExists = true
break
}
}
}
}
if specErr != nil && !runtimeExists {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "ct not found"})
return
}
if specErr == nil {
if err := validators.CheckStoragePoolsCT(spec, cfg); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
}
taskID := enqueueWork(svc.Tasks, "ct."+action, func(ctx context.Context) (interface{}, error) {
unlock := svc.StoreLock(id)
defer unlock()

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path"
@@ -293,7 +292,7 @@ func buildIsoPureGo(outPath, userDataPath, metaDataPath string) error {
dirRec := buildDirRecord(0, 0, 0, true) // current dir
dirRec = append(dirRec, buildDirRecord(0, 0, 0, true)...) // parent (same)
for _, e := range entries {
dirRec = append(dirRec, buildDirRecord(byteLen(e.name), e.start, e.size, false, e.name)...)
dirRec = append(dirRec, buildDirRecord(byte(len(e.name)), e.start, e.size, false, e.name)...)
}
dirRec = padTo(dirRec, sectorSize)
if err := writeAt(f, rootSector*int64(sectorSize), dirRec); err != nil {

10
pkg/state/store_test.go Normal file
View File

@@ -0,0 +1,10 @@
package state
import "testing"
func TestTrimExt(t *testing.T) {
got := trimExt("foo.yaml")
if got != "foo" {
t.Fatalf("expected foo, got %s", got)
}
}

View File

@@ -0,0 +1,20 @@
package validators
import (
"testing"
"jagacloud/node-agent/pkg/compute/libvirt"
"jagacloud/node-agent/pkg/config"
)
func TestPoolExists(t *testing.T) {
cfg := config.Config{StoragePools: []config.StoragePool{{Name: "local"}}}
err := CheckStoragePoolsVM([]libvirt.DiskSpec{{Pool: "local"}}, cfg)
if err != nil {
t.Fatalf("expected pool to be valid: %v", err)
}
err = CheckStoragePoolsVM([]libvirt.DiskSpec{{Pool: "missing"}}, cfg)
if err == nil {
t.Fatalf("expected error for missing pool")
}
}