handler.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 misc
  15. import (
  16. "context"
  17. "fmt"
  18. "net"
  19. "net/http"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/tristate"
  22. "yunion.io/x/pkg/util/httputils"
  23. "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/appsrv"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  26. "yunion.io/x/onecloud/pkg/compute/models"
  27. "yunion.io/x/onecloud/pkg/compute/options"
  28. "yunion.io/x/onecloud/pkg/httperrors"
  29. "yunion.io/x/onecloud/pkg/mcclient/auth"
  30. )
  31. func AddMiscHandler(prefix string, app *appsrv.Application) {
  32. prefix = fmt.Sprintf("%s/misc", prefix)
  33. addHandler("GET", fmt.Sprintf("%s/bm-agent-url", prefix), getBmAgentUrl, app)
  34. addHandler("GET", fmt.Sprintf("%s/bm-prepare-script", prefix), getBmPrepareScript, app)
  35. }
  36. func addHandler(method, prefix string, f appsrv.FilterHandler, app *appsrv.Application) {
  37. handler := auth.Authenticate(f)
  38. app.AddHandler(method, prefix, handler)
  39. }
  40. func getBmAgentUrl(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  41. var err error
  42. ipAddr := r.URL.Query().Get("ssh_ip")
  43. if len(ipAddr) == 0 {
  44. ipAddr, _, err = net.SplitHostPort(r.RemoteAddr)
  45. if err != nil {
  46. httperrors.NewInternalServerError("Parse remote ip error %s", err)
  47. return
  48. }
  49. }
  50. log.Infof("getBmAgentUrl request ipaddr %s", ipAddr)
  51. n, err := models.NetworkManager.GetOnPremiseNetworkOfIP(ipAddr, "", tristate.None)
  52. if n == nil {
  53. log.Errorf("failed get network of ip %s: %s", ipAddr, err)
  54. httperrors.NotFoundError(ctx, w, "Network not found")
  55. return
  56. }
  57. wire, _ := n.GetWire()
  58. zoneId := wire.ZoneId
  59. bmAgent := models.BaremetalagentManager.GetAgent(compute.AgentTypeBaremetal, zoneId)
  60. if bmAgent == nil {
  61. httperrors.InternalServerError(ctx, w, "Baremetal agent not found")
  62. return
  63. }
  64. ret := fmt.Sprintf("%s %s", bmAgent.ManagerUri, ipAddr)
  65. fmt.Fprintf(w, "%s", ret)
  66. }
  67. func getBmPrepareScript(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  68. if len(options.Options.BaremetalPreparePackageUrl) == 0 {
  69. httperrors.NotAcceptableError(ctx, w, "Baremetal package not prepared")
  70. return
  71. }
  72. regionUrl, err := auth.GetPublicServiceURL("compute_v2", options.Options.Region, "", httputils.POST)
  73. if err != nil {
  74. httperrors.InternalServerError(ctx, w, "%v", err)
  75. return
  76. }
  77. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  78. var script string
  79. script += fmt.Sprintf("curl -fsSL -k -o ./baremetal_prepare.tar.gz %s;",
  80. options.Options.BaremetalPreparePackageUrl)
  81. script += "mkdir ./baremetal_prepare;"
  82. script += "tar -zxf ./baremetal_prepare.tar.gz -C ./baremetal_prepare;"
  83. script += fmt.Sprintf("./baremetal_prepare/prepare.sh %s %s",
  84. userCred.GetTokenString(), regionUrl)
  85. fmt.Fprintf(w, "%s", script)
  86. }