handler.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 hosthandler
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "os"
  20. "time"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/gotypes"
  24. "yunion.io/x/pkg/utils"
  25. "yunion.io/x/onecloud/pkg/appsrv"
  26. "yunion.io/x/onecloud/pkg/hostman/hostinfo"
  27. "yunion.io/x/onecloud/pkg/hostman/hostinfo/hostconsts"
  28. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  29. "yunion.io/x/onecloud/pkg/httperrors"
  30. "yunion.io/x/onecloud/pkg/mcclient/auth"
  31. "yunion.io/x/onecloud/pkg/util/timeutils2"
  32. )
  33. type actionFunc func(context.Context, string, jsonutils.JSONObject) (interface{}, error)
  34. var (
  35. keyWords = []string{"hosts"}
  36. )
  37. func AddHostHandler(prefix string, app *appsrv.Application) {
  38. for _, keyword := range keyWords {
  39. for action, f := range map[string]actionFunc{
  40. "sync": hostSync,
  41. "probe-isolated-devices": hostProbeIsolatedDevices,
  42. "shutdown-servers-on-host-down": setOnHostDown,
  43. "restart-host-agent": hostRestart,
  44. } {
  45. app.AddHandler("POST",
  46. fmt.Sprintf("%s/%s/<sid>/%s", prefix, keyword, action),
  47. auth.Authenticate(hostActions(f)),
  48. )
  49. }
  50. }
  51. }
  52. func setOnHostDown(ctx context.Context, hostId string, body jsonutils.JSONObject) (interface{}, error) {
  53. if !body.Contains("shutdown_servers") {
  54. return nil, httperrors.NewMissingParameterError("shutdown_servers")
  55. }
  56. if jsonutils.QueryBoolean(body, "shutdown_servers", false) {
  57. hostinfo.Instance().SetOnHostDown(hostconsts.SHUTDOWN_SERVERS)
  58. } else {
  59. hostinfo.Instance().SetOnHostDown("")
  60. }
  61. return nil, nil
  62. }
  63. func hostActions(f actionFunc) appsrv.FilterHandler {
  64. return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  65. params, _, body := appsrv.FetchEnv(ctx, w, r)
  66. if body == nil {
  67. body = jsonutils.NewDict()
  68. }
  69. var sid = params["<sid>"]
  70. res, err := f(ctx, sid, body)
  71. if err != nil {
  72. hostutils.Response(ctx, w, err)
  73. } else if !gotypes.IsNil(res) {
  74. hostutils.Response(ctx, w, res)
  75. } else {
  76. hostutils.ResponseOk(ctx, w)
  77. }
  78. }
  79. }
  80. func hostSync(ctx context.Context, hostId string, body jsonutils.JSONObject) (interface{}, error) {
  81. return hostinfo.Instance().UpdateSyncInfo(hostId, body)
  82. }
  83. func hostRestart(ctx context.Context, hostId string, body jsonutils.JSONObject) (interface{}, error) {
  84. log.Infof("Received host restart request, going to restart host-agent.")
  85. timeutils2.AddTimeout(time.Second*3, func() {
  86. utils.DumpAllGoroutineStack(log.Logger().Out)
  87. hostinfo.Stop()
  88. hostutils.GetWorkManager().Stop()
  89. os.Exit(1)
  90. })
  91. return nil, nil
  92. }
  93. func hostProbeIsolatedDevices(ctx context.Context, hostId string, body jsonutils.JSONObject) (interface{}, error) {
  94. return hostinfo.Instance().ProbeSyncIsolatedDevices(hostId, body)
  95. }