playbook.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. type ITask interface {
  20. MarshalYAML() (interface{}, error)
  21. }
  22. type Task struct {
  23. Name string
  24. WithPlugin string
  25. WithPluginVal interface{}
  26. When string
  27. Register string
  28. IgnoreErrors bool
  29. Vars map[string]interface{}
  30. ModuleName string
  31. ModuleArgs map[string]interface{}
  32. }
  33. func (t *Task) MarshalYAML() (interface{}, error) {
  34. r := map[string]interface{}{
  35. t.ModuleName: t.ModuleArgs,
  36. }
  37. if t.Name != "" {
  38. r["name"] = t.Name
  39. }
  40. if t.WithPlugin != "" && t.WithPluginVal != nil {
  41. r["with_"+t.WithPlugin] = t.WithPluginVal
  42. }
  43. if t.When != "" {
  44. r["when"] = t.When
  45. }
  46. if t.Register != "" {
  47. r["register"] = t.Register
  48. }
  49. if t.IgnoreErrors {
  50. r["ignore_errors"] = "yes"
  51. }
  52. if len(t.Vars) > 0 {
  53. r["vars"] = t.Vars
  54. }
  55. return r, nil
  56. }
  57. type ShellTask struct {
  58. Name string
  59. WithPlugin string
  60. WithPluginVal interface{}
  61. When string
  62. Register string
  63. IgnoreErrors bool
  64. Vars map[string]interface{}
  65. Script string
  66. ModuleArgs map[string]interface{}
  67. }
  68. func (t *ShellTask) MarshalYAML() (interface{}, error) {
  69. r := map[string]interface{}{
  70. "shell": t.Script,
  71. }
  72. if len(t.ModuleArgs) > 0 {
  73. r["args"] = t.ModuleArgs
  74. }
  75. if t.Name != "" {
  76. r["name"] = t.Name
  77. }
  78. if t.WithPlugin != "" && t.WithPluginVal != nil {
  79. r["with_"+t.WithPlugin] = t.WithPluginVal
  80. }
  81. if t.When != "" {
  82. r["when"] = t.When
  83. }
  84. if t.Register != "" {
  85. r["register"] = t.Register
  86. }
  87. if t.IgnoreErrors {
  88. r["ignore_errors"] = "yes"
  89. }
  90. if len(t.Vars) > 0 {
  91. r["vars"] = t.Vars
  92. }
  93. return r, nil
  94. }
  95. type IncludeRole struct {
  96. Name string
  97. Tags []string
  98. Vars map[string]interface{}
  99. When string
  100. }
  101. func (r *IncludeRole) MarshalYAML() (interface{}, error) {
  102. out := map[string]interface{}{
  103. "include_role": map[string]interface{}{"name": r.Name},
  104. }
  105. if len(r.Tags) > 0 {
  106. out["tags"] = r.Tags
  107. }
  108. if len(r.Vars) > 0 {
  109. out["vars"] = r.Vars
  110. }
  111. if len(r.When) > 0 {
  112. out["when"] = r.When
  113. }
  114. return out, nil
  115. }
  116. type Block struct {
  117. Name string
  118. WithPlugin string
  119. WithPluginVal interface{}
  120. When string
  121. Register string
  122. IgnoreErrors bool
  123. Vars map[string]interface{}
  124. Tasks []ITask
  125. }
  126. func NewBlock(tasks ...ITask) *Block {
  127. b := &Block{
  128. Tasks: tasks,
  129. }
  130. return b
  131. }
  132. func (b *Block) MarshalYAML() (interface{}, error) {
  133. r := map[string]interface{}{}
  134. if len(b.Tasks) > 0 {
  135. tasks := make([]interface{}, len(b.Tasks))
  136. for i := range tasks {
  137. var err error
  138. tasks[i], err = b.Tasks[i].MarshalYAML()
  139. if err != nil {
  140. return nil, err
  141. }
  142. }
  143. r["block"] = tasks
  144. }
  145. if b.Name != "" {
  146. r["name"] = b.Name
  147. }
  148. if b.WithPlugin != "" && b.WithPluginVal != nil {
  149. r["with_"+b.WithPlugin] = b.WithPluginVal
  150. }
  151. if b.When != "" {
  152. r["when"] = b.When
  153. }
  154. if b.Register != "" {
  155. r["register"] = b.Register
  156. }
  157. if b.IgnoreErrors {
  158. r["ignore_errors"] = "yes"
  159. }
  160. if len(b.Vars) > 0 {
  161. r["vars"] = b.Vars
  162. }
  163. return r, nil
  164. }
  165. type Play struct {
  166. Name string
  167. RemoteUser string
  168. Vars map[string]interface{}
  169. IgnoreErrors bool
  170. Hosts string
  171. Tasks []ITask
  172. }
  173. func NewPlay(tasks ...ITask) *Play {
  174. play := &Play{
  175. Tasks: tasks,
  176. }
  177. return play
  178. }
  179. func (play *Play) MarshalYAML() (interface{}, error) {
  180. if play.Hosts == "" {
  181. return nil, fmt.Errorf("hosts is required but not set")
  182. }
  183. r := map[string]interface{}{
  184. "hosts": play.Hosts,
  185. }
  186. if len(play.Tasks) > 0 {
  187. tasks := make([]interface{}, len(play.Tasks))
  188. for i := range tasks {
  189. var err error
  190. tasks[i], err = play.Tasks[i].MarshalYAML()
  191. if err != nil {
  192. return nil, err
  193. }
  194. }
  195. r["tasks"] = tasks
  196. }
  197. if play.Name != "" {
  198. r["name"] = play.Name
  199. }
  200. if play.RemoteUser != "" {
  201. r["remote_user"] = play.RemoteUser
  202. }
  203. if len(play.Vars) > 0 {
  204. r["vars"] = play.Vars
  205. }
  206. if play.IgnoreErrors {
  207. r["ignore_errors"] = "yes"
  208. }
  209. return r, nil
  210. }
  211. type Playbook struct {
  212. Plays []*Play
  213. }
  214. func NewPlaybook(plays ...*Play) *Playbook {
  215. pb := &Playbook{
  216. Plays: plays,
  217. }
  218. return pb
  219. }
  220. func (pb *Playbook) MarshalYAML() (interface{}, error) {
  221. r := make([]interface{}, len(pb.Plays))
  222. for i := range pb.Plays {
  223. var err error
  224. r[i], err = pb.Plays[i].MarshalYAML()
  225. if err != nil {
  226. return nil, err
  227. }
  228. }
  229. return r, nil
  230. }
  231. func (pb *Playbook) String() string {
  232. b, err := yaml.Marshal(pb)
  233. if err != nil {
  234. // panic early
  235. panic(err)
  236. }
  237. return string(b)
  238. }