added basic servers implementation
This commit is contained in:
@@ -4,10 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
instancemanager "git.acooldomain.co/server-manager/backend-kubernetes-go/instance_manager"
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/models"
|
||||
@@ -63,6 +60,22 @@ func (self *InstanceManager) getVolume(ctx context.Context, serverId string) (*v
|
||||
|
||||
// General
|
||||
// Read Only
|
||||
func (self *InstanceManager) GetImage(ctx context.Context, imageId string) (*instancemanager.Image, error) {
|
||||
imageInspect, _, err := self.client.ImageInspectWithRaw(ctx, imageId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if imageInspect.Config.Labels["type"] != "game" {
|
||||
return nil, fmt.Errorf("Image not found")
|
||||
}
|
||||
|
||||
image := convertImageInspectToInstanceImage(imageInspect)
|
||||
|
||||
return &image, nil
|
||||
|
||||
}
|
||||
|
||||
func (self *InstanceManager) ListImages(ctx context.Context) ([]instancemanager.Image, error) {
|
||||
imageFilters, err := convertLabelsToFilter(ImageLabels{Type: Game})
|
||||
if err != nil {
|
||||
@@ -81,14 +94,7 @@ func (self *InstanceManager) ListImages(ctx context.Context) ([]instancemanager.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ports := convertImagePortsToPorts(imageInspect.Config.ExposedPorts)
|
||||
imageId := convertContainerImageToImage(rawImage.RepoTags[0])
|
||||
images[i] = instancemanager.Image{
|
||||
Registry: imageId.Registry,
|
||||
Tag: imageId.Tag,
|
||||
Ports: ports,
|
||||
Command: strings.Join(imageInspect.Config.Cmd, " "),
|
||||
}
|
||||
images[i] = convertImageInspectToInstanceImage(imageInspect)
|
||||
}
|
||||
|
||||
return images, nil
|
||||
@@ -117,6 +123,7 @@ func (self *InstanceManager) GetServer(ctx context.Context, serverId string) (*i
|
||||
RunningCommand: "",
|
||||
RunningImage: nil,
|
||||
Ports: nil,
|
||||
Domain: self.config.GamesDomain,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -125,7 +132,7 @@ func (self *InstanceManager) GetServer(ctx context.Context, serverId string) (*i
|
||||
running := serverContainer.State == "running"
|
||||
|
||||
runningCommand := serverContainer.Command
|
||||
image := convertContainerImageToImage(serverContainer.Image)
|
||||
image := convertImageStringToModelsImage(serverContainer.Image)
|
||||
|
||||
return &instancemanager.Server{
|
||||
Id: volume.Name,
|
||||
@@ -133,6 +140,7 @@ func (self *InstanceManager) GetServer(ctx context.Context, serverId string) (*i
|
||||
RunningCommand: runningCommand,
|
||||
Ports: convertContainerPortsToPorts(serverContainer.Ports),
|
||||
RunningImage: &image,
|
||||
Domain: self.config.GamesDomain,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -156,6 +164,7 @@ func (self *InstanceManager) ListServers(ctx context.Context) ([]instancemanager
|
||||
RunningCommand: "",
|
||||
Ports: nil,
|
||||
RunningImage: nil,
|
||||
Domain: self.config.GamesDomain,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +186,7 @@ func (self *InstanceManager) ListServers(ctx context.Context) ([]instancemanager
|
||||
continue
|
||||
}
|
||||
|
||||
image := convertContainerImageToImage(container.Image)
|
||||
image := convertImageStringToModelsImage(container.Image)
|
||||
|
||||
serverStatus[containerLabels.VolumeId].Ports = convertContainerPortsToPorts(container.Ports)
|
||||
serverStatus[containerLabels.VolumeId].Running = true
|
||||
@@ -197,7 +206,12 @@ func (self *InstanceManager) ListServers(ctx context.Context) ([]instancemanager
|
||||
}
|
||||
|
||||
// State Changing
|
||||
func (self *InstanceManager) StartServer(ctx context.Context, serverId string, image instancemanager.Image, command string, ports []models.Port) error {
|
||||
func (self *InstanceManager) StartServer(ctx context.Context,
|
||||
serverId string,
|
||||
imageId string,
|
||||
command string,
|
||||
ports []models.Port,
|
||||
) error {
|
||||
server, err := self.GetServer(ctx, serverId)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -207,7 +221,6 @@ func (self *InstanceManager) StartServer(ctx context.Context, serverId string, i
|
||||
return fmt.Errorf("Server %s already running", serverId)
|
||||
}
|
||||
|
||||
imageId := image.Registry + ":" + image.Tag
|
||||
containerLabels := ContainerLabels{
|
||||
VolumeId: server.Id,
|
||||
Type: "GAME",
|
||||
@@ -216,6 +229,10 @@ func (self *InstanceManager) StartServer(ctx context.Context, serverId string, i
|
||||
volumes := make(map[string]struct{})
|
||||
|
||||
var portMapping nat.PortMap = make(nat.PortMap)
|
||||
image, err := self.GetImage(ctx, imageId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(ports) == 0 {
|
||||
for _, port := range image.Ports {
|
||||
@@ -232,8 +249,6 @@ func (self *InstanceManager) StartServer(ctx context.Context, serverId string, i
|
||||
}
|
||||
}
|
||||
|
||||
rawImage, _, err := self.client.ImageInspectWithRaw(ctx, imageId)
|
||||
|
||||
if command == "" {
|
||||
command = image.Command
|
||||
}
|
||||
@@ -274,7 +289,7 @@ func (self *InstanceManager) StartServer(ctx context.Context, serverId string, i
|
||||
},
|
||||
&container.HostConfig{
|
||||
AutoRemove: true,
|
||||
Mounts: []mount.Mount{{Source: server.Id, Target: rawImage.Config.WorkingDir, Type: "volume"}},
|
||||
Mounts: []mount.Mount{{Source: server.Id, Target: image.WorkingDir, Type: "volume"}},
|
||||
PortBindings: portMapping,
|
||||
ConsoleSize: [2]uint{1000, 1000},
|
||||
},
|
||||
@@ -328,6 +343,7 @@ func (self *InstanceManager) CreateServer(ctx context.Context) (*instancemanager
|
||||
Running: false,
|
||||
RunningImage: nil,
|
||||
RunningCommand: "",
|
||||
Domain: self.config.GamesDomain,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
instancemanager "git.acooldomain.co/server-manager/backend-kubernetes-go/instance_manager"
|
||||
@@ -21,6 +20,7 @@ func convertLabelsToFilter(labels any) (*filters.Args, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range *labelMap {
|
||||
args.Add("label", fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
@@ -29,7 +29,6 @@ func convertLabelsToFilter(labels any) (*filters.Args, error) {
|
||||
}
|
||||
|
||||
func convertLabelsToMap(labels any) (*map[string]string, error) {
|
||||
|
||||
raw, err := json.Marshal(labels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -80,7 +79,7 @@ func convertContainerPortsToPorts(ports []types.Port) []models.Port {
|
||||
return containerPorts
|
||||
}
|
||||
|
||||
func convertContainerImageToImage(image string) models.Image {
|
||||
func convertImageStringToModelsImage(image string) models.Image {
|
||||
imageSegments := strings.Split(image, ":")
|
||||
imageRegistry := imageSegments[0]
|
||||
imageTag := imageSegments[1]
|
||||
@@ -91,6 +90,20 @@ func convertContainerImageToImage(image string) models.Image {
|
||||
}
|
||||
}
|
||||
|
||||
func convertImageInspectToInstanceImage(image types.ImageInspect) instancemanager.Image {
|
||||
modelsImage := convertImageStringToModelsImage(image.RepoTags[0])
|
||||
|
||||
ports := convertImagePortsToPorts(image.Config.ExposedPorts)
|
||||
|
||||
return instancemanager.Image{
|
||||
Registry: modelsImage.Registry,
|
||||
Tag: modelsImage.Tag,
|
||||
Command: strings.Join(image.Config.Cmd, " "),
|
||||
Ports: ports,
|
||||
WorkingDir: image.Config.WorkingDir,
|
||||
}
|
||||
}
|
||||
|
||||
func convertContainerLabelsToStruct(labels map[string]string) (*ContainerLabels, error) {
|
||||
var containerLabels ContainerLabels
|
||||
|
||||
@@ -104,6 +117,7 @@ func convertContainerLabelsToStruct(labels map[string]string) (*ContainerLabels,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &containerLabels, nil
|
||||
}
|
||||
|
||||
@@ -120,6 +134,7 @@ func convertVolumeLabelsToStruct(labels map[string]string) (*VolumeLabels, error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &volumeLabels, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user