Code cleanup
This commit is contained in:
parent
c57708d1e0
commit
af241f3de4
|
@ -86,7 +86,7 @@ func rootCmdRun(*cobra.Command, []string) {
|
|||
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)))
|
||||
return
|
||||
}
|
||||
|
|
|
@ -48,8 +48,6 @@ func New(data []byte) (*Installer, error) {
|
|||
},
|
||||
}
|
||||
|
||||
s.Init()
|
||||
|
||||
s.Allocations.DefaultMapping.Ip = getString(data, "allocations", "default", "ip")
|
||||
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
|
||||
// so that everything gets instantiated correctly on the struct.
|
||||
s2, err := server.FromConfiguration(c, &config.Get().System)
|
||||
s2, err := server.FromConfiguration(c)
|
||||
|
||||
return &Installer{
|
||||
server: s2,
|
||||
|
|
|
@ -4,14 +4,13 @@ import "sync"
|
|||
|
||||
type Collection struct {
|
||||
items []*Server
|
||||
mutex *sync.Mutex
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// Create a new collection from a slice of servers.
|
||||
func NewCollection(servers []*Server) *Collection {
|
||||
return &Collection{
|
||||
items: servers,
|
||||
mutex: &sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,16 +21,15 @@ func (c *Collection) All() []*Server {
|
|||
|
||||
// Adds an item to the collection store.
|
||||
func (c *Collection) Add(s *Server) {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
|
||||
c.Lock()
|
||||
c.items = append(c.items, s)
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
// Returns only those items matching the filter criteria.
|
||||
func (c *Collection) Filter(filter func(*Server) bool) []*Server {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
r := make([]*Server, 0)
|
||||
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
|
||||
// found a nil result is returned.
|
||||
func (c *Collection) Find(filter func(*Server) bool) *Server {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
for _, v := range c.items {
|
||||
if filter(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.
|
||||
func (c *Collection) Remove(filter func(*Server) bool) {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
r := make([]*Server, 0)
|
||||
for _, v := range c.items {
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -72,7 +72,7 @@ type Server struct {
|
|||
|
||||
// Internal mutex used to block actions that need to occur sequentially, such as
|
||||
// writing the configuration to the disk.
|
||||
mutex *sync.Mutex
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// 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
|
||||
// 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
|
||||
// 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
|
||||
|
@ -179,7 +174,7 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
|
|||
go func(uuid string, data *api.ServerConfigurationResponse) {
|
||||
defer wg.Done()
|
||||
|
||||
s, err := FromConfiguration(data, cfg)
|
||||
s, err := FromConfiguration(data)
|
||||
if err != nil {
|
||||
zap.S().Errorw("failed to load server, skipping...", zap.String("server", uuid), zap.Error(err))
|
||||
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
|
||||
// given struct using a YAML marshaler. This will also configure the given environment
|
||||
// for a server.
|
||||
func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.SystemConfiguration) (*Server, error) {
|
||||
func FromConfiguration(data *api.ServerConfigurationResponse) (*Server, error) {
|
||||
s := new(Server)
|
||||
|
||||
if err := defaults.Set(s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.Init()
|
||||
|
||||
if err := s.UpdateDataStructure(data.Settings, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -231,7 +224,7 @@ func FromConfiguration(data *api.ServerConfigurationResponse, cfg *config.System
|
|||
Server: s,
|
||||
}
|
||||
s.Filesystem = Filesystem{
|
||||
Configuration: cfg,
|
||||
Configuration: &config.Get().System,
|
||||
Server: s,
|
||||
}
|
||||
s.Resources = ResourceUsage{}
|
||||
|
|
Loading…
Reference in New Issue
Block a user