Code cleanup

This commit is contained in:
Dane Everitt 2020-04-10 18:03:35 -07:00
parent c57708d1e0
commit af241f3de4
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 17 additions and 56 deletions

View File

@ -86,7 +86,7 @@ func rootCmdRun(*cobra.Command, []string) {
zap.S().Infow("finished ensuring file permissions") zap.S().Infow("finished ensuring file permissions")
} }
if err := server.LoadDirectory("data/servers", &c.System); err != nil { if err := server.LoadDirectory(); err != nil {
zap.S().Fatalw("failed to load server configurations", zap.Error(errors.WithStack(err))) zap.S().Fatalw("failed to load server configurations", zap.Error(errors.WithStack(err)))
return return
} }

View File

@ -48,8 +48,6 @@ func New(data []byte) (*Installer, error) {
}, },
} }
s.Init()
s.Allocations.DefaultMapping.Ip = getString(data, "allocations", "default", "ip") s.Allocations.DefaultMapping.Ip = getString(data, "allocations", "default", "ip")
s.Allocations.DefaultMapping.Port = int(getInt(data, "allocations", "default", "port")) s.Allocations.DefaultMapping.Port = int(getInt(data, "allocations", "default", "port"))
@ -94,7 +92,7 @@ func New(data []byte) (*Installer, error) {
// Create a new server instance using the configuration we wrote to the disk // Create a new server instance using the configuration we wrote to the disk
// so that everything gets instantiated correctly on the struct. // so that everything gets instantiated correctly on the struct.
s2, err := server.FromConfiguration(c, &config.Get().System) s2, err := server.FromConfiguration(c)
return &Installer{ return &Installer{
server: s2, server: s2,

View File

@ -4,14 +4,13 @@ import "sync"
type Collection struct { type Collection struct {
items []*Server items []*Server
mutex *sync.Mutex sync.RWMutex
} }
// Create a new collection from a slice of servers. // Create a new collection from a slice of servers.
func NewCollection(servers []*Server) *Collection { func NewCollection(servers []*Server) *Collection {
return &Collection{ return &Collection{
items: servers, items: servers,
mutex: &sync.Mutex{},
} }
} }
@ -22,16 +21,15 @@ func (c *Collection) All() []*Server {
// Adds an item to the collection store. // Adds an item to the collection store.
func (c *Collection) Add(s *Server) { func (c *Collection) Add(s *Server) {
c.mutex.Lock() c.Lock()
defer c.mutex.Unlock()
c.items = append(c.items, s) c.items = append(c.items, s)
c.Unlock()
} }
// Returns only those items matching the filter criteria. // Returns only those items matching the filter criteria.
func (c *Collection) Filter(filter func(*Server) bool) []*Server { func (c *Collection) Filter(filter func(*Server) bool) []*Server {
c.mutex.Lock() c.RLock()
defer c.mutex.Unlock() defer c.RUnlock()
r := make([]*Server, 0) r := make([]*Server, 0)
for _, v := range c.items { for _, v := range c.items {
@ -46,6 +44,9 @@ func (c *Collection) Filter(filter func(*Server) bool) []*Server {
// Returns a single element from the collection matching the filter. If nothing is // Returns a single element from the collection matching the filter. If nothing is
// found a nil result is returned. // found a nil result is returned.
func (c *Collection) Find(filter func(*Server) bool) *Server { func (c *Collection) Find(filter func(*Server) bool) *Server {
c.RLock()
defer c.RUnlock()
for _, v := range c.items { for _, v := range c.items {
if filter(v) { if filter(v) {
return v return v
@ -57,8 +58,8 @@ func (c *Collection) Find(filter func(*Server) bool) *Server {
// Removes all items from the collection that match the filter function. // Removes all items from the collection that match the filter function.
func (c *Collection) Remove(filter func(*Server) bool) { func (c *Collection) Remove(filter func(*Server) bool) {
c.mutex.Lock() c.Lock()
defer c.mutex.Unlock() defer c.Unlock()
r := make([]*Server, 0) r := make([]*Server, 0)
for _, v := range c.items { for _, v := range c.items {

View File

@ -1,31 +0,0 @@
package server
import (
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"os"
)
// Writes the server configuration to the disk. The saved configuration will be returned
// back to the calling function to use if desired.
func (s *Server) WriteConfigurationToDisk() ([]byte, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
f, err := os.Create("data/servers/" + s.Uuid + ".yml")
if err != nil {
return nil, errors.WithStack(err)
}
defer f.Close()
b, err := yaml.Marshal(&s)
if err != nil {
return nil, errors.WithStack(err)
}
if _, err := f.Write(b); err != nil {
return nil, errors.WithStack(err)
}
return b, nil
}

View File

@ -72,7 +72,7 @@ type Server struct {
// Internal mutex used to block actions that need to occur sequentially, such as // Internal mutex used to block actions that need to occur sequentially, such as
// writing the configuration to the disk. // writing the configuration to the disk.
mutex *sync.Mutex sync.RWMutex
} }
// 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
@ -139,14 +139,9 @@ type Allocations struct {
Mappings map[string][]int `json:"mappings"` Mappings map[string][]int `json:"mappings"`
} }
// Initializes the default required internal struct components for a Server.
func (s *Server) Init() {
s.mutex = &sync.Mutex{}
}
// Iterates over a given directory and loads all of the servers listed before returning // Iterates over a given directory and loads all of the servers listed before returning
// them to the calling function. // them to the calling function.
func LoadDirectory(dir string, cfg *config.SystemConfiguration) error { func LoadDirectory() error {
// We could theoretically use a standard wait group here, however doing // We could theoretically use a standard wait group here, however doing
// that introduces the potential to crash the program due to too many // that introduces the potential to crash the program due to too many
// open files. This wouldn't happen on a small setup, but once the daemon is // open files. This wouldn't happen on a small setup, but once the daemon is
@ -179,7 +174,7 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
go func(uuid string, data *api.ServerConfigurationResponse) { go func(uuid string, data *api.ServerConfigurationResponse) {
defer wg.Done() defer wg.Done()
s, err := FromConfiguration(data, cfg) s, err := FromConfiguration(data)
if err != nil { if err != nil {
zap.S().Errorw("failed to load server, skipping...", zap.String("server", uuid), zap.Error(err)) zap.S().Errorw("failed to load server, skipping...", zap.String("server", uuid), zap.Error(err))
return return
@ -204,15 +199,13 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
// Initializes a server using a data byte array. This will be marshaled into the // Initializes a server using a data byte array. This will be marshaled into the
// given struct using a YAML marshaler. This will also configure the given environment // given struct using a YAML marshaler. This will also configure the given environment
// for a server. // for a server.
func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.SystemConfiguration) (*Server, error) { func FromConfiguration(data *api.ServerConfigurationResponse) (*Server, error) {
s := new(Server) s := new(Server)
if err := defaults.Set(s); err != nil { if err := defaults.Set(s); err != nil {
return nil, err return nil, err
} }
s.Init()
if err := s.UpdateDataStructure(data.Settings, false); err != nil { if err := s.UpdateDataStructure(data.Settings, false); err != nil {
return nil, err return nil, err
} }
@ -231,7 +224,7 @@ func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.System
Server: s, Server: s,
} }
s.Filesystem = Filesystem{ s.Filesystem = Filesystem{
Configuration: cfg, Configuration: &config.Get().System,
Server: s, Server: s,
} }
s.Resources = ResourceUsage{} s.Resources = ResourceUsage{}