add more codes

This commit is contained in:
2025-11-23 22:37:27 +07:00
parent b506a64ed1
commit 4fcd71ca05
10 changed files with 448 additions and 52 deletions

View File

@@ -15,6 +15,7 @@ import (
"jagacloud/node-agent/pkg/config"
"jagacloud/node-agent/pkg/containers/lxc"
"jagacloud/node-agent/pkg/containers/podman"
"jagacloud/node-agent/pkg/storage"
"jagacloud/node-agent/pkg/tasks"
"jagacloud/node-agent/pkg/validators"
)
@@ -88,6 +89,14 @@ func handleCreateVM(cfg config.Config, svc Services) http.HandlerFunc {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
// Resolve disk paths for dir pools
for i := range spec.Disks {
if spec.Disks[i].Path == "" && spec.Disks[i].Pool != "" {
if path, err := storage.ResolveVolume(toPoolConfigs(cfg.StoragePools), spec.Disks[i].Pool, spec.Disks[i].Name+".qcow2"); err == nil {
spec.Disks[i].Path = path
}
}
}
if err := svc.Store.SaveVM(spec); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
@@ -162,6 +171,12 @@ func lifecycleVM(cfg config.Config, svc Services, action string) http.HandlerFun
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
} else {
// runtime-only VM: attempt pool validation via virsh and config
if err := validators.CheckStoragePoolsRuntime([]string{}, 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)
@@ -234,6 +249,11 @@ func handleCreateCT(cfg config.Config, svc Services) http.HandlerFunc {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
if spec.RootfsPool != "" && spec.RootfsSizeG > 0 && spec.RootfsPath == "" {
if path, err := storage.ResolveVolume(toPoolConfigs(cfg.StoragePools), spec.RootfsPool, spec.ID+"-rootfs"); err == nil {
spec.RootfsPath = path
}
}
if err := svc.Store.SaveCT(spec); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
@@ -474,3 +494,17 @@ func validateVM(spec libvirt.VMSpec, cfg config.Config) error {
// storage pools validated elsewhere
return nil
}
// toPoolConfigs converts config pools to storage pool configs.
func toPoolConfigs(p []config.StoragePool) []storage.PoolConfig {
out := make([]storage.PoolConfig, 0, len(p))
for _, sp := range p {
out = append(out, storage.PoolConfig{
Name: sp.Name,
Type: sp.Type,
Path: sp.Path,
VG: sp.Path, // reuse Path for now; real config should split
})
}
return out
}