provision_handler.go 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/porter-dev/porter/internal/forms"
  8. "github.com/porter-dev/porter/internal/kubernetes"
  9. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  10. "github.com/porter-dev/porter/internal/models"
  11. segment "gopkg.in/segmentio/analytics-go.v3"
  12. "fmt"
  13. "github.com/porter-dev/porter/internal/adapter"
  14. )
  15. // HandleProvisionTestInfra will create a test resource by deploying a provisioner
  16. // container pod
  17. func (app *App) HandleProvisionTestInfra(w http.ResponseWriter, r *http.Request) {
  18. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  19. if err != nil || projID == 0 {
  20. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  21. return
  22. }
  23. form := &forms.CreateTestInfra{
  24. ProjectID: uint(projID),
  25. }
  26. // decode from JSON to form value
  27. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  28. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  29. return
  30. }
  31. // convert the form to an aws infra instance
  32. infra, err := form.ToInfra()
  33. if err != nil {
  34. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  35. return
  36. }
  37. // handle write to the database
  38. infra, err = app.Repo.Infra.CreateInfra(infra)
  39. if err != nil {
  40. app.handleErrorDataWrite(err, w)
  41. return
  42. }
  43. _, err = app.ProvisionerAgent.ProvisionTest(
  44. uint(projID),
  45. infra,
  46. *app.Repo,
  47. provisioner.Apply,
  48. &app.DBConf,
  49. app.RedisConf,
  50. app.ServerConf.ProvisionerImageTag,
  51. app.ServerConf.ProvisionerImagePullSecret,
  52. )
  53. if err != nil {
  54. infra.Status = models.StatusError
  55. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  56. app.handleErrorInternal(err, w)
  57. return
  58. }
  59. app.Logger.Info().Msgf("New test infra created: %d", infra.ID)
  60. w.WriteHeader(http.StatusCreated)
  61. infraExt := infra.Externalize()
  62. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  63. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  64. return
  65. }
  66. }
  67. // HandleDestroyTestInfra destroys test infra
  68. func (app *App) HandleDestroyTestInfra(w http.ResponseWriter, r *http.Request) {
  69. // get path parameters
  70. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  71. if err != nil {
  72. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  73. return
  74. }
  75. // read infra to get id
  76. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  77. if err != nil {
  78. app.handleErrorDataRead(err, w)
  79. return
  80. }
  81. // launch provisioning destruction pod
  82. agent, err := kubernetes.GetAgentInClusterConfig()
  83. if err != nil {
  84. infra.Status = models.StatusError
  85. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  86. app.handleErrorDataRead(err, w)
  87. return
  88. }
  89. // mark infra for deletion
  90. infra.Status = models.StatusDestroying
  91. infra, err = app.Repo.Infra.UpdateInfra(infra)
  92. if err != nil {
  93. app.handleErrorDataWrite(err, w)
  94. return
  95. }
  96. _, err = agent.ProvisionTest(
  97. infra.ProjectID,
  98. infra,
  99. *app.Repo,
  100. provisioner.Destroy,
  101. &app.DBConf,
  102. app.RedisConf,
  103. app.ServerConf.ProvisionerImageTag,
  104. app.ServerConf.ProvisionerImagePullSecret,
  105. )
  106. if err != nil {
  107. app.handleErrorInternal(err, w)
  108. return
  109. }
  110. app.Logger.Info().Msgf("Test infra marked for destruction: %d", infra.ID)
  111. w.WriteHeader(http.StatusOK)
  112. }
  113. // HandleProvisionAWSECRInfra provisions a new aws ECR instance for a project
  114. func (app *App) HandleProvisionAWSECRInfra(w http.ResponseWriter, r *http.Request) {
  115. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  116. if err != nil || projID == 0 {
  117. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  118. return
  119. }
  120. form := &forms.CreateECRInfra{
  121. ProjectID: uint(projID),
  122. }
  123. // decode from JSON to form value
  124. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  125. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  126. return
  127. }
  128. // validate the form
  129. if err := app.validator.Struct(form); err != nil {
  130. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  131. return
  132. }
  133. // convert the form to an aws infra instance
  134. infra, err := form.ToInfra()
  135. if err != nil {
  136. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  137. return
  138. }
  139. // handle write to the database
  140. infra, err = app.Repo.Infra.CreateInfra(infra)
  141. if err != nil {
  142. app.handleErrorDataWrite(err, w)
  143. return
  144. }
  145. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  146. if err != nil {
  147. infra.Status = models.StatusError
  148. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  149. app.handleErrorDataRead(err, w)
  150. return
  151. }
  152. // launch provisioning pod
  153. _, err = app.ProvisionerAgent.ProvisionECR(
  154. uint(projID),
  155. awsInt,
  156. form.ECRName,
  157. *app.Repo,
  158. infra,
  159. provisioner.Apply,
  160. &app.DBConf,
  161. app.RedisConf,
  162. app.ServerConf.ProvisionerImageTag,
  163. app.ServerConf.ProvisionerImagePullSecret,
  164. )
  165. if err != nil {
  166. infra.Status = models.StatusError
  167. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  168. app.handleErrorInternal(err, w)
  169. return
  170. }
  171. app.Logger.Info().Msgf("New aws ecr infra created: %d", infra.ID)
  172. w.WriteHeader(http.StatusCreated)
  173. infraExt := infra.Externalize()
  174. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  175. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  176. return
  177. }
  178. }
  179. // HandleDestroyAWSECRInfra destroys ecr infra
  180. func (app *App) HandleDestroyAWSECRInfra(w http.ResponseWriter, r *http.Request) {
  181. // get path parameters
  182. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  183. if err != nil {
  184. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  185. return
  186. }
  187. // read infra to get id
  188. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  189. if err != nil {
  190. app.handleErrorDataRead(err, w)
  191. return
  192. }
  193. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  194. form := &forms.DestroyECRInfra{}
  195. // decode from JSON to form value
  196. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  197. infra.Status = models.StatusError
  198. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  199. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  200. return
  201. }
  202. // validate the form
  203. if err := app.validator.Struct(form); err != nil {
  204. infra.Status = models.StatusError
  205. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  206. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  207. return
  208. }
  209. // launch provisioning destruction pod
  210. // mark infra for deletion
  211. infra.Status = models.StatusDestroying
  212. infra, err = app.Repo.Infra.UpdateInfra(infra)
  213. if err != nil {
  214. app.handleErrorDataWrite(err, w)
  215. return
  216. }
  217. _, err = app.ProvisionerAgent.ProvisionECR(
  218. infra.ProjectID,
  219. awsInt,
  220. form.ECRName,
  221. *app.Repo,
  222. infra,
  223. provisioner.Destroy,
  224. &app.DBConf,
  225. app.RedisConf,
  226. app.ServerConf.ProvisionerImageTag,
  227. app.ServerConf.ProvisionerImagePullSecret,
  228. )
  229. if err != nil {
  230. app.handleErrorInternal(err, w)
  231. return
  232. }
  233. app.Logger.Info().Msgf("AWS ECR infra marked for destruction: %d", infra.ID)
  234. w.WriteHeader(http.StatusOK)
  235. }
  236. // HandleProvisionAWSEKSInfra provisions a new aws EKS instance for a project
  237. func (app *App) HandleProvisionAWSEKSInfra(w http.ResponseWriter, r *http.Request) {
  238. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  239. userID, err := app.getUserIDFromRequest(r)
  240. if err != nil || projID == 0 {
  241. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  242. return
  243. }
  244. form := &forms.CreateEKSInfra{
  245. ProjectID: uint(projID),
  246. }
  247. // decode from JSON to form value
  248. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  249. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  250. return
  251. }
  252. // validate the form
  253. if err := app.validator.Struct(form); err != nil {
  254. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  255. return
  256. }
  257. // convert the form to an aws infra instance
  258. infra, err := form.ToInfra()
  259. if err != nil {
  260. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  261. return
  262. }
  263. // handle write to the database
  264. infra, err = app.Repo.Infra.CreateInfra(infra)
  265. if err != nil {
  266. app.handleErrorDataWrite(err, w)
  267. return
  268. }
  269. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  270. if err != nil {
  271. infra.Status = models.StatusError
  272. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  273. app.handleErrorDataRead(err, w)
  274. return
  275. }
  276. // launch provisioning pod
  277. _, err = app.ProvisionerAgent.ProvisionEKS(
  278. uint(projID),
  279. awsInt,
  280. form.EKSName,
  281. form.MachineType,
  282. *app.Repo,
  283. infra,
  284. provisioner.Apply,
  285. &app.DBConf,
  286. app.RedisConf,
  287. app.ServerConf.ProvisionerImageTag,
  288. app.ServerConf.ProvisionerImagePullSecret,
  289. )
  290. if err != nil {
  291. infra.Status = models.StatusError
  292. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  293. app.handleErrorInternal(err, w)
  294. return
  295. }
  296. app.Logger.Info().Msgf("New aws eks infra created: %d", infra.ID)
  297. if app.segmentClient != nil {
  298. client := *app.segmentClient
  299. client.Enqueue(segment.Track{
  300. UserId: fmt.Sprintf("%v", userID),
  301. Event: "Provisioned a New EKS Cluster",
  302. Properties: segment.NewProperties().
  303. Set("project_id", projID).
  304. Set("cluster_name", form.EKSName),
  305. })
  306. }
  307. w.WriteHeader(http.StatusCreated)
  308. infraExt := infra.Externalize()
  309. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  310. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  311. return
  312. }
  313. }
  314. // HandleDestroyAWSEKSInfra destroys eks infra
  315. func (app *App) HandleDestroyAWSEKSInfra(w http.ResponseWriter, r *http.Request) {
  316. // get path parameters
  317. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  318. userID, err := app.getUserIDFromRequest(r)
  319. if err != nil {
  320. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  321. return
  322. }
  323. // read infra to get id
  324. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  325. if err != nil {
  326. app.handleErrorDataRead(err, w)
  327. return
  328. }
  329. awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(infra.AWSIntegrationID)
  330. form := &forms.DestroyEKSInfra{}
  331. // decode from JSON to form value
  332. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  333. infra.Status = models.StatusError
  334. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  335. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  336. return
  337. }
  338. // validate the form
  339. if err := app.validator.Struct(form); err != nil {
  340. infra.Status = models.StatusError
  341. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  342. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  343. return
  344. }
  345. // launch provisioning destruction pod
  346. // mark infra for deletion
  347. infra.Status = models.StatusDestroying
  348. infra, err = app.Repo.Infra.UpdateInfra(infra)
  349. if err != nil {
  350. app.handleErrorDataWrite(err, w)
  351. return
  352. }
  353. _, err = app.ProvisionerAgent.ProvisionEKS(
  354. infra.ProjectID,
  355. awsInt,
  356. form.EKSName,
  357. "",
  358. *app.Repo,
  359. infra,
  360. provisioner.Destroy,
  361. &app.DBConf,
  362. app.RedisConf,
  363. app.ServerConf.ProvisionerImageTag,
  364. app.ServerConf.ProvisionerImagePullSecret,
  365. )
  366. if err != nil {
  367. app.handleErrorInternal(err, w)
  368. return
  369. }
  370. app.Logger.Info().Msgf("AWS EKS infra marked for destruction: %d", infra.ID)
  371. if app.segmentClient != nil {
  372. client := *app.segmentClient
  373. client.Enqueue(segment.Track{
  374. UserId: fmt.Sprintf("%v", userID),
  375. Event: "Destroyed an EKS Cluster",
  376. Properties: segment.NewProperties().
  377. Set("project_id", infra.ProjectID).
  378. Set("cluster_name", form.EKSName),
  379. })
  380. }
  381. w.WriteHeader(http.StatusOK)
  382. }
  383. // HandleProvisionGCPGCRInfra enables GCR for a project
  384. func (app *App) HandleProvisionGCPGCRInfra(w http.ResponseWriter, r *http.Request) {
  385. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  386. if err != nil || projID == 0 {
  387. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  388. return
  389. }
  390. form := &forms.CreateGCRInfra{
  391. ProjectID: uint(projID),
  392. }
  393. // decode from JSON to form value
  394. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  395. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  396. return
  397. }
  398. // validate the form
  399. if err := app.validator.Struct(form); err != nil {
  400. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  401. return
  402. }
  403. // convert the form to an aws infra instance
  404. infra, err := form.ToInfra()
  405. if err != nil {
  406. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  407. return
  408. }
  409. // handle write to the database
  410. infra, err = app.Repo.Infra.CreateInfra(infra)
  411. if err != nil {
  412. app.handleErrorDataWrite(err, w)
  413. return
  414. }
  415. gcpInt, err := app.Repo.GCPIntegration.ReadGCPIntegration(infra.GCPIntegrationID)
  416. if err != nil {
  417. infra.Status = models.StatusError
  418. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  419. app.handleErrorDataRead(err, w)
  420. return
  421. }
  422. // launch provisioning pod
  423. _, err = app.ProvisionerAgent.ProvisionGCR(
  424. uint(projID),
  425. gcpInt,
  426. *app.Repo,
  427. infra,
  428. provisioner.Apply,
  429. &app.DBConf,
  430. app.RedisConf,
  431. app.ServerConf.ProvisionerImageTag,
  432. app.ServerConf.ProvisionerImagePullSecret,
  433. )
  434. if err != nil {
  435. infra.Status = models.StatusError
  436. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  437. app.handleErrorInternal(err, w)
  438. return
  439. }
  440. app.Logger.Info().Msgf("New gcp gcr infra created: %d", infra.ID)
  441. w.WriteHeader(http.StatusCreated)
  442. infraExt := infra.Externalize()
  443. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  444. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  445. return
  446. }
  447. }
  448. // HandleProvisionGCPGKEInfra provisions a new GKE instance for a project
  449. func (app *App) HandleProvisionGCPGKEInfra(w http.ResponseWriter, r *http.Request) {
  450. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  451. userID, err := app.getUserIDFromRequest(r)
  452. if err != nil || projID == 0 {
  453. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  454. return
  455. }
  456. form := &forms.CreateGKEInfra{
  457. ProjectID: uint(projID),
  458. }
  459. // decode from JSON to form value
  460. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  461. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  462. return
  463. }
  464. // validate the form
  465. if err := app.validator.Struct(form); err != nil {
  466. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  467. return
  468. }
  469. // convert the form to an aws infra instance
  470. infra, err := form.ToInfra()
  471. if err != nil {
  472. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  473. return
  474. }
  475. // handle write to the database
  476. infra, err = app.Repo.Infra.CreateInfra(infra)
  477. if err != nil {
  478. app.handleErrorDataWrite(err, w)
  479. return
  480. }
  481. gcpInt, err := app.Repo.GCPIntegration.ReadGCPIntegration(infra.GCPIntegrationID)
  482. if err != nil {
  483. infra.Status = models.StatusError
  484. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  485. app.handleErrorDataRead(err, w)
  486. return
  487. }
  488. // launch provisioning pod
  489. _, err = app.ProvisionerAgent.ProvisionGKE(
  490. uint(projID),
  491. gcpInt,
  492. form.GKEName,
  493. *app.Repo,
  494. infra,
  495. provisioner.Apply,
  496. &app.DBConf,
  497. app.RedisConf,
  498. app.ServerConf.ProvisionerImageTag,
  499. app.ServerConf.ProvisionerImagePullSecret,
  500. )
  501. if err != nil {
  502. infra.Status = models.StatusError
  503. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  504. app.handleErrorInternal(err, w)
  505. return
  506. }
  507. app.Logger.Info().Msgf("New gcp gke infra created: %d", infra.ID)
  508. if app.segmentClient != nil {
  509. client := *app.segmentClient
  510. client.Enqueue(segment.Track{
  511. UserId: fmt.Sprintf("%v", userID),
  512. Event: "Provisioned a New GKE Cluster",
  513. Properties: segment.NewProperties().
  514. Set("project_id", projID).
  515. Set("cluster_name", form.GKEName),
  516. })
  517. }
  518. w.WriteHeader(http.StatusCreated)
  519. infraExt := infra.Externalize()
  520. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  521. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  522. return
  523. }
  524. }
  525. // HandleDestroyGCPGKEInfra destroys gke infra
  526. func (app *App) HandleDestroyGCPGKEInfra(w http.ResponseWriter, r *http.Request) {
  527. // get path parameters
  528. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  529. userID, err := app.getUserIDFromRequest(r)
  530. if err != nil {
  531. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  532. return
  533. }
  534. // read infra to get id
  535. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  536. if err != nil {
  537. app.handleErrorDataRead(err, w)
  538. return
  539. }
  540. gcpInt, err := app.Repo.GCPIntegration.ReadGCPIntegration(infra.GCPIntegrationID)
  541. form := &forms.DestroyGKEInfra{}
  542. // decode from JSON to form value
  543. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  544. infra.Status = models.StatusError
  545. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  546. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  547. return
  548. }
  549. // validate the form
  550. if err := app.validator.Struct(form); err != nil {
  551. infra.Status = models.StatusError
  552. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  553. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  554. return
  555. }
  556. // launch provisioning destruction pod
  557. // mark infra for deletion
  558. infra.Status = models.StatusDestroying
  559. infra, err = app.Repo.Infra.UpdateInfra(infra)
  560. if err != nil {
  561. app.handleErrorDataWrite(err, w)
  562. return
  563. }
  564. _, err = app.ProvisionerAgent.ProvisionGKE(
  565. infra.ProjectID,
  566. gcpInt,
  567. form.GKEName,
  568. *app.Repo,
  569. infra,
  570. provisioner.Destroy,
  571. &app.DBConf,
  572. app.RedisConf,
  573. app.ServerConf.ProvisionerImageTag,
  574. app.ServerConf.ProvisionerImagePullSecret,
  575. )
  576. if err != nil {
  577. app.handleErrorInternal(err, w)
  578. return
  579. }
  580. app.Logger.Info().Msgf("GCP GKE infra marked for destruction: %d", infra.ID)
  581. if app.segmentClient != nil {
  582. client := *app.segmentClient
  583. client.Enqueue(segment.Track{
  584. UserId: fmt.Sprintf("%v", userID),
  585. Event: "Destroyed a GKE Cluster",
  586. Properties: segment.NewProperties().
  587. Set("project_id", infra.ProjectID).
  588. Set("cluster_name", form.GKEName),
  589. })
  590. }
  591. w.WriteHeader(http.StatusOK)
  592. }
  593. // HandleGetProvisioningLogs returns real-time logs of the provisioning process via websockets
  594. func (app *App) HandleGetProvisioningLogs(w http.ResponseWriter, r *http.Request) {
  595. // get path parameters
  596. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  597. if err != nil {
  598. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  599. return
  600. }
  601. // read infra to get id
  602. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  603. if err != nil {
  604. app.handleErrorDataRead(err, w)
  605. return
  606. }
  607. streamName := infra.GetUniqueName()
  608. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  609. // upgrade to websocket.
  610. conn, err := upgrader.Upgrade(w, r, nil)
  611. if err != nil {
  612. app.handleErrorUpgradeWebsocket(err, w)
  613. }
  614. client, err := adapter.NewRedisClient(app.RedisConf)
  615. if err != nil {
  616. app.handleErrorInternal(err, w)
  617. return
  618. }
  619. err = provisioner.ResourceStream(client, streamName, conn)
  620. if err != nil {
  621. app.handleErrorWebsocketWrite(err, w)
  622. return
  623. }
  624. }
  625. // HandleProvisionDODOCRInfra provisions a new digitalocean DOCR instance for a project
  626. func (app *App) HandleProvisionDODOCRInfra(w http.ResponseWriter, r *http.Request) {
  627. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  628. if err != nil || projID == 0 {
  629. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  630. return
  631. }
  632. form := &forms.CreateDOCRInfra{
  633. ProjectID: uint(projID),
  634. }
  635. // decode from JSON to form value
  636. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  637. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  638. return
  639. }
  640. // validate the form
  641. if err := app.validator.Struct(form); err != nil {
  642. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  643. return
  644. }
  645. // convert the form to an aws infra instance
  646. infra, err := form.ToInfra()
  647. if err != nil {
  648. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  649. return
  650. }
  651. // handle write to the database
  652. infra, err = app.Repo.Infra.CreateInfra(infra)
  653. if err != nil {
  654. app.handleErrorDataWrite(err, w)
  655. return
  656. }
  657. oauthInt, err := app.Repo.OAuthIntegration.ReadOAuthIntegration(infra.DOIntegrationID)
  658. if err != nil {
  659. infra.Status = models.StatusError
  660. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  661. app.handleErrorDataRead(err, w)
  662. return
  663. }
  664. // launch provisioning pod
  665. _, err = app.ProvisionerAgent.ProvisionDOCR(
  666. uint(projID),
  667. oauthInt,
  668. app.DOConf,
  669. *app.Repo,
  670. form.DOCRName,
  671. form.DOCRSubscriptionTier,
  672. infra,
  673. provisioner.Apply,
  674. &app.DBConf,
  675. app.RedisConf,
  676. app.ServerConf.ProvisionerImageTag,
  677. app.ServerConf.ProvisionerImagePullSecret,
  678. )
  679. if err != nil {
  680. infra.Status = models.StatusError
  681. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  682. app.handleErrorInternal(err, w)
  683. return
  684. }
  685. app.Logger.Info().Msgf("New do docr infra created: %d", infra.ID)
  686. w.WriteHeader(http.StatusCreated)
  687. infraExt := infra.Externalize()
  688. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  689. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  690. return
  691. }
  692. }
  693. // HandleDestroyAWSDOCRInfra destroys docr infra
  694. func (app *App) HandleDestroyDODOCRInfra(w http.ResponseWriter, r *http.Request) {
  695. // get path parameters
  696. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  697. if err != nil {
  698. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  699. return
  700. }
  701. // read infra to get id
  702. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  703. if err != nil {
  704. app.handleErrorDataRead(err, w)
  705. return
  706. }
  707. oauthInt, err := app.Repo.OAuthIntegration.ReadOAuthIntegration(infra.DOIntegrationID)
  708. form := &forms.DestroyDOCRInfra{}
  709. // decode from JSON to form value
  710. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  711. infra.Status = models.StatusError
  712. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  713. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  714. return
  715. }
  716. // validate the form
  717. if err := app.validator.Struct(form); err != nil {
  718. infra.Status = models.StatusError
  719. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  720. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  721. return
  722. }
  723. // launch provisioning destruction pod
  724. // mark infra for deletion
  725. infra.Status = models.StatusDestroying
  726. infra, err = app.Repo.Infra.UpdateInfra(infra)
  727. if err != nil {
  728. app.handleErrorDataWrite(err, w)
  729. return
  730. }
  731. _, err = app.ProvisionerAgent.ProvisionDOCR(
  732. infra.ProjectID,
  733. oauthInt,
  734. app.DOConf,
  735. *app.Repo,
  736. form.DOCRName,
  737. "basic", // this doesn't matter for destroy
  738. infra,
  739. provisioner.Destroy,
  740. &app.DBConf,
  741. app.RedisConf,
  742. app.ServerConf.ProvisionerImageTag,
  743. app.ServerConf.ProvisionerImagePullSecret,
  744. )
  745. if err != nil {
  746. app.handleErrorInternal(err, w)
  747. return
  748. }
  749. app.Logger.Info().Msgf("DO DOCR infra marked for destruction: %d", infra.ID)
  750. w.WriteHeader(http.StatusOK)
  751. }
  752. // HandleProvisionDODOKSInfra provisions a new DO DOKS instance for a project
  753. func (app *App) HandleProvisionDODOKSInfra(w http.ResponseWriter, r *http.Request) {
  754. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  755. userID, err := app.getUserIDFromRequest(r)
  756. if err != nil || projID == 0 {
  757. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  758. return
  759. }
  760. form := &forms.CreateDOKSInfra{
  761. ProjectID: uint(projID),
  762. }
  763. // decode from JSON to form value
  764. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  765. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  766. return
  767. }
  768. // validate the form
  769. if err := app.validator.Struct(form); err != nil {
  770. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  771. return
  772. }
  773. // convert the form to an aws infra instance
  774. infra, err := form.ToInfra()
  775. if err != nil {
  776. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  777. return
  778. }
  779. // handle write to the database
  780. infra, err = app.Repo.Infra.CreateInfra(infra)
  781. if err != nil {
  782. app.handleErrorDataWrite(err, w)
  783. return
  784. }
  785. oauthInt, err := app.Repo.OAuthIntegration.ReadOAuthIntegration(infra.DOIntegrationID)
  786. if err != nil {
  787. infra.Status = models.StatusError
  788. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  789. app.handleErrorDataRead(err, w)
  790. return
  791. }
  792. // launch provisioning pod
  793. _, err = app.ProvisionerAgent.ProvisionDOKS(
  794. uint(projID),
  795. oauthInt,
  796. app.DOConf,
  797. *app.Repo,
  798. form.DORegion,
  799. form.DOKSName,
  800. infra,
  801. provisioner.Apply,
  802. &app.DBConf,
  803. app.RedisConf,
  804. app.ServerConf.ProvisionerImageTag,
  805. app.ServerConf.ProvisionerImagePullSecret,
  806. )
  807. if err != nil {
  808. infra.Status = models.StatusError
  809. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  810. app.handleErrorInternal(err, w)
  811. return
  812. }
  813. app.Logger.Info().Msgf("New do doks infra created: %d", infra.ID)
  814. if app.segmentClient != nil {
  815. client := *app.segmentClient
  816. client.Enqueue(segment.Track{
  817. UserId: fmt.Sprintf("%v", userID),
  818. Event: "Provisioned a New DOKS Cluster",
  819. Properties: segment.NewProperties().
  820. Set("project_id", projID).
  821. Set("cluster_name", form.DOKSName),
  822. })
  823. }
  824. w.WriteHeader(http.StatusCreated)
  825. infraExt := infra.Externalize()
  826. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  827. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  828. return
  829. }
  830. }
  831. // HandleDestroyDODOKSInfra destroys DOKS infra
  832. func (app *App) HandleDestroyDODOKSInfra(w http.ResponseWriter, r *http.Request) {
  833. // get path parameters
  834. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  835. userID, err := app.getUserIDFromRequest(r)
  836. if err != nil {
  837. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  838. return
  839. }
  840. // read infra to get id
  841. infra, err := app.Repo.Infra.ReadInfra(uint(infraID))
  842. if err != nil {
  843. app.handleErrorDataRead(err, w)
  844. return
  845. }
  846. oauthInt, err := app.Repo.OAuthIntegration.ReadOAuthIntegration(infra.DOIntegrationID)
  847. form := &forms.DestroyDOKSInfra{}
  848. // decode from JSON to form value
  849. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  850. infra.Status = models.StatusError
  851. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  852. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  853. return
  854. }
  855. // validate the form
  856. if err := app.validator.Struct(form); err != nil {
  857. infra.Status = models.StatusError
  858. infra, _ = app.Repo.Infra.UpdateInfra(infra)
  859. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  860. return
  861. }
  862. // launch provisioning destruction pod
  863. // mark infra for deletion
  864. infra.Status = models.StatusDestroying
  865. infra, err = app.Repo.Infra.UpdateInfra(infra)
  866. if err != nil {
  867. app.handleErrorDataWrite(err, w)
  868. return
  869. }
  870. _, err = app.ProvisionerAgent.ProvisionDOKS(
  871. infra.ProjectID,
  872. oauthInt,
  873. app.DOConf,
  874. *app.Repo,
  875. "nyc1",
  876. form.DOKSName,
  877. infra,
  878. provisioner.Destroy,
  879. &app.DBConf,
  880. app.RedisConf,
  881. app.ServerConf.ProvisionerImageTag,
  882. app.ServerConf.ProvisionerImagePullSecret,
  883. )
  884. if err != nil {
  885. app.handleErrorInternal(err, w)
  886. return
  887. }
  888. app.Logger.Info().Msgf("DO DOKS infra marked for destruction: %d", infra.ID)
  889. if app.segmentClient != nil {
  890. client := *app.segmentClient
  891. client.Enqueue(segment.Track{
  892. UserId: fmt.Sprintf("%v", userID),
  893. Event: "Destroyed a DOKS Cluster",
  894. Properties: segment.NewProperties().
  895. Set("project_id", infra.ProjectID).
  896. Set("cluster_name", form.DOKSName),
  897. })
  898. }
  899. w.WriteHeader(http.StatusOK)
  900. }