deploy_handler.go 10 KB

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