Docker template generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dotege.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/csmith/dotege/docker"
  5. "github.com/csmith/dotege/model"
  6. "github.com/docker/docker/client"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. "os"
  10. "os/signal"
  11. "strings"
  12. "syscall"
  13. "time"
  14. )
  15. func monitorSignals() <-chan bool {
  16. signals := make(chan os.Signal, 1)
  17. done := make(chan bool, 1)
  18. signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
  19. go func() {
  20. sig := <-signals
  21. fmt.Printf("Received %s signal\n", sig)
  22. done <- true
  23. }()
  24. return done
  25. }
  26. func main() {
  27. config := zap.NewDevelopmentConfig()
  28. config.DisableCaller = true
  29. config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
  30. config.OutputPaths = []string{"stdout"}
  31. config.ErrorOutputPaths = []string{"stdout"}
  32. logger, _ := config.Build()
  33. sugar := logger.Sugar()
  34. sugar.Info("Dotege is starting")
  35. done := monitorSignals()
  36. containerChan := make(chan model.Container, 1)
  37. expiryChan := make(chan string, 1)
  38. labelConfig := model.LabelConfig{
  39. Hostnames: "com.chameth.vhost",
  40. }
  41. cli, err := client.NewEnvClient()
  42. if err != nil {
  43. panic(err)
  44. }
  45. templateGenerator := NewTemplateGenerator(sugar)
  46. templateGenerator.AddTemplate(model.TemplateConfig{
  47. Source: "./templates/domains.txt.tpl",
  48. Destination: "domains.txt",
  49. })
  50. templateGenerator.AddTemplate(model.TemplateConfig{
  51. Source: "./templates/haproxy.cfg.tpl",
  52. Destination: "haproxy.cfg",
  53. })
  54. monitor := docker.NewContainerMonitor(sugar, cli, containerChan, expiryChan)
  55. go monitor.Monitor()
  56. go func() {
  57. containers := make(map[string]model.Container)
  58. timer := time.NewTimer(time.Hour)
  59. timer.Stop()
  60. for {
  61. select {
  62. case container := <-containerChan:
  63. containers[container.Name] = container
  64. timer.Reset(100 * time.Millisecond)
  65. case name := <-expiryChan:
  66. delete(containers, name)
  67. timer.Reset(100 * time.Millisecond)
  68. case <-timer.C:
  69. templateGenerator.Generate(Context{
  70. Containers: containers,
  71. Hostnames: getHostnames(containers, labelConfig),
  72. })
  73. }
  74. }
  75. }()
  76. <-done
  77. err = cli.Close()
  78. if err != nil {
  79. panic(err)
  80. }
  81. }
  82. func getHostnames(containers map[string]model.Container, config model.LabelConfig) (hostnames map[string]model.Hostname) {
  83. hostnames = make(map[string]model.Hostname)
  84. for _, container := range containers {
  85. if label, ok := container.Labels[config.Hostnames]; ok {
  86. names := strings.Split(strings.Replace(label, ",", " ", -1), " ")
  87. if hostname, ok := hostnames[names[0]]; ok {
  88. hostname.Containers = append(hostname.Containers, container)
  89. } else {
  90. hostnames[names[0]] = model.Hostname{
  91. Name: names[0],
  92. Alternatives: make(map[string]bool),
  93. Containers: []model.Container{container},
  94. }
  95. }
  96. addAlternatives(hostnames[names[0]], names[1:])
  97. }
  98. }
  99. return
  100. }
  101. func addAlternatives(hostname model.Hostname, alternatives []string) {
  102. for _, alternative := range alternatives {
  103. hostname.Alternatives[alternative] = true
  104. }
  105. }