provision_handler.go 3.5 KB

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