env.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package initialize
  15. import (
  16. "net"
  17. "os"
  18. "path"
  19. "regexp"
  20. "strings"
  21. "github.com/rancher/os/config/cloudinit/config"
  22. "github.com/rancher/os/config/cloudinit/datasource"
  23. "github.com/rancher/os/config/cloudinit/system"
  24. )
  25. const DefaultSSHKeyName = "coreos-cloudinit"
  26. type Environment struct {
  27. root string
  28. configRoot string
  29. workspace string
  30. sshKeyName string
  31. substitutions map[string]string
  32. }
  33. // NewEnvironment TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow
  34. func NewEnvironment(root, configRoot, workspace, sshKeyName string, metadata datasource.Metadata) *Environment {
  35. firstNonNull := func(ip net.IP, env string) string {
  36. if ip == nil {
  37. return env
  38. }
  39. return ip.String()
  40. }
  41. substitutions := map[string]string{
  42. "$public_ipv4": firstNonNull(metadata.PublicIPv4, os.Getenv("COREOS_PUBLIC_IPV4")),
  43. "$private_ipv4": firstNonNull(metadata.PrivateIPv4, os.Getenv("COREOS_PRIVATE_IPV4")),
  44. "$public_ipv6": firstNonNull(metadata.PublicIPv6, os.Getenv("COREOS_PUBLIC_IPV6")),
  45. "$private_ipv6": firstNonNull(metadata.PrivateIPv6, os.Getenv("COREOS_PRIVATE_IPV6")),
  46. }
  47. return &Environment{root, configRoot, workspace, sshKeyName, substitutions}
  48. }
  49. func (e *Environment) Workspace() string {
  50. return path.Join(e.root, e.workspace)
  51. }
  52. func (e *Environment) Root() string {
  53. return e.root
  54. }
  55. func (e *Environment) ConfigRoot() string {
  56. return e.configRoot
  57. }
  58. func (e *Environment) SSHKeyName() string {
  59. return e.sshKeyName
  60. }
  61. func (e *Environment) SetSSHKeyName(name string) {
  62. e.sshKeyName = name
  63. }
  64. // Apply goes through the map of substitutions and replaces all instances of
  65. // the keys with their respective values. It supports escaping substitutions
  66. // with a leading '\'.
  67. func (e *Environment) Apply(data string) string {
  68. for key, val := range e.substitutions {
  69. matchKey := strings.Replace(key, `$`, `\$`, -1)
  70. replKey := strings.Replace(key, `$`, `$$`, -1)
  71. // "key" -> "val"
  72. data = regexp.MustCompile(`([^\\]|^)`+matchKey).ReplaceAllString(data, `${1}`+val)
  73. // "\key" -> "key"
  74. data = regexp.MustCompile(`\\`+matchKey).ReplaceAllString(data, replKey)
  75. }
  76. return data
  77. }
  78. func (e *Environment) DefaultEnvironmentFile() *system.EnvFile {
  79. ef := system.EnvFile{
  80. File: &system.File{File: config.File{
  81. Path: "/etc/environment",
  82. }},
  83. Vars: map[string]string{},
  84. }
  85. if ip, ok := e.substitutions["$public_ipv4"]; ok && len(ip) > 0 {
  86. ef.Vars["COREOS_PUBLIC_IPV4"] = ip
  87. }
  88. if ip, ok := e.substitutions["$private_ipv4"]; ok && len(ip) > 0 {
  89. ef.Vars["COREOS_PRIVATE_IPV4"] = ip
  90. }
  91. if ip, ok := e.substitutions["$public_ipv6"]; ok && len(ip) > 0 {
  92. ef.Vars["COREOS_PUBLIC_IPV6"] = ip
  93. }
  94. if ip, ok := e.substitutions["$private_ipv6"]; ok && len(ip) > 0 {
  95. ef.Vars["COREOS_PRIVATE_IPV6"] = ip
  96. }
  97. if len(ef.Vars) == 0 {
  98. return nil
  99. }
  100. return &ef
  101. }