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:
parent
a3fcd7c3e0
commit
4a1682cc96
|
@ -15,7 +15,7 @@ type dockerEnvironment struct {
|
|||
container *docker.Container
|
||||
context context.Context
|
||||
|
||||
server *server
|
||||
server *ServerStruct
|
||||
}
|
||||
|
||||
// Ensure DockerEnvironment implements Environment
|
||||
|
@ -25,7 +25,7 @@ var _ Environment = &dockerEnvironment{}
|
|||
// instance and connects to the docker client on the host system
|
||||
// If the container is already running it will try to reattach
|
||||
// to the running container
|
||||
func NewDockerEnvironment(server *server) (Environment, error) {
|
||||
func NewDockerEnvironment(server *ServerStruct) (Environment, error) {
|
||||
env := dockerEnvironment{}
|
||||
|
||||
env.server = server
|
||||
|
@ -59,7 +59,7 @@ func (env *dockerEnvironment) reattach() error {
|
|||
// Create creates the docker container for the environment and applies all
|
||||
// settings to it
|
||||
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
|
||||
imageParts := strings.Split(env.server.Service().DockerImage, ":")
|
||||
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")
|
||||
err := env.client.PullImage(pullImageOpts, docker.AuthConfiguration{})
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -98,7 +98,7 @@ func (env *dockerEnvironment) Create() error {
|
|||
}
|
||||
container, err := env.client.CreateContainer(createContainerOpts)
|
||||
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
|
||||
}
|
||||
env.server.DockerContainer.ID = container.ID
|
||||
|
@ -109,12 +109,12 @@ func (env *dockerEnvironment) Create() error {
|
|||
|
||||
// Destroy removes the environment's docker container
|
||||
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{
|
||||
ID: env.server.DockerContainer.ID,
|
||||
})
|
||||
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 nil
|
||||
|
@ -122,7 +122,7 @@ func (env *dockerEnvironment) Destroy() error {
|
|||
|
||||
// Start starts the environment's docker container
|
||||
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 {
|
||||
log.WithError(err).Error("Failed to start docker container")
|
||||
return err
|
||||
|
@ -132,7 +132,7 @@ func (env *dockerEnvironment) Start() error {
|
|||
|
||||
// Stop stops the environment's docker container
|
||||
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 {
|
||||
log.WithError(err).Error("Failed to stop docker container")
|
||||
return err
|
||||
|
@ -141,7 +141,7 @@ func (env *dockerEnvironment) Stop() 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{
|
||||
ID: env.container.ID,
|
||||
}); err != nil {
|
||||
|
|
|
@ -8,9 +8,9 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func testServer() *server {
|
||||
return &server{
|
||||
UUID: "testuuid-something-something",
|
||||
func testServer() *ServerStruct {
|
||||
return &ServerStruct{
|
||||
ID: "testuuid-something-something",
|
||||
service: &service{
|
||||
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 {
|
||||
s = testServer()
|
||||
}
|
||||
|
|
|
@ -19,10 +19,10 @@ type Server interface {
|
|||
HasPermission(string, string) bool
|
||||
}
|
||||
|
||||
// Server is a single instance of a Service managed by the panel
|
||||
type server struct {
|
||||
// UUID is the unique identifier of the server
|
||||
UUID string `json:"uuid"`
|
||||
// server is a single instance of a Service managed by the panel
|
||||
type ServerStruct struct {
|
||||
// ID is the unique identifier of the server
|
||||
ID string `json:"uuid"`
|
||||
|
||||
// ServiceName is the name of the service. It is mainly used to allow storing the service
|
||||
// in the config
|
||||
|
@ -73,9 +73,11 @@ type dockerContainer struct {
|
|||
}
|
||||
|
||||
// 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
|
||||
func LoadServerConfigurations(path string) error {
|
||||
|
@ -83,7 +85,7 @@ func LoadServerConfigurations(path string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
servers = make(map[string]*server)
|
||||
servers = make(serversMap)
|
||||
|
||||
for _, file := range serverFiles {
|
||||
if !file.IsDir() {
|
||||
|
@ -91,42 +93,61 @@ func LoadServerConfigurations(path string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
servers[server.UUID] = server
|
||||
servers[server.ID] = server
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadServerConfiguration(path string) (*server, error) {
|
||||
func loadServerConfiguration(path string) (*ServerStruct, error) {
|
||||
file, err := ioutil.ReadFile(path)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
server := &server{}
|
||||
server := &ServerStruct{}
|
||||
if err := json.Unmarshal(file, server); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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
|
||||
func GetServer(uuid string) Server {
|
||||
server := servers[uuid]
|
||||
func GetServer(id string) Server {
|
||||
server := servers[id]
|
||||
if server == nil {
|
||||
return nil // https://golang.org/doc/faq#nil_error
|
||||
}
|
||||
return server
|
||||
}
|
||||
|
||||
// NewServer creates a new Server
|
||||
func NewServer() Server {
|
||||
return new(server)
|
||||
// CreateServer creates a new server
|
||||
func CreateServer(server *ServerStruct) (Server, error) {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -136,21 +157,21 @@ func (s *server) Start() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *server) Stop() error {
|
||||
func (s *ServerStruct) Stop() error {
|
||||
/*if err := s.Environment().Stop(); err != nil {
|
||||
return err
|
||||
}*/
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *server) Exec(command string) error {
|
||||
func (s *ServerStruct) Exec(command string) error {
|
||||
/*if err := s.Environment().Exec(command); err != nil {
|
||||
return err
|
||||
}*/
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *server) Rebuild() error {
|
||||
func (s *ServerStruct) Rebuild() error {
|
||||
/*if err := s.Environment().ReCreate(); err != nil {
|
||||
return err
|
||||
}*/
|
||||
|
@ -158,7 +179,7 @@ func (s *server) Rebuild() error {
|
|||
}
|
||||
|
||||
// Service returns the server's service configuration
|
||||
func (s *server) Service() *service {
|
||||
func (s *ServerStruct) Service() *service {
|
||||
if s.service == nil {
|
||||
// TODO: Properly use the correct service, mock for now.
|
||||
s.service = &service{
|
||||
|
@ -170,12 +191,12 @@ func (s *server) Service() *service {
|
|||
}
|
||||
|
||||
// UUIDShort returns the first block of the UUID
|
||||
func (s *server) UUIDShort() string {
|
||||
return s.UUID[0:strings.Index(s.UUID, "-")]
|
||||
func (s *ServerStruct) UUIDShort() string {
|
||||
return s.ID[0:strings.Index(s.ID, "-")]
|
||||
}
|
||||
|
||||
// Environment returns the servers environment
|
||||
func (s *server) Environment() (Environment, error) {
|
||||
func (s *ServerStruct) Environment() (Environment, error) {
|
||||
var err error
|
||||
if s.environment == nil {
|
||||
switch s.Service().EnvironmentName {
|
||||
|
@ -190,7 +211,7 @@ func (s *server) Environment() (Environment, error) {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
if key == token {
|
||||
for _, perm := range perms {
|
||||
|
@ -203,3 +224,7 @@ func (s *server) HasPermission(token string, permission string) bool {
|
|||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *ServerStruct) save() {
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user