apply.go 12 KB

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