apply.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package v2
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "github.com/fatih/color"
  10. "github.com/porter-dev/api-contracts/generated/go/helpers"
  11. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  12. api "github.com/porter-dev/porter/api/client"
  13. "github.com/porter-dev/porter/cli/cmd/config"
  14. )
  15. // Apply implements the functionality of the `porter apply` command for validate apply v2 projects
  16. func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, porterYamlPath string) error {
  17. if len(porterYamlPath) == 0 {
  18. return fmt.Errorf("porter yaml is empty")
  19. }
  20. porterYaml, err := os.ReadFile(filepath.Clean(porterYamlPath))
  21. if err != nil {
  22. return fmt.Errorf("could not read porter yaml file: %w", err)
  23. }
  24. b64YAML := base64.StdEncoding.EncodeToString(porterYaml)
  25. parseResp, err := client.ParseYAML(ctx, cliConf.Project, cliConf.Cluster, b64YAML)
  26. if err != nil {
  27. return fmt.Errorf("error calling parse yaml endpoint: %w", err)
  28. }
  29. if parseResp.B64AppProto == "" {
  30. return errors.New("b64 app proto is empty")
  31. }
  32. targetResp, err := client.DefaultDeploymentTarget(ctx, cliConf.Project, cliConf.Cluster)
  33. if err != nil {
  34. return fmt.Errorf("error calling default deployment target endpoint: %w", err)
  35. }
  36. if targetResp.DeploymentTargetID == "" {
  37. return errors.New("deployment target id is empty")
  38. }
  39. validateResp, err := client.ValidatePorterApp(ctx, cliConf.Project, cliConf.Cluster, parseResp.B64AppProto, targetResp.DeploymentTargetID)
  40. if err != nil {
  41. return fmt.Errorf("error calling validate endpoint: %w", err)
  42. }
  43. if validateResp.ValidatedBase64AppProto == "" {
  44. return errors.New("validated b64 app proto is empty")
  45. }
  46. base64AppProto := validateResp.ValidatedBase64AppProto
  47. applyResp, err := client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, validateResp.ValidatedBase64AppProto, targetResp.DeploymentTargetID, "")
  48. if err != nil {
  49. return fmt.Errorf("error calling apply endpoint: %w", err)
  50. }
  51. if applyResp.AppRevisionId == "" {
  52. return errors.New("app revision id is empty")
  53. }
  54. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_BUILD {
  55. buildSettings, err := buildSettingsFromBase64AppProto(base64AppProto)
  56. if err != nil {
  57. return fmt.Errorf("error building settings from base64 app proto: %w", err)
  58. }
  59. currentAppRevisionResp, err := client.CurrentAppRevision(ctx, cliConf.Project, cliConf.Cluster, buildSettings.AppName, targetResp.DeploymentTargetID)
  60. if err != nil {
  61. return fmt.Errorf("error getting current app revision: %w", err)
  62. }
  63. if currentAppRevisionResp.B64AppProto == "" {
  64. return errors.New("current app revision b64 app proto is empty")
  65. }
  66. currentImageTag, err := imageTagFromBase64AppProto(currentAppRevisionResp.B64AppProto)
  67. if err != nil {
  68. return fmt.Errorf("error getting image tag from current app revision: %w", err)
  69. }
  70. buildSettings.CurrentImageTag = currentImageTag
  71. buildSettings.ProjectID = cliConf.Project
  72. err = build(ctx, client, buildSettings)
  73. if err != nil {
  74. return fmt.Errorf("error building app: %w", err)
  75. }
  76. applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId)
  77. if err != nil {
  78. return fmt.Errorf("error calling apply endpoint after build: %w", err)
  79. }
  80. }
  81. if applyResp.CLIAction != porterv1.EnumCLIAction_ENUM_CLI_ACTION_NONE {
  82. return fmt.Errorf("unexpected CLI action: %s", applyResp.CLIAction)
  83. }
  84. color.New(color.FgGreen).Printf("Successfully applied Porter YAML as revision %v, next action: %v\n", applyResp.AppRevisionId, applyResp.CLIAction) // nolint:errcheck,gosec
  85. return nil
  86. }
  87. func buildSettingsFromBase64AppProto(base64AppProto string) (buildInput, error) {
  88. var buildSettings buildInput
  89. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  90. if err != nil {
  91. return buildSettings, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  92. }
  93. app := &porterv1.PorterApp{}
  94. err = helpers.UnmarshalContractObject(decoded, app)
  95. if err != nil {
  96. return buildSettings, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  97. }
  98. if app.Name == "" {
  99. return buildSettings, fmt.Errorf("app does not contain name")
  100. }
  101. if app.Build == nil {
  102. return buildSettings, fmt.Errorf("app does not contain build settings")
  103. }
  104. if app.Image == nil {
  105. return buildSettings, fmt.Errorf("app does not contain image settings")
  106. }
  107. return buildInput{
  108. AppName: app.Name,
  109. BuildContext: app.Build.Context,
  110. Dockerfile: app.Build.Dockerfile,
  111. BuildMethod: app.Build.Method,
  112. Builder: app.Build.Builder,
  113. BuildPacks: app.Build.Buildpacks,
  114. ImageTag: app.Image.Tag,
  115. RepositoryURL: app.Image.Repository,
  116. }, nil
  117. }
  118. func imageTagFromBase64AppProto(base64AppProto string) (string, error) {
  119. var image string
  120. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  121. if err != nil {
  122. return image, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  123. }
  124. app := &porterv1.PorterApp{}
  125. err = helpers.UnmarshalContractObject(decoded, app)
  126. if err != nil {
  127. return image, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  128. }
  129. if app.Image == nil {
  130. return image, fmt.Errorf("app does not contain image settings")
  131. }
  132. if app.Image.Tag == "" {
  133. return image, fmt.Errorf("app does not contain image tag")
  134. }
  135. return app.Image.Tag, nil
  136. }