2
0

environment.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package stack
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. api "github.com/porter-dev/porter/api/client"
  8. "github.com/porter-dev/porter/cli/cmd/config"
  9. )
  10. type GitHubMetadata struct {
  11. Repo, RepoOwner, BranchFrom, PRName, PullRequestID string
  12. }
  13. type EnvironmentMeta struct {
  14. EnvironmentConfigID uint `json:"environment_config_id"`
  15. Namespace string `json:"namespace"`
  16. GitHubMetadata GitHubMetadata `json:"github_metadata"`
  17. }
  18. func HandleEnvironmentConfiguration(
  19. client *api.Client,
  20. cliConf *config.CLIConfig,
  21. applicationName string,
  22. ) (string, EnvironmentMeta, error) {
  23. var namespace string
  24. envMeta := EnvironmentMeta{}
  25. environmentConfigID := os.Getenv("PORTER_ENVIRONMENT_ID")
  26. if environmentConfigID != "" {
  27. eci, err := strconv.Atoi(environmentConfigID)
  28. if err != nil {
  29. return "", envMeta, fmt.Errorf("unable to parse PORTER_ENVIRONMENT_ID: %w", err)
  30. }
  31. ghMeta, err := getGitDeployMeta()
  32. if err != nil {
  33. return "", envMeta, fmt.Errorf("unable to deploy to environmet: %w", err)
  34. }
  35. envConf, err := client.GetEnvironmentConfig(context.Background(), cliConf.Project, cliConf.Cluster, uint(eci))
  36. if err != nil {
  37. return "", envMeta, fmt.Errorf("unable to read environment config from DB: %w", err)
  38. }
  39. namespace = formatNamespaceForEnvironment(envConf.Name, ghMeta.RepoOwner, ghMeta.Repo, ghMeta.PullRequestID)
  40. envMeta.EnvironmentConfigID = uint(eci)
  41. envMeta.Namespace = namespace
  42. envMeta.GitHubMetadata = ghMeta
  43. } else {
  44. namespace = fmt.Sprintf("porter-stack-%s", applicationName)
  45. }
  46. return namespace, envMeta, nil
  47. }
  48. func formatNamespaceForEnvironment(envName, repoOwner, repo, pullRequestID string) string {
  49. return fmt.Sprintf("porter-env-%s-%s-%s-%s", envName, repoOwner, repo, pullRequestID)
  50. }
  51. func getGitDeployMeta() (GitHubMetadata, error) {
  52. ghMeta := GitHubMetadata{}
  53. branchFrom := os.Getenv("PORTER_BRANCH_FROM")
  54. if branchFrom == "" {
  55. return ghMeta, fmt.Errorf("PORTER_BRANCH_FROM not set")
  56. }
  57. ghMeta.BranchFrom = branchFrom
  58. repoName := os.Getenv("PORTER_REPO_NAME")
  59. if repoName == "" {
  60. return ghMeta, fmt.Errorf("PORTER_REPO_NAME not set")
  61. }
  62. ghMeta.Repo = repoName
  63. repoOwner := os.Getenv("PORTER_REPO_OWNER")
  64. if repoOwner == "" {
  65. return ghMeta, fmt.Errorf("PORTER_REPO_OWNER not set")
  66. }
  67. ghMeta.RepoOwner = repoOwner
  68. prName := os.Getenv("PORTER_PR_NAME")
  69. if prName == "" {
  70. return ghMeta, fmt.Errorf("PORTER_PR_NAME not set")
  71. }
  72. ghMeta.PRName = prName
  73. prIDStr := os.Getenv("PORTER_PULL_REQUEST_ID")
  74. if prIDStr == "" {
  75. return ghMeta, fmt.Errorf("PORTER_PULL_REQUEST_ID not set")
  76. }
  77. ghMeta.PullRequestID = prIDStr
  78. return ghMeta, nil
  79. }