apply.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 b64AppProto string
  25. targetResp, err := client.DefaultDeploymentTarget(ctx, cliConf.Project, cliConf.Cluster)
  26. if err != nil {
  27. return fmt.Errorf("error calling default deployment target endpoint: %w", err)
  28. }
  29. if targetResp.DeploymentTargetID == "" {
  30. return errors.New("deployment target id is empty")
  31. }
  32. if len(porterYamlPath) != 0 {
  33. porterYaml, err := os.ReadFile(filepath.Clean(porterYamlPath))
  34. if err != nil {
  35. return fmt.Errorf("could not read porter yaml file: %w", err)
  36. }
  37. b64YAML := base64.StdEncoding.EncodeToString(porterYaml)
  38. // last argument is passed to accommodate users with v1 porter yamls
  39. parseResp, err := client.ParseYAML(ctx, cliConf.Project, cliConf.Cluster, b64YAML, appName)
  40. if err != nil {
  41. return fmt.Errorf("error calling parse yaml endpoint: %w", err)
  42. }
  43. if parseResp.B64AppProto == "" {
  44. return errors.New("b64 app proto is empty")
  45. }
  46. b64AppProto = parseResp.B64AppProto
  47. // we only need to create the app if a porter yaml is provided (otherwise it must already exist)
  48. createPorterAppDBEntryInp, err := createPorterAppDbEntryInputFromProtoAndEnv(parseResp.B64AppProto)
  49. if err != nil {
  50. return fmt.Errorf("error creating porter app db entry input from proto: %w", err)
  51. }
  52. err = client.CreatePorterAppDBEntry(ctx, cliConf.Project, cliConf.Cluster, createPorterAppDBEntryInp)
  53. if err != nil {
  54. return fmt.Errorf("error creating porter app db entry: %w", err)
  55. }
  56. // override app name if provided
  57. appName, err = appNameFromB64AppProto(parseResp.B64AppProto)
  58. if err != nil {
  59. return fmt.Errorf("error getting app name from b64 app proto: %w", err)
  60. }
  61. envGroupResp, err := client.CreateOrUpdateAppEnvironment(ctx, cliConf.Project, cliConf.Cluster, appName, targetResp.DeploymentTargetID, parseResp.EnvVariables, parseResp.EnvSecrets)
  62. if err != nil {
  63. return fmt.Errorf("error calling create or update app environment group endpoint: %w", err)
  64. }
  65. b64AppProto, err = updateAppEnvGroupInProto(ctx, b64AppProto, envGroupResp.EnvGroupName, envGroupResp.EnvGroupVersion)
  66. if err != nil {
  67. return fmt.Errorf("error updating app env group in proto: %w", err)
  68. }
  69. color.New(color.FgGreen).Printf("Successfully parsed Porter YAML: applying app \"%s\"\n", appName) // nolint:errcheck,gosec
  70. }
  71. var commitSHA string
  72. if os.Getenv("PORTER_COMMIT_SHA") != "" {
  73. commitSHA = os.Getenv("PORTER_COMMIT_SHA")
  74. } else if os.Getenv("GITHUB_SHA") != "" {
  75. commitSHA = os.Getenv("GITHUB_SHA")
  76. } else if commit, err := git.LastCommit(); err == nil && commit != nil {
  77. commitSHA = commit.Sha
  78. }
  79. validateResp, err := client.ValidatePorterApp(ctx, cliConf.Project, cliConf.Cluster, appName, b64AppProto, targetResp.DeploymentTargetID, commitSHA)
  80. if err != nil {
  81. return fmt.Errorf("error calling validate endpoint: %w", err)
  82. }
  83. if validateResp.ValidatedBase64AppProto == "" {
  84. return errors.New("validated b64 app proto is empty")
  85. }
  86. base64AppProto := validateResp.ValidatedBase64AppProto
  87. applyResp, err := client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, base64AppProto, targetResp.DeploymentTargetID, "", forceBuild)
  88. if err != nil {
  89. return fmt.Errorf("error calling apply endpoint: %w", err)
  90. }
  91. if applyResp.AppRevisionId == "" {
  92. return errors.New("app revision id is empty")
  93. }
  94. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_BUILD {
  95. color.New(color.FgGreen).Printf("Building new image...\n") // nolint:errcheck,gosec
  96. eventID, _ := createBuildEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID)
  97. if commitSHA == "" {
  98. 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.")
  99. }
  100. buildSettings, err := buildSettingsFromBase64AppProto(base64AppProto)
  101. if err != nil {
  102. return fmt.Errorf("error building settings from base64 app proto: %w", err)
  103. }
  104. currentAppRevisionResp, err := client.CurrentAppRevision(ctx, cliConf.Project, cliConf.Cluster, appName, targetResp.DeploymentTargetID)
  105. if err != nil {
  106. return fmt.Errorf("error getting current app revision: %w", err)
  107. }
  108. if currentAppRevisionResp == nil {
  109. return errors.New("current app revision is nil")
  110. }
  111. appRevision := currentAppRevisionResp.AppRevision
  112. if appRevision.B64AppProto == "" {
  113. return errors.New("current app revision b64 app proto is empty")
  114. }
  115. currentImageTag, err := imageTagFromBase64AppProto(appRevision.B64AppProto)
  116. if err != nil {
  117. return fmt.Errorf("error getting image tag from current app revision: %w", err)
  118. }
  119. buildSettings.CurrentImageTag = currentImageTag
  120. buildSettings.ProjectID = cliConf.Project
  121. buildEnv, err := client.GetBuildEnv(ctx, cliConf.Project, cliConf.Cluster, appName, targetResp.DeploymentTargetID)
  122. if err != nil {
  123. return fmt.Errorf("error getting build env: %w", err)
  124. }
  125. buildSettings.Env = buildEnv.BuildEnvVariables
  126. err = build(ctx, client, buildSettings)
  127. buildMetadata := make(map[string]interface{})
  128. buildMetadata["end_time"] = time.Now().UTC()
  129. if err != nil {
  130. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, types.PorterAppEventStatus_Failed, buildMetadata)
  131. _, _ = client.UpdateRevisionStatus(ctx, cliConf.Project, cliConf.Cluster, appName, applyResp.AppRevisionId, models.AppRevisionStatus_BuildFailed)
  132. return fmt.Errorf("error building app: %w", err)
  133. }
  134. color.New(color.FgGreen).Printf("Successfully built image (tag: %s)\n", buildSettings.ImageTag) // nolint:errcheck,gosec
  135. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, types.PorterAppEventStatus_Success, buildMetadata)
  136. applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId, !forceBuild)
  137. if err != nil {
  138. return fmt.Errorf("apply error post-build: %w", err)
  139. }
  140. }
  141. color.New(color.FgGreen).Printf("Image tag exists in repository\n") // nolint:errcheck,gosec
  142. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_TRACK_PREDEPLOY {
  143. color.New(color.FgGreen).Printf("Waiting for predeploy to complete...\n") // nolint:errcheck,gosec
  144. now := time.Now().UTC()
  145. eventID, _ := createPredeployEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, now, applyResp.AppRevisionId)
  146. eventStatus := types.PorterAppEventStatus_Success
  147. for {
  148. if time.Since(now) > checkPredeployTimeout {
  149. return errors.New("timed out waiting for predeploy to complete")
  150. }
  151. predeployStatusResp, err := client.PredeployStatus(ctx, cliConf.Project, cliConf.Cluster, appName, applyResp.AppRevisionId)
  152. if err != nil {
  153. return fmt.Errorf("error calling predeploy status endpoint: %w", err)
  154. }
  155. if predeployStatusResp.Status == porter_app.PredeployStatus_Failed {
  156. eventStatus = types.PorterAppEventStatus_Failed
  157. break
  158. }
  159. if predeployStatusResp.Status == porter_app.PredeployStatus_Successful {
  160. break
  161. }
  162. time.Sleep(checkPredeployFrequency)
  163. }
  164. metadata := make(map[string]interface{})
  165. metadata["end_time"] = time.Now().UTC()
  166. _ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, targetResp.DeploymentTargetID, eventID, eventStatus, metadata)
  167. applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId, !forceBuild)
  168. if err != nil {
  169. return fmt.Errorf("apply error post-predeploy: %w", err)
  170. }
  171. }
  172. if applyResp.CLIAction != porterv1.EnumCLIAction_ENUM_CLI_ACTION_NONE {
  173. return fmt.Errorf("unexpected CLI action: %s", applyResp.CLIAction)
  174. }
  175. color.New(color.FgGreen).Printf("Successfully applied new revision %s for app %s\n", applyResp.AppRevisionId, appName) // nolint:errcheck,gosec
  176. return nil
  177. }
  178. // checkPredeployTimeout is the maximum amount of time the CLI will wait for a predeploy to complete before calling apply again
  179. const checkPredeployTimeout = 60 * time.Minute
  180. // checkPredeployFrequency is the frequency at which the CLI will check the status of a predeploy
  181. const checkPredeployFrequency = 10 * time.Second
  182. func appNameFromB64AppProto(base64AppProto string) (string, error) {
  183. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  184. if err != nil {
  185. return "", fmt.Errorf("unable to decode base64 app for revision: %w", err)
  186. }
  187. app := &porterv1.PorterApp{}
  188. err = helpers.UnmarshalContractObject(decoded, app)
  189. if err != nil {
  190. return "", fmt.Errorf("unable to unmarshal app for revision: %w", err)
  191. }
  192. if app.Name == "" {
  193. return "", fmt.Errorf("app does not contain name")
  194. }
  195. return app.Name, nil
  196. }
  197. func createPorterAppDbEntryInputFromProtoAndEnv(base64AppProto string) (api.CreatePorterAppDBEntryInput, error) {
  198. var input api.CreatePorterAppDBEntryInput
  199. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  200. if err != nil {
  201. return input, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  202. }
  203. app := &porterv1.PorterApp{}
  204. err = helpers.UnmarshalContractObject(decoded, app)
  205. if err != nil {
  206. return input, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  207. }
  208. if app.Name == "" {
  209. return input, fmt.Errorf("app does not contain name")
  210. }
  211. input.AppName = app.Name
  212. if app.Build != nil {
  213. if os.Getenv("GITHUB_REPOSITORY_ID") == "" {
  214. input.Local = true
  215. return input, nil
  216. }
  217. gitRepoId, err := strconv.Atoi(os.Getenv("GITHUB_REPOSITORY_ID"))
  218. if err != nil {
  219. return input, fmt.Errorf("unable to parse GITHUB_REPOSITORY_ID to int: %w", err)
  220. }
  221. input.GitRepoID = uint(gitRepoId)
  222. input.GitRepoName = os.Getenv("GITHUB_REPOSITORY")
  223. input.GitBranch = os.Getenv("GITHUB_REF_NAME")
  224. input.PorterYamlPath = "porter.yaml"
  225. return input, nil
  226. }
  227. if app.Image != nil {
  228. input.ImageRepository = app.Image.Repository
  229. input.ImageTag = app.Image.Tag
  230. return input, nil
  231. }
  232. return input, fmt.Errorf("app does not contain build or image settings")
  233. }
  234. func buildSettingsFromBase64AppProto(base64AppProto string) (buildInput, error) {
  235. var buildSettings buildInput
  236. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  237. if err != nil {
  238. return buildSettings, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  239. }
  240. app := &porterv1.PorterApp{}
  241. err = helpers.UnmarshalContractObject(decoded, app)
  242. if err != nil {
  243. return buildSettings, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  244. }
  245. if app.Name == "" {
  246. return buildSettings, fmt.Errorf("app does not contain name")
  247. }
  248. if app.Build == nil {
  249. return buildSettings, fmt.Errorf("app does not contain build settings")
  250. }
  251. if app.Image == nil {
  252. return buildSettings, fmt.Errorf("app does not contain image settings")
  253. }
  254. return buildInput{
  255. AppName: app.Name,
  256. BuildContext: app.Build.Context,
  257. Dockerfile: app.Build.Dockerfile,
  258. BuildMethod: app.Build.Method,
  259. Builder: app.Build.Builder,
  260. BuildPacks: app.Build.Buildpacks,
  261. ImageTag: app.Image.Tag,
  262. RepositoryURL: app.Image.Repository,
  263. }, nil
  264. }
  265. func imageTagFromBase64AppProto(base64AppProto string) (string, error) {
  266. var image string
  267. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  268. if err != nil {
  269. return image, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  270. }
  271. app := &porterv1.PorterApp{}
  272. err = helpers.UnmarshalContractObject(decoded, app)
  273. if err != nil {
  274. return image, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  275. }
  276. if app.Image == nil {
  277. return image, fmt.Errorf("app does not contain image settings")
  278. }
  279. if app.Image.Tag == "" {
  280. return image, fmt.Errorf("app does not contain image tag")
  281. }
  282. return app.Image.Tag, nil
  283. }
  284. func updateAppEnvGroupInProto(ctx context.Context, base64AppProto string, envGroupName string, envGroupVersion int) (string, error) {
  285. var editedB64AppProto string
  286. decoded, err := base64.StdEncoding.DecodeString(base64AppProto)
  287. if err != nil {
  288. return editedB64AppProto, fmt.Errorf("unable to decode base64 app for revision: %w", err)
  289. }
  290. app := &porterv1.PorterApp{}
  291. err = helpers.UnmarshalContractObject(decoded, app)
  292. if err != nil {
  293. return editedB64AppProto, fmt.Errorf("unable to unmarshal app for revision: %w", err)
  294. }
  295. envGroupExists := false
  296. for _, envGroup := range app.EnvGroups {
  297. if envGroup.Name == envGroupName {
  298. envGroup.Version = int64(envGroupVersion)
  299. envGroupExists = true
  300. break
  301. }
  302. }
  303. if !envGroupExists {
  304. app.EnvGroups = append(app.EnvGroups, &porterv1.EnvGroup{
  305. Name: envGroupName,
  306. Version: int64(envGroupVersion),
  307. })
  308. }
  309. marshalled, err := helpers.MarshalContractObject(ctx, app)
  310. if err != nil {
  311. return editedB64AppProto, fmt.Errorf("unable to marshal app back to json: %w", err)
  312. }
  313. editedB64AppProto = base64.StdEncoding.EncodeToString(marshalled)
  314. return editedB64AppProto, nil
  315. }