conf_svr.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/infra/config/model"
  6. "go-common/library/log"
  7. )
  8. // Push push new ver to config-service.
  9. func (s *Service) Push(c context.Context, treeID int64, env, zone, bver string, ver int64) (err error) {
  10. svr := svrFmt(treeID, env, zone)
  11. arg := &model.ArgConf{
  12. App: svr,
  13. BuildVer: bver,
  14. Ver: ver,
  15. }
  16. if err = s.confSvr.PushV4(c, arg); err != nil {
  17. log.Error("PushV4(%v) error(%v)", arg, err)
  18. }
  19. return
  20. }
  21. // SetToken set token to config-service.
  22. func (s *Service) SetToken(c context.Context, treeID int64, env, zone, token string) (err error) {
  23. svr := svrFmt(treeID, env, zone)
  24. arg := &model.ArgToken{
  25. App: svr,
  26. Token: token,
  27. }
  28. if err = s.confSvr.SetTokenV4(c, arg); err != nil {
  29. log.Error("SetToken(%v) error(%v)", arg, err)
  30. }
  31. return
  32. }
  33. // Hosts get hosts.
  34. func (s *Service) Hosts(c context.Context, treeID int64, appName, env, zone string) (hosts []*model.Host, err error) {
  35. svr := svrFmt(treeID, env, zone)
  36. if hosts, err = s.confSvr.Hosts(c, svr); err != nil {
  37. log.Error("Hosts(%v) error(%v)", svr, err)
  38. return
  39. }
  40. if len(hosts) == 0 {
  41. hosts = make([]*model.Host, 0)
  42. return
  43. }
  44. for _, host := range hosts {
  45. host.Service = appName
  46. }
  47. return
  48. }
  49. // ClearHost clear hosts.
  50. func (s *Service) ClearHost(c context.Context, treeID int64, env, zone string) (err error) {
  51. svr := svrFmt(treeID, env, zone)
  52. if err = s.confSvr.ClearHost(c, svr); err != nil {
  53. log.Error("Hosts(%v) error(%v)", svr, err)
  54. }
  55. return
  56. }
  57. func svrFmt(treeID int64, env, zone string) string {
  58. return fmt.Sprintf("%d_%s_%s", treeID, env, zone)
  59. }
  60. // PushForce push new ver to config-service.
  61. func (s *Service) PushForce(c context.Context, treeID int64, env, zone, bver string, ver int64, hosts map[string]string, sType int8) (err error) {
  62. svr := svrFmt(treeID, env, zone)
  63. arg := &model.ArgConf{
  64. App: svr,
  65. BuildVer: bver,
  66. Ver: ver,
  67. Env: env,
  68. Hosts: hosts,
  69. SType: sType,
  70. }
  71. if err = s.confSvr.Force(c, arg); err != nil {
  72. log.Error("PushForce(%v) error(%v)", arg, err)
  73. }
  74. return
  75. }