diff --git a/src/server/config/config.go b/src/server/config/config.go index 61535d5be8310fd39ef3c759ec0d353814f58155..f29717306e1be8d6743851d848c9202fc7c4f563 100644 --- a/src/server/config/config.go +++ b/src/server/config/config.go @@ -54,7 +54,7 @@ func GetInstance() *Config { once.Do(func() { certsDir := "../certs" configDir := "../config" - dataDir := "../dataDir" + dataDir := "../data" conf, err := ini.LoadSources(ini.LoadOptions{ Loose: false, diff --git a/src/server/go.mod b/src/server/go.mod index 51b421ab32ffcbcf18f935c9d64f80bc0508617f..8bab31ad3c237c06adbd717fd9e4f6fac2fdbb8f 100644 --- a/src/server/go.mod +++ b/src/server/go.mod @@ -20,8 +20,6 @@ replace nexus-server/vms => ./vms replace nexus-server/utils => ./utils -replace nexus-server/paths => ./paths - replace nexus-server/config => ./config replace nexus-server/cleaner => ./cleaner @@ -36,7 +34,6 @@ require ( github.com/sirupsen/logrus v1.9.0 nexus-server/cleaner v0.0.0-00010101000000-000000000000 nexus-server/logger v0.0.0-00010101000000-000000000000 - nexus-server/paths v0.0.0-00010101000000-000000000000 nexus-server/router v0.0.0-00010101000000-000000000000 nexus-server/users v0.0.0-00010101000000-000000000000 nexus-server/utils v0.0.0-00010101000000-000000000000 diff --git a/src/server/nexus-server.go b/src/server/nexus-server.go index 6ce3ca6f58eb36cfe4f3bfd05631f1bf58324201..969ff7070dd8fdd68f2b1ef8c9fb47878edf72ca 100644 --- a/src/server/nexus-server.go +++ b/src/server/nexus-server.go @@ -5,7 +5,6 @@ import ( "path" "nexus-server/vms" "nexus-server/exec" - "nexus-server/paths" "nexus-server/users" "nexus-server/utils" "nexus-server/router" @@ -24,8 +23,7 @@ func main() { var appname = path.Base(os.Args[0]) log.Info(appname+" version "+version.Get().String()) - // These two lines must be executed very early on (basically, here). - paths.Init() + // Initialize the RNG' seed to the current time. utils.RandInit() if err := exec.CheckQemuSystem(); err != nil { diff --git a/src/server/paths/go.mod b/src/server/paths/go.mod deleted file mode 100644 index bc7d2470ffa5a6b054567956fd33b5f50124d694..0000000000000000000000000000000000000000 --- a/src/server/paths/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module nexus-server/paths - -go 1.18 diff --git a/src/server/paths/paths.go b/src/server/paths/paths.go deleted file mode 100644 index 6363766e4bd82756b17a22395f9bd46ecf604a52..0000000000000000000000000000000000000000 --- a/src/server/paths/paths.go +++ /dev/null @@ -1,36 +0,0 @@ -package paths - -import ( - "path/filepath" - "nexus-server/logger" -) - -type Paths struct { - UsersFile string - DataDir string - VMsDir string - TemplatesDir string - CertsDir string - TmpDir string -} - -var log = logger.GetInstance() -var paths *Paths - -func GetInstance() *Paths { - return paths -} - -func Init() { - certs := "../certs" - conf := "../config" - data := "../data" - paths = &Paths { - UsersFile: filepath.Join(conf, "/users.json"), - DataDir: data, - VMsDir: filepath.Join(data, "/vms"), - TemplatesDir: filepath.Join(data, "/templates"), - CertsDir: certs, - TmpDir: filepath.Join(data, "/tmp"), - } -} diff --git a/src/server/router/router.go b/src/server/router/router.go index cd90f7ad1858cfc40d3b0d16baeed3f17950bf0b..d4f471db6b539c44f95ec5b392ed111cd77c649f 100644 --- a/src/server/router/router.go +++ b/src/server/router/router.go @@ -9,7 +9,6 @@ import ( "net/http" "os/signal" "path/filepath" - "nexus-server/paths" "nexus-server/users" "nexus-server/logger" "nexus-server/config" @@ -108,7 +107,7 @@ func (router *Router)Start(port int) { // Starts server in a dedicated goroutine. go func() { - certsDir := paths.GetInstance().CertsDir + certsDir := conf.CertsDir if err := router.echo.StartTLS(":"+strconv.Itoa(port), filepath.Join(certsDir, "/server-cert.pem"), filepath.Join(certsDir, "/server-key.pem")); err != nil { if err != http.ErrServerClosed { log.Fatal("Server error: "+err.Error()) diff --git a/src/server/router/routerTemplates.go b/src/server/router/routerTemplates.go index 4535628a55299831f3b816aae806f297389b0306..6c7f51fdb08128c86e7cd2a9ea5b981a5242176c 100644 --- a/src/server/router/routerTemplates.go +++ b/src/server/router/routerTemplates.go @@ -8,7 +8,6 @@ import ( "nexus-common/caps" "nexus-common/params" "nexus-server/vms" - "nexus-server/paths" "nexus-server/users" "github.com/labstack/echo/v4" "github.com/google/uuid" @@ -148,13 +147,12 @@ func (r *RouterTemplates)CreateTemplateFromQCOW(c echo.Context) error { } defer src.Close() - tmpDir := paths.GetInstance().TmpDir uuid, err := uuid.NewRandom() if err != nil { log.Error("Failed creating random UUID: "+err.Error()) return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) } - uploadedFile := filepath.Join(tmpDir, "upload_"+uuid.String()+".qcow") + uploadedFile := filepath.Join(conf.Core.TmpDir, "upload_"+uuid.String()+".qcow") dst, err := os.Create(uploadedFile) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) diff --git a/src/server/router/routerVMs.go b/src/server/router/routerVMs.go index 4d9d2e34cf729385a96672f7824923bd04fba902..34cca62f1651ca66f79599623ac1483c4c102f12 100644 --- a/src/server/router/routerVMs.go +++ b/src/server/router/routerVMs.go @@ -10,7 +10,6 @@ import ( "nexus-common/params" "nexus-server/vms" "nexus-server/users" - "nexus-server/paths" "github.com/google/uuid" "github.com/labstack/echo/v4" "github.com/go-playground/validator/v10" @@ -406,8 +405,7 @@ func (r *RouterVMs)ExportVMDir(c echo.Context) error { } // Creates a unique tar filename. - tmpDir := paths.GetInstance().TmpDir - tarGzFile := filepath.Join(tmpDir, "exportdir_"+vm.GetID().String()+".tar.gz") + tarGzFile := filepath.Join(conf.Core.TmpDir, "exportdir_"+vm.GetID().String()+".tar.gz") // Extracts VM's p.Dir directory into tarGzFile on the host. if err := r.vms.ExportVMFiles(vm, p.Dir, tarGzFile); err != nil { @@ -446,13 +444,12 @@ func (r *RouterVMs)ImportFilesToVM(c echo.Context) error { } defer src.Close() - tmpDir := paths.GetInstance().TmpDir uuid, err := uuid.NewRandom() if err != nil { log.Error("Failed creating random UUID: "+err.Error()) return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) } - uploadedtarGzFile := filepath.Join(tmpDir, "upload_"+uuid.String()+".tar") + uploadedtarGzFile := filepath.Join(conf.Core.TmpDir, "upload_"+uuid.String()+".tar") dst, err := os.Create(uploadedtarGzFile) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) diff --git a/src/server/users/users.go b/src/server/users/users.go index 35422def69d9fbe2addbfde03c5f48d5d394b117..9da6928fd948bc42a555ff62a469f159a91c4234 100644 --- a/src/server/users/users.go +++ b/src/server/users/users.go @@ -6,8 +6,8 @@ import ( "sync" "errors" "encoding/json" - "nexus-server/paths" "nexus-server/logger" + "nexus-server/config" ) type Users struct { @@ -17,6 +17,7 @@ type Users struct { } var log = logger.GetInstance() +var conf = config.GetInstance() var users *Users // Returns a Users "singleton". @@ -27,7 +28,7 @@ func GetUsersInstance() *Users { // Creates users by reading them from the config directory. func InitUsers() error { - usersFile := paths.GetInstance().UsersFile + usersFile := conf.UsersFile users = &Users { m: make(map[string]User), file: usersFile, rwlock: new(sync.RWMutex) } return users.createUsersFromFile() } diff --git a/src/server/vms/templates.go b/src/server/vms/templates.go index 843943b98568e364462c9f2f895dcdfa2634731f..0110a3db7aa3d3aef23ed7c567bced5173bca886 100644 --- a/src/server/vms/templates.go +++ b/src/server/vms/templates.go @@ -8,7 +8,6 @@ import( "errors" "path/filepath" t "nexus-common/template" - "nexus-server/paths" "nexus-server/utils" "github.com/google/uuid" ) @@ -36,7 +35,7 @@ func GetTemplatesInstance() *Templates { // REMARK: path is the root directory where templates reside. // Concurrency: unsafe func InitTemplates() error { - templatesDir := paths.GetInstance().TemplatesDir + templatesDir := conf.TemplatesDir templates = &Templates { m: make(map[string]*Template), dir: templatesDir, rwlock: new(sync.RWMutex) } dirs, err := utils.GetSubDirs(templatesDir) diff --git a/src/server/vms/vms.go b/src/server/vms/vms.go index 87c267aa08b0bbf4d7be6fe281d1923ca5555818..72a0cbb92507472cebde7b3fc75753b57d1a31c7 100644 --- a/src/server/vms/vms.go +++ b/src/server/vms/vms.go @@ -15,7 +15,6 @@ import ( "nexus-common/caps" "nexus-common/params" "nexus-server/exec" - "nexus-server/paths" "nexus-server/utils" "nexus-server/users" "nexus-server/logger" @@ -50,7 +49,7 @@ func GetVMsInstance() *VMs { // REMARK: path is the root directory where VMs reside. // Concurrency: unsafe func InitVMs() error { - vmsDir := paths.GetInstance().VMsDir + vmsDir := conf.VMsDir vms = &VMs { m: make(map[string]*VM), dir: vmsDir, rwlock: new(sync.RWMutex), usedRAM: 0 } vms.usedPorts[conf.Core.APIDefaultPort] = true @@ -243,7 +242,7 @@ func (vms *VMs)StartVMWithCreds(vmID uuid.UUID, port int, checkPort bool, pwd st // Function that executes the VM in QEMU using the specified spice port and password. runQemuFn := func(vm *VM, port int, pwd, pwdFile string, endofExecFn endOfExecCallback) error { - certsDir := paths.GetInstance().CertsDir + certsDir := conf.CertsDir cmd, err := exec.NewQemuSystem(vm.qgaSock, vm.v.Cpus, vm.v.Ram, string(vm.v.Nic), vm.v.UsbDevs, filepath.Join(vm.dir, vmDiskFile), port, pwdFile, certsDir) if err != nil { log.Error(prefix+"filepath join error: "+err.Error())