hooks.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. _, err := client.CreatePorterApp(
  79. context.Background(),
  80. t.ProjectID,
  81. t.ClusterID,
  82. t.ApplicationName,
  83. req,
  84. )
  85. if err != nil {
  86. if shouldCreate {
  87. return fmt.Errorf("error creating app %s: %w", t.ApplicationName, err)
  88. }
  89. return fmt.Errorf("error updating app %s: %w", t.ApplicationName, err)
  90. }
  91. if t.EnvironmentMeta != nil {
  92. // create preview env record
  93. }
  94. return nil
  95. }
  96. func (t *DeployAppHook) OnConsolidatedErrors(map[string]error) {}
  97. func (t *DeployAppHook) OnError(error) {}