update.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 system
  15. import (
  16. "bufio"
  17. "fmt"
  18. "io"
  19. "os"
  20. "path"
  21. "reflect"
  22. "sort"
  23. "strings"
  24. "github.com/rancher/os/config/cloudinit/config"
  25. )
  26. const (
  27. locksmithUnit = "locksmithd.service"
  28. updateEngineUnit = "update-engine.service"
  29. )
  30. // Update is a top-level structure which contains its underlying configuration,
  31. // config.Update, a function for reading the configuration (the default
  32. // implementation reading from the filesystem), and provides the system-specific
  33. // File() and Unit().
  34. type Update struct {
  35. ReadConfig func() (io.Reader, error)
  36. config.Update
  37. }
  38. func DefaultReadConfig() (io.Reader, error) {
  39. etcUpdate := path.Join("/etc", "coreos", "update.conf")
  40. usrUpdate := path.Join("/usr", "share", "coreos", "update.conf")
  41. f, err := os.Open(etcUpdate)
  42. if os.IsNotExist(err) {
  43. f, err = os.Open(usrUpdate)
  44. }
  45. return f, err
  46. }
  47. // File generates an `/etc/coreos/update.conf` file (if any update
  48. // configuration options are set in cloud-config) by either rewriting the
  49. // existing file on disk, or starting from `/usr/share/coreos/update.conf`
  50. func (uc Update) File() (*File, error) {
  51. if config.IsZero(uc.Update) {
  52. return nil, nil
  53. }
  54. if err := config.AssertStructValid(uc.Update); err != nil {
  55. return nil, err
  56. }
  57. // Generate the list of possible substitutions to be performed based on the options that are configured
  58. subs := map[string]string{}
  59. uct := reflect.TypeOf(uc.Update)
  60. ucv := reflect.ValueOf(uc.Update)
  61. for i := 0; i < uct.NumField(); i++ {
  62. val := ucv.Field(i).String()
  63. if val == "" {
  64. continue
  65. }
  66. env := uct.Field(i).Tag.Get("env")
  67. subs[env] = fmt.Sprintf("%s=%s", env, val)
  68. }
  69. conf, err := uc.ReadConfig()
  70. if err != nil {
  71. return nil, err
  72. }
  73. scanner := bufio.NewScanner(conf)
  74. var out string
  75. for scanner.Scan() {
  76. line := scanner.Text()
  77. for env, value := range subs {
  78. if strings.HasPrefix(line, env) {
  79. line = value
  80. delete(subs, env)
  81. break
  82. }
  83. }
  84. out += line
  85. out += "\n"
  86. if err := scanner.Err(); err != nil {
  87. return nil, err
  88. }
  89. }
  90. for _, key := range sortedKeys(subs) {
  91. out += subs[key]
  92. out += "\n"
  93. }
  94. return &File{config.File{
  95. Path: path.Join("etc", "coreos", "update.conf"),
  96. RawFilePermissions: "0644",
  97. Content: out,
  98. }}, nil
  99. }
  100. // Units generates units for the cloud-init initializer to act on:
  101. // - a locksmith Unit, if "reboot-strategy" was set in cloud-config
  102. // - an update_engine Unit, if "group" or "server" was set in cloud-config
  103. func (uc Update) Units() []Unit {
  104. var units []Unit
  105. if uc.Update.RebootStrategy != "" {
  106. ls := &Unit{config.Unit{
  107. Name: locksmithUnit,
  108. Command: "restart",
  109. Mask: false,
  110. Runtime: true,
  111. }}
  112. if uc.Update.RebootStrategy == "off" {
  113. ls.Command = "stop"
  114. ls.Mask = true
  115. }
  116. units = append(units, *ls)
  117. }
  118. if uc.Update.Group != "" || uc.Update.Server != "" {
  119. ue := Unit{config.Unit{
  120. Name: updateEngineUnit,
  121. Command: "restart",
  122. }}
  123. units = append(units, ue)
  124. }
  125. return units
  126. }
  127. func sortedKeys(m map[string]string) (keys []string) {
  128. for key := range m {
  129. keys = append(keys, key)
  130. }
  131. sort.Strings(keys)
  132. return
  133. }