apply.go 9.8 KB

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