diff --git a/cmd/just-deploy/main.go b/cmd/just-deploy/main.go index c2204bf..b88beab 100644 --- a/cmd/just-deploy/main.go +++ b/cmd/just-deploy/main.go @@ -21,7 +21,7 @@ var flags struct { } func main() { - port := adapter.FindOpenLocalPort() + port := adapter.FindOpenLocalPort(8080) app := api.NewApplication(port) diff --git a/internal/adapter/browser.go b/internal/adapter/browser.go index 8b9a722..ec0e0d1 100644 --- a/internal/adapter/browser.go +++ b/internal/adapter/browser.go @@ -49,8 +49,7 @@ func isPortIsUse(host string, port int) bool { return true } -func FindOpenLocalPort() string { - port := 8080 +func FindOpenLocalPort(port int) string { for { if !isPortIsUse("localhost", port) { break diff --git a/internal/adapter/database/database.go b/internal/adapter/database/database.go index 32d1830..465f4ef 100644 --- a/internal/adapter/database/database.go +++ b/internal/adapter/database/database.go @@ -3,20 +3,21 @@ package database import ( "cchalop1.com/deploy/internal/api/dto" "github.com/docker/docker/api/types/container" - "github.com/docker/go-connections/nat" ) type ServicesConfig struct { - Name string `json:"name"` - Icon string `json:"icon"` - Env []dto.Env `json:"env"` - Config container.Config + Name string `json:"name"` + Icon string `json:"icon"` + Env []dto.Env `json:"env"` + DefaultPort int `json:"defaultPort"` + Config container.Config } var servicesConfigMap = map[string]ServicesConfig{ "Postgres": { - Name: "Postgres", - Icon: "https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Postgresql_elephant.svg/640px-Postgresql_elephant.svg.png", + DefaultPort: 5432, + Name: "Postgres", + Icon: "https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Postgresql_elephant.svg/640px-Postgresql_elephant.svg.png", Env: []dto.Env{ {Name: "POSTGRES_USER", Value: "", IsSecret: false}, {Name: "POSTGRES_PASSWORD", Value: "", IsSecret: true}, @@ -25,9 +26,6 @@ var servicesConfigMap = map[string]ServicesConfig{ Config: container.Config{ Image: "postgres:latest", Cmd: []string{"postgres"}, - ExposedPorts: map[nat.Port]struct{}{ - "5432/tcp": {}, - }, Env: []string{ "POSTGRES_USER=$POSTGRES_USER", "POSTGRES_PASSWORD=$POSTGRES_PASSWORD", @@ -36,17 +34,15 @@ var servicesConfigMap = map[string]ServicesConfig{ }, }, "Mongo": { - Name: "Mongo", - Icon: "https://upload.wikimedia.org/wikipedia/fr/thumb/4/45/MongoDB-Logo.svg/1280px-MongoDB-Logo.svg.png", + DefaultPort: 27017, + Name: "Mongo", + Icon: "https://upload.wikimedia.org/wikipedia/fr/thumb/4/45/MongoDB-Logo.svg/1280px-MongoDB-Logo.svg.png", Env: []dto.Env{ {Name: "MONGO_INITDB_ROOT_USERNAME", Value: "", IsSecret: false}, {Name: "MONGO_INITDB_ROOT_PASSWORD", Value: "", IsSecret: true}, }, Config: container.Config{ Image: "mongo", - ExposedPorts: map[nat.Port]struct{}{ - "27017/tcp": {}, - }, Env: []string{ "MONGO_INITDB_ROOT_USERNAME=$MONGO_INITDB_ROOT_USERNAME", "MONGO_INITDB_ROOT_PASSWORD=$MONGO_INITDB_ROOT_PASSWORD", @@ -54,22 +50,21 @@ var servicesConfigMap = map[string]ServicesConfig{ }, }, "Redis": { - Name: "Redis", - Icon: "https://grafikart.fr/uploads/icons/redis.svg", + DefaultPort: 6379, + Name: "Redis", + Icon: "https://grafikart.fr/uploads/icons/redis.svg", Env: []dto.Env{ {Name: "REDIS_PASSWORD", Value: "", IsSecret: true}, }, Config: container.Config{ Image: "redis:latest", Cmd: []string{"redis-server", "--requirepass", "$REDIS_PASSWORD"}, - ExposedPorts: map[nat.Port]struct{}{ - "6379/tcp": {}, - }, }, }, "Keycloak": { - Name: "Keycloak", - Icon: "https://www.keycloak.org/resources/images/logo.svg", + DefaultPort: 8080, + Name: "Keycloak", + Icon: "https://www.keycloak.org/resources/images/logo.svg", Env: []dto.Env{ {Name: "KEYCLOAK_USER", Value: "", IsSecret: false}, {Name: "KEYCLOAK_PASSWORD", Value: "", IsSecret: true}, @@ -77,9 +72,6 @@ var servicesConfigMap = map[string]ServicesConfig{ Config: container.Config{ Image: "quay.io/keycloak/keycloak", Cmd: []string{"start-dev"}, - ExposedPorts: map[nat.Port]struct{}{ - "8080/tcp": {}, - }, Env: []string{ "KEYCLOAK_USER=$KEYCLOAK_USER", "KEYCLOAK_PASSWORD=$KEYCLOAK_PASSWORD", diff --git a/internal/adapter/docker.go b/internal/adapter/docker.go index efbbce1..78918ff 100644 --- a/internal/adapter/docker.go +++ b/internal/adapter/docker.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os/exec" + "strconv" "cchalop1.com/deploy/internal/adapter/database" "cchalop1.com/deploy/internal/api/dto" @@ -357,8 +358,20 @@ func (d *DockerAdapter) GetLogsOfContainer(containerName string) ([]string, erro return lines, nil } -func (d *DockerAdapter) RunService(service database.ServicesConfig, containerHostName string) { - con, err := d.client.ContainerCreate(context.Background(), &service.Config, &container.HostConfig{}, +func (d *DockerAdapter) RunService(service database.ServicesConfig, exposedPort string, containerHostName string) { + internalPort := strconv.Itoa(service.DefaultPort) + "/tcp" + hostConfig := &container.HostConfig{ + PortBindings: nat.PortMap{ + nat.Port(internalPort): []nat.PortBinding{ + { + HostIP: "0.0.0.0", + HostPort: exposedPort, + }, + }, + }, + } + + con, err := d.client.ContainerCreate(context.Background(), &service.Config, hostConfig, &network.NetworkingConfig{}, &v1.Platform{}, containerHostName) if err != nil { diff --git a/internal/application/create-service.go b/internal/application/create-service.go index 8d958b0..da02dbe 100644 --- a/internal/application/create-service.go +++ b/internal/application/create-service.go @@ -4,6 +4,7 @@ import ( "errors" "strings" + "cchalop1.com/deploy/internal/adapter" "cchalop1.com/deploy/internal/adapter/database" "cchalop1.com/deploy/internal/api/dto" "cchalop1.com/deploy/internal/api/service" @@ -53,10 +54,7 @@ func generateContainerHostname(serviceName string, deployId *string) string { } func getPortsForService(service database.ServicesConfig) string { - for port := range service.Config.ExposedPorts { - return strings.Split(string(port), "/")[0] - } - return "" + return adapter.FindOpenLocalPort(service.DefaultPort) } func createServiceLinkToDeploy(deployService *service.DeployService, createServiceDto dto.CreateServiceDto) (domain.Service, error) { @@ -170,13 +168,13 @@ func createServiceForProject(deployService *service.DeployService, createService containerHostname := generateContainerHostname(service.Name, nil) - deployService.DockerAdapter.RunService(service, containerHostname) + exposedPort := getPortsForService(service) - envs = append(envs, dto.Env{Name: strings.ToUpper(service.Name) + "_HOSTNAME", Value: "localhost"}) + deployService.DockerAdapter.RunService(service, exposedPort, containerHostname) - port := getPortsForService(service) + envs = append(envs, dto.Env{Name: strings.ToUpper(service.Name) + "_HOSTNAME", Value: "localhost"}) - envs = append(envs, dto.Env{Name: strings.ToUpper(service.Name) + "_PORT", Value: port}) + envs = append(envs, dto.Env{Name: strings.ToUpper(service.Name) + "_PORT", Value: exposedPort}) domainService := domain.Service{ Id: utils.GenerateRandomPassword(5),