Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select if you want to modify docker-compose or external environment file #8

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ statsServiceName: "stats"
statsContainerName: "stats"
table: "silos"
chainId: "replace-with-actual-chain-id"
# When the following is set, docker-compose will be left as is and instead the common environment file will be updated
pathToCommonEnvironmentFile: ""
76 changes: 76 additions & 0 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,82 @@ func (d *Docker) UpdateServiceEnv(compose map[string]interface{}, serviceName st
return compose, updated, nil
}

func (d *Docker) AppyAndUpdateEachService(compose map[string]interface{}, updates []EnvUpdate) ([]Container, error) {
pathToCommonEnvironmentFile := viper.GetString("pathToCommonEnvironmentFile")

if pathToCommonEnvironmentFile == "" {
return d.applyAndUpdateEachServiceUsingCompose(compose, updates)
}

envViper := viper.New()
envViper.SetConfigFile(pathToCommonEnvironmentFile)
if err := envViper.ReadInConfig(); err != nil {
err = fmt.Errorf("error loading %s env file: %w", pathToCommonEnvironmentFile, err)
return nil, err
}
return d.applyAndUpdateEachServiceUsingEnvFile(envViper, updates)
}

func (d *Docker) applyAndUpdateEachServiceUsingEnvFile(variables *viper.Viper, updates []EnvUpdate) ([]Container, error) {
var containers []Container
for _, env := range updates {
if (variables.GetString(env.Key) != env.Value) {
variables.Set(env.Key, env.Value)

fmt.Printf("Updated %s service environment: %+v\n", env.ServiceName, env)
containers = append(containers, Container{
Name: env.ContainerName,
ServiceName: env.ServiceName,
})
}
}

file, err := os.Create(variables.ConfigFileUsed())
if err != nil {
return nil, fmt.Errorf("error opening .env file: %v", err)
}
defer file.Close()

for _, key := range viper.AllKeys() {
_, err := fmt.Fprintf(file, "%s=%s\n", key, viper.GetString(key))
if err != nil {
return nil, fmt.Errorf("error writing to .env file: %v", err)
}
}

return containers, nil
}

func (d *Docker) applyAndUpdateEachServiceUsingCompose(compose map[string]interface{}, updates []EnvUpdate) ([]Container, error) {
var containers []Container
var err error

// Apply updates to each service
for _, env := range updates {
var updated bool
compose, updated, err = d.UpdateServiceEnv(compose, env.ServiceName, map[string]interface{}{
env.Key: env.Value,
})
if err != nil {
err = fmt.Errorf("failed to update %s service environment: %w", env.ServiceName, err)
return nil, err
}
if updated {
fmt.Printf("Updated %s service environment: %+v\n", env.ServiceName, env)
containers = append(containers, Container{
Name: env.ContainerName,
ServiceName: env.ServiceName,
})
}
}

if err = d.WriteComposeFile(compose); err != nil {
err = fmt.Errorf("failed to write compose file: %w", err)
return nil, err
}
return containers, nil
}

// UniqueContainerNames returns a sorted list of unique container names
func (d *Docker) UniqueContainers(containers []Container) []Container {
unique := make(map[string]Container)
Expand Down
8 changes: 8 additions & 0 deletions internal/docker/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package docker

type EnvUpdate struct {
ServiceName string
Key string
Value string
ContainerName string
}
30 changes: 5 additions & 25 deletions internal/handlers/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (h *CoinHandler) Handle(record *Record) HandlerResult {
return result
}

updates := []EnvUpdate{
// Define environment updates for each service
updates := []docker.EnvUpdate{
{
ServiceName: viper.GetString("frontendServiceName"),
Key: "NEXT_PUBLIC_NETWORK_CURRENCY_SYMBOL",
Expand All @@ -56,30 +57,9 @@ func (h *CoinHandler) Handle(record *Record) HandlerResult {
},
}

// Define environment updates for each service

// Apply updates to each service
for _, env := range updates {
var updated bool
compose, updated, err = h.docker.UpdateServiceEnv(compose, env.ServiceName, map[string]interface{}{
env.Key: env.Value,
})
if err != nil {
result.Error = fmt.Errorf("failed to update %s service environment: %w", env.ServiceName, err)
return result
}
if updated {
fmt.Printf("Updated %s service environment: %+v\n", env.ServiceName, env)
result.ContainersToRestart = append(result.ContainersToRestart, docker.Container{
Name: env.ContainerName,
ServiceName: env.ServiceName,
})
}
}

if err = h.docker.WriteComposeFile(compose); err != nil {
result.Error = fmt.Errorf("failed to write compose file: %w", err)
return result
result.ContainersToRestart, err = h.docker.AppyAndUpdateEachService(compose, updates)
if err != nil {
result.Error = err
}

return result
Expand Down
72 changes: 33 additions & 39 deletions internal/handlers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,63 +38,57 @@ func (h *ImageHandler) Handle(record *Record) HandlerResult {
return result
}

compose, err := h.docker.ReadComposeFile()
if err != nil {
result.Error = fmt.Errorf("failed to read compose file: %w", err)
return result
}

frontendServiceName := viper.GetString("frontendServiceName")
frontendContainerName := viper.GetString("frontendContainerName")

updates := map[string]map[string]interface{}{
frontendServiceName: {},
}

// Validate and update light logo URL
if err := h.validateImage(record.LightLogoURL); err != nil {
result.Error = fmt.Errorf("invalid light logo URL: %w", err)
} else {
updates[frontendServiceName]["NEXT_PUBLIC_NETWORK_LOGO"] = record.LightLogoURL
return result
}

// Validate and update dark logo URL
if err := h.validateImage(record.DarkLogoURL); err != nil {
result.Error = fmt.Errorf("invalid dark logo URL: %w", err)
} else {
updates[frontendServiceName]["NEXT_PUBLIC_NETWORK_LOGO_DARK"] = record.DarkLogoURL
return result
}

// Validate and update favicon URL
if err := h.validateImage(record.FaviconURL); err != nil {
result.Error = fmt.Errorf("invalid favicon URL: %w", err)
} else {
updates[frontendServiceName]["NEXT_PUBLIC_NETWORK_ICON"] = record.FaviconURL
return result
}

// Apply updates to services
for service, env := range updates {
var updated bool
compose, updated, err = h.docker.UpdateServiceEnv(compose, service, env)
if err != nil {
result.Error = fmt.Errorf("failed to update %s service environment: %w", service, err)
return result
}
if updated {
fmt.Printf("Updated %s service environment: %+v\n", service, env)
fmt.Printf("Frontend container name: %s\n", frontendContainerName)
fmt.Printf("Frontend service name: %s\n", frontendServiceName)
result.ContainersToRestart = append(result.ContainersToRestart, docker.Container{
Name: frontendContainerName,
ServiceName: frontendServiceName,
})
}
compose, err := h.docker.ReadComposeFile()
if err != nil {
result.Error = fmt.Errorf("failed to read compose file: %w", err)
return result
}

err = h.docker.WriteComposeFile(compose)
frontendServiceName := viper.GetString("frontendServiceName")
frontendContainerName := viper.GetString("frontendContainerName")

updates := []docker.EnvUpdate{
{
ServiceName: frontendServiceName,
Key: "NEXT_PUBLIC_NETWORK_LOGO",
Value: record.LightLogoURL,
ContainerName: frontendContainerName,
},
{
ServiceName: frontendServiceName,
Key: "NEXT_PUBLIC_NETWORK_LOGO_DARK",
Value: record.DarkLogoURL,
ContainerName: frontendContainerName,
},
{
ServiceName: frontendServiceName,
Key: "NEXT_PUBLIC_NETWORK_ICON",
Value: record.FaviconURL,
ContainerName: frontendContainerName,
},
}

result.ContainersToRestart, err = h.docker.AppyAndUpdateEachService(compose, updates)
if err != nil {
result.Error = fmt.Errorf("failed to write compose file: %w", err)
return result
result.Error = err
}

return result
Expand Down
7 changes: 0 additions & 7 deletions internal/handlers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,3 @@ func NewBaseHandler() BaseHandler {
docker: docker.NewDocker(),
}
}

type EnvUpdate struct {
ServiceName string
Key string
Value string
ContainerName string
}
4 changes: 3 additions & 1 deletion internal/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ func (s *Subscription) Subscribe(worker *worker.Worker) error {
continue
}

fmt.Printf("Received event: %s\n", record.Event)
if (record.Event != "phx_reply") { // Filter out this one to avoid spamming logs
fmt.Printf("Received event: %s\n", record.Event)
}
if record.Event == "postgres_changes" {
table := viper.GetString("table")
if record.Payload.Data.Table == table {
Expand Down
Loading