180 lines
3.9 KiB
Go
180 lines
3.9 KiB
Go
package docker
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
instancemanager "git.acooldomain.co/server-manager/backend/instancemanager"
|
|
"git.acooldomain.co/server-manager/backend/models"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/go-connections/nat"
|
|
)
|
|
|
|
func convertLabelsToFilter(labels any) (*filters.Args, error) {
|
|
args := filters.NewArgs()
|
|
|
|
labelMap, err := convertLabelsToMap(labels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for key, value := range *labelMap {
|
|
args.Add("label", fmt.Sprintf("%s=%s", key, value))
|
|
}
|
|
|
|
return &args, nil
|
|
}
|
|
|
|
func convertLabelsToMap(labels any) (*map[string]string, error) {
|
|
raw, err := json.Marshal(labels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var rawMap map[string]any
|
|
|
|
err = json.Unmarshal(raw, &rawMap)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stringifiedMap := stringifyMap(rawMap)
|
|
|
|
return &stringifiedMap, nil
|
|
}
|
|
|
|
func stringifyMap(m map[string]any) map[string]string {
|
|
stringifiedMap := make(map[string]string)
|
|
for key, value := range m {
|
|
stringifiedMap[key] = fmt.Sprintf("%v", value)
|
|
}
|
|
|
|
return stringifiedMap
|
|
}
|
|
|
|
func convertContainerPortsToPorts(ports []types.Port) []models.Port {
|
|
containerPorts := make([]models.Port, len(ports))
|
|
logger := log.Default()
|
|
for i, port := range ports {
|
|
var portProtocol models.PortProtocol
|
|
switch port.Type {
|
|
case "TCP":
|
|
portProtocol = models.TCP
|
|
case "UDP":
|
|
portProtocol = models.UDP
|
|
default:
|
|
logger.Println(fmt.Sprintf("Unkown Port Protocol %s assuming TCP", port.Type))
|
|
portProtocol = models.TCP
|
|
}
|
|
|
|
containerPorts[i] = models.Port{
|
|
PublicPort: port.PublicPort,
|
|
ContainerPort: port.PrivatePort,
|
|
Protocol: portProtocol,
|
|
}
|
|
}
|
|
|
|
return containerPorts
|
|
}
|
|
|
|
func convertImageStringToModelsImage(image string) models.Image {
|
|
imageSegments := strings.Split(image, ":")
|
|
imageRegistry := imageSegments[0]
|
|
imageTag := imageSegments[1]
|
|
|
|
return models.Image{
|
|
Registry: imageRegistry,
|
|
Tag: imageTag,
|
|
}
|
|
}
|
|
|
|
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
|
|
|
|
rawLabels, err := json.Marshal(labels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = json.Unmarshal(rawLabels, &labels)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &containerLabels, nil
|
|
}
|
|
|
|
func convertVolumeLabelsToStruct(labels map[string]string) (*VolumeLabels, error) {
|
|
var volumeLabels VolumeLabels
|
|
|
|
rawLabels, err := json.Marshal(labels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = json.Unmarshal(rawLabels, &labels)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &volumeLabels, nil
|
|
}
|
|
|
|
func convertImageLabelsToStruct(labels map[string]string) (*ImageLabels, error) {
|
|
var imageLabels ImageLabels
|
|
|
|
rawLabels, err := json.Marshal(labels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = json.Unmarshal(rawLabels, &labels)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &imageLabels, nil
|
|
}
|
|
|
|
func convertImagePortsToPorts(rawPorts nat.PortSet) []instancemanager.Port {
|
|
ports := make([]instancemanager.Port, len(rawPorts))
|
|
i := 0
|
|
for imagePort := range rawPorts {
|
|
portNumber := imagePort.Int()
|
|
var protocol models.PortProtocol
|
|
switch imagePort.Proto() {
|
|
case "TCP":
|
|
protocol = models.TCP
|
|
case "UDP":
|
|
protocol = models.UDP
|
|
default:
|
|
log.Default().Println(fmt.Sprintf("Unknown port protocol %s using TCP", imagePort.Proto()))
|
|
protocol = models.TCP
|
|
}
|
|
ports[i] = instancemanager.Port{
|
|
Number: uint16(portNumber),
|
|
Protocol: protocol,
|
|
}
|
|
i++
|
|
}
|
|
return ports
|
|
}
|