provision_handler.go 23 KB

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