Implement server deletion

This commit is contained in:
Dane Everitt
2019-12-21 23:23:56 -08:00
parent 038436c781
commit 0866b84d8e
5 changed files with 100 additions and 4 deletions

View File

@@ -1,7 +1,18 @@
package server
import "sync"
type Collection struct {
items []*Server
mutex *sync.Mutex
}
// Create a new collection from a slice of servers.
func NewCollection(servers []*Server) *Collection {
return &Collection{
items: servers,
mutex: &sync.Mutex{},
}
}
// Return all of the items in the collection.
@@ -11,11 +22,17 @@ 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.items = append(c.items, s)
}
// 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()
defer c.mutex.Unlock()
r := make([]*Server, 0)
for _, v := range c.items {
if filter(v) {
@@ -28,7 +45,7 @@ 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 {
func (c *Collection) Find(filter func(*Server) bool) *Server {
for _, v := range c.items {
if filter(v) {
return v
@@ -36,4 +53,19 @@ func (c *Collection) Find(filter func (*Server) bool) *Server {
}
return nil
}
}
// 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()
r := make([]*Server, 0)
for _, v := range c.items {
if !filter(v) {
r = append(r, v)
}
}
c.items = r
}

View File

@@ -40,6 +40,10 @@ type Environment interface {
// is not running no error should be returned.
Terminate(signal os.Signal) error
// Destroys the environment removing any containers that were created (in Docker
// environments at least).
Destroy() error
// Returns the exit state of the process. The first result is the exit code, the second
// determines if the process was killed by the system OOM killer.
ExitState() (uint32, bool, error)

View File

@@ -325,6 +325,18 @@ func (d *DockerEnvironment) Terminate(signal os.Signal) error {
)
}
// Remove the Docker container from the machine. If the container is currently running
// it will be forcibly stopped by Docker.
func (d *DockerEnvironment) Destroy() error {
ctx := context.Background()
return d.Client.ContainerRemove(ctx, d.Server.Uuid, types.ContainerRemoveOptions{
RemoveVolumes: true,
RemoveLinks: false,
Force: true,
})
}
// Determine the container exit state and return the exit code and wether or not
// the container was killed by the OOM killer.
func (d *DockerEnvironment) ExitState() (uint32, bool, error) {

View File

@@ -158,7 +158,7 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
return err
}
servers = new(Collection)
servers = NewCollection(nil)
for _, file := range f {
if !strings.HasSuffix(file.Name(), ".yml") || file.IsDir() {