deploy_handler.go 10 KB

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