Skip to content

Commit

Permalink
Add optional external docker configuration on demand
Browse files Browse the repository at this point in the history
docker compose will contain default and o11y network. o11y as external network
will be avaliable for external docker-compose instances
  • Loading branch information
n0npax committed May 26, 2023
1 parent a3784c6 commit 6cbd4d6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 20 deletions.
2 changes: 1 addition & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func New(version, commit, date string) *cli.App {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(c), func(i, j int) { c[i], c[j] = c[j], c[i] })

colors := make(map[int]func(...interface{}) string)
colors := make(map[int]func(...any) string)
for i, attr := range c {
colors[i] = color.New(attr).SprintFunc()
}
Expand Down
120 changes: 101 additions & 19 deletions internal/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ func genStartCommand() *cli.Command {
Usage: "sets a docker registry to pull images from",
Value: "registry-1.docker.io",
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "debug mode",
Value: false,
},
&cli.BoolFlag{
Name: "external-network",
Usage: "external network mode for docker compose",
Value: false,
},
},
Action: func(c *cli.Context) error {
fmt.Println("✨ Starting...")
Expand All @@ -39,14 +50,32 @@ func genStartCommand() *cli.Command {
return err
}

// Modify the Docker Compose file with the registry prefix
dockerComposePath := filepath.Join(targetDir, "files", "grafana", "run-o11y-run", "docker-compose.yaml")
err = addRegistryPrefix(dockerComposePath, c.String("registry"))
if err != nil {

// Modify the Docker Compose file with the registry prefix
if err = addRegistryPrefix(dockerComposePath, c.String("registry")); err != nil {
fmt.Println("Error adding registry prefix to Docker Compose file:", err)
return err
}

// Modify the Docker Compose to expose named network for other Docker Composes
if c.Bool("external-network") {
if err = addExternalNetwork(dockerComposePath); err != nil {
fmt.Println("Error adding network config to Docker Compose file:", err)
return err
}
}

if c.Bool("debug") {
fmt.Println("🐛 Debug mode enabled. Printing Docker Compose file...")
fmt.Println()
data, err := os.ReadFile(dockerComposePath)
if err != nil {
return err
}
fmt.Printf("%s\n", data)
}

// Run the Docker Compose up command
err = runDockerCompose(filepath.Join(targetDir, "files", "grafana", "run-o11y-run"), "up", "")
if err != nil {
Expand All @@ -63,32 +92,86 @@ func genStartCommand() *cli.Command {
// addRegistryPrefix adds the registry prefix to the image field of the Docker Compose file
func addRegistryPrefix(filePath, registry string) error {
// Read the Docker Compose file
data, err := os.ReadFile(filePath)
composeMap, err := dockeComposeMap(filePath)
if err != nil {
return fmt.Errorf("failed to read Docker Compose file: %w", err)
return err
}

// Unmarshal the YAML into a map
var composeMap map[interface{}]interface{}
err = yaml.Unmarshal(data, &composeMap)
// Modify the image field with the registry prefix for all services
services, ok := composeMap["services"].(map[any]any)
if ok {
for name, sAny := range services {
service, ok := sAny.(map[any]any)
if !ok {
return fmt.Errorf("unexpected type for service")
}
image, ok := service["image"].(string)
if ok {
service["image"] = fmt.Sprintf("%s/%s", registry, image)
} else {
return fmt.Errorf("error during injecting external registry to service(%s) image defintion", name)
}
}
} else {
return fmt.Errorf("error during injecting external registry to service image defintion")
}

return writeDockerCompose(filePath, composeMap)

}

// addExternalNetwork adds the registry prefix to the image field of the Docker Compose file
func addExternalNetwork(filePath string) error {
// Read the Docker Compose file
composeMap, err := dockeComposeMap(filePath)
if err != nil {
return fmt.Errorf("failed to unmarshal Docker Compose YAML: %w", err)
return err
}

// Modify the image field with the registry prefix for all services
services, ok := composeMap["services"].(map[interface{}]interface{})
// Modify newtorks field with the external network
services, ok := composeMap["services"].(map[any]any)
if ok {
for _, service := range services {
serviceMap, ok := service.(map[interface{}]interface{})
if ok {
image, ok := serviceMap["image"].(string)
if ok {
serviceMap["image"] = fmt.Sprintf("%s/%s", registry, image)
}
for _, sAny := range services {
service, ok := sAny.(map[any]any)
if !ok {
return fmt.Errorf("unexpected type for service")
}
service["networks"] = []string{"o11y", "default"}
}
} else {
return fmt.Errorf("error during injecting external network to service defintion")
}

// global networks
composeMap["networks"] = map[string]map[string]any{
"default": {
"driver": "bridge",
},
"o11y": {
"external": true,
},
}
return writeDockerCompose(filePath, composeMap)
}

// dockeComposeMap returns a map which represents the Docker Compose file
func dockeComposeMap(filePath string) (map[any]any, error) {
// Read the Docker Compose file
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read Docker Compose file: %w", err)
}

// Unmarshal the YAML into a map
var composeMap map[any]any
err = yaml.Unmarshal(data, &composeMap)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal Docker Compose YAML: %w", err)
}
return composeMap, nil
}

func writeDockerCompose(filePath string, composeMap map[any]any) error {
// Marshal the modified YAML back into bytes
modifiedData, err := yaml.Marshal(composeMap)
if err != nil {
Expand All @@ -100,6 +183,5 @@ func addRegistryPrefix(filePath, registry string) error {
if err != nil {
return fmt.Errorf("failed to write modified Docker Compose file: %w", err)
}

return nil
}

0 comments on commit 6cbd4d6

Please sign in to comment.