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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/docker/docker/client"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. "io/ioutil"
  10. "os"
  11. "os/signal"
  12. "path"
  13. "strings"
  14. "syscall"
  15. "time"
  16. )
  17. // Container models a docker container that is running on the system.
  18. type Container struct {
  19. Id string
  20. Name string
  21. Labels map[string]string
  22. }
  23. // Hostname describes a DNS name used for proxying, retrieving certificates, etc.
  24. type Hostname struct {
  25. Name string
  26. Alternatives map[string]bool
  27. Containers []*Container
  28. CertDestination string
  29. RequiresAuth bool
  30. AuthGroup string
  31. }
  32. var (
  33. logger *zap.SugaredLogger
  34. certificateManager *CertificateManager
  35. config *Config
  36. dockerClient *client.Client
  37. containers = make(map[string]*Container)
  38. )
  39. func monitorSignals() <-chan bool {
  40. signals := make(chan os.Signal, 1)
  41. done := make(chan bool, 1)
  42. signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
  43. go func() {
  44. sig := <-signals
  45. fmt.Printf("Received %s signal\n", sig)
  46. done <- true
  47. }()
  48. return done
  49. }
  50. func createLogger() *zap.SugaredLogger {
  51. zapConfig := zap.NewDevelopmentConfig()
  52. zapConfig.DisableCaller = true
  53. zapConfig.DisableStacktrace = true
  54. zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
  55. zapConfig.OutputPaths = []string{"stdout"}
  56. zapConfig.ErrorOutputPaths = []string{"stdout"}
  57. logger, _ := zapConfig.Build()
  58. return logger.Sugar()
  59. }
  60. func createTemplateGenerator(templates []TemplateConfig) *TemplateGenerator {
  61. templateGenerator := NewTemplateGenerator()
  62. for _, template := range templates {
  63. templateGenerator.AddTemplate(template)
  64. }
  65. return templateGenerator
  66. }
  67. func createCertificateManager(config AcmeConfig) {
  68. certificateManager = NewCertificateManager(logger, config.Endpoint, config.KeyType, config.DnsProvider, config.CacheLocation)
  69. err := certificateManager.Init(config.Email)
  70. if err != nil {
  71. panic(err)
  72. }
  73. }
  74. func main() {
  75. logger = createLogger()
  76. logger.Info("Dotege is starting")
  77. doneChan := monitorSignals()
  78. createConfig()
  79. var err error
  80. dockerStopChan := make(chan struct{})
  81. dockerClient, err = client.NewEnvClient()
  82. if err != nil {
  83. panic(err)
  84. }
  85. templateGenerator := createTemplateGenerator(config.Templates)
  86. createCertificateManager(config.Acme)
  87. jitterTimer := time.NewTimer(time.Minute)
  88. redeployTimer := time.NewTicker(time.Hour * 24)
  89. updatedContainers := make(map[string]*Container)
  90. go func() {
  91. err := monitorContainers(dockerClient, dockerStopChan, func(container *Container) {
  92. containers[container.Name] = container
  93. updatedContainers[container.Name] = container
  94. jitterTimer.Reset(100 * time.Millisecond)
  95. }, func(name string) {
  96. delete(updatedContainers, name)
  97. delete(containers, name)
  98. jitterTimer.Reset(100 * time.Millisecond)
  99. })
  100. if err != nil {
  101. logger.Fatal("Error monitoring containers: ", err.Error())
  102. }
  103. }()
  104. go func() {
  105. for {
  106. select {
  107. case <-jitterTimer.C:
  108. hostnames := getHostnames(containers)
  109. updated := templateGenerator.Generate(Context{
  110. Containers: containers,
  111. Hostnames: hostnames,
  112. })
  113. for name, container := range updatedContainers {
  114. certDeployed := deployCertForContainer(container)
  115. updated = updated || certDeployed
  116. delete(updatedContainers, name)
  117. }
  118. if updated {
  119. signalContainer()
  120. }
  121. case <-redeployTimer.C:
  122. logger.Info("Performing periodic certificate refresh")
  123. for _, container := range containers {
  124. deployCertForContainer(container)
  125. signalContainer()
  126. }
  127. }
  128. }
  129. }()
  130. <-doneChan
  131. dockerStopChan <- struct{}{}
  132. err = dockerClient.Close()
  133. if err != nil {
  134. panic(err)
  135. }
  136. }
  137. func signalContainer() {
  138. for _, s := range config.Signals {
  139. container, ok := containers[s.Name]
  140. if ok {
  141. logger.Debugf("Killing container %s with signal %s", s.Name, s.Signal)
  142. err := dockerClient.ContainerKill(context.Background(), container.Id, s.Signal)
  143. if err != nil {
  144. logger.Errorf("Unable to send signal %s to container %s: %s", s.Signal, s.Name, err.Error())
  145. }
  146. } else {
  147. logger.Warnf("Couldn't signal container %s as it is not running", s.Name)
  148. }
  149. }
  150. }
  151. func getHostnamesForContainer(container *Container) []string {
  152. if label, ok := container.Labels[config.Labels.Hostnames]; ok {
  153. return strings.Split(strings.Replace(label, ",", " ", -1), " ")
  154. } else {
  155. return []string{}
  156. }
  157. }
  158. func getHostnames(containers map[string]*Container) (hostnames map[string]*Hostname) {
  159. hostnames = make(map[string]*Hostname)
  160. for _, container := range containers {
  161. if label, ok := container.Labels[config.Labels.Hostnames]; ok {
  162. names := strings.Split(strings.Replace(label, ",", " ", -1), " ")
  163. if hostname, ok := hostnames[names[0]]; ok {
  164. hostname.Containers = append(hostname.Containers, container)
  165. } else {
  166. hostnames[names[0]] = &Hostname{
  167. Name: names[0],
  168. Alternatives: make(map[string]bool),
  169. Containers: []*Container{container},
  170. CertDestination: config.DefaultCertDestination,
  171. }
  172. }
  173. addAlternatives(hostnames[names[0]], names[1:])
  174. if label, ok = container.Labels[config.Labels.RequireAuth]; ok {
  175. hostnames[names[0]].RequiresAuth = true
  176. hostnames[names[0]].AuthGroup = label
  177. }
  178. }
  179. }
  180. return
  181. }
  182. func addAlternatives(hostname *Hostname, alternatives []string) {
  183. for _, alternative := range alternatives {
  184. hostname.Alternatives[alternative] = true
  185. }
  186. }
  187. func deployCertForContainer(container *Container) bool {
  188. hostnames := getHostnamesForContainer(container)
  189. if len(hostnames) == 0 {
  190. logger.Debugf("No labels found for container %s", container.Name)
  191. return false
  192. }
  193. err, cert := certificateManager.GetCertificate(hostnames)
  194. if err != nil {
  195. logger.Warnf("Unable to generate certificate for %s: %s", container.Name, err.Error())
  196. return false
  197. } else {
  198. return deployCert(cert)
  199. }
  200. }
  201. func deployCert(certificate *SavedCertificate) bool {
  202. target := path.Join(config.DefaultCertDestination, fmt.Sprintf("%s.pem", certificate.Domains[0]))
  203. content := append(certificate.Certificate, certificate.PrivateKey...)
  204. buf, _ := ioutil.ReadFile(target)
  205. if bytes.Equal(buf, content) {
  206. logger.Debugf("Certificate was up to date: %s", target)
  207. return false
  208. }
  209. err := ioutil.WriteFile(target, content, 0700)
  210. if err != nil {
  211. logger.Warnf("Unable to write certificate %s - %s", target, err.Error())
  212. return false
  213. } else {
  214. logger.Infof("Updated certificate file %s", target)
  215. return true
  216. }
  217. }