agentparams.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "encoding/base64"
  17. "fmt"
  18. "reflect"
  19. "text/template"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. compute_models "yunion.io/x/onecloud/pkg/compute/models"
  23. )
  24. func dataFromParams(p interface{}) map[string]interface{} {
  25. data := jsonutils.Marshal(p)
  26. ret := make(map[string]interface{})
  27. err := data.Unmarshal(&ret)
  28. if err != nil {
  29. log.Errorf("unmarshal map[string]interface{} error %s", err)
  30. return nil
  31. }
  32. return ret
  33. }
  34. type AgentParams struct {
  35. AgentModel *compute_models.SLoadbalancerAgent
  36. KeepalivedConfigTmpl *template.Template
  37. HaproxyConfigTmpl *template.Template
  38. TelegrafConfigTmpl *template.Template
  39. Data map[string]map[string]interface{}
  40. }
  41. func NewAgentParams(agent *compute_models.SLoadbalancerAgent) (*AgentParams, error) {
  42. b64s := map[string]string{
  43. "keepalived_conf_tmpl": agent.Params.KeepalivedConfTmpl,
  44. "haproxy_conf_tmpl": agent.Params.HaproxyConfTmpl,
  45. "telegraf_conf_tmpl": agent.Params.TelegrafConfTmpl,
  46. }
  47. tmpls := map[string]*template.Template{}
  48. for name, b64 := range b64s {
  49. d, err := base64.StdEncoding.DecodeString(b64)
  50. if err != nil {
  51. return nil, fmt.Errorf("%s: invalid base64 string: %s", name, err)
  52. }
  53. tmpl, err := template.New(name).Parse(string(d))
  54. if err != nil {
  55. return nil, fmt.Errorf("%s: invalid template: %s", name, err)
  56. }
  57. tmpls[name] = tmpl
  58. }
  59. dataAgent := map[string]interface{}{
  60. "id": agent.Id,
  61. "name": agent.Name,
  62. "ip": agent.IP,
  63. }
  64. agent.Params.Vrrp.Interface = agent.Interface
  65. agent.Params.Vrrp.Priority = agent.Priority
  66. data := map[string]map[string]interface{}{
  67. "agent": dataAgent,
  68. "vrrp": dataFromParams(agent.Params.Vrrp),
  69. "haproxy": dataFromParams(agent.Params.Haproxy),
  70. "telegraf": dataFromParams(agent.Params.Telegraf),
  71. }
  72. agentParams := &AgentParams{
  73. AgentModel: agent,
  74. KeepalivedConfigTmpl: tmpls["keepalived_conf_tmpl"],
  75. HaproxyConfigTmpl: tmpls["haproxy_conf_tmpl"],
  76. TelegrafConfigTmpl: tmpls["telegraf_conf_tmpl"],
  77. Data: data,
  78. }
  79. return agentParams, nil
  80. }
  81. func (p *AgentParams) Equals(p2 *AgentParams) bool {
  82. if p == nil && p2 == nil {
  83. return true
  84. }
  85. if p == nil || p2 == nil {
  86. return false
  87. }
  88. agentP := p.AgentModel
  89. agentP2 := p2.AgentModel
  90. if !reflect.DeepEqual(agentP.Params, agentP2.Params) {
  91. return false
  92. }
  93. keys := []string{"notify_script", "unicast_peer"}
  94. for _, key := range keys {
  95. v := p.GetVrrpParams(key)
  96. v2 := p2.GetVrrpParams(key)
  97. if !reflect.DeepEqual(v, v2) {
  98. return false
  99. }
  100. }
  101. return true
  102. }
  103. func (p *AgentParams) setXxParams(xx, k string, v interface{}) map[string]interface{} {
  104. var dt map[string]interface{}
  105. d, ok := p.Data[xx]
  106. if !ok {
  107. dt = map[string]interface{}{}
  108. p.Data[xx] = dt
  109. } else {
  110. dt = d
  111. }
  112. dt[k] = v
  113. return dt
  114. }
  115. func (p *AgentParams) getXxParams(xx, k string) interface{} {
  116. dt, ok := p.Data[xx]
  117. if !ok {
  118. return nil
  119. }
  120. return dt[k]
  121. }
  122. func (p *AgentParams) SetVrrpParams(k string, v interface{}) map[string]interface{} {
  123. return p.setXxParams("vrrp", k, v)
  124. }
  125. func (p *AgentParams) GetVrrpParams(k string) interface{} {
  126. return p.getXxParams("vrrp", k)
  127. }
  128. func (p *AgentParams) SetHaproxyParams(k string, v interface{}) map[string]interface{} {
  129. return p.setXxParams("haproxy", k, v)
  130. }
  131. func (p *AgentParams) SetTelegrafParams(k string, v interface{}) map[string]interface{} {
  132. return p.setXxParams("telegraf", k, v)
  133. }
  134. func (p *AgentParams) KeepalivedConfig() {
  135. }