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.5KB

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