provision_handler.go 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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/internal/analytics"
  9. "github.com/porter-dev/porter/internal/forms"
  10. "github.com/porter-dev/porter/internal/kubernetes"
  11. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  12. "github.com/porter-dev/porter/internal/models"
  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. 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.Externalize()
  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 = models.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 = models.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 = models.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 = models.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 = models.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.Externalize()
  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 = models.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 = models.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.Externalize()
  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 = models.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 = models.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 = models.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 = models.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 = models.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.Externalize()
  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 = models.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 = models.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 = models.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 = models.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 = models.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.Externalize()
  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 = models.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 = models.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 = models.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. }