github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/build/docker-compose-generator/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  type DockerCompose struct {
    12  	Version  string                `yaml:"version"`
    13  	Services map[string]*Container `yaml:"services"`
    14  }
    15  
    16  type Container struct {
    17  	Command   string   `yaml:"command,omitempty"`
    18  	Image     string   `yaml:"image,omitempty"`
    19  	Network   []string `yaml:"networks,omitempty"`
    20  	DependsOn []string `yaml:"depends_on,omitempty"`
    21  }
    22  
    23  func main() {
    24  	validServices := map[string]int{
    25  		"mysql":              3306,
    26  		"postgres":           5432,
    27  		"minio":              9000,
    28  		"inbucket":           10080,
    29  		"openldap":           389,
    30  		"elasticsearch":      9200,
    31  		"dejavu":             1358,
    32  		"keycloak":           8080,
    33  		"prometheus":         9090,
    34  		"grafana":            3000,
    35  		"mysql-read-replica": 3306, // FIXME: not recorgnizing the successfully running service on port 3307.
    36  	}
    37  	command := []string{}
    38  	for _, arg := range os.Args[1:] {
    39  		port, ok := validServices[arg]
    40  		if !ok {
    41  			panic(fmt.Sprintf("Unknown service %s", arg))
    42  		}
    43  		command = append(command, fmt.Sprintf("%s:%d", arg, port))
    44  	}
    45  
    46  	var dockerCompose DockerCompose
    47  	dockerCompose.Version = "2.4"
    48  	dockerCompose.Services = map[string]*Container{}
    49  	dockerCompose.Services["start_dependencies"] = &Container{
    50  		Image:     "mattermost/mattermost-wait-for-dep:latest",
    51  		Network:   []string{"hk-test"},
    52  		DependsOn: os.Args[1:],
    53  		Command:   strings.Join(command, " "),
    54  	}
    55  	resultData, err := yaml.Marshal(dockerCompose)
    56  	if err != nil {
    57  		panic(fmt.Sprintf("Unable to serialize the docker-compose file: %s.", err.Error()))
    58  	}
    59  	fmt.Println(string(resultData))
    60  }