Skip to content

Commit

Permalink
fix: find and expose container service port
Browse files Browse the repository at this point in the history
  • Loading branch information
cchalop1 committed Sep 13, 2024
1 parent 3478fae commit 0c16a87
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/just-deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var flags struct {
}

func main() {
port := adapter.FindOpenLocalPort()
port := adapter.FindOpenLocalPort(8080)

app := api.NewApplication(port)

Expand Down
3 changes: 1 addition & 2 deletions internal/adapter/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 17 additions & 25 deletions internal/adapter/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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",
Expand All @@ -36,50 +34,44 @@ 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",
},
},
},
"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},
},
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",
Expand Down
17 changes: 15 additions & 2 deletions internal/adapter/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os/exec"
"strconv"

"cchalop1.com/deploy/internal/adapter/database"
"cchalop1.com/deploy/internal/api/dto"
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions internal/application/create-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 0c16a87

Please sign in to comment.