hooks.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package stack
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "strings"
  7. "github.com/fatih/color"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/cli/cmd/config"
  11. )
  12. type DeployAppHook struct {
  13. Client *api.Client
  14. ApplicationName string
  15. ProjectID, ClusterID uint
  16. BuildImageDriverName string
  17. PorterYAML []byte
  18. Builder string
  19. Namespace string
  20. EnvironmentMeta EnvironmentMeta
  21. }
  22. func (t *DeployAppHook) PreApply() error {
  23. err := config.ValidateCLIEnvironment()
  24. if err != nil {
  25. errMsg := composePreviewMessage("porter CLI is not configured correctly", Error)
  26. return fmt.Errorf("%s: %w", errMsg, err)
  27. }
  28. return nil
  29. }
  30. func (t *DeployAppHook) DataQueries() map[string]interface{} {
  31. res := map[string]interface{}{
  32. "image": fmt.Sprintf("{$.%s.image}", t.BuildImageDriverName),
  33. }
  34. return res
  35. }
  36. // deploy the app
  37. func (t *DeployAppHook) PostApply(driverOutput map[string]interface{}) error {
  38. client := config.GetAPIClient()
  39. _, err := client.GetRelease(
  40. context.Background(),
  41. t.ProjectID,
  42. t.ClusterID,
  43. t.Namespace,
  44. t.ApplicationName,
  45. )
  46. shouldCreate := err != nil
  47. if err != nil {
  48. color.New(color.FgYellow).Printf("Could not read release for app %s (%s): attempting creation\n", t.ApplicationName, err.Error())
  49. } else {
  50. color.New(color.FgGreen).Printf("Found release for app %s: attempting update\n", t.ApplicationName)
  51. }
  52. return t.applyApp(client, shouldCreate, driverOutput)
  53. }
  54. func (t *DeployAppHook) applyApp(client *api.Client, shouldCreate bool, driverOutput map[string]interface{}) error {
  55. var imageInfo types.ImageInfo
  56. image, ok := driverOutput["image"].(string)
  57. // if it contains a $, then it means the query didn't resolve to anything
  58. if ok && !strings.Contains(image, "$") {
  59. imageSpl := strings.Split(image, ":")
  60. if len(imageSpl) == 2 {
  61. imageInfo = types.ImageInfo{
  62. Repository: imageSpl[0],
  63. Tag: imageSpl[1],
  64. }
  65. } else {
  66. return fmt.Errorf("could not parse image info %s", image)
  67. }
  68. }
  69. req := &types.CreatePorterAppRequest{
  70. ClusterID: t.ClusterID,
  71. ProjectID: t.ProjectID,
  72. PorterYAMLBase64: base64.StdEncoding.EncodeToString(t.PorterYAML),
  73. ImageInfo: imageInfo,
  74. OverrideRelease: false, // deploying from the cli will never delete release resources, only append or override
  75. Builder: t.Builder,
  76. Namespace: t.Namespace,
  77. }
  78. if t.EnvironmentMeta.EnvironmentConfigID != 0 {
  79. req.EnvironmentConfigID = t.EnvironmentMeta.EnvironmentConfigID
  80. }
  81. _, err := client.CreatePorterApp(
  82. context.Background(),
  83. t.ProjectID,
  84. t.ClusterID,
  85. t.ApplicationName,
  86. req,
  87. )
  88. if err != nil {
  89. if shouldCreate {
  90. return fmt.Errorf("error creating app %s: %w", t.ApplicationName, err)
  91. }
  92. return fmt.Errorf("error updating app %s: %w", t.ApplicationName, err)
  93. }
  94. if t.EnvironmentMeta.GitHubMetadata.BranchFrom != "" {
  95. color.New(color.FgGreen).Printf("Creating preview environment for app %s based on branch '%s'\n", t.ApplicationName, t.EnvironmentMeta.GitHubMetadata.BranchFrom)
  96. // create preview env record
  97. _, err = client.CreatePreviewEnvironment(
  98. context.Background(),
  99. t.ProjectID,
  100. t.ClusterID,
  101. &types.CreatePreviewEnvironmentRequest{
  102. EnvironmentConfigID: t.EnvironmentMeta.EnvironmentConfigID,
  103. GitRepoOwner: t.EnvironmentMeta.GitHubMetadata.RepoOwner,
  104. GitRepoName: t.EnvironmentMeta.GitHubMetadata.Repo,
  105. Branch: t.EnvironmentMeta.GitHubMetadata.BranchFrom,
  106. },
  107. )
  108. if err != nil {
  109. return fmt.Errorf("error creating preview environment: %w", err)
  110. }
  111. }
  112. return nil
  113. }
  114. func (t *DeployAppHook) OnConsolidatedErrors(map[string]error) {}
  115. func (t *DeployAppHook) OnError(error) {}