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.

config.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xenolf/lego/certcrypto"
  5. "github.com/xenolf/lego/lego"
  6. "os"
  7. )
  8. const (
  9. envCertDestinationKey = "DOTEGE_CERT_DESTINATION"
  10. envCertDestinationDefault = "/data/certs/"
  11. envDnsProviderKey = "DOTEGE_DNS_PROVIDER"
  12. envAcmeEmailKey = "DOTEGE_ACME_EMAIL"
  13. envAcmeEndpointKey = "DOTEGE_ACME_ENDPOINT"
  14. envAcmeKeyTypeKey = "DOTEGE_ACME_KEY_TYPE"
  15. envAcmeKeyTypeDefault = "P384"
  16. envAcmeCacheLocationKey = "DOTEGE_ACME_CACHE_FILE"
  17. envAcmeCacheLocationDefault = "/data/config/certs.json"
  18. envSignalContainerKey = "DOTEGE_SIGNAL_CONTAINER"
  19. envSignalContainerDefault = ""
  20. envSignalTypeKey = "DOTEGE_SIGNAL_TYPE"
  21. envSignalTypeDefault = "HUP"
  22. envTemplateDestinationKey = "DOTEGE_TEMPLATE_DESTINATION"
  23. envTemplateDestinationDefault = "/data/output/haproxy.cfg"
  24. envTemplateSourceKey = "DOTEGE_TEMPLATE_SOURCE"
  25. envTemplateSourceDefault = "./templates/haproxy.cfg.tpl"
  26. )
  27. // Config is the user-definable configuration for Dotege.
  28. type Config struct {
  29. Templates []TemplateConfig
  30. Signals []ContainerSignal
  31. Labels LabelConfig
  32. DefaultCertDestination string
  33. Acme AcmeConfig
  34. }
  35. // TemplateConfig configures a single template for the generator.
  36. type TemplateConfig struct {
  37. Source string
  38. Destination string
  39. }
  40. // ContainerSignal describes a container that should be sent a signal when the config/certs change.
  41. type ContainerSignal struct {
  42. Name string
  43. Signal string
  44. }
  45. // LabelConfig describes the labels used for various properties.
  46. type LabelConfig struct {
  47. Hostnames string
  48. RequireAuth string
  49. }
  50. // AcmeConfig describes the configuration to use for getting certs using ACME.
  51. type AcmeConfig struct {
  52. Email string
  53. DnsProvider string
  54. Endpoint string
  55. KeyType certcrypto.KeyType
  56. CacheLocation string
  57. }
  58. func requiredVar(key string) (value string) {
  59. value, ok := os.LookupEnv(key)
  60. if !ok {
  61. panic(fmt.Errorf("required environmental variable not defined: %s", key))
  62. }
  63. return
  64. }
  65. func optionalVar(key string, fallback string) (value string) {
  66. value, ok := os.LookupEnv(key)
  67. if !ok {
  68. value = fallback
  69. }
  70. return
  71. }
  72. func createSignalConfig() []ContainerSignal {
  73. name := optionalVar(envSignalContainerKey, envSignalContainerDefault)
  74. if name == envSignalContainerDefault {
  75. return []ContainerSignal{}
  76. } else {
  77. return []ContainerSignal{
  78. {
  79. Name: name,
  80. Signal: optionalVar(envSignalTypeKey, envSignalTypeDefault),
  81. },
  82. }
  83. }
  84. }
  85. func createConfig() *Config {
  86. return &Config{
  87. Templates: []TemplateConfig{
  88. {
  89. Source: optionalVar(envTemplateSourceKey, envTemplateSourceDefault),
  90. Destination: optionalVar(envTemplateDestinationKey, envTemplateDestinationDefault),
  91. },
  92. },
  93. Labels: LabelConfig{
  94. Hostnames: "com.chameth.vhost",
  95. RequireAuth: "com.chameth.auth",
  96. },
  97. Acme: AcmeConfig{
  98. DnsProvider: requiredVar(envDnsProviderKey),
  99. Email: requiredVar(envAcmeEmailKey),
  100. Endpoint: optionalVar(envAcmeEndpointKey, lego.LEDirectoryProduction),
  101. KeyType: certcrypto.KeyType(optionalVar(envAcmeKeyTypeKey, envAcmeKeyTypeDefault)),
  102. CacheLocation: optionalVar(envAcmeCacheLocationKey, envAcmeCacheLocationDefault),
  103. },
  104. Signals: createSignalConfig(),
  105. DefaultCertDestination: optionalVar(envCertDestinationKey, envCertDestinationDefault),
  106. }
  107. }