diff --git a/cmd/root.go b/cmd/root.go index efc5b6a..a90eab2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -164,6 +164,12 @@ func rootCmdRun(cmd *cobra.Command, _ []string) { for _, serv := range server.GetServers().All() { s := serv + // For each server we encounter make sure the root data directory exists. + if err := s.EnsureDataDirectoryExists(); err != nil { + s.Log().Error("could not create root data directory for server: not loading server...") + continue + } + pool.Submit(func() { s.Log().Info("configuring server environment and restoring to previous state") diff --git a/server/filesystem.go b/server/filesystem.go deleted file mode 100644 index d86b526..0000000 --- a/server/filesystem.go +++ /dev/null @@ -1,30 +0,0 @@ -package server - -import ( - "os" - - "github.com/pterodactyl/wings/server/filesystem" -) - -func (s *Server) Filesystem() *filesystem.Filesystem { - return s.fs -} - -// Ensures that the data directory for the server instance exists. -func (s *Server) EnsureDataDirectoryExists() error { - if _, err := os.Stat(s.fs.Path()); err != nil && !os.IsNotExist(err) { - return err - } else if err != nil { - // Create the server data directory because it does not currently exist - // on the system. - if err := os.MkdirAll(s.fs.Path(), 0700); err != nil { - return err - } - - if err := s.fs.Chown("/"); err != nil { - s.Log().WithField("error", err).Warn("failed to chown server data directory") - } - } - - return nil -} diff --git a/server/server.go b/server/server.go index f7acc9a..0e1fb05 100644 --- a/server/server.go +++ b/server/server.go @@ -3,6 +3,7 @@ package server import ( "context" "fmt" + "os" "strings" "sync" @@ -221,3 +222,27 @@ func (s *Server) ProcessConfiguration() *api.ProcessConfiguration { return s.procConfig } + +// Filesystem returns an instance of the filesystem for this server. +func (s *Server) Filesystem() *filesystem.Filesystem { + return s.fs +} + +// EnsureDataDirectoryExists ensures that the data directory for the server +// instance exists. +func (s *Server) EnsureDataDirectoryExists() error { + if _, err := os.Lstat(s.fs.Path()); err != nil { + if os.IsNotExist(err) { + s.Log().Debug("server: creating root directory and setting permissions") + if err := os.MkdirAll(s.fs.Path(), 0700); err != nil { + return errors.WithStack(err) + } + if err := s.fs.Chown("/"); err != nil { + s.Log().WithField("error", err).Warn("server: failed to chown server data directory") + } + } else { + return errors.WrapIf(err, "server: failed to stat server root directory") + } + } + return nil +} \ No newline at end of file