inventory.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 ansiblev2
  15. import (
  16. "fmt"
  17. "github.com/go-yaml/yaml"
  18. )
  19. func newVars(args ...interface{}) map[string]interface{} {
  20. if len(args)&1 != 0 {
  21. panic("odd number of args for key/value pairs!")
  22. }
  23. vars := map[string]interface{}{}
  24. for i := 0; i < len(args); i += 2 {
  25. if k, ok := args[i].(string); !ok {
  26. panic(fmt.Sprintf("the %drd key is not string type: %#v", i, args[i]))
  27. } else {
  28. vars[k] = args[i+1]
  29. }
  30. }
  31. return vars
  32. }
  33. type Host struct {
  34. Vars map[string]interface{}
  35. }
  36. func NewHost(args ...interface{}) *Host {
  37. return &Host{
  38. Vars: newVars(args...),
  39. }
  40. }
  41. type HostGroup struct {
  42. Hosts map[string]*Host
  43. Children map[string]*HostGroup
  44. Vars map[string]interface{}
  45. }
  46. func NewHostGroup(args ...interface{}) *HostGroup {
  47. return &HostGroup{
  48. Vars: newVars(args...),
  49. }
  50. }
  51. func (hg *HostGroup) SetHost(name string, host *Host) *HostGroup {
  52. if hg.Hosts == nil {
  53. hg.Hosts = map[string]*Host{}
  54. }
  55. hg.Hosts[name] = host
  56. return hg
  57. }
  58. func (hg *HostGroup) SetChild(name string, child *HostGroup) *HostGroup {
  59. if hg.Children == nil {
  60. hg.Children = map[string]*HostGroup{}
  61. }
  62. hg.Children[name] = child
  63. return hg
  64. }
  65. func (hg *HostGroup) MarshalYAML() (interface{}, error) {
  66. hosts := map[string]interface{}{}
  67. for name := range hg.Hosts {
  68. hosts[name] = hg.Hosts[name].Vars
  69. }
  70. children := map[string]interface{}{}
  71. for name := range hg.Children {
  72. children[name], _ = hg.Children[name].MarshalYAML()
  73. }
  74. r := map[string]interface{}{}
  75. if len(hosts) > 0 {
  76. r["hosts"] = hosts
  77. }
  78. if len(children) > 0 {
  79. r["children"] = children
  80. }
  81. if len(hg.Vars) > 0 {
  82. r["vars"] = hg.Vars
  83. }
  84. return r, nil
  85. }
  86. type Inventory struct {
  87. HostGroup
  88. }
  89. func NewInventory(args ...interface{}) *Inventory {
  90. hg := NewHostGroup(args...)
  91. inv := &Inventory{
  92. HostGroup: *hg,
  93. }
  94. return inv
  95. }
  96. func (inv *Inventory) MarshalYAML() (interface{}, error) {
  97. all, _ := inv.HostGroup.MarshalYAML()
  98. r := map[string]interface{}{
  99. "all": all,
  100. }
  101. return r, nil
  102. }
  103. func (inv *Inventory) String() string {
  104. b, _ := yaml.Marshal(inv)
  105. return string(b)
  106. }