provision_handler.go 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "fmt"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/adapter"
  10. "github.com/porter-dev/porter/internal/analytics"
  11. "github.com/porter-dev/porter/internal/forms"
  12. "github.com/porter-dev/porter/internal/kubernetes"
  13. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  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 = types.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.ToInfraType()
  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 = types.StatusError
  85. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  86. app.handleErrorDataRead(err, w)
  87. return
  88. }
  89. // mark infra for deletion
  90. infra.Status = types.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 = types.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 = types.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.ToInfraType()
  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 = types.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 = types.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 = types.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 = types.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 = types.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. app.analyticsClient.Track(analytics.CreateSegmentNewClusterEvent(
  298. &analytics.NewClusterEventOpts{
  299. UserId: fmt.Sprintf("%d", userID),
  300. ProjId: fmt.Sprintf("%d", infra.ProjectID),
  301. ClusterName: form.EKSName,
  302. ClusterType: "EKS",
  303. EventType: "provisioned",
  304. },
  305. ))
  306. w.WriteHeader(http.StatusCreated)
  307. infraExt := infra.ToInfraType()
  308. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  309. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  310. return
  311. }
  312. }
  313. // HandleDestroyAWSEKSInfra destroys eks infra
  314. func (app *App) HandleDestroyAWSEKSInfra(w http.ResponseWriter, r *http.Request) {
  315. // get path parameters
  316. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  317. userID, err := app.getUserIDFromRequest(r)
  318. if err != nil {
  319. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  320. return
  321. }
  322. // read infra to get id
  323. infra, err := app.Repo.Infra().ReadInfra(uint(infraID))
  324. if err != nil {
  325. app.handleErrorDataRead(err, w)
  326. return
  327. }
  328. awsInt, err := app.Repo.AWSIntegration().ReadAWSIntegration(infra.AWSIntegrationID)
  329. form := &forms.DestroyEKSInfra{}
  330. // decode from JSON to form value
  331. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  332. infra.Status = types.StatusError
  333. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  334. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  335. return
  336. }
  337. // validate the form
  338. if err := app.validator.Struct(form); err != nil {
  339. infra.Status = types.StatusError
  340. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  341. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  342. return
  343. }
  344. // launch provisioning destruction pod
  345. // mark infra for deletion
  346. infra.Status = types.StatusDestroying
  347. infra, err = app.Repo.Infra().UpdateInfra(infra)
  348. if err != nil {
  349. app.handleErrorDataWrite(err, w)
  350. return
  351. }
  352. _, err = app.ProvisionerAgent.ProvisionEKS(
  353. infra.ProjectID,
  354. awsInt,
  355. form.EKSName,
  356. "",
  357. app.Repo,
  358. infra,
  359. provisioner.Destroy,
  360. &app.DBConf,
  361. app.RedisConf,
  362. app.ServerConf.ProvisionerImageTag,
  363. app.ServerConf.ProvisionerImagePullSecret,
  364. )
  365. if err != nil {
  366. app.handleErrorInternal(err, w)
  367. return
  368. }
  369. app.Logger.Info().Msgf("AWS EKS infra marked for destruction: %d", infra.ID)
  370. app.analyticsClient.Track(analytics.CreateSegmentNewClusterEvent(
  371. &analytics.NewClusterEventOpts{
  372. UserId: fmt.Sprintf("%d", userID),
  373. ProjId: fmt.Sprintf("%d", infra.ProjectID),
  374. ClusterName: form.EKSName,
  375. ClusterType: "EKS",
  376. EventType: "destroyed",
  377. },
  378. ))
  379. w.WriteHeader(http.StatusOK)
  380. }
  381. // HandleProvisionGCPGCRInfra enables GCR for a project
  382. func (app *App) HandleProvisionGCPGCRInfra(w http.ResponseWriter, r *http.Request) {
  383. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  384. if err != nil || projID == 0 {
  385. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  386. return
  387. }
  388. form := &forms.CreateGCRInfra{
  389. ProjectID: uint(projID),
  390. }
  391. // decode from JSON to form value
  392. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  393. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  394. return
  395. }
  396. // validate the form
  397. if err := app.validator.Struct(form); err != nil {
  398. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  399. return
  400. }
  401. // convert the form to an aws infra instance
  402. infra, err := form.ToInfra()
  403. if err != nil {
  404. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  405. return
  406. }
  407. // handle write to the database
  408. infra, err = app.Repo.Infra().CreateInfra(infra)
  409. if err != nil {
  410. app.handleErrorDataWrite(err, w)
  411. return
  412. }
  413. gcpInt, err := app.Repo.GCPIntegration().ReadGCPIntegration(infra.GCPIntegrationID)
  414. if err != nil {
  415. infra.Status = types.StatusError
  416. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  417. app.handleErrorDataRead(err, w)
  418. return
  419. }
  420. // launch provisioning pod
  421. _, err = app.ProvisionerAgent.ProvisionGCR(
  422. uint(projID),
  423. gcpInt,
  424. app.Repo,
  425. infra,
  426. provisioner.Apply,
  427. &app.DBConf,
  428. app.RedisConf,
  429. app.ServerConf.ProvisionerImageTag,
  430. app.ServerConf.ProvisionerImagePullSecret,
  431. )
  432. if err != nil {
  433. infra.Status = types.StatusError
  434. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  435. app.handleErrorInternal(err, w)
  436. return
  437. }
  438. app.Logger.Info().Msgf("New gcp gcr infra created: %d", infra.ID)
  439. w.WriteHeader(http.StatusCreated)
  440. infraExt := infra.ToInfraType()
  441. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  442. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  443. return
  444. }
  445. }
  446. // HandleProvisionGCPGKEInfra provisions a new GKE instance for a project
  447. func (app *App) HandleProvisionGCPGKEInfra(w http.ResponseWriter, r *http.Request) {
  448. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  449. userID, err := app.getUserIDFromRequest(r)
  450. if err != nil || projID == 0 {
  451. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  452. return
  453. }
  454. form := &forms.CreateGKEInfra{
  455. ProjectID: uint(projID),
  456. }
  457. // decode from JSON to form value
  458. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  459. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  460. return
  461. }
  462. // validate the form
  463. if err := app.validator.Struct(form); err != nil {
  464. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  465. return
  466. }
  467. // convert the form to an aws infra instance
  468. infra, err := form.ToInfra()
  469. if err != nil {
  470. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  471. return
  472. }
  473. // handle write to the database
  474. infra, err = app.Repo.Infra().CreateInfra(infra)
  475. if err != nil {
  476. app.handleErrorDataWrite(err, w)
  477. return
  478. }
  479. gcpInt, err := app.Repo.GCPIntegration().ReadGCPIntegration(infra.GCPIntegrationID)
  480. if err != nil {
  481. infra.Status = types.StatusError
  482. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  483. app.handleErrorDataRead(err, w)
  484. return
  485. }
  486. // launch provisioning pod
  487. _, err = app.ProvisionerAgent.ProvisionGKE(
  488. uint(projID),
  489. gcpInt,
  490. form.GKEName,
  491. app.Repo,
  492. infra,
  493. provisioner.Apply,
  494. &app.DBConf,
  495. app.RedisConf,
  496. app.ServerConf.ProvisionerImageTag,
  497. app.ServerConf.ProvisionerImagePullSecret,
  498. )
  499. if err != nil {
  500. infra.Status = types.StatusError
  501. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  502. app.handleErrorInternal(err, w)
  503. return
  504. }
  505. app.Logger.Info().Msgf("New gcp gke infra created: %d", infra.ID)
  506. app.analyticsClient.Track(analytics.CreateSegmentNewClusterEvent(
  507. &analytics.NewClusterEventOpts{
  508. UserId: fmt.Sprintf("%d", userID),
  509. ProjId: fmt.Sprintf("%d", infra.ProjectID),
  510. ClusterName: form.GKEName,
  511. ClusterType: "GKE",
  512. EventType: "provisioned",
  513. },
  514. ))
  515. w.WriteHeader(http.StatusCreated)
  516. infraExt := infra.ToInfraType()
  517. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  518. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  519. return
  520. }
  521. }
  522. // HandleDestroyGCPGKEInfra destroys gke infra
  523. func (app *App) HandleDestroyGCPGKEInfra(w http.ResponseWriter, r *http.Request) {
  524. // get path parameters
  525. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  526. userID, err := app.getUserIDFromRequest(r)
  527. if err != nil {
  528. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  529. return
  530. }
  531. // read infra to get id
  532. infra, err := app.Repo.Infra().ReadInfra(uint(infraID))
  533. if err != nil {
  534. app.handleErrorDataRead(err, w)
  535. return
  536. }
  537. gcpInt, err := app.Repo.GCPIntegration().ReadGCPIntegration(infra.GCPIntegrationID)
  538. form := &forms.DestroyGKEInfra{}
  539. // decode from JSON to form value
  540. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  541. infra.Status = types.StatusError
  542. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  543. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  544. return
  545. }
  546. // validate the form
  547. if err := app.validator.Struct(form); err != nil {
  548. infra.Status = types.StatusError
  549. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  550. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  551. return
  552. }
  553. // launch provisioning destruction pod
  554. // mark infra for deletion
  555. infra.Status = types.StatusDestroying
  556. infra, err = app.Repo.Infra().UpdateInfra(infra)
  557. if err != nil {
  558. app.handleErrorDataWrite(err, w)
  559. return
  560. }
  561. _, err = app.ProvisionerAgent.ProvisionGKE(
  562. infra.ProjectID,
  563. gcpInt,
  564. form.GKEName,
  565. app.Repo,
  566. infra,
  567. provisioner.Destroy,
  568. &app.DBConf,
  569. app.RedisConf,
  570. app.ServerConf.ProvisionerImageTag,
  571. app.ServerConf.ProvisionerImagePullSecret,
  572. )
  573. if err != nil {
  574. app.handleErrorInternal(err, w)
  575. return
  576. }
  577. app.Logger.Info().Msgf("GCP GKE infra marked for destruction: %d", infra.ID)
  578. app.analyticsClient.Track(analytics.CreateSegmentNewClusterEvent(
  579. &analytics.NewClusterEventOpts{
  580. UserId: fmt.Sprintf("%d", userID),
  581. ProjId: fmt.Sprintf("%d", infra.ProjectID),
  582. ClusterName: form.GKEName,
  583. ClusterType: "GKE",
  584. EventType: "destroyed",
  585. },
  586. ))
  587. w.WriteHeader(http.StatusOK)
  588. }
  589. // HandleGetProvisioningLogs returns real-time logs of the provisioning process via websockets
  590. func (app *App) HandleGetProvisioningLogs(w http.ResponseWriter, r *http.Request) {
  591. // get path parameters
  592. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  593. if err != nil {
  594. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  595. return
  596. }
  597. // read infra to get id
  598. infra, err := app.Repo.Infra().ReadInfra(uint(infraID))
  599. if err != nil {
  600. app.handleErrorDataRead(err, w)
  601. return
  602. }
  603. streamName := infra.GetUniqueName()
  604. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  605. // upgrade to websocket.
  606. conn, err := upgrader.Upgrade(w, r, nil)
  607. if err != nil {
  608. app.handleErrorUpgradeWebsocket(err, w)
  609. }
  610. client, err := adapter.NewRedisClient(app.RedisConf)
  611. if err != nil {
  612. app.handleErrorInternal(err, w)
  613. return
  614. }
  615. err = provisioner.ResourceStream(client, streamName, conn)
  616. if err != nil {
  617. app.handleErrorWebsocketWrite(err, w)
  618. return
  619. }
  620. }
  621. // HandleProvisionDODOCRInfra provisions a new digitalocean DOCR instance for a project
  622. func (app *App) HandleProvisionDODOCRInfra(w http.ResponseWriter, r *http.Request) {
  623. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  624. if err != nil || projID == 0 {
  625. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  626. return
  627. }
  628. form := &forms.CreateDOCRInfra{
  629. ProjectID: uint(projID),
  630. }
  631. // decode from JSON to form value
  632. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  633. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  634. return
  635. }
  636. // validate the form
  637. if err := app.validator.Struct(form); err != nil {
  638. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  639. return
  640. }
  641. // convert the form to an aws infra instance
  642. infra, err := form.ToInfra()
  643. if err != nil {
  644. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  645. return
  646. }
  647. // handle write to the database
  648. infra, err = app.Repo.Infra().CreateInfra(infra)
  649. if err != nil {
  650. app.handleErrorDataWrite(err, w)
  651. return
  652. }
  653. oauthInt, err := app.Repo.OAuthIntegration().ReadOAuthIntegration(infra.DOIntegrationID)
  654. if err != nil {
  655. infra.Status = types.StatusError
  656. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  657. app.handleErrorDataRead(err, w)
  658. return
  659. }
  660. // launch provisioning pod
  661. _, err = app.ProvisionerAgent.ProvisionDOCR(
  662. uint(projID),
  663. oauthInt,
  664. app.DOConf,
  665. app.Repo,
  666. form.DOCRName,
  667. form.DOCRSubscriptionTier,
  668. infra,
  669. provisioner.Apply,
  670. &app.DBConf,
  671. app.RedisConf,
  672. app.ServerConf.ProvisionerImageTag,
  673. app.ServerConf.ProvisionerImagePullSecret,
  674. )
  675. if err != nil {
  676. infra.Status = types.StatusError
  677. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  678. app.handleErrorInternal(err, w)
  679. return
  680. }
  681. app.Logger.Info().Msgf("New do docr infra created: %d", infra.ID)
  682. w.WriteHeader(http.StatusCreated)
  683. infraExt := infra.ToInfraType()
  684. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  685. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  686. return
  687. }
  688. }
  689. // HandleDestroyAWSDOCRInfra destroys docr infra
  690. func (app *App) HandleDestroyDODOCRInfra(w http.ResponseWriter, r *http.Request) {
  691. // get path parameters
  692. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  693. if err != nil {
  694. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  695. return
  696. }
  697. // read infra to get id
  698. infra, err := app.Repo.Infra().ReadInfra(uint(infraID))
  699. if err != nil {
  700. app.handleErrorDataRead(err, w)
  701. return
  702. }
  703. oauthInt, err := app.Repo.OAuthIntegration().ReadOAuthIntegration(infra.DOIntegrationID)
  704. form := &forms.DestroyDOCRInfra{}
  705. // decode from JSON to form value
  706. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  707. infra.Status = types.StatusError
  708. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  709. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  710. return
  711. }
  712. // validate the form
  713. if err := app.validator.Struct(form); err != nil {
  714. infra.Status = types.StatusError
  715. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  716. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  717. return
  718. }
  719. // launch provisioning destruction pod
  720. // mark infra for deletion
  721. infra.Status = types.StatusDestroying
  722. infra, err = app.Repo.Infra().UpdateInfra(infra)
  723. if err != nil {
  724. app.handleErrorDataWrite(err, w)
  725. return
  726. }
  727. _, err = app.ProvisionerAgent.ProvisionDOCR(
  728. infra.ProjectID,
  729. oauthInt,
  730. app.DOConf,
  731. app.Repo,
  732. form.DOCRName,
  733. "basic", // this doesn't matter for destroy
  734. infra,
  735. provisioner.Destroy,
  736. &app.DBConf,
  737. app.RedisConf,
  738. app.ServerConf.ProvisionerImageTag,
  739. app.ServerConf.ProvisionerImagePullSecret,
  740. )
  741. if err != nil {
  742. app.handleErrorInternal(err, w)
  743. return
  744. }
  745. app.Logger.Info().Msgf("DO DOCR infra marked for destruction: %d", infra.ID)
  746. w.WriteHeader(http.StatusOK)
  747. }
  748. // HandleProvisionDODOKSInfra provisions a new DO DOKS instance for a project
  749. func (app *App) HandleProvisionDODOKSInfra(w http.ResponseWriter, r *http.Request) {
  750. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  751. userID, err := app.getUserIDFromRequest(r)
  752. if err != nil || projID == 0 {
  753. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  754. return
  755. }
  756. form := &forms.CreateDOKSInfra{
  757. ProjectID: uint(projID),
  758. }
  759. // decode from JSON to form value
  760. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  761. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  762. return
  763. }
  764. // validate the form
  765. if err := app.validator.Struct(form); err != nil {
  766. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  767. return
  768. }
  769. // convert the form to an aws infra instance
  770. infra, err := form.ToInfra()
  771. if err != nil {
  772. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  773. return
  774. }
  775. // handle write to the database
  776. infra, err = app.Repo.Infra().CreateInfra(infra)
  777. if err != nil {
  778. app.handleErrorDataWrite(err, w)
  779. return
  780. }
  781. oauthInt, err := app.Repo.OAuthIntegration().ReadOAuthIntegration(infra.DOIntegrationID)
  782. if err != nil {
  783. infra.Status = types.StatusError
  784. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  785. app.handleErrorDataRead(err, w)
  786. return
  787. }
  788. // launch provisioning pod
  789. _, err = app.ProvisionerAgent.ProvisionDOKS(
  790. uint(projID),
  791. oauthInt,
  792. app.DOConf,
  793. app.Repo,
  794. form.DORegion,
  795. form.DOKSName,
  796. infra,
  797. provisioner.Apply,
  798. &app.DBConf,
  799. app.RedisConf,
  800. app.ServerConf.ProvisionerImageTag,
  801. app.ServerConf.ProvisionerImagePullSecret,
  802. )
  803. if err != nil {
  804. infra.Status = types.StatusError
  805. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  806. app.handleErrorInternal(err, w)
  807. return
  808. }
  809. app.Logger.Info().Msgf("New do doks infra created: %d", infra.ID)
  810. app.analyticsClient.Track(analytics.CreateSegmentNewClusterEvent(
  811. &analytics.NewClusterEventOpts{
  812. UserId: fmt.Sprintf("%d", userID),
  813. ProjId: fmt.Sprintf("%d", infra.ProjectID),
  814. ClusterName: form.DOKSName,
  815. ClusterType: "DOKS",
  816. EventType: "provisioned",
  817. },
  818. ))
  819. w.WriteHeader(http.StatusCreated)
  820. infraExt := infra.ToInfraType()
  821. if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  822. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  823. return
  824. }
  825. }
  826. // HandleDestroyDODOKSInfra destroys DOKS infra
  827. func (app *App) HandleDestroyDODOKSInfra(w http.ResponseWriter, r *http.Request) {
  828. // get path parameters
  829. infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
  830. userID, err := app.getUserIDFromRequest(r)
  831. if err != nil {
  832. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  833. return
  834. }
  835. // read infra to get id
  836. infra, err := app.Repo.Infra().ReadInfra(uint(infraID))
  837. if err != nil {
  838. app.handleErrorDataRead(err, w)
  839. return
  840. }
  841. oauthInt, err := app.Repo.OAuthIntegration().ReadOAuthIntegration(infra.DOIntegrationID)
  842. form := &forms.DestroyDOKSInfra{}
  843. // decode from JSON to form value
  844. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  845. infra.Status = types.StatusError
  846. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  847. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  848. return
  849. }
  850. // validate the form
  851. if err := app.validator.Struct(form); err != nil {
  852. infra.Status = types.StatusError
  853. infra, _ = app.Repo.Infra().UpdateInfra(infra)
  854. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  855. return
  856. }
  857. // launch provisioning destruction pod
  858. // mark infra for deletion
  859. infra.Status = types.StatusDestroying
  860. infra, err = app.Repo.Infra().UpdateInfra(infra)
  861. if err != nil {
  862. app.handleErrorDataWrite(err, w)
  863. return
  864. }
  865. _, err = app.ProvisionerAgent.ProvisionDOKS(
  866. infra.ProjectID,
  867. oauthInt,
  868. app.DOConf,
  869. app.Repo,
  870. "nyc1",
  871. form.DOKSName,
  872. infra,
  873. provisioner.Destroy,
  874. &app.DBConf,
  875. app.RedisConf,
  876. app.ServerConf.ProvisionerImageTag,
  877. app.ServerConf.ProvisionerImagePullSecret,
  878. )
  879. if err != nil {
  880. app.handleErrorInternal(err, w)
  881. return
  882. }
  883. app.Logger.Info().Msgf("DO DOKS infra marked for destruction: %d", infra.ID)
  884. app.analyticsClient.Track(analytics.CreateSegmentNewClusterEvent(
  885. &analytics.NewClusterEventOpts{
  886. UserId: fmt.Sprintf("%d", userID),
  887. ProjId: fmt.Sprintf("%d", infra.ProjectID),
  888. ClusterName: form.DOKSName,
  889. ClusterType: "DOKS",
  890. EventType: "destroyed",
  891. },
  892. ))
  893. w.WriteHeader(http.StatusOK)
  894. }