provision_handler.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. "github.com/porter-dev/porter/internal/adapter"
  12. )
  13. // HandleProvisionTest will create a test resource by deploying a provisioner
  14. // container pod
  15. func (app *App) HandleProvisionTest(w http.ResponseWriter, r *http.Request) {
  16. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  17. if err != nil || projID == 0 {
  18. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  19. return
  20. }
  21. // create a new agent
  22. agent, err := kubernetes.GetAgentInClusterConfig()
  23. if err != nil {
  24. app.handleErrorDataRead(err, w)
  25. return
  26. }
  27. _, err = agent.ProvisionTest(uint(projID))
  28. if err != nil {
  29. app.handleErrorInternal(err, w)
  30. return
  31. }
  32. w.WriteHeader(http.StatusOK)
  33. }
  34. // HandleProvisionAWSECRInfra provisions a new aws ECR instance for a project
  35. func (app *App) HandleProvisionAWSECRInfra(w http.ResponseWriter, r *http.Request) {
  36. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  37. if err != nil || projID == 0 {
  38. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  39. return
  40. }
  41. form := &forms.CreateECRInfra{
  42. ProjectID: uint(projID),
  43. }
  44. // decode from JSON to form value
  45. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  46. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  47. return
  48. }
  49. // validate the form
  50. if err := app.validator.Struct(form); err != nil {
  51. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  52. return
  53. }
  54. // convert the form to an aws infra instance
  55. infra, err := form.ToAWSInfra()
  56. if err != nil {
  57. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  58. return
  59. }
  60. // handle write to the database
  61. infra, err = app.Repo.AWSInfra.CreateAWSInfra(infra)
  62. if err != nil {
  63. app.handleErrorDataWrite(err, w)
  64. return
  65. }
  66. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  67. if err != nil {
  68. app.handleErrorDataRead(err, w)
  69. return
  70. }
  71. // launch provisioning pod
  72. agent, err := kubernetes.GetAgentInClusterConfig()
  73. if err != nil {
  74. app.handleErrorDataRead(err, w)
  75. return
  76. }
  77. _, err = agent.ProvisionECR(
  78. uint(projID),
  79. awsInt,
  80. form.ECRName,
  81. infra,
  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. // HandleProvisionAWSEKSInfra provisions a new aws EKS instance for a project
  96. func (app *App) HandleProvisionAWSEKSInfra(w http.ResponseWriter, r *http.Request) {
  97. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  98. if err != nil || projID == 0 {
  99. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  100. return
  101. }
  102. form := &forms.CreateEKSInfra{
  103. ProjectID: uint(projID),
  104. }
  105. // decode from JSON to form value
  106. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  107. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  108. return
  109. }
  110. // validate the form
  111. if err := app.validator.Struct(form); err != nil {
  112. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  113. return
  114. }
  115. // convert the form to an aws infra instance
  116. infra, err := form.ToAWSInfra()
  117. if err != nil {
  118. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  119. return
  120. }
  121. // handle write to the database
  122. infra, err = app.Repo.AWSInfra.CreateAWSInfra(infra)
  123. if err != nil {
  124. app.handleErrorDataWrite(err, w)
  125. return
  126. }
  127. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  128. if err != nil {
  129. app.handleErrorDataRead(err, w)
  130. return
  131. }
  132. // launch provisioning pod
  133. agent, err := kubernetes.GetAgentInClusterConfig()
  134. if err != nil {
  135. app.handleErrorDataRead(err, w)
  136. return
  137. }
  138. _, err = agent.ProvisionEKS(
  139. uint(projID),
  140. awsInt,
  141. form.EKSName,
  142. infra,
  143. )
  144. if err != nil {
  145. app.handleErrorInternal(err, w)
  146. return
  147. }
  148. app.Logger.Info().Msgf("New aws eks infra created: %d", infra.ID)
  149. w.WriteHeader(http.StatusCreated)
  150. infraExt := infra.Externalize()
  151. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  152. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  153. return
  154. }
  155. }
  156. // HandleGetProvisioningLogs returns real-time logs of the provisioning process via websockets
  157. func (app *App) HandleGetProvisioningLogs(w http.ResponseWriter, r *http.Request) {
  158. // get path parameters
  159. kind := chi.URLParam(r, "kind")
  160. projectID := chi.URLParam(r, "project_id")
  161. infraID := chi.URLParam(r, "infra_id")
  162. streamName := fmt.Sprintf("%s-%s-%s", kind, projectID, infraID)
  163. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  164. // upgrade to websocket.
  165. conn, err := upgrader.Upgrade(w, r, nil)
  166. if err != nil {
  167. app.handleErrorUpgradeWebsocket(err, w)
  168. }
  169. client, err := adapter.NewRedisClient(app.RedisConf)
  170. if err != nil {
  171. app.handleErrorInternal(err, w)
  172. return
  173. }
  174. err = provisioner.ResourceStream(client, streamName, conn)
  175. if err != nil {
  176. app.handleErrorWebsocketWrite(err, w)
  177. return
  178. }
  179. }