2
0

provision_handler.go 23 KB

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