provision_handler.go 26 KB

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