2
0

deploy.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. // GetReleaseLatestRevision gets the latest revision of a Helm release
  12. type GetReleaseWebhookResponse models.ReleaseExternal
  13. func (c *Client) GetReleaseWebhook(
  14. ctx context.Context,
  15. projID, clusterID uint,
  16. name, namespace string,
  17. ) (*GetReleaseWebhookResponse, error) {
  18. req, err := http.NewRequest(
  19. "GET",
  20. fmt.Sprintf("%s/projects/%d/releases/%s/webhook_token?"+url.Values{
  21. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  22. "namespace": []string{namespace},
  23. "storage": []string{"secret"},
  24. }.Encode(), c.BaseURL, projID, name),
  25. nil,
  26. )
  27. if err != nil {
  28. return nil, err
  29. }
  30. req = req.WithContext(ctx)
  31. bodyResp := &GetReleaseWebhookResponse{}
  32. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  33. if httpErr != nil {
  34. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  35. }
  36. return nil, err
  37. }
  38. return bodyResp, nil
  39. }
  40. // DeployWithWebhook deploys an application with an image tag using a unique webhook URI
  41. func (c *Client) DeployWithWebhook(
  42. ctx context.Context,
  43. webhook, tag string,
  44. ) error {
  45. req, err := http.NewRequest(
  46. "POST",
  47. fmt.Sprintf("%s/webhooks/deploy/%s?commit=%s", c.BaseURL, webhook, tag),
  48. nil,
  49. )
  50. if err != nil {
  51. return err
  52. }
  53. req = req.WithContext(ctx)
  54. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  55. if httpErr != nil {
  56. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  57. }
  58. return err
  59. }
  60. return nil
  61. }
  62. type UpdateBatchImageRequest struct {
  63. ImageRepoURI string `json:"image_repo_uri"`
  64. Tag string `json:"tag"`
  65. }
  66. // UpdateBatchImage updates all releases that use a certain image with a new tag
  67. func (c *Client) UpdateBatchImage(
  68. ctx context.Context,
  69. projID, clusterID uint,
  70. namespace string,
  71. updateImageReq *UpdateBatchImageRequest,
  72. ) error {
  73. data, err := json.Marshal(updateImageReq)
  74. if err != nil {
  75. return nil
  76. }
  77. req, err := http.NewRequest(
  78. "POST",
  79. fmt.Sprintf("%s/projects/%d/releases/image/update/batch?"+url.Values{
  80. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  81. "namespace": []string{namespace},
  82. "storage": []string{"secret"},
  83. }.Encode(), c.BaseURL, projID),
  84. strings.NewReader(string(data)),
  85. )
  86. if err != nil {
  87. return err
  88. }
  89. req = req.WithContext(ctx)
  90. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  91. if httpErr != nil {
  92. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  93. }
  94. return err
  95. }
  96. return nil
  97. }
  98. type DeployTemplateGitAction struct {
  99. GitRepo string `json:"git_repo"`
  100. GitBranch string `json:"git_branch"`
  101. ImageRepoURI string `json:"image_repo_uri"`
  102. DockerfilePath string `json:"dockerfile_path"`
  103. FolderPath string `json:"folder_path"`
  104. GitRepoID uint `json:"git_repo_id"`
  105. BuildEnv map[string]string `json:"env"`
  106. RegistryID uint `json:"registry_id"`
  107. }
  108. type DeployTemplateRequest struct {
  109. TemplateName string `json:"templateName"`
  110. ImageURL string `json:"imageURL"`
  111. FormValues map[string]interface{} `json:"formValues"`
  112. Namespace string `json:"namespace"`
  113. Name string `json:"name"`
  114. GitAction *DeployTemplateGitAction `json:"github_action"`
  115. }
  116. func (c *Client) DeployTemplate(
  117. ctx context.Context,
  118. projID, clusterID uint,
  119. templateName string,
  120. templateVersion string,
  121. deployReq *DeployTemplateRequest,
  122. ) error {
  123. data, err := json.Marshal(deployReq)
  124. if err != nil {
  125. return err
  126. }
  127. req, err := http.NewRequest(
  128. "POST",
  129. fmt.Sprintf("%s/projects/%d/deploy/%s/%s?"+url.Values{
  130. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  131. "storage": []string{"secret"},
  132. }.Encode(), c.BaseURL, projID, templateName, templateVersion),
  133. strings.NewReader(string(data)),
  134. )
  135. if err != nil {
  136. return err
  137. }
  138. req = req.WithContext(ctx)
  139. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  140. if httpErr != nil {
  141. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  142. }
  143. return err
  144. }
  145. return nil
  146. }
  147. type UpgradeReleaseRequest struct {
  148. Values string `json:"values"`
  149. Namespace string `json:"namespace"`
  150. }
  151. func (c *Client) UpgradeRelease(
  152. ctx context.Context,
  153. projID, clusterID uint,
  154. name string,
  155. upgradeReq *UpgradeReleaseRequest,
  156. ) error {
  157. data, err := json.Marshal(upgradeReq)
  158. if err != nil {
  159. return err
  160. }
  161. req, err := http.NewRequest(
  162. "POST",
  163. fmt.Sprintf("%s/projects/%d/releases/%s/upgrade?"+url.Values{
  164. "namespace": []string{upgradeReq.Namespace},
  165. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  166. "storage": []string{"secret"},
  167. }.Encode(), c.BaseURL, projID, name),
  168. strings.NewReader(string(data)),
  169. )
  170. if err != nil {
  171. return err
  172. }
  173. req = req.WithContext(ctx)
  174. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  175. if httpErr != nil {
  176. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  177. }
  178. return err
  179. }
  180. return nil
  181. }