control: rename server to ServerStruct as it's needed in the api

control: add getServers() function to get a slice of all servers
This commit is contained in:
Jakob Schrettenbrunner 2017-09-01 00:01:32 +02:00
parent a3fcd7c3e0
commit 4a1682cc96
3 changed files with 63 additions and 38 deletions

View File

@ -15,7 +15,7 @@ type dockerEnvironment struct {
container *docker.Container container *docker.Container
context context.Context context context.Context
server *server server *ServerStruct
} }
// Ensure DockerEnvironment implements Environment // Ensure DockerEnvironment implements Environment
@ -25,7 +25,7 @@ var _ Environment = &dockerEnvironment{}
// instance and connects to the docker client on the host system // instance and connects to the docker client on the host system
// If the container is already running it will try to reattach // If the container is already running it will try to reattach
// to the running container // to the running container
func NewDockerEnvironment(server *server) (Environment, error) { func NewDockerEnvironment(server *ServerStruct) (Environment, error) {
env := dockerEnvironment{} env := dockerEnvironment{}
env.server = server env.server = server
@ -59,7 +59,7 @@ func (env *dockerEnvironment) reattach() error {
// Create creates the docker container for the environment and applies all // Create creates the docker container for the environment and applies all
// settings to it // settings to it
func (env *dockerEnvironment) Create() error { func (env *dockerEnvironment) Create() error {
log.WithField("serverID", env.server.UUID).Debug("Creating docker environment") log.WithField("serverID", env.server.ID).Debug("Creating docker environment")
// Split image repository and tag to feed it to the library // Split image repository and tag to feed it to the library
imageParts := strings.Split(env.server.Service().DockerImage, ":") imageParts := strings.Split(env.server.Service().DockerImage, ":")
imageRepoParts := strings.Split(imageParts[0], "/") imageRepoParts := strings.Split(imageParts[0], "/")
@ -77,7 +77,7 @@ func (env *dockerEnvironment) Create() error {
log.WithField("image", env.server.service.DockerImage).Debug("Pulling docker image") log.WithField("image", env.server.service.DockerImage).Debug("Pulling docker image")
err := env.client.PullImage(pullImageOpts, docker.AuthConfiguration{}) err := env.client.PullImage(pullImageOpts, docker.AuthConfiguration{})
if err != nil { if err != nil {
log.WithError(err).WithField("serverID", env.server.UUID).Error("Failed to create docker environment") log.WithError(err).WithField("serverID", env.server.ID).Error("Failed to create docker environment")
return err return err
} }
@ -98,7 +98,7 @@ func (env *dockerEnvironment) Create() error {
} }
container, err := env.client.CreateContainer(createContainerOpts) container, err := env.client.CreateContainer(createContainerOpts)
if err != nil { if err != nil {
log.WithError(err).WithField("serverID", env.server.UUID).Error("Failed to create docker container") log.WithError(err).WithField("serverID", env.server.ID).Error("Failed to create docker container")
return err return err
} }
env.server.DockerContainer.ID = container.ID env.server.DockerContainer.ID = container.ID
@ -109,12 +109,12 @@ func (env *dockerEnvironment) Create() error {
// Destroy removes the environment's docker container // Destroy removes the environment's docker container
func (env *dockerEnvironment) Destroy() error { func (env *dockerEnvironment) Destroy() error {
log.WithField("serverID", env.server.UUID).Debug("Destroying docker environment") log.WithField("serverID", env.server.ID).Debug("Destroying docker environment")
err := env.client.RemoveContainer(docker.RemoveContainerOptions{ err := env.client.RemoveContainer(docker.RemoveContainerOptions{
ID: env.server.DockerContainer.ID, ID: env.server.DockerContainer.ID,
}) })
if err != nil { if err != nil {
log.WithError(err).WithField("serverID", env.server.UUID).Error("Failed to destroy docker environment") log.WithError(err).WithField("serverID", env.server.ID).Error("Failed to destroy docker environment")
return err return err
} }
return nil return nil
@ -122,7 +122,7 @@ func (env *dockerEnvironment) Destroy() error {
// Start starts the environment's docker container // Start starts the environment's docker container
func (env *dockerEnvironment) Start() error { func (env *dockerEnvironment) Start() error {
log.WithField("serverID", env.server.UUID).Debug("Starting service in docker environment") log.WithField("serverID", env.server.ID).Debug("Starting service in docker environment")
if err := env.client.StartContainer(env.container.ID, nil); err != nil { if err := env.client.StartContainer(env.container.ID, nil); err != nil {
log.WithError(err).Error("Failed to start docker container") log.WithError(err).Error("Failed to start docker container")
return err return err
@ -132,7 +132,7 @@ func (env *dockerEnvironment) Start() error {
// Stop stops the environment's docker container // Stop stops the environment's docker container
func (env *dockerEnvironment) Stop() error { func (env *dockerEnvironment) Stop() error {
log.WithField("serverID", env.server.UUID).Debug("Stopping service in docker environment") log.WithField("serverID", env.server.ID).Debug("Stopping service in docker environment")
if err := env.client.StopContainer(env.container.ID, 20000); err != nil { if err := env.client.StopContainer(env.container.ID, 20000); err != nil {
log.WithError(err).Error("Failed to stop docker container") log.WithError(err).Error("Failed to stop docker container")
return err return err
@ -141,7 +141,7 @@ func (env *dockerEnvironment) Stop() error {
} }
func (env *dockerEnvironment) Kill() error { func (env *dockerEnvironment) Kill() error {
log.WithField("serverID", env.server.UUID).Debug("Killing service in docker environment") log.WithField("serverID", env.server.ID).Debug("Killing service in docker environment")
if err := env.client.KillContainer(docker.KillContainerOptions{ if err := env.client.KillContainer(docker.KillContainerOptions{
ID: env.container.ID, ID: env.container.ID,
}); err != nil { }); err != nil {

View File

@ -8,9 +8,9 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func testServer() *server { func testServer() *ServerStruct {
return &server{ return &ServerStruct{
UUID: "testuuid-something-something", ID: "testuuid-something-something",
service: &service{ service: &service{
DockerImage: "alpine:latest", DockerImage: "alpine:latest",
}, },
@ -124,7 +124,7 @@ func TestExecDockerEnvironment(t *testing.T) {
} }
func createTestDockerEnv(s *server) (*dockerEnvironment, error) { func createTestDockerEnv(s *ServerStruct) (*dockerEnvironment, error) {
if s == nil { if s == nil {
s = testServer() s = testServer()
} }

View File

@ -19,10 +19,10 @@ type Server interface {
HasPermission(string, string) bool HasPermission(string, string) bool
} }
// Server is a single instance of a Service managed by the panel // server is a single instance of a Service managed by the panel
type server struct { type ServerStruct struct {
// UUID is the unique identifier of the server // ID is the unique identifier of the server
UUID string `json:"uuid"` ID string `json:"uuid"`
// ServiceName is the name of the service. It is mainly used to allow storing the service // ServiceName is the name of the service. It is mainly used to allow storing the service
// in the config // in the config
@ -73,9 +73,11 @@ type dockerContainer struct {
} }
// ensure server implements Server // ensure server implements Server
var _ Server = &server{} var _ Server = &ServerStruct{}
var servers map[string]*server type serversMap map[string]*ServerStruct
var servers = make(serversMap)
// LoadServerConfigurations loads the configured servers from a specified path // LoadServerConfigurations loads the configured servers from a specified path
func LoadServerConfigurations(path string) error { func LoadServerConfigurations(path string) error {
@ -83,7 +85,7 @@ func LoadServerConfigurations(path string) error {
if err != nil { if err != nil {
return err return err
} }
servers = make(map[string]*server) servers = make(serversMap)
for _, file := range serverFiles { for _, file := range serverFiles {
if !file.IsDir() { if !file.IsDir() {
@ -91,42 +93,61 @@ func LoadServerConfigurations(path string) error {
if err != nil { if err != nil {
return err return err
} }
servers[server.UUID] = server servers[server.ID] = server
} }
} }
return nil return nil
} }
func loadServerConfiguration(path string) (*server, error) { func loadServerConfiguration(path string) (*ServerStruct, error) {
file, err := ioutil.ReadFile(path) file, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
server := &server{} server := &ServerStruct{}
if err := json.Unmarshal(file, server); err != nil { if err := json.Unmarshal(file, server); err != nil {
return nil, err return nil, err
} }
return server, nil return server, nil
} }
// GetServers returns an array of all servers the daemon manages
func GetServers() []Server {
serverArray := make([]Server, len(servers))
i := 0
for _, s := range servers {
serverArray[i] = s
i++
}
return serverArray
}
// GetServer returns the server identified by the provided uuid // GetServer returns the server identified by the provided uuid
func GetServer(uuid string) Server { func GetServer(id string) Server {
server := servers[uuid] server := servers[id]
if server == nil { if server == nil {
return nil // https://golang.org/doc/faq#nil_error return nil // https://golang.org/doc/faq#nil_error
} }
return server return server
} }
// NewServer creates a new Server // CreateServer creates a new server
func NewServer() Server { func CreateServer(server *ServerStruct) (Server, error) {
return new(server) servers[server.ID] = server
return server, nil
} }
func (s *server) Start() error { // DeleteServer deletes a server and all related files
// NOTE: This is not reversible.
func DeleteServer(uuid string) error {
delete(servers, uuid)
return nil
}
func (s *ServerStruct) Start() error {
/*if err := s.Environment().Create(); err != nil { /*if err := s.Environment().Create(); err != nil {
return err return err
} }
@ -136,21 +157,21 @@ func (s *server) Start() error {
return nil return nil
} }
func (s *server) Stop() error { func (s *ServerStruct) Stop() error {
/*if err := s.Environment().Stop(); err != nil { /*if err := s.Environment().Stop(); err != nil {
return err return err
}*/ }*/
return nil return nil
} }
func (s *server) Exec(command string) error { func (s *ServerStruct) Exec(command string) error {
/*if err := s.Environment().Exec(command); err != nil { /*if err := s.Environment().Exec(command); err != nil {
return err return err
}*/ }*/
return nil return nil
} }
func (s *server) Rebuild() error { func (s *ServerStruct) Rebuild() error {
/*if err := s.Environment().ReCreate(); err != nil { /*if err := s.Environment().ReCreate(); err != nil {
return err return err
}*/ }*/
@ -158,7 +179,7 @@ func (s *server) Rebuild() error {
} }
// Service returns the server's service configuration // Service returns the server's service configuration
func (s *server) Service() *service { func (s *ServerStruct) Service() *service {
if s.service == nil { if s.service == nil {
// TODO: Properly use the correct service, mock for now. // TODO: Properly use the correct service, mock for now.
s.service = &service{ s.service = &service{
@ -170,12 +191,12 @@ func (s *server) Service() *service {
} }
// UUIDShort returns the first block of the UUID // UUIDShort returns the first block of the UUID
func (s *server) UUIDShort() string { func (s *ServerStruct) UUIDShort() string {
return s.UUID[0:strings.Index(s.UUID, "-")] return s.ID[0:strings.Index(s.ID, "-")]
} }
// Environment returns the servers environment // Environment returns the servers environment
func (s *server) Environment() (Environment, error) { func (s *ServerStruct) Environment() (Environment, error) {
var err error var err error
if s.environment == nil { if s.environment == nil {
switch s.Service().EnvironmentName { switch s.Service().EnvironmentName {
@ -190,7 +211,7 @@ func (s *server) Environment() (Environment, error) {
} }
// HasPermission checks wether a provided token has a specific permission // HasPermission checks wether a provided token has a specific permission
func (s *server) HasPermission(token string, permission string) bool { func (s *ServerStruct) HasPermission(token string, permission string) bool {
for key, perms := range s.Keys { for key, perms := range s.Keys {
if key == token { if key == token {
for _, perm := range perms { for _, perm := range perms {
@ -203,3 +224,7 @@ func (s *server) HasPermission(token string, permission string) bool {
} }
return false return false
} }
func (s *ServerStruct) save() {
}