ansibleplaybooks.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 options
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. apis "yunion.io/x/onecloud/pkg/apis/ansible"
  21. "yunion.io/x/onecloud/pkg/util/ansible"
  22. )
  23. type AnsiblePlaybookIdOptions struct {
  24. ID string `help:"name/id of the playbook"`
  25. }
  26. type AnsiblePlaybookListOptions struct {
  27. BaseListOptions
  28. }
  29. type AnsiblePlaybookCommonOptions struct {
  30. Host []string `help:"name or id of server or host in format '<[server:]id|host:id>|ipaddr var=val'"`
  31. Mod []string `help:"ansible modules and their arguments in format 'name k1=v1 k2=v2'"`
  32. File []string `help:"files for use by modules, e.g. name=content, name=@file"`
  33. }
  34. func (opts *AnsiblePlaybookCommonOptions) ToPlaybook() (*ansible.Playbook, error) {
  35. if len(opts.Mod) == 0 {
  36. return nil, fmt.Errorf("Requires at least one --mod argument")
  37. }
  38. if len(opts.Host) == 0 {
  39. return nil, fmt.Errorf("Requires at least one server/host to operate on")
  40. }
  41. pb := ansible.NewPlaybook()
  42. hosts := []ansible.Host{}
  43. for _, s := range opts.Host {
  44. host, err := ansible.ParseHostLine(s)
  45. if err != nil {
  46. return nil, err
  47. }
  48. hosts = append(hosts, host)
  49. }
  50. pb.Inventory = ansible.Inventory{Hosts: hosts}
  51. for _, s := range opts.Mod {
  52. module, err := ansible.ParseModuleLine(s)
  53. if err != nil {
  54. return nil, err
  55. }
  56. pb.Modules = append(pb.Modules, module)
  57. }
  58. files := map[string][]byte{}
  59. for _, s := range opts.File {
  60. i := strings.IndexByte(s, '=')
  61. if i < 0 {
  62. return nil, fmt.Errorf("missing '=' in argument for --file. Read command help")
  63. }
  64. name := strings.TrimSpace(s[:i])
  65. if name == "" {
  66. return nil, fmt.Errorf("empty file name: %s", s)
  67. }
  68. v := s[i+1:]
  69. if len(v) > 0 && v[0] == '@' {
  70. path := v[1:]
  71. d, err := ioutil.ReadFile(path)
  72. if err != nil {
  73. return nil, fmt.Errorf("read file %s: %v", path, err)
  74. }
  75. files[name] = d
  76. } else {
  77. files[name] = []byte(v)
  78. }
  79. }
  80. pb.Files = files
  81. return pb, nil
  82. }
  83. type AnsiblePlaybookCreateOptions struct {
  84. NAME string `help:"name of the playbook"`
  85. AnsiblePlaybookCommonOptions
  86. }
  87. func (opts *AnsiblePlaybookCreateOptions) Params() (*jsonutils.JSONDict, error) {
  88. pb, err := opts.AnsiblePlaybookCommonOptions.ToPlaybook()
  89. if err != nil {
  90. return nil, err
  91. }
  92. input := &apis.AnsiblePlaybookCreateInput{
  93. Name: opts.NAME,
  94. Playbook: *pb,
  95. }
  96. params := input.JSON(input)
  97. return params, nil
  98. }
  99. type AnsiblePlaybookUpdateOptions struct {
  100. ID string `json:"-" help:"name/id of the playbook"`
  101. Name string
  102. AnsiblePlaybookCommonOptions
  103. }
  104. func (opts *AnsiblePlaybookUpdateOptions) Params() (*jsonutils.JSONDict, error) {
  105. pb, err := opts.AnsiblePlaybookCommonOptions.ToPlaybook()
  106. if err != nil {
  107. return nil, err
  108. }
  109. input := &apis.AnsiblePlaybookUpdateInput{
  110. Name: opts.Name,
  111. Playbook: *pb,
  112. }
  113. params := input.JSON(input)
  114. return params, nil
  115. }