deploy.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package client
  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/clusters/%d/namespaces/%s/releases/image/batch", c.BaseURL, projID, clusterID, namespace),
  80. strings.NewReader(string(data)),
  81. )
  82. if err != nil {
  83. return err
  84. }
  85. req = req.WithContext(ctx)
  86. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  87. if httpErr != nil {
  88. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  89. }
  90. return err
  91. }
  92. return nil
  93. }
  94. type DeployTemplateGitAction struct {
  95. GitRepo string `json:"git_repo"`
  96. GitBranch string `json:"git_branch"`
  97. ImageRepoURI string `json:"image_repo_uri"`
  98. DockerfilePath string `json:"dockerfile_path"`
  99. FolderPath string `json:"folder_path"`
  100. GitRepoID uint `json:"git_repo_id"`
  101. BuildEnv map[string]string `json:"env"`
  102. RegistryID uint `json:"registry_id"`
  103. }
  104. type DeployTemplateRequest struct {
  105. TemplateName string `json:"templateName"`
  106. ImageURL string `json:"imageURL"`
  107. FormValues map[string]interface{} `json:"formValues"`
  108. Namespace string `json:"namespace"`
  109. Name string `json:"name"`
  110. GitAction *DeployTemplateGitAction `json:"github_action"`
  111. }
  112. func (c *Client) DeployTemplate(
  113. ctx context.Context,
  114. projID, clusterID uint,
  115. templateName string,
  116. templateVersion string,
  117. deployReq *DeployTemplateRequest,
  118. ) error {
  119. data, err := json.Marshal(deployReq)
  120. if err != nil {
  121. return err
  122. }
  123. req, err := http.NewRequest(
  124. "POST",
  125. fmt.Sprintf("%s/projects/%d/deploy/%s/%s?"+url.Values{
  126. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  127. "storage": []string{"secret"},
  128. }.Encode(), c.BaseURL, projID, templateName, templateVersion),
  129. strings.NewReader(string(data)),
  130. )
  131. if err != nil {
  132. return err
  133. }
  134. req = req.WithContext(ctx)
  135. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  136. if httpErr != nil {
  137. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  138. }
  139. return err
  140. }
  141. return nil
  142. }
  143. type UpgradeReleaseRequest struct {
  144. Values string `json:"values"`
  145. Namespace string `json:"namespace"`
  146. }
  147. func (c *Client) UpgradeRelease(
  148. ctx context.Context,
  149. projID, clusterID uint,
  150. name string,
  151. upgradeReq *UpgradeReleaseRequest,
  152. ) error {
  153. data, err := json.Marshal(upgradeReq)
  154. if err != nil {
  155. return err
  156. }
  157. req, err := http.NewRequest(
  158. "POST",
  159. fmt.Sprintf("%s/projects/%d/releases/%s/upgrade?"+url.Values{
  160. "namespace": []string{upgradeReq.Namespace},
  161. "cluster_id": []string{fmt.Sprintf("%d", clusterID)},
  162. "storage": []string{"secret"},
  163. }.Encode(), c.BaseURL, projID, name),
  164. strings.NewReader(string(data)),
  165. )
  166. if err != nil {
  167. return err
  168. }
  169. req = req.WithContext(ctx)
  170. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  171. if httpErr != nil {
  172. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  173. }
  174. return err
  175. }
  176. return nil
  177. }