provision_handler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "github.com/go-chi/chi"
  8. "github.com/porter-dev/porter/internal/forms"
  9. "github.com/porter-dev/porter/internal/kubernetes"
  10. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  11. )
  12. // HandleProvisionTest will create a test resource by deploying a provisioner
  13. // container pod
  14. func (app *App) HandleProvisionTest(w http.ResponseWriter, r *http.Request) {
  15. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  16. if err != nil || projID == 0 {
  17. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  18. return
  19. }
  20. // create a new agent
  21. agent, err := kubernetes.GetAgentInClusterConfig()
  22. if err != nil {
  23. app.handleErrorDataRead(err, w)
  24. return
  25. }
  26. _, err = agent.ProvisionTest(uint(projID))
  27. if err != nil {
  28. app.handleErrorInternal(err, w)
  29. return
  30. }
  31. w.WriteHeader(http.StatusOK)
  32. }
  33. // HandleProvisionAWSECRInfra provisions a new aws ECR instance for a project
  34. func (app *App) HandleProvisionAWSECRInfra(w http.ResponseWriter, r *http.Request) {
  35. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  36. if err != nil || projID == 0 {
  37. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  38. return
  39. }
  40. form := &forms.CreateECRInfra{
  41. ProjectID: uint(projID),
  42. }
  43. // decode from JSON to form value
  44. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  45. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  46. return
  47. }
  48. // validate the form
  49. if err := app.validator.Struct(form); err != nil {
  50. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  51. return
  52. }
  53. // convert the form to an aws infra instance
  54. infra, err := form.ToAWSInfra()
  55. if err != nil {
  56. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  57. return
  58. }
  59. // handle write to the database
  60. infra, err = app.Repo.AWSInfra.CreateAWSInfra(infra)
  61. if err != nil {
  62. app.handleErrorDataWrite(err, w)
  63. return
  64. }
  65. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  66. if err != nil {
  67. app.handleErrorDataRead(err, w)
  68. return
  69. }
  70. // launch provisioning pod
  71. agent, err := kubernetes.GetAgentInClusterConfig()
  72. if err != nil {
  73. app.handleErrorDataRead(err, w)
  74. return
  75. }
  76. _, err = agent.ProvisionECR(
  77. uint(projID),
  78. awsInt,
  79. form.ECRName,
  80. )
  81. if err != nil {
  82. app.handleErrorInternal(err, w)
  83. return
  84. }
  85. app.Logger.Info().Msgf("New aws ecr infra created: %d", infra.ID)
  86. w.WriteHeader(http.StatusCreated)
  87. infraExt := infra.Externalize()
  88. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  89. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  90. return
  91. }
  92. }
  93. // HandleGetProvisioningLogs returns real-time logs of the provisioning process via websockets
  94. func (app *App) HandleGetProvisioningLogs(w http.ResponseWriter, r *http.Request) {
  95. // get path parameters
  96. kind := chi.URLParam(r, "kind")
  97. projectID := chi.URLParam(r, "project_id")
  98. infraID := chi.URLParam(r, "infra_id")
  99. streamName := fmt.Sprintf("%s-%s-%s", kind, projectID, infraID)
  100. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  101. // upgrade to websocket.
  102. conn, err := upgrader.Upgrade(w, r, nil)
  103. if err != nil {
  104. app.handleErrorUpgradeWebsocket(err, w)
  105. }
  106. err = provisioner.ResourceStream(app.RedisClient, streamName, conn)
  107. if err != nil {
  108. app.handleErrorWebsocketWrite(err, w)
  109. return
  110. }
  111. }