Cleanup server stuff since that was getting unweildy

This commit is contained in:
Dane Everitt 2019-04-20 16:26:55 -07:00
parent 870adffc14
commit 49ca2e2404
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
6 changed files with 27 additions and 48 deletions

10
http.go
View File

@ -162,19 +162,19 @@ func (rt *Router) routeServerPower(w http.ResponseWriter, r *http.Request, ps ht
go func(a string, s *server.Server) { go func(a string, s *server.Server) {
switch a { switch a {
case "start": case "start":
if err := s.Environment().Start(); err != nil { if err := s.Environment.Start(); err != nil {
zap.S().Error(err, zap.String("server", s.Uuid), zap.String("action", "start")) zap.S().Error(err, zap.String("server", s.Uuid), zap.String("action", "start"))
} }
break break
case "stop": case "stop":
if err := s.Environment().Stop(); err != nil { if err := s.Environment.Stop(); err != nil {
zap.S().Error(err, zap.String("server", s.Uuid), zap.String("action", "stop")) zap.S().Error(err, zap.String("server", s.Uuid), zap.String("action", "stop"))
} }
break break
case "restart": case "restart":
break break
case "kill": case "kill":
if err := s.Environment().Terminate(os.Kill); err != nil { if err := s.Environment.Terminate(os.Kill); err != nil {
zap.S().Error(err, zap.String("server", s.Uuid), zap.String("action", "kill")) zap.S().Error(err, zap.String("server", s.Uuid), zap.String("action", "kill"))
} }
} }
@ -206,7 +206,7 @@ func (rt *Router) routeServerLogs(w http.ResponseWriter, r *http.Request, ps htt
func (rt *Router) routeServerFileRead(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (rt *Router) routeServerFileRead(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
s := rt.Servers.Get(ps.ByName("server")) s := rt.Servers.Get(ps.ByName("server"))
cleaned, err := s.Filesystem().SafePath(ps.ByName("path")) cleaned, err := s.Filesystem.SafePath(ps.ByName("path"))
if err != nil { if err != nil {
http.Error(w, "could not determine path", http.StatusInternalServerError) http.Error(w, "could not determine path", http.StatusInternalServerError)
return return
@ -248,7 +248,7 @@ func (rt *Router) routeServerFileRead(w http.ResponseWriter, r *http.Request, ps
func (rt *Router) routeServerFileList(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (rt *Router) routeServerFileList(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
s := rt.Servers.Get(ps.ByName("server")) s := rt.Servers.Get(ps.ByName("server"))
stats, err := s.Filesystem().ListDirectory(ps.ByName("path")) stats, err := s.Filesystem.ListDirectory(ps.ByName("path"))
if os.IsNotExist(err) { if os.IsNotExist(err) {
http.NotFound(w, r) http.NotFound(w, r)
return return

View File

@ -288,7 +288,7 @@ func (d *DockerEnvironment) Create() error {
Mounts: []mount.Mount{ Mounts: []mount.Mount{
{ {
Target: "/home/container", Target: "/home/container",
Source: d.Server.Filesystem().Path(), Source: d.Server.Filesystem.Path(),
Type: mount.TypeBind, Type: mount.TypeBind,
ReadOnly: false, ReadOnly: false,
}, },

View File

@ -112,7 +112,7 @@ func (fs *Filesystem) HasSpaceAvailable() bool {
} }
var size int64 var size int64
if x, exists := fs.Server.Cache().Get("disk_used"); exists { if x, exists := fs.Server.Cache.Get("disk_used"); exists {
size = x.(int64) size = x.(int64)
} }
@ -124,7 +124,7 @@ func (fs *Filesystem) HasSpaceAvailable() bool {
if size, err := fs.DirectorySize("/"); err != nil { if size, err := fs.DirectorySize("/"); err != nil {
zap.S().Warnw("failed to determine directory size", zap.String("server", fs.Server.Uuid), zap.Error(err)) zap.S().Warnw("failed to determine directory size", zap.String("server", fs.Server.Uuid), zap.Error(err))
} else { } else {
fs.Server.Cache().Set("disk_used", size, time.Minute*5) fs.Server.Cache.Set("disk_used", size, time.Minute*5)
} }
} }

View File

@ -54,15 +54,15 @@ type Server struct {
Image string `json:"image,omitempty"` Image string `json:"image,omitempty"`
} `json:"container,omitempty"` } `json:"container,omitempty"`
environment Environment Environment Environment `json:"-"`
fs *Filesystem Filesystem *Filesystem `json:"-"`
// Server cache used to store frequently requested information in memory and make // Server cache used to store frequently requested information in memory and make
// certain long operations return faster. For example, FS disk space usage. // certain long operations return faster. For example, FS disk space usage.
cache *cache.Cache Cache *cache.Cache `json:"-"`
emitter *emitter.Emitter Emitter *emitter.Emitter `json:"-"`
} }
// The build settings for a given server that impact docker container creation and // The build settings for a given server that impact docker container creation and
@ -206,9 +206,10 @@ func FromConfiguration(data []byte, cfg *config.SystemConfiguration) (*Server, e
return nil, err return nil, err
} }
s.environment = env s.Emitter = &emitter.Emitter{}
s.cache = cache.New(time.Minute * 10, time.Minute * 15) s.Environment = env
s.fs = &Filesystem{ s.Cache = cache.New(time.Minute * 10, time.Minute * 15)
s.Filesystem = &Filesystem{
Root: cfg.Data, Root: cfg.Data,
Server: s, Server: s,
} }
@ -218,19 +219,7 @@ func FromConfiguration(data []byte, cfg *config.SystemConfiguration) (*Server, e
// Reads the log file for a server up to a specified number of bytes. // Reads the log file for a server up to a specified number of bytes.
func (s *Server) ReadLogfile(len int64) ([]string, error) { func (s *Server) ReadLogfile(len int64) ([]string, error) {
return s.Environment().Readlog(len) return s.Environment.Readlog(len)
}
func (s *Server) Environment() Environment {
return s.environment
}
func (s *Server) Filesystem() *Filesystem {
return s.fs
}
func (s *Server) Cache() *cache.Cache {
return s.cache
} }
// Sets the state of the server process. // Sets the state of the server process.
@ -252,7 +241,7 @@ func (s *Server) SetState(state ProcessState) error {
// Determine if the server is bootable in it's current state or not. This will not // Determine if the server is bootable in it's current state or not. This will not
// indicate why a server is not bootable, only if it is. // indicate why a server is not bootable, only if it is.
func (s *Server) IsBootable() bool { func (s *Server) IsBootable() bool {
exists, _ := s.environment.Exists() exists, _ := s.Environment.Exists()
return exists return exists
} }
@ -260,15 +249,5 @@ func (s *Server) IsBootable() bool {
// Initalizes a server instance. This will run through and ensure that the environment // Initalizes a server instance. This will run through and ensure that the environment
// for the server is setup, and that all of the necessary files are created. // for the server is setup, and that all of the necessary files are created.
func (s *Server) CreateEnvironment() error { func (s *Server) CreateEnvironment() error {
return s.environment.Create() return s.Environment.Create()
}
// Returns the server event emitter instance. If one has not been setup yet, a new instance
// will be created.
func (s *Server) Emitter() *emitter.Emitter {
if s.emitter == nil {
s.emitter = &emitter.Emitter{}
}
return s.emitter
} }

View File

@ -78,13 +78,13 @@ func (wsm *WebsocketMessage) HandleInbound(c *websocket.Conn) error {
var err error var err error
switch strings.Join(wsm.Args, "") { switch strings.Join(wsm.Args, "") {
case "start": case "start":
err = wsm.server.Environment().Start() err = wsm.server.Environment.Start()
break break
case "stop": case "stop":
err = wsm.server.Environment().Stop() err = wsm.server.Environment.Stop()
break break
case "restart": case "restart":
err = wsm.server.Environment().Terminate(os.Kill) err = wsm.server.Environment.Terminate(os.Kill)
break break
} }
@ -94,7 +94,7 @@ func (wsm *WebsocketMessage) HandleInbound(c *websocket.Conn) error {
} }
case "send logs": case "send logs":
{ {
logs, err := wsm.server.Environment().Readlog(1024 * 5) logs, err := wsm.server.Environment.Readlog(1024 * 5)
if err != nil { if err != nil {
return err return err
} }
@ -110,7 +110,7 @@ func (wsm *WebsocketMessage) HandleInbound(c *websocket.Conn) error {
} }
case "send command": case "send command":
{ {
return wsm.server.Environment().SendCommand(strings.Join(wsm.Args, "")) return wsm.server.Environment.SendCommand(strings.Join(wsm.Args, ""))
} }
} }

View File

@ -64,15 +64,15 @@ func main() {
zap.S().Infow("loaded configuration for server", zap.String("server", s.Uuid)) zap.S().Infow("loaded configuration for server", zap.String("server", s.Uuid))
zap.S().Infow("ensuring envrionment exists", zap.String("server", s.Uuid)) zap.S().Infow("ensuring envrionment exists", zap.String("server", s.Uuid))
if err := s.CreateEnvironment(); err != nil { if err := s.Environment.Create(); err != nil {
zap.S().Errorw("failed to create an environment for server", zap.String("server", s.Uuid), zap.Error(err)) zap.S().Errorw("failed to create an environment for server", zap.String("server", s.Uuid), zap.Error(err))
} }
if r, err := s.Environment().IsRunning(); err != nil { if r, err := s.Environment.IsRunning(); err != nil {
zap.S().Errorw("error checking server environment status", zap.String("server", s.Uuid), zap.Error(err)) zap.S().Errorw("error checking server environment status", zap.String("server", s.Uuid), zap.Error(err))
} else if r { } else if r {
zap.S().Infow("detected server is running, re-attaching to process", zap.String("server", s.Uuid)) zap.S().Infow("detected server is running, re-attaching to process", zap.String("server", s.Uuid))
if err := s.Environment().Attach(); err != nil { if err := s.Environment.Attach(); err != nil {
zap.S().Errorw("error attaching to server environment", zap.String("server", s.Uuid), zap.Error(err)) zap.S().Errorw("error attaching to server environment", zap.String("server", s.Uuid), zap.Error(err))
} }
} }