apply.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. api "github.com/porter-dev/porter/api/client"
  17. "github.com/porter-dev/porter/cli/cmd/config"
  18. )
  19. // ApplyInput is the input for the Apply function
  20. type ApplyInput struct {
  21. // CLIConfig is the CLI configuration
  22. CLIConfig config.CLIConfig
  23. // Client is the Porter API client
  24. Client api.Client
  25. // PorterYamlPath is the path to the porter.yaml file
  26. PorterYamlPath string
  27. // AppName is the name of the app
  28. AppName string
  29. // PreviewApply is true when Apply should create a new deployment target matching current git branch and apply to that target
  30. PreviewApply bool
  31. }
  32. // Apply implements the functionality of the `porter apply` command for validate apply v2 projects
  33. func Apply(ctx context.Context, inp ApplyInput) error {
  34. cliConf := inp.CLIConfig
  35. client := inp.Client
  36. deploymentTargetID, err := deploymentTargetFromConfig(ctx, client, cliConf.Project, cliConf.Cluster, inp.PreviewApply)
  37. if err != nil {
  38. return fmt.Errorf("error getting deployment target from config: %w", err)
  39. }
  40. var prNumber int
  41. prNumberEnv := os.Getenv("PORTER_PR_NUMBER")
  42. if prNumberEnv != "" {
  43. prNumber, err = strconv.Atoi(prNumberEnv)
  44. if err != nil {
  45. return fmt.Errorf("error parsing PORTER_PR_NUMBER to int: %w", err)
  46. }
  47. }
  48. porterYamlExists := len(inp.PorterYamlPath) != 0
  49. if porterYamlExists {
  50. _, err := os.Stat(filepath.Clean(inp.PorterYamlPath))
  51. if err != nil {
  52. if !os.IsNotExist(err) {
  53. return fmt.Errorf("error checking if porter yaml exists at path %s: %w", inp.PorterYamlPath, err)
  54. }
  55. // If a path was specified but the file does not exist, we will not immediately error out.
  56. // This supports users migrated from v1 who use a workflow file that always specifies a porter yaml path
  57. // in the apply command.
  58. porterYamlExists = false
  59. }
  60. }
  61. var b64YAML string
  62. if porterYamlExists {
  63. porterYaml, err := os.ReadFile(filepath.Clean(inp.PorterYamlPath))
  64. if err != nil {
  65. return fmt.Errorf("could not read porter yaml file: %w", err)
  66. }
  67. b64YAML = base64.StdEncoding.EncodeToString(porterYaml)
  68. color.New(color.FgGreen).Printf("Using Porter YAML at path: %s\n", inp.PorterYamlPath) // nolint:errcheck,gosec
  69. }
  70. commitSHA := commitSHAFromEnv()
  71. gitSource, err := gitSourceFromEnv()
  72. if err != nil {
  73. return fmt.Errorf("error getting git source from env: %w", err)
  74. }
  75. updateInput := api.UpdateAppInput{
  76. ProjectID: cliConf.Project,
  77. ClusterID: cliConf.Cluster,
  78. Name: inp.AppName,
  79. GitSource: gitSource,
  80. DeploymentTargetId: deploymentTargetID,
  81. Base64PorterYAML: b64YAML,
  82. CommitSHA: commitSHA,
  83. }
  84. updateResp, err := client.UpdateApp(ctx, updateInput)
  85. if err != nil {
  86. return fmt.Errorf("error calling update app endpoint: %w", err)
  87. }
  88. if updateResp.AppRevisionId == "" {
  89. return errors.New("app revision id is empty")
  90. }
  91. appName := updateResp.AppName
  92. if updateResp.CLIAction == porter_app.CLIAction_Build {
  93. color.New(color.FgGreen).Printf("Building new image...\n") // nolint:errcheck,gosec
  94. eventID, _ := createBuildEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, commitSHA)
  95. reportBuildFailureInput := reportBuildFailureInput{
  96. client: client,
  97. appName: appName,
  98. cliConf: cliConf,
  99. deploymentTargetID: deploymentTargetID,
  100. appRevisionID: updateResp.AppRevisionId,
  101. eventID: eventID,
  102. commitSHA: commitSHA,
  103. prNumber: prNumber,
  104. }
  105. if commitSHA == "" {
  106. err := 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")
  107. reportBuildFailureInput.buildError = err
  108. _ = reportBuildFailure(ctx, reportBuildFailureInput)
  109. return err
  110. }
  111. buildSettings, err := client.GetBuildFromRevision(ctx, cliConf.Project, cliConf.Cluster, appName, updateResp.AppRevisionId)
  112. if err != nil {
  113. err := fmt.Errorf("error getting build from revision: %w", err)
  114. reportBuildFailureInput.buildError = err
  115. _ = reportBuildFailure(ctx, reportBuildFailureInput)
  116. return err
  117. }
  118. buildInput, err := buildInputFromBuildSettings(cliConf.Project, appName, buildSettings.Image, buildSettings.Build)
  119. if err != nil {
  120. err := fmt.Errorf("error creating build input from build settings: %w", err)
  121. reportBuildFailureInput.buildError = err
  122. _ = reportBuildFailure(ctx, reportBuildFailureInput)
  123. return err
  124. }
  125. buildOutput := build(ctx, client, buildInput)
  126. if buildOutput.Error != nil {
  127. err := fmt.Errorf("error building app: %w", buildOutput.Error)
  128. reportBuildFailureInput.buildLogs = buildOutput.Logs
  129. reportBuildFailureInput.buildError = buildOutput.Error
  130. _ = reportBuildFailure(ctx, reportBuildFailureInput)
  131. return err
  132. }
  133. color.New(color.FgGreen).Printf("Successfully built image (tag: %s)\n", buildSettings.Image.Tag) // nolint:errcheck,gosec
  134. buildMetadata := make(map[string]interface{})
  135. buildMetadata["end_time"] = time.Now().UTC()
  136. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, types.PorterAppEventType_Build, eventID, types.PorterAppEventStatus_Success, buildMetadata)
  137. updateInput = api.UpdateAppInput{
  138. ProjectID: cliConf.Project,
  139. ClusterID: cliConf.Cluster,
  140. Name: appName,
  141. AppRevisionID: updateResp.AppRevisionId,
  142. Base64PorterYAML: b64YAML,
  143. DeploymentTargetId: deploymentTargetID,
  144. }
  145. updateResp, err = client.UpdateApp(ctx, updateInput)
  146. if err != nil {
  147. return fmt.Errorf("apply error post-build: %w", err)
  148. }
  149. }
  150. color.New(color.FgGreen).Printf("Image tag exists in repository\n") // nolint:errcheck,gosec
  151. if updateResp.CLIAction == porter_app.CLIAction_TrackPredeploy {
  152. color.New(color.FgGreen).Printf("Waiting for predeploy to complete...\n") // nolint:errcheck,gosec
  153. now := time.Now().UTC()
  154. eventID, _ := createPredeployEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, now, updateResp.AppRevisionId, commitSHA)
  155. metadata := make(map[string]interface{})
  156. eventStatus := types.PorterAppEventStatus_Success
  157. for {
  158. if time.Since(now) > checkPredeployTimeout {
  159. eventStatus = types.PorterAppEventStatus_Failed
  160. metadata["end_time"] = time.Now().UTC()
  161. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, types.PorterAppEventType_PreDeploy, eventID, eventStatus, metadata)
  162. return errors.New("timed out waiting for predeploy to complete")
  163. }
  164. predeployStatusResp, err := client.PredeployStatus(ctx, cliConf.Project, cliConf.Cluster, appName, updateResp.AppRevisionId)
  165. if err != nil {
  166. eventStatus = types.PorterAppEventStatus_Failed
  167. metadata["end_time"] = time.Now().UTC()
  168. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, types.PorterAppEventType_PreDeploy, eventID, eventStatus, metadata)
  169. return fmt.Errorf("error calling predeploy status endpoint: %w", err)
  170. }
  171. if predeployStatusResp.Status == porter_app.PredeployStatus_Failed {
  172. eventStatus = types.PorterAppEventStatus_Failed
  173. break
  174. }
  175. if predeployStatusResp.Status == porter_app.PredeployStatus_Successful {
  176. break
  177. }
  178. time.Sleep(checkPredeployFrequency)
  179. }
  180. metadata["end_time"] = time.Now().UTC()
  181. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, types.PorterAppEventType_PreDeploy, eventID, eventStatus, metadata)
  182. updateResp, err = client.UpdateApp(ctx, updateInput)
  183. if err != nil {
  184. return fmt.Errorf("apply error post-predeploy: %w", err)
  185. }
  186. }
  187. if updateResp.CLIAction != porter_app.CLIAction_NoAction {
  188. return fmt.Errorf("unexpected CLI action: %d", updateResp.CLIAction)
  189. }
  190. _, _ = client.ReportRevisionStatus(ctx, api.ReportRevisionStatusInput{
  191. ProjectID: cliConf.Project,
  192. ClusterID: cliConf.Cluster,
  193. AppName: appName,
  194. AppRevisionID: updateResp.AppRevisionId,
  195. PRNumber: prNumber,
  196. CommitSHA: commitSHA,
  197. })
  198. color.New(color.FgGreen).Printf("Successfully applied new revision %s for app %s\n", updateResp.AppRevisionId, appName) // nolint:errcheck,gosec
  199. return nil
  200. }
  201. func commitSHAFromEnv() string {
  202. var commitSHA string
  203. if os.Getenv("PORTER_COMMIT_SHA") != "" {
  204. commitSHA = os.Getenv("PORTER_COMMIT_SHA")
  205. } else if os.Getenv("GITHUB_SHA") != "" {
  206. commitSHA = os.Getenv("GITHUB_SHA")
  207. } else if commit, err := git.LastCommit(); err == nil && commit != nil {
  208. commitSHA = commit.Sha
  209. }
  210. return commitSHA
  211. }
  212. // checkPredeployTimeout is the maximum amount of time the CLI will wait for a predeploy to complete before calling apply again
  213. const checkPredeployTimeout = 60 * time.Minute
  214. // checkPredeployFrequency is the frequency at which the CLI will check the status of a predeploy
  215. const checkPredeployFrequency = 10 * time.Second
  216. func gitSourceFromEnv() (porter_app.GitSource, error) {
  217. var source porter_app.GitSource
  218. var repoID uint
  219. if os.Getenv("GITHUB_REPOSITORY_ID") != "" {
  220. id, err := strconv.Atoi(os.Getenv("GITHUB_REPOSITORY_ID"))
  221. if err != nil {
  222. return source, fmt.Errorf("unable to parse GITHUB_REPOSITORY_ID to int: %w", err)
  223. }
  224. repoID = uint(id)
  225. }
  226. return porter_app.GitSource{
  227. GitBranch: os.Getenv("GITHUB_REF_NAME"),
  228. GitRepoID: repoID,
  229. GitRepoName: os.Getenv("GITHUB_REPOSITORY"),
  230. }, nil
  231. }
  232. func buildInputFromBuildSettings(projectID uint, appName string, image porter_app.Image, build porter_app.BuildSettings) (buildInput, error) {
  233. var buildSettings buildInput
  234. if appName == "" {
  235. return buildSettings, errors.New("app name is empty")
  236. }
  237. if image.Tag == "" {
  238. return buildSettings, errors.New("image tag is empty")
  239. }
  240. if build.Method == "" {
  241. return buildSettings, errors.New("build method is empty")
  242. }
  243. return buildInput{
  244. ProjectID: projectID,
  245. AppName: appName,
  246. BuildContext: build.Context,
  247. Dockerfile: build.Dockerfile,
  248. BuildMethod: build.Method,
  249. Builder: build.Builder,
  250. BuildPacks: build.Buildpacks,
  251. ImageTag: image.Tag,
  252. RepositoryURL: image.Repository,
  253. }, nil
  254. }
  255. func deploymentTargetFromConfig(ctx context.Context, client api.Client, projectID, clusterID uint, previewApply bool) (string, error) {
  256. var deploymentTargetID string
  257. if os.Getenv("PORTER_DEPLOYMENT_TARGET_ID") != "" {
  258. deploymentTargetID = os.Getenv("PORTER_DEPLOYMENT_TARGET_ID")
  259. }
  260. if deploymentTargetID == "" {
  261. targetResp, err := client.DefaultDeploymentTarget(ctx, projectID, clusterID)
  262. if err != nil {
  263. return deploymentTargetID, fmt.Errorf("error calling default deployment target endpoint: %w", err)
  264. }
  265. deploymentTargetID = targetResp.DeploymentTargetID
  266. }
  267. if previewApply {
  268. var branchName string
  269. // branch name is set to different values in the GH env, depending on whether or not the workflow is triggered by a PR
  270. // issue is being tracked here: https://github.com/github/docs/issues/15319
  271. if os.Getenv("GITHUB_HEAD_REF") != "" {
  272. branchName = os.Getenv("GITHUB_HEAD_REF")
  273. } else if os.Getenv("GITHUB_REF_NAME") != "" {
  274. branchName = os.Getenv("GITHUB_REF_NAME")
  275. } else if branch, err := git.CurrentBranch(); err == nil {
  276. branchName = branch
  277. }
  278. if branchName == "" {
  279. return deploymentTargetID, errors.New("branch name is empty. Please run apply in a git repository with access to the git CLI")
  280. }
  281. targetResp, err := client.CreateDeploymentTarget(ctx, projectID, clusterID, branchName, true)
  282. if err != nil {
  283. return deploymentTargetID, fmt.Errorf("error calling create deployment target endpoint: %w", err)
  284. }
  285. deploymentTargetID = targetResp.DeploymentTargetID
  286. }
  287. if deploymentTargetID == "" {
  288. return deploymentTargetID, errors.New("deployment target id is empty")
  289. }
  290. return deploymentTargetID, nil
  291. }
  292. type reportBuildFailureInput struct {
  293. client api.Client
  294. appName string
  295. cliConf config.CLIConfig
  296. deploymentTargetID string
  297. appRevisionID string
  298. eventID string
  299. buildError error
  300. buildLogs string
  301. commitSHA string
  302. prNumber int
  303. }
  304. func reportBuildFailure(ctx context.Context, inp reportBuildFailureInput) error {
  305. _, err := inp.client.UpdateRevisionStatus(ctx, inp.cliConf.Project, inp.cliConf.Cluster, inp.appName, inp.appRevisionID, models.AppRevisionStatus_BuildFailed)
  306. if err != nil {
  307. return err
  308. }
  309. buildMetadata := make(map[string]interface{})
  310. buildMetadata["end_time"] = time.Now().UTC()
  311. // the below is a temporary solution until we can report build errors via telemetry from the CLI
  312. errorStringMap := make(map[string]string)
  313. errorStringMap["build-error"] = fmt.Sprintf("%+v", inp.buildError)
  314. b64BuildLogs := base64.StdEncoding.EncodeToString([]byte(inp.buildLogs))
  315. // the key name below must be kept the same so that reportBuildStatus in the CreateOrUpdatePorterAppEvent handler reports logs correctly
  316. errorStringMap["b64-build-logs"] = b64BuildLogs
  317. buildMetadata["errors"] = errorStringMap
  318. err = updateExistingEvent(ctx, inp.client, inp.appName, inp.cliConf.Project, inp.cliConf.Cluster, inp.deploymentTargetID, types.PorterAppEventType_Build, inp.eventID, types.PorterAppEventStatus_Failed, buildMetadata)
  319. if err != nil {
  320. return err
  321. }
  322. _, err = inp.client.ReportRevisionStatus(ctx, api.ReportRevisionStatusInput{
  323. ProjectID: inp.cliConf.Project,
  324. ClusterID: inp.cliConf.Cluster,
  325. AppName: inp.appName,
  326. AppRevisionID: inp.appRevisionID,
  327. PRNumber: inp.prNumber,
  328. CommitSHA: inp.commitSHA,
  329. })
  330. if err != nil {
  331. return err
  332. }
  333. return nil
  334. }