hooks.go 3.8 KB

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