apply.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package stack
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "github.com/fatih/color"
  9. api "github.com/porter-dev/porter/api/client"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/cli/cmd/config"
  12. switchboardTypes "github.com/porter-dev/switchboard/pkg/types"
  13. switchboardWorker "github.com/porter-dev/switchboard/pkg/worker"
  14. "gopkg.in/yaml.v3"
  15. )
  16. type StackConf struct {
  17. apiClient *api.Client
  18. parsed *Application
  19. stackName, namespace string
  20. projectID, clusterID uint
  21. }
  22. func CreateApplicationDeploy(client *api.Client, worker *switchboardWorker.Worker, app *Application, applicationName string, cliConf *config.CLIConfig) ([]*switchboardTypes.Resource, error) {
  23. // we need to know the builder so that we can inject launcher to the start command later if heroku builder is used
  24. var builder string
  25. namespace, envMeta, err := HandleEnvironmentConfiguration(client, cliConf, applicationName)
  26. if err != nil {
  27. return nil, err
  28. }
  29. stackConf, err := createStackConf(client, app, namespace, applicationName, cliConf.Project, cliConf.Cluster)
  30. if err != nil {
  31. return nil, fmt.Errorf("error parsing porter.yaml: %w", err)
  32. }
  33. resources, builder, err := createV1BuildResources(client, app, stackConf, envMeta)
  34. if err != nil {
  35. return nil, err
  36. }
  37. applicationBytes, err := yaml.Marshal(app)
  38. if err != nil {
  39. return nil, fmt.Errorf("malformed application definition: %w", err)
  40. }
  41. deployStackHook := &DeployAppHook{
  42. Client: client,
  43. ApplicationName: applicationName,
  44. ProjectID: cliConf.Project,
  45. ClusterID: cliConf.Cluster,
  46. BuildImageDriverName: GetBuildImageDriverName(applicationName),
  47. PorterYAML: applicationBytes,
  48. Builder: builder,
  49. Namespace: namespace,
  50. EnvironmentMeta: envMeta,
  51. }
  52. worker.RegisterHook("deploy-stack", deployStackHook)
  53. if os.Getenv("GITHUB_RUN_ID") != "" {
  54. err := createAppEvent(client, applicationName, cliConf)
  55. if err != nil {
  56. return nil, err
  57. }
  58. }
  59. return resources, nil
  60. }
  61. // Create app event to signfy start of build
  62. func createAppEvent(client *api.Client, applicationName string, cliConf *config.CLIConfig) error {
  63. req := &types.CreateOrUpdatePorterAppEventRequest{
  64. Status: "PROGRESSING",
  65. Type: types.PorterAppEventType_Build,
  66. TypeExternalSource: "GITHUB",
  67. Metadata: map[string]any{
  68. "action_run_id": os.Getenv("GITHUB_RUN_ID"),
  69. "org": os.Getenv("GITHUB_REPOSITORY_OWNER"),
  70. },
  71. }
  72. repoNameSplit := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")
  73. if len(repoNameSplit) != 2 {
  74. return fmt.Errorf("unable to parse GITHUB_REPOSITORY")
  75. }
  76. req.Metadata["repo"] = repoNameSplit[1]
  77. actionRunID := os.Getenv("GITHUB_RUN_ID")
  78. if actionRunID != "" {
  79. arid, err := strconv.Atoi(actionRunID)
  80. if err != nil {
  81. return fmt.Errorf("unable to parse GITHUB_RUN_ID as int: %w", err)
  82. }
  83. req.Metadata["action_run_id"] = arid
  84. }
  85. repoOwnerAccountID := os.Getenv("GITHUB_REPOSITORY_OWNER_ID")
  86. if repoOwnerAccountID != "" {
  87. arid, err := strconv.Atoi(repoOwnerAccountID)
  88. if err != nil {
  89. return fmt.Errorf("unable to parse GITHUB_REPOSITORY_OWNER_ID as int: %w", err)
  90. }
  91. req.Metadata["github_account_id"] = arid
  92. }
  93. ctx := context.Background()
  94. _, err := client.CreateOrUpdatePorterAppEvent(ctx, cliConf.Project, cliConf.Cluster, applicationName, req)
  95. if err != nil {
  96. return fmt.Errorf("unable to create porter app build event: %w", err)
  97. }
  98. return nil
  99. }
  100. func createV1BuildResources(client *api.Client, app *Application, stackConf *StackConf, envMeta EnvironmentMeta) ([]*switchboardTypes.Resource, string, error) {
  101. resources := make([]*switchboardTypes.Resource, 0)
  102. // look up build settings from DB if none specified in porter.yaml
  103. if stackConf.parsed.Build == nil {
  104. color.New(color.FgYellow).Printf("No build values specified in porter.yaml, attempting to load stack build settings instead \n")
  105. var converted Build
  106. if envMeta.EnvironmentConfigID == 0 {
  107. res, err := client.GetPorterApp(context.Background(), stackConf.projectID, stackConf.clusterID, stackConf.stackName)
  108. if err != nil {
  109. return nil, "", fmt.Errorf("unable to read build info from DB: %w", err)
  110. }
  111. converted = convertToBuild(res)
  112. } else {
  113. color.New(color.FgYellow).Printf("Looking for application %s in specified environment \n", stackConf.stackName)
  114. res, err := client.GetPorterAppByEnvironment(context.Background(), stackConf.projectID, stackConf.clusterID, envMeta.EnvironmentConfigID, stackConf.stackName)
  115. if err != nil {
  116. return nil, "", fmt.Errorf("unable to read build info from DB: %w", err)
  117. }
  118. converted = convertToBuild(res)
  119. }
  120. stackConf.parsed.Build = &converted
  121. }
  122. // only include build and push steps if an image is not already specified
  123. if stackConf.parsed.Build.Image == nil {
  124. bi, pi, builder, err := createV1BuildResourcesFromPorterYaml(stackConf)
  125. if err != nil {
  126. return nil, "", err
  127. }
  128. resources = append(resources, bi, pi)
  129. // also excluding use of pre-deploy with pre-built imges
  130. preDeploy, cmd, err := createPreDeployResource(client,
  131. stackConf.parsed.Release,
  132. stackConf.stackName,
  133. bi.Name,
  134. pi.Name,
  135. stackConf.projectID,
  136. stackConf.clusterID,
  137. stackConf.parsed.Env,
  138. )
  139. if err != nil {
  140. return nil, "", err
  141. }
  142. if preDeploy != nil {
  143. color.New(color.FgYellow).Printf("Found pre-deploy command to run before deploying apps: %s \n", cmd)
  144. resources = append(resources, preDeploy)
  145. } else {
  146. color.New(color.FgYellow).Printf("No pre-deploy command found in porter.yaml or helm. \n")
  147. }
  148. return resources, builder, nil
  149. }
  150. return resources, "", nil
  151. }
  152. func createStackConf(client *api.Client, app *Application, namespace string, stackName string, projectID uint, clusterID uint) (*StackConf, error) {
  153. err := config.ValidateCLIEnvironment()
  154. if err != nil {
  155. errMsg := composePreviewMessage("porter CLI is not configured correctly", Error)
  156. return nil, fmt.Errorf("%s: %w", errMsg, err)
  157. }
  158. releaseEnvVars := getEnvFromRelease(client, stackName, projectID, clusterID)
  159. if releaseEnvVars != nil {
  160. color.New(color.FgYellow).Printf("Reading build env from release\n")
  161. app.Env = mergeStringMaps(app.Env, releaseEnvVars)
  162. }
  163. return &StackConf{
  164. apiClient: client,
  165. parsed: app,
  166. stackName: stackName,
  167. projectID: projectID,
  168. clusterID: clusterID,
  169. namespace: namespace,
  170. }, nil
  171. }
  172. func createV1BuildResourcesFromPorterYaml(stackConf *StackConf) (*switchboardTypes.Resource, *switchboardTypes.Resource, string, error) {
  173. bi, err := stackConf.parsed.Build.getV1BuildImage(stackConf.stackName, stackConf.parsed.Env, stackConf.namespace)
  174. if err != nil {
  175. return nil, nil, "", err
  176. }
  177. pi, err := stackConf.parsed.Build.getV1PushImage(stackConf.stackName, stackConf.namespace)
  178. if err != nil {
  179. return nil, nil, "", err
  180. }
  181. return bi, pi, stackConf.parsed.Build.GetBuilder(), nil
  182. }
  183. func convertToBuild(porterApp *types.PorterApp) Build {
  184. var context *string
  185. if porterApp.BuildContext != "" {
  186. context = &porterApp.BuildContext
  187. }
  188. var method *string
  189. var m string
  190. if porterApp.RepoName == "" {
  191. m = "registry"
  192. method = &m
  193. } else if porterApp.Dockerfile == "" {
  194. m = "pack"
  195. method = &m
  196. } else {
  197. m = "docker"
  198. method = &m
  199. }
  200. var builder *string
  201. if porterApp.Builder != "" {
  202. builder = &porterApp.Builder
  203. }
  204. var buildpacks []*string
  205. if porterApp.Buildpacks != "" {
  206. bpSlice := strings.Split(porterApp.Buildpacks, ",")
  207. buildpacks = make([]*string, len(bpSlice))
  208. for i, bp := range bpSlice {
  209. temp := bp
  210. buildpacks[i] = &temp
  211. }
  212. }
  213. var dockerfile *string
  214. if porterApp.Dockerfile != "" {
  215. dockerfile = &porterApp.Dockerfile
  216. }
  217. var image *string
  218. if porterApp.ImageRepoURI != "" {
  219. image = &porterApp.ImageRepoURI
  220. }
  221. return Build{
  222. Context: context,
  223. Method: method,
  224. Builder: builder,
  225. Buildpacks: buildpacks,
  226. Dockerfile: dockerfile,
  227. Image: image,
  228. }
  229. }
  230. func getEnvFromRelease(client *api.Client, stackName string, projectID uint, clusterID uint) map[string]string {
  231. var envVarsStringMap map[string]string
  232. namespace := fmt.Sprintf("porter-stack-%s", stackName)
  233. release, err := client.GetRelease(
  234. context.Background(),
  235. projectID,
  236. clusterID,
  237. namespace,
  238. stackName,
  239. )
  240. if err == nil && release != nil {
  241. for key, val := range release.Config {
  242. if key != "global" && isMapStringInterface(val) {
  243. appConfig := val.(map[string]interface{})
  244. if appConfig != nil {
  245. if container, ok := appConfig["container"]; ok {
  246. if containerMap, ok := container.(map[string]interface{}); ok {
  247. if env, ok := containerMap["env"]; ok {
  248. if envMap, ok := env.(map[string]interface{}); ok {
  249. if normal, ok := envMap["normal"]; ok {
  250. if normalMap, ok := normal.(map[string]interface{}); ok {
  251. convertedMap, err := toStringMap(normalMap)
  252. if err == nil && len(convertedMap) > 0 {
  253. envVarsStringMap = convertedMap
  254. break
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. return envVarsStringMap
  267. }
  268. func isMapStringInterface(val interface{}) bool {
  269. _, ok := val.(map[string]interface{})
  270. return ok
  271. }
  272. func toStringMap(m map[string]interface{}) (map[string]string, error) {
  273. result := make(map[string]string)
  274. for k, v := range m {
  275. strVal, ok := v.(string)
  276. if !ok {
  277. return nil, fmt.Errorf("value for key %q is not a string", k)
  278. }
  279. result[k] = strVal
  280. }
  281. return result, nil
  282. }
  283. func mergeStringMaps(base, override map[string]string) map[string]string {
  284. result := make(map[string]string)
  285. if base == nil && override == nil {
  286. return result
  287. }
  288. for k, v := range base {
  289. result[k] = v
  290. }
  291. for k, v := range override {
  292. result[k] = v
  293. }
  294. return result
  295. }