routers_realize.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 models
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/pkg/errors"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/util/rand"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/mcclient/auth"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules/ansible"
  25. "yunion.io/x/onecloud/pkg/util/ansiblev2"
  26. )
  27. func (router *SRouter) realize(ctx context.Context, userCred mcclient.TokenCredential) error {
  28. plays := []*ansiblev2.Play{
  29. router.playEssential(),
  30. }
  31. host, err := router.ansibleHost()
  32. if err != nil {
  33. return err
  34. }
  35. if router.RealizeWgIfaces {
  36. plays = append(plays,
  37. router.playInstallWireguard(),
  38. router.playDeployWireguardNetworks(),
  39. )
  40. }
  41. if router.RealizeRoutes {
  42. playRoutes, err := router.playDeployRoutes()
  43. if err != nil {
  44. return err
  45. }
  46. plays = append(plays, playRoutes)
  47. }
  48. if router.RealizeRules {
  49. playRules, err := router.playDeployRules()
  50. if err != nil {
  51. return err
  52. }
  53. plays = append(plays, playRules)
  54. }
  55. inv := ansiblev2.NewInventory()
  56. inv.SetHost(router.Name, host)
  57. pb := ansiblev2.NewPlaybook(plays...)
  58. files := router.playFilesStr()
  59. params := jsonutils.NewDict()
  60. params.Set("creator_mark", jsonutils.NewString("router:"+router.Id))
  61. params.Set("name", jsonutils.NewString(router.Name+"-"+fmt.Sprintf("%d-", router.UpdateVersion)+rand.String(5)))
  62. params.Set("inventory", jsonutils.NewString(inv.String()))
  63. params.Set("playbook", jsonutils.NewString(pb.String()))
  64. params.Set("files", jsonutils.NewString(files))
  65. cliSess := auth.GetSession(ctx, userCred, "")
  66. if _, err := ansible.AnsiblePlaybooksV2.Create(cliSess, params); err != nil {
  67. return errors.WithMessagef(err, "create ansible task")
  68. }
  69. return nil
  70. }
  71. func (router *SRouter) PerformRealize(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  72. err := router.realize(ctx, userCred)
  73. if err != nil {
  74. return nil, httperrors.NewBadRequestError("%s", err)
  75. }
  76. return nil, nil
  77. }