coreosutils.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2019 Yunion
  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 coreosutils
  15. import (
  16. "encoding/base64"
  17. "fmt"
  18. "strings"
  19. yaml "gopkg.in/yaml.v2"
  20. "yunion.io/x/pkg/utils"
  21. )
  22. type SUnitDropins struct {
  23. Name string `yaml:"name,omitempty"`
  24. Content string `yaml:"content,omitempty"`
  25. }
  26. type SUnits struct {
  27. Name string `yaml:"name,omitempty"`
  28. Mask *bool `yaml:"mask,omitempty"`
  29. Enable *bool `yaml:"enable,omitempty"`
  30. Runtime *bool `yaml:"runtime,omitempty"`
  31. Command string `yaml:"command,omitempty"`
  32. Content string `yaml:"content,omitempty"`
  33. dropIns *SUnitDropins `yaml:"drop_ins,omitempty"`
  34. }
  35. type SUser struct {
  36. Name string `yaml:"name,omitempty"`
  37. Passwd string `yaml:"passwd,omitempty"`
  38. SshAuthorizedKeys []string `yaml:"ssh_authorized_keys,omitempty"`
  39. }
  40. func NewUser(name, passwd string, pubkeys []string, nohash bool) SUser {
  41. if !nohash {
  42. // TODO: replace with crypt
  43. passwd, _ = utils.EncryptAESBase64("$6$SALT$", passwd)
  44. }
  45. return SUser{
  46. Name: name,
  47. Passwd: passwd,
  48. SshAuthorizedKeys: pubkeys,
  49. }
  50. }
  51. type SWriteFile struct {
  52. Path string `yaml:"path,omitempty"`
  53. Content string `yaml:"content,omitempty"`
  54. Permissions string `yaml:"permissions,omitempty"`
  55. Owner string `yaml:"owner,omitempty"`
  56. Encoding string `yaml:"encoding,omitempty"`
  57. }
  58. func NewWriteFile(spath, content, perm, owner string, isbase64 bool) SWriteFile {
  59. res := SWriteFile{}
  60. if isbase64 {
  61. res.Encoding = "base64"
  62. res.Content = base64.StdEncoding.EncodeToString([]byte(content))
  63. } else {
  64. res.Content = content
  65. }
  66. res.Path = spath
  67. res.Permissions = perm
  68. res.Owner = owner
  69. return res
  70. }
  71. type SCloudConfig struct {
  72. Hostname string `yaml:"hostname,omitempty"`
  73. Users []SUser `yaml:"users,omitempty"`
  74. Coreos map[string]interface{} `yaml:"coreos,omitempty"`
  75. WriteFiles []SWriteFile `yaml:"write_files,omitempty"`
  76. ManageEtcHosts string `yaml:"manage_etc_hosts,omitempty"`
  77. }
  78. func NewCloudConfig() *SCloudConfig {
  79. res := new(SCloudConfig)
  80. res.Users = make([]SUser, 0)
  81. res.Coreos = map[string]interface{}{"units": []SUnits{}}
  82. res.WriteFiles = make([]SWriteFile, 0)
  83. return res
  84. }
  85. func (c *SCloudConfig) SetHostname(hn string) {
  86. c.Hostname = hn
  87. }
  88. func (c *SCloudConfig) SetEtcHosts(line string) {
  89. c.ManageEtcHosts = line
  90. }
  91. func (c *SCloudConfig) AddUser(name, passwd string, pubkeys []string, nohash bool) {
  92. c.Users = append(c.Users, NewUser(name, passwd, pubkeys, nohash))
  93. }
  94. func (c *SCloudConfig) HasUser(name string) bool {
  95. for _, u := range c.Users {
  96. if u.Name == name {
  97. return true
  98. }
  99. }
  100. return false
  101. }
  102. func (c *SCloudConfig) AddWriteFile(spath, content, prem, owner string, base64 bool) {
  103. if len(prem) == 0 {
  104. prem = "0644"
  105. }
  106. if len(owner) == 0 {
  107. owner = "root"
  108. }
  109. c.WriteFiles = append(c.WriteFiles, NewWriteFile(spath, content, prem, owner, base64))
  110. }
  111. func (c *SCloudConfig) HasWriteFile(spath string) bool {
  112. for _, f := range c.WriteFiles {
  113. if f.Path == spath {
  114. return true
  115. }
  116. }
  117. return false
  118. }
  119. func (c *SCloudConfig) AddUnits(name string, mask, enable, runtime *bool, content, command string, dropins *SUnitDropins) {
  120. u := SUnits{
  121. Name: name,
  122. Mask: mask,
  123. Enable: enable,
  124. Runtime: runtime,
  125. Content: content,
  126. Command: command,
  127. dropIns: dropins,
  128. }
  129. units := c.Coreos["units"].([]SUnits)
  130. units = append(units, u)
  131. c.Coreos["units"] = units
  132. }
  133. func (c *SCloudConfig) AddSwap(dev string) {
  134. name := fmt.Sprintf("%s.swap", strings.Replace(dev[1:], "/", "-", -1))
  135. cont := "[Unit]\n"
  136. cont += fmt.Sprintf("Description=Enable swap on %s\n", dev)
  137. cont += "[Swap]\n"
  138. cont += fmt.Sprintf("What=%s\n", dev)
  139. c.AddUnits(name, nil, nil, nil, cont, "start", nil)
  140. }
  141. func (c *SCloudConfig) AddPartition(dev, mtpath, fs string) {
  142. name := fmt.Sprintf("%s.mount", strings.Replace(mtpath[1:], "/", "-", -1))
  143. cont := "[Unit]\n"
  144. cont += fmt.Sprintf("Description=Mount %s on %s\n", dev, mtpath)
  145. cont += "[Mount]\n"
  146. cont += fmt.Sprintf("What=%s\n", dev)
  147. cont += fmt.Sprintf("Where=%s\n", mtpath)
  148. cont += fmt.Sprintf("Type=%s\n", fs)
  149. c.AddUnits(name, nil, nil, nil, cont, "start", nil)
  150. }
  151. func (c *SCloudConfig) SetTimezone(tz string) {
  152. name := "settimezone.service"
  153. cont := "[Unit]\n"
  154. cont += "Description=Set the timezone\n"
  155. cont += "[Service]\n"
  156. cont += fmt.Sprintf("ExecStart=/usr/bin/timedatectl set-timezone %s\n", tz)
  157. cont += "RemainAfterExit=yes\n"
  158. cont += "Type=oneshot\n"
  159. c.AddUnits(name, nil, nil, nil, cont, "start", nil)
  160. conf := ""
  161. for i := 0; i < 4; i++ {
  162. conf += fmt.Sprintf("server %d.pool.ntp.org\n", i)
  163. }
  164. conf += "restrict default nomodify nopeer noquery limited kod\n"
  165. conf += "restrict 127.0.0.1\n"
  166. conf += "restrict [::1]\n"
  167. c.AddWriteFile("/etc/ntp.conf", conf, "", "", false)
  168. }
  169. func (c *SCloudConfig) AddConfig(name, cfg string) {
  170. c.Coreos[name] = cfg
  171. }
  172. func (c *SCloudConfig) YunionInit() {
  173. VERSION := "0.0.2"
  174. cont := "id: yunion\n"
  175. cont += "name: Yunion Yun\n"
  176. cont += fmt.Sprintf("version-id: %s\n", VERSION)
  177. cont += "home-url: https://yunionyun.com/\n"
  178. c.AddConfig("oem", cont)
  179. mark := true
  180. c.AddUnits("user-configdrive.service", &mark, nil, nil, "", "", nil)
  181. c.AddUnits("user-configvirtfs.service", &mark, nil, nil, "", "", nil)
  182. }
  183. func (c *SCloudConfig) String() string {
  184. ys, _ := yaml.Marshal(c)
  185. return "#cloud-config\n\n" + string(ys)
  186. }
  187. // func (c *SCloudConfig) String() string {
  188. // ret, err := yaml.Marshal(c)
  189. // }