deploy_handler.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "github.com/go-chi/chi"
  10. "github.com/porter-dev/porter/internal/forms"
  11. "github.com/porter-dev/porter/internal/helm"
  12. "github.com/porter-dev/porter/internal/helm/loader"
  13. "github.com/porter-dev/porter/internal/integrations/ci/actions"
  14. "github.com/porter-dev/porter/internal/models"
  15. "github.com/porter-dev/porter/internal/repository"
  16. "gopkg.in/yaml.v2"
  17. )
  18. // HandleDeployTemplate triggers a chart deployment from a template
  19. func (app *App) HandleDeployTemplate(w http.ResponseWriter, r *http.Request) {
  20. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  21. if err != nil || projID == 0 {
  22. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  23. return
  24. }
  25. name := chi.URLParam(r, "name")
  26. version := chi.URLParam(r, "version")
  27. // if version passed as latest, pass empty string to loader to get latest
  28. if version == "latest" {
  29. version = ""
  30. }
  31. getChartForm := &forms.ChartForm{
  32. Name: name,
  33. Version: version,
  34. RepoURL: app.ServerConf.DefaultApplicationHelmRepoURL,
  35. }
  36. // if a repo_url is passed as query param, it will be populated
  37. vals, err := url.ParseQuery(r.URL.RawQuery)
  38. if err != nil {
  39. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  40. return
  41. }
  42. getChartForm.PopulateRepoURLFromQueryParams(vals)
  43. chart, err := loader.LoadChartPublic(getChartForm.RepoURL, getChartForm.Name, getChartForm.Version)
  44. if err != nil {
  45. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  46. return
  47. }
  48. form := &forms.InstallChartTemplateForm{
  49. ReleaseForm: &forms.ReleaseForm{
  50. Form: &helm.Form{
  51. Repo: app.Repo,
  52. DigitalOceanOAuth: app.DOConf,
  53. },
  54. },
  55. ChartTemplateForm: &forms.ChartTemplateForm{},
  56. }
  57. form.ReleaseForm.PopulateHelmOptionsFromQueryParams(
  58. vals,
  59. app.Repo.Cluster,
  60. )
  61. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  62. app.handleErrorFormDecoding(err, ErrUserDecode, w)
  63. return
  64. }
  65. agent, err := app.getAgentFromReleaseForm(
  66. w,
  67. r,
  68. form.ReleaseForm,
  69. )
  70. if err != nil {
  71. app.handleErrorFormDecoding(err, ErrUserDecode, w)
  72. return
  73. }
  74. registries, err := app.Repo.Registry.ListRegistriesByProjectID(uint(projID))
  75. if err != nil {
  76. app.handleErrorDataRead(err, w)
  77. return
  78. }
  79. conf := &helm.InstallChartConfig{
  80. Chart: chart,
  81. Name: form.ChartTemplateForm.Name,
  82. Namespace: form.ReleaseForm.Form.Namespace,
  83. Values: form.ChartTemplateForm.FormValues,
  84. Cluster: form.ReleaseForm.Cluster,
  85. Repo: *app.Repo,
  86. Registries: registries,
  87. }
  88. rel, err := agent.InstallChart(conf, app.DOConf)
  89. if err != nil {
  90. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  91. Code: ErrReleaseDeploy,
  92. Errors: []string{"error installing a new chart: " + err.Error()},
  93. }, w)
  94. return
  95. }
  96. token, err := repository.GenerateRandomBytes(16)
  97. if err != nil {
  98. app.handleErrorInternal(err, w)
  99. return
  100. }
  101. // create release with webhook token in db
  102. image, ok := rel.Config["image"].(map[string]interface{})
  103. if !ok {
  104. app.handleErrorInternal(fmt.Errorf("Could not find field image in config"), w)
  105. return
  106. }
  107. repository := image["repository"]
  108. repoStr, ok := repository.(string)
  109. if !ok {
  110. app.handleErrorInternal(fmt.Errorf("Could not find field repository in config"), w)
  111. return
  112. }
  113. release := &models.Release{
  114. ClusterID: form.ReleaseForm.Form.Cluster.ID,
  115. ProjectID: form.ReleaseForm.Form.Cluster.ProjectID,
  116. Namespace: form.ReleaseForm.Form.Namespace,
  117. Name: form.ChartTemplateForm.Name,
  118. WebhookToken: token,
  119. ImageRepoURI: repoStr,
  120. }
  121. _, err = app.Repo.Release.CreateRelease(release)
  122. if err != nil {
  123. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  124. Code: ErrReleaseDeploy,
  125. Errors: []string{"error creating a webhook: " + err.Error()},
  126. }, w)
  127. }
  128. // if github action config is linked, call the github action config handler
  129. if form.GithubActionConfig != nil {
  130. gaForm := &forms.CreateGitAction{
  131. ReleaseID: release.ID,
  132. GitRepo: form.GithubActionConfig.GitRepo,
  133. GitBranch: form.GithubActionConfig.GitBranch,
  134. ImageRepoURI: form.GithubActionConfig.ImageRepoURI,
  135. DockerfilePath: form.GithubActionConfig.DockerfilePath,
  136. GitRepoID: form.GithubActionConfig.GitRepoID,
  137. BuildEnv: form.GithubActionConfig.BuildEnv,
  138. RegistryID: form.GithubActionConfig.RegistryID,
  139. }
  140. // validate the form
  141. if err := app.validator.Struct(form); err != nil {
  142. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  143. return
  144. }
  145. app.createGitActionFromForm(projID, release, form.ChartTemplateForm.Name, gaForm, w, r)
  146. }
  147. w.WriteHeader(http.StatusOK)
  148. }
  149. // HandleDeployAddon triggers a addon deployment from a template
  150. func (app *App) HandleDeployAddon(w http.ResponseWriter, r *http.Request) {
  151. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  152. if err != nil || projID == 0 {
  153. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  154. return
  155. }
  156. name := chi.URLParam(r, "name")
  157. version := chi.URLParam(r, "version")
  158. // if version passed as latest, pass empty string to loader to get latest
  159. if version == "latest" {
  160. version = ""
  161. }
  162. getChartForm := &forms.ChartForm{
  163. Name: name,
  164. Version: version,
  165. RepoURL: app.ServerConf.DefaultApplicationHelmRepoURL,
  166. }
  167. // if a repo_url is passed as query param, it will be populated
  168. vals, err := url.ParseQuery(r.URL.RawQuery)
  169. if err != nil {
  170. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  171. return
  172. }
  173. getChartForm.PopulateRepoURLFromQueryParams(vals)
  174. chart, err := loader.LoadChartPublic(getChartForm.RepoURL, getChartForm.Name, getChartForm.Version)
  175. if err != nil {
  176. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  177. return
  178. }
  179. form := &forms.InstallChartTemplateForm{
  180. ReleaseForm: &forms.ReleaseForm{
  181. Form: &helm.Form{
  182. Repo: app.Repo,
  183. DigitalOceanOAuth: app.DOConf,
  184. },
  185. },
  186. ChartTemplateForm: &forms.ChartTemplateForm{},
  187. }
  188. form.ReleaseForm.PopulateHelmOptionsFromQueryParams(
  189. vals,
  190. app.Repo.Cluster,
  191. )
  192. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  193. app.handleErrorFormDecoding(err, ErrUserDecode, w)
  194. return
  195. }
  196. agent, err := app.getAgentFromReleaseForm(
  197. w,
  198. r,
  199. form.ReleaseForm,
  200. )
  201. if err != nil {
  202. app.handleErrorFormDecoding(err, ErrUserDecode, w)
  203. return
  204. }
  205. registries, err := app.Repo.Registry.ListRegistriesByProjectID(uint(projID))
  206. if err != nil {
  207. app.handleErrorDataRead(err, w)
  208. return
  209. }
  210. conf := &helm.InstallChartConfig{
  211. Chart: chart,
  212. Name: form.ChartTemplateForm.Name,
  213. Namespace: form.ReleaseForm.Form.Namespace,
  214. Values: form.ChartTemplateForm.FormValues,
  215. Cluster: form.ReleaseForm.Cluster,
  216. Repo: *app.Repo,
  217. Registries: registries,
  218. }
  219. _, err = agent.InstallChart(conf, app.DOConf)
  220. if err != nil {
  221. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  222. Code: ErrReleaseDeploy,
  223. Errors: []string{"error installing a new chart: " + err.Error()},
  224. }, w)
  225. return
  226. }
  227. w.WriteHeader(http.StatusOK)
  228. }
  229. // HandleUninstallTemplate triggers a chart deployment from a template
  230. func (app *App) HandleUninstallTemplate(w http.ResponseWriter, r *http.Request) {
  231. name := chi.URLParam(r, "name")
  232. vals, err := url.ParseQuery(r.URL.RawQuery)
  233. if err != nil {
  234. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  235. return
  236. }
  237. form := &forms.GetReleaseForm{
  238. ReleaseForm: &forms.ReleaseForm{
  239. Form: &helm.Form{
  240. Repo: app.Repo,
  241. },
  242. },
  243. Name: name,
  244. }
  245. agent, err := app.getAgentFromQueryParams(
  246. w,
  247. r,
  248. form.ReleaseForm,
  249. form.ReleaseForm.PopulateHelmOptionsFromQueryParams,
  250. )
  251. // errors are handled in app.getAgentFromQueryParams
  252. if err != nil {
  253. return
  254. }
  255. resp, err := agent.UninstallChart(name)
  256. if err != nil {
  257. return
  258. }
  259. // update the github actions env if the release exists and is built from source
  260. if cName := resp.Release.Chart.Metadata.Name; cName == "job" || cName == "web" || cName == "worker" {
  261. clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
  262. if err != nil {
  263. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  264. Code: ErrReleaseReadData,
  265. Errors: []string{"release not found"},
  266. }, w)
  267. }
  268. release, err := app.Repo.Release.ReadRelease(uint(clusterID), name, resp.Release.Namespace)
  269. if release != nil {
  270. gitAction := release.GitActionConfig
  271. if gitAction.ID != 0 {
  272. // parse env into build env
  273. cEnv := &ContainerEnvConfig{}
  274. rawValues, err := yaml.Marshal(resp.Release.Config)
  275. if err != nil {
  276. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  277. Code: ErrReleaseReadData,
  278. Errors: []string{"could not get values of previous revision"},
  279. }, w)
  280. }
  281. yaml.Unmarshal(rawValues, cEnv)
  282. gr, err := app.Repo.GitRepo.ReadGitRepo(gitAction.GitRepoID)
  283. if err != nil {
  284. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  285. Code: ErrReleaseReadData,
  286. Errors: []string{"github repo integration not found"},
  287. }, w)
  288. }
  289. repoSplit := strings.Split(gitAction.GitRepo, "/")
  290. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  291. if err != nil || projID == 0 {
  292. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  293. return
  294. }
  295. gaRunner := &actions.GithubActions{
  296. ServerURL: app.ServerConf.ServerURL,
  297. GitIntegration: gr,
  298. GitRepoName: repoSplit[1],
  299. GitRepoOwner: repoSplit[0],
  300. Repo: *app.Repo,
  301. GithubConf: app.GithubProjectConf,
  302. WebhookToken: release.WebhookToken,
  303. ProjectID: uint(projID),
  304. ReleaseName: name,
  305. GitBranch: gitAction.GitBranch,
  306. DockerFilePath: gitAction.DockerfilePath,
  307. FolderPath: gitAction.FolderPath,
  308. ImageRepoURL: gitAction.ImageRepoURI,
  309. BuildEnv: cEnv.Container.Env.Normal,
  310. }
  311. err = gaRunner.Cleanup()
  312. if err != nil {
  313. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  314. Code: ErrReleaseReadData,
  315. Errors: []string{"could not remove github action"},
  316. }, w)
  317. }
  318. }
  319. }
  320. }
  321. w.WriteHeader(http.StatusOK)
  322. return
  323. }