provision_handler.go 24 KB

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