hooks.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. BuildEventID string
  22. }
  23. func (t *DeployAppHook) PreApply() error {
  24. err := config.ValidateCLIEnvironment()
  25. if err != nil {
  26. errMsg := composePreviewMessage("porter CLI is not configured correctly", Error)
  27. return fmt.Errorf("%s: %w", errMsg, err)
  28. }
  29. buildEventId, err := createAppEvent(t.Client, t.ApplicationName, t.ProjectID, t.ClusterID)
  30. if err != nil {
  31. return err
  32. }
  33. t.BuildEventID = buildEventId
  34. return nil
  35. }
  36. func (t *DeployAppHook) DataQueries() map[string]interface{} {
  37. res := map[string]interface{}{
  38. "image": fmt.Sprintf("{$.%s.image}", t.BuildImageDriverName),
  39. }
  40. return res
  41. }
  42. // deploy the app
  43. func (t *DeployAppHook) PostApply(driverOutput map[string]interface{}) error {
  44. eventRequest := types.CreateOrUpdatePorterAppEventRequest{
  45. Status: "SUCCESS",
  46. Type: types.PorterAppEventType_Build,
  47. Metadata: map[string]any{},
  48. ID: t.BuildEventID,
  49. }
  50. _, _ = t.Client.CreateOrUpdatePorterAppEvent(context.Background(), t.ProjectID, t.ClusterID, t.ApplicationName, &eventRequest)
  51. _, err := t.Client.GetRelease(
  52. context.Background(),
  53. t.ProjectID,
  54. t.ClusterID,
  55. t.Namespace,
  56. t.ApplicationName,
  57. )
  58. shouldCreate := err != nil
  59. if err != nil {
  60. color.New(color.FgYellow).Printf("Could not read release for app %s (%s): attempting creation\n", t.ApplicationName, err.Error())
  61. } else {
  62. color.New(color.FgGreen).Printf("Found release for app %s: attempting update\n", t.ApplicationName)
  63. }
  64. return t.applyApp(shouldCreate, driverOutput)
  65. }
  66. func (t *DeployAppHook) applyApp(shouldCreate bool, driverOutput map[string]interface{}) error {
  67. var imageInfo types.ImageInfo
  68. image, ok := driverOutput["image"].(string)
  69. // if it contains a $, then it means the query didn't resolve to anything
  70. if ok && !strings.Contains(image, "$") {
  71. imageSpl := strings.Split(image, ":")
  72. if len(imageSpl) == 2 {
  73. imageInfo = types.ImageInfo{
  74. Repository: imageSpl[0],
  75. Tag: imageSpl[1],
  76. }
  77. } else {
  78. return fmt.Errorf("could not parse image info %s", image)
  79. }
  80. }
  81. req := &types.CreatePorterAppRequest{
  82. ClusterID: t.ClusterID,
  83. ProjectID: t.ProjectID,
  84. PorterYAMLBase64: base64.StdEncoding.EncodeToString(t.PorterYAML),
  85. ImageInfo: imageInfo,
  86. OverrideRelease: false, // deploying from the cli will never delete release resources, only append or override
  87. Builder: t.Builder,
  88. Namespace: t.Namespace,
  89. EnvironmentConfigID: t.EnvironmentMeta.EnvironmentConfigID,
  90. }
  91. _, err := t.Client.CreatePorterApp(
  92. context.Background(),
  93. t.ProjectID,
  94. t.ClusterID,
  95. t.ApplicationName,
  96. req,
  97. )
  98. if err != nil {
  99. if shouldCreate {
  100. return fmt.Errorf("error creating app %s: %w", t.ApplicationName, err)
  101. }
  102. return fmt.Errorf("error updating app %s: %w", t.ApplicationName, err)
  103. }
  104. return nil
  105. }
  106. func (t *DeployAppHook) OnConsolidatedErrors(errors map[string]error) {
  107. errorStringMap := make(map[string]string)
  108. for k, v := range errors {
  109. errorStringMap[k] = fmt.Sprintf("%+v", v)
  110. }
  111. eventRequest := types.CreateOrUpdatePorterAppEventRequest{
  112. Status: "FAILED",
  113. Type: types.PorterAppEventType_Build,
  114. Metadata: map[string]any{
  115. "errors": errorStringMap,
  116. },
  117. ID: t.BuildEventID,
  118. }
  119. _, _ = t.Client.CreateOrUpdatePorterAppEvent(context.Background(), t.ProjectID, t.ClusterID, t.ApplicationName, &eventRequest)
  120. }
  121. func (t *DeployAppHook) OnError(err error) {
  122. t.OnConsolidatedErrors(map[string]error{
  123. "pre-apply": err,
  124. })
  125. }