apply.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package v2
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "strconv"
  10. "time"
  11. "github.com/porter-dev/porter/api/server/handlers/porter_app"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/cli/cli/git"
  15. "github.com/fatih/color"
  16. "github.com/porter-dev/api-contracts/generated/go/helpers"
  17. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  18. api "github.com/porter-dev/porter/api/client"
  19. "github.com/porter-dev/porter/cli/cmd/config"
  20. )
  21. // Apply implements the functionality of the `porter apply` command for validate apply v2 projects
  22. func Apply(ctx context.Context, cliConf config.CLIConfig, client api.Client, porterYamlPath string, appName string) error {
  23. const forceBuild = true
  24. var yamlB64 string
  25. if len(porterYamlPath) != 0 {
  26. porterYaml, err := os.ReadFile(filepath.Clean(porterYamlPath))
  27. if err != nil {
  28. return fmt.Errorf("could not read porter yaml file: %w", err)
  29. }
  30. b64YAML := base64.StdEncoding.EncodeToString(porterYaml)
  31. // last argument is passed to accommodate users with v1 porter yamls
  32. parseResp, err := client.ParseYAML(ctx, cliConf.Project, cliConf.Cluster, b64YAML, appName)
  33. if err != nil {
  34. return fmt.Errorf("error calling parse yaml endpoint: %w", err)
  35. }
  36. if parseResp.B64AppProto == "" {
  37. return errors.New("b64 app proto is empty")
  38. }
  39. yamlB64 = parseResp.B64AppProto
  40. // override app name if provided
  41. appName, err = appNameFromB64AppProto(parseResp.B64AppProto)
  42. if err != nil {
  43. return fmt.Errorf("error getting app name from b64 app proto: %w", err)
  44. }
  45. color.New(color.FgGreen).Printf("Successfully parsed Porter YAML: applying app \"%s\"\n", appName) // nolint:errcheck,gosec
  46. }
  47. targetResp, err := client.DefaultDeploymentTarget(ctx, cliConf.Project, cliConf.Cluster)
  48. if err != nil {
  49. return fmt.Errorf("error calling default deployment target endpoint: %w", err)
  50. }
  51. if targetResp.DeploymentTargetID == "" {
  52. return errors.New("deployment target id is empty")
  53. }
  54. var commitSHA string
  55. if os.Getenv("PORTER_COMMIT_SHA") != "" {
  56. commitSHA = os.Getenv("PORTER_COMMIT_SHA")
  57. } else if os.Getenv("GITHUB_SHA") != "" {
  58. commitSHA = os.Getenv("GITHUB_SHA")
  59. } else if commit, err := git.LastCommit(); err == nil && commit != nil {
  60. commitSHA = commit.Sha
  61. }
  62. validateResp, err := client.ValidatePorterApp(ctx, cliConf.Project, cliConf.Cluster, appName, yamlB64, targetResp.DeploymentTargetID, commitSHA)
  63. if err != nil {
  64. return fmt.Errorf("error calling validate endpoint: %w", err)
  65. }
  66. if validateResp.ValidatedBase64AppProto == "" {
  67. return errors.New("validated b64 app proto is empty")
  68. }
  69. base64AppProto := validateResp.ValidatedBase64AppProto
  70. createPorterAppDBEntryInp, err := createPorterAppDbEntryInputFromProtoAndEnv(validateResp.ValidatedBase64AppProto)
  71. if err != nil {
  72. return fmt.Errorf("error creating porter app db entry input from proto: %w", err)
  73. }
  74. err = client.CreatePorterAppDBEntry(ctx, cliConf.Project, cliConf.Cluster, createPorterAppDBEntryInp)
  75. if err != nil {
  76. return fmt.Errorf("error creating porter app db entry: %w", err)
  77. }
  78. applyResp, err := client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, base64AppProto, targetResp.DeploymentTargetID, "", forceBuild)
  79. if err != nil {
  80. return fmt.Errorf("error calling apply endpoint: %w", err)
  81. }
  82. if applyResp.AppRevisionId == "" {
  83. return errors.New("app revision id is empty")
  84. }
  85. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_BUILD {
  86. color.New(color.FgGreen).Printf("Building new image...\n") // nolint:errcheck,gosec
  87. eventID, _ := createBuildEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID)
  88. if commitSHA == "" {
  89. return errors.New("Build is required but commit SHA cannot be identified. Please set the PORTER_COMMIT_SHA environment variable or run apply in git repository with access to the git CLI.")
  90. }
  91. buildSettings, err := buildSettingsFromBase64AppProto(base64AppProto)
  92. if err != nil {
  93. return fmt.Errorf("error building settings from base64 app proto: %w", err)
  94. }
  95. currentAppRevisionResp, err := client.CurrentAppRevision(ctx, cliConf.Project, cliConf.Cluster, appName, targetResp.DeploymentTargetID)
  96. if err != nil {
  97. return fmt.Errorf("error getting current app revision: %w", err)
  98. }
  99. if currentAppRevisionResp == nil {
  100. return errors.New("current app revision is nil")
  101. }
  102. appRevision := currentAppRevisionResp.AppRevision
  103. if appRevision.B64AppProto == "" {
  104. return errors.New("current app revision b64 app proto is empty")
  105. }
  106. currentImageTag, err := imageTagFromBase64AppProto(appRevision.B64AppProto)
  107. if err != nil {
  108. return fmt.Errorf("error getting image tag from current app revision: %w", err)
  109. }
  110. buildSettings.CurrentImageTag = currentImageTag
  111. buildSettings.ProjectID = cliConf.Project
  112. buildEnv, err := client.GetBuildEnv(ctx, cliConf.Project, cliConf.Cluster, base64AppProto)
  113. if err != nil {
  114. return fmt.Errorf("error getting build env: %w", err)
  115. }
  116. buildSettings.Env = buildEnv.BuildEnvVariables
  117. err = build(ctx, client, buildSettings)
  118. buildMetadata := make(map[string]interface{})
  119. buildMetadata["end_time"] = time.Now().UTC()
  120. if err != nil {
  121. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, types.PorterAppEventStatus_Failed, buildMetadata)
  122. _, _ = client.UpdateRevisionStatus(ctx, cliConf.Project, cliConf.Cluster, appName, applyResp.AppRevisionId, models.AppRevisionStatus_BuildFailed)
  123. return fmt.Errorf("error building app: %w", err)
  124. }
  125. color.New(color.FgGreen).Printf("Successfully built image (tag: %s)\n", buildSettings.ImageTag) // nolint:errcheck,gosec
  126. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, types.PorterAppEventStatus_Success, buildMetadata)
  127. applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId, !forceBuild)
  128. if err != nil {
  129. return fmt.Errorf("apply error post-build: %w", err)
  130. }
  131. }
  132. color.New(color.FgGreen).Printf("Image tag exists in repository\n") // nolint:errcheck,gosec
  133. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_TRACK_PREDEPLOY {
  134. color.New(color.FgGreen).Printf("Waiting for predeploy to complete...\n") // nolint:errcheck,gosec
  135. now := time.Now().UTC()
  136. eventID, _ := createPredeployEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, now, applyResp.AppRevisionId)
  137. eventStatus := types.PorterAppEventStatus_Success
  138. for {
  139. if time.Since(now) > checkPredeployTimeout {
  140. return errors.New("timed out waiting for predeploy to complete")
  141. }
  142. predeployStatusResp, err := client.PredeployStatus(ctx, cliConf.Project, cliConf.Cluster, appName, applyResp.AppRevisionId)
  143. if err != nil {
  144. return fmt.Errorf("error calling predeploy status endpoint: %w", err)
  145. }
  146. if predeployStatusResp.Status == porter_app.PredeployStatus_Failed {
  147. eventStatus = types.PorterAppEventStatus_Failed
  148. break
  149. }
  150. if predeployStatusResp.Status == porter_app.PredeployStatus_Successful {
  151. break
  152. }
  153. time.Sleep(checkPredeployFrequency)
  154. }
  155. metadata := make(map[string]interface{})
  156. metadata["end_time"] = time.Now().UTC()
  157. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, eventStatus, metadata)
  158. applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId, !forceBuild)
  159. if err != nil {
  160. return fmt.Errorf("apply error post-predeploy: %w", err)
  161. }
  162. }
  163. if applyResp.CLIAction != porterv1.EnumCLIAction_ENUM_CLI_ACTION_NONE {
  164. return fmt.Errorf("unexpected CLI action: %s", applyResp.CLIAction)
  165. }
  166. color.New(color.FgGreen).Printf("Successfully applied new revision %s for app %s\n", applyResp.AppRevisionId, appName) // nolint:errcheck,gosec
  167. return nil
  168. }
  169. // checkPredeployTimeout is the maximum amount of time the CLI will wait for a predeploy to complete before calling apply again
  170. const checkPredeployTimeout = 60 * time.Minute
  171. // checkPredeployFrequency is the frequency at which the CLI will check the status of a predeploy
  172. const checkPredeployFrequency = 10 * time.Second
  173. func appNameFromB64AppProto(base64AppProto string) (string, error) {
  174. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  175. if err != nil {
  176. return "", fmt.Errorf("unable to decode base64 app for revision: %w", err)
  177. }
  178. app := &porterv1.PorterApp{}
  179. err = helpers.UnmarshalContractObject(decoded, app)
  180. if err != nil {
  181. return "", fmt.Errorf("unable to unmarshal app for revision: %w", err)
  182. }
  183. if app.Name == "" {
  184. return "", fmt.Errorf("app does not contain name")
  185. }
  186. return app.Name, nil
  187. }
  188. func createPorterAppDbEntryInputFromProtoAndEnv(base64AppProto string) (api.CreatePorterAppDBEntryInput, error) {
  189. var input api.CreatePorterAppDBEntryInput
  190. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  191. if err != nil {
  192. return input, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  193. }
  194. app := &porterv1.PorterApp{}
  195. err = helpers.UnmarshalContractObject(decoded, app)
  196. if err != nil {
  197. return input, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  198. }
  199. if app.Name == "" {
  200. return input, fmt.Errorf("app does not contain name")
  201. }
  202. input.AppName = app.Name
  203. if app.Build != nil {
  204. if os.Getenv("GITHUB_REPOSITORY_ID") == "" {
  205. input.Local = true
  206. return input, nil
  207. }
  208. gitRepoId, err := strconv.Atoi(os.Getenv("GITHUB_REPOSITORY_ID"))
  209. if err != nil {
  210. return input, fmt.Errorf("unable to parse GITHUB_REPOSITORY_ID to int: %w", err)
  211. }
  212. input.GitRepoID = uint(gitRepoId)
  213. input.GitRepoName = os.Getenv("GITHUB_REPOSITORY")
  214. input.GitBranch = os.Getenv("GITHUB_REF_NAME")
  215. input.PorterYamlPath = "porter.yaml"
  216. return input, nil
  217. }
  218. if app.Image != nil {
  219. input.ImageRepository = app.Image.Repository
  220. input.ImageTag = app.Image.Tag
  221. return input, nil
  222. }
  223. return input, fmt.Errorf("app does not contain build or image settings")
  224. }
  225. func buildSettingsFromBase64AppProto(base64AppProto string) (buildInput, error) {
  226. var buildSettings buildInput
  227. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  228. if err != nil {
  229. return buildSettings, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  230. }
  231. app := &porterv1.PorterApp{}
  232. err = helpers.UnmarshalContractObject(decoded, app)
  233. if err != nil {
  234. return buildSettings, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  235. }
  236. if app.Name == "" {
  237. return buildSettings, fmt.Errorf("app does not contain name")
  238. }
  239. if app.Build == nil {
  240. return buildSettings, fmt.Errorf("app does not contain build settings")
  241. }
  242. if app.Image == nil {
  243. return buildSettings, fmt.Errorf("app does not contain image settings")
  244. }
  245. return buildInput{
  246. AppName: app.Name,
  247. BuildContext: app.Build.Context,
  248. Dockerfile: app.Build.Dockerfile,
  249. BuildMethod: app.Build.Method,
  250. Builder: app.Build.Builder,
  251. BuildPacks: app.Build.Buildpacks,
  252. ImageTag: app.Image.Tag,
  253. RepositoryURL: app.Image.Repository,
  254. }, nil
  255. }
  256. func imageTagFromBase64AppProto(base64AppProto string) (string, error) {
  257. var image string
  258. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  259. if err != nil {
  260. return image, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  261. }
  262. app := &porterv1.PorterApp{}
  263. err = helpers.UnmarshalContractObject(decoded, app)
  264. if err != nil {
  265. return image, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  266. }
  267. if app.Image == nil {
  268. return image, fmt.Errorf("app does not contain image settings")
  269. }
  270. if app.Image.Tag == "" {
  271. return image, fmt.Errorf("app does not contain image tag")
  272. }
  273. return app.Image.Tag, nil
  274. }