mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-02 22:51:34 -06:00
add export as docker-compose.yaml.
This commit is contained in:
@@ -59,4 +59,6 @@ func (manager *ApiManagerCtx) Mount(r chi.Router) {
|
||||
r.Post("/restart", manager.roomGenericAction(manager.rooms.Restart))
|
||||
r.Post("/recreate", manager.roomRecreate)
|
||||
})
|
||||
|
||||
r.Get("/docker-compose.yaml", manager.dockerCompose)
|
||||
}
|
||||
|
||||
@@ -186,3 +186,14 @@ func (manager *ApiManagerCtx) roomGenericAction(action func(id string) error) fu
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func (manager *ApiManagerCtx) dockerCompose(w http.ResponseWriter, r *http.Request) {
|
||||
response, err := manager.rooms.ExportAsDockerCompose()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/yaml")
|
||||
w.Write(response)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/m1k1o/neko-rooms/internal/config"
|
||||
"github.com/m1k1o/neko-rooms/internal/policies"
|
||||
@@ -82,6 +83,151 @@ func (manager *RoomManagerCtx) List(labels map[string]string) ([]types.RoomEntry
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (manager *RoomManagerCtx) ExportAsDockerCompose() ([]byte, error) {
|
||||
services := map[string]any{}
|
||||
|
||||
dockerCompose := map[string]any{
|
||||
"version": "3.8",
|
||||
"networks": map[string]any{
|
||||
"default": map[string]any{
|
||||
"external": map[string]any{
|
||||
"name": manager.config.InstanceNetwork,
|
||||
},
|
||||
},
|
||||
},
|
||||
"services": services,
|
||||
}
|
||||
|
||||
containers, err := manager.listContainers(map[string]string{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, container := range containers {
|
||||
containerJson, err := manager.inspectContainer(container.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
labels, err := manager.extractLabels(containerJson.Config.Labels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
containerName := containerJson.Name
|
||||
containerName = strings.TrimPrefix(containerName, "/")
|
||||
|
||||
service := map[string]any{}
|
||||
services[containerName] = service
|
||||
|
||||
service["image"] = labels.NekoImage
|
||||
service["container_name"] = containerName
|
||||
service["hostname"] = containerJson.Config.Hostname
|
||||
service["restart"] = containerJson.HostConfig.RestartPolicy.Name
|
||||
|
||||
// privileged
|
||||
if containerJson.HostConfig.Privileged {
|
||||
service["privileged"] = true
|
||||
}
|
||||
|
||||
// total shm memory usage
|
||||
service["shm_size"] = containerJson.HostConfig.ShmSize
|
||||
|
||||
// capabilites
|
||||
capAdd := []string{}
|
||||
for _, cap := range containerJson.HostConfig.CapAdd {
|
||||
capAdd = append(capAdd, string(cap))
|
||||
}
|
||||
if len(capAdd) > 0 {
|
||||
service["cap_add"] = capAdd
|
||||
}
|
||||
|
||||
// resources
|
||||
resources := map[string]any{}
|
||||
{
|
||||
limits := map[string]string{}
|
||||
// TODO: CPUShares
|
||||
if containerJson.HostConfig.NanoCPUs > 0 {
|
||||
limits["cpus"] = fmt.Sprintf("%f", float64(containerJson.HostConfig.NanoCPUs)/1000000000)
|
||||
}
|
||||
if containerJson.HostConfig.Memory > 0 {
|
||||
limits["memory"] = fmt.Sprintf("%dM", containerJson.HostConfig.Memory/1024/1024)
|
||||
}
|
||||
if len(limits) > 0 {
|
||||
resources["limits"] = limits
|
||||
}
|
||||
|
||||
deviceRequests := []any{}
|
||||
for _, device := range containerJson.HostConfig.DeviceRequests {
|
||||
deviceRequests = append(deviceRequests, map[string]any{
|
||||
"driver": device.Driver,
|
||||
"count": device.Count,
|
||||
"capabilities": device.Capabilities,
|
||||
})
|
||||
}
|
||||
if len(deviceRequests) > 0 {
|
||||
resources["reservations"] = map[string]any{
|
||||
"devices": deviceRequests,
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(resources) > 0 {
|
||||
service["deploy"] = map[string]any{
|
||||
"resources": resources,
|
||||
}
|
||||
}
|
||||
|
||||
// ports
|
||||
ports := []string{}
|
||||
for port, host := range containerJson.HostConfig.PortBindings {
|
||||
for _, binding := range host {
|
||||
ports = append(ports, fmt.Sprintf("%s:%s", binding.HostPort, port))
|
||||
}
|
||||
}
|
||||
if len(ports) > 0 {
|
||||
service["ports"] = ports
|
||||
}
|
||||
|
||||
// environment variables
|
||||
if len(containerJson.Config.Env) > 0 {
|
||||
service["environment"] = containerJson.Config.Env
|
||||
}
|
||||
|
||||
// volumes
|
||||
volumes := []string{}
|
||||
for _, mount := range containerJson.Mounts {
|
||||
if mount.RW {
|
||||
volumes = append(volumes, fmt.Sprintf("%s:%s:ro", mount.Source, mount.Destination))
|
||||
} else {
|
||||
volumes = append(volumes, fmt.Sprintf("%s:%s", mount.Source, mount.Destination))
|
||||
}
|
||||
}
|
||||
if len(volumes) > 0 {
|
||||
service["volumes"] = volumes
|
||||
}
|
||||
|
||||
// devices
|
||||
devices := []string{}
|
||||
for _, device := range containerJson.HostConfig.Devices {
|
||||
devices = append(devices, fmt.Sprintf("%s:%s:%s", device.PathOnHost, device.PathInContainer, device.CgroupPermissions))
|
||||
}
|
||||
if len(devices) > 0 {
|
||||
service["devices"] = devices
|
||||
}
|
||||
|
||||
// labels
|
||||
labelsArr := []string{}
|
||||
for key, val := range containerJson.Config.Labels {
|
||||
labelsArr = append(labelsArr, fmt.Sprintf("%s=%s", key, val))
|
||||
}
|
||||
if len(labelsArr) > 0 {
|
||||
service["labels"] = labelsArr
|
||||
}
|
||||
}
|
||||
|
||||
return yaml.Marshal(dockerCompose)
|
||||
}
|
||||
|
||||
func (manager *RoomManagerCtx) Create(settings types.RoomSettings) (string, error) {
|
||||
if settings.Name != "" && !dockerNames.RestrictedNamePattern.MatchString(settings.Name) {
|
||||
return "", fmt.Errorf("invalid container name, must match %s", dockerNames.RestrictedNameChars)
|
||||
|
||||
@@ -283,6 +283,7 @@ type RoomMember struct {
|
||||
type RoomManager interface {
|
||||
Config() RoomsConfig
|
||||
List(labels map[string]string) ([]RoomEntry, error)
|
||||
ExportAsDockerCompose() ([]byte, error)
|
||||
|
||||
Create(settings RoomSettings) (string, error)
|
||||
GetEntry(id string) (*RoomEntry, error)
|
||||
|
||||
Reference in New Issue
Block a user