apply.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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)
  34. if err != nil {
  35. return nil, fmt.Errorf("error parsing porter.yaml for build resources: %w", 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) ([]*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. res, err := client.GetPorterApp(context.Background(), stackConf.projectID, stackConf.clusterID, stackConf.stackName)
  106. if err != nil {
  107. return nil, "", fmt.Errorf("unable to read build info from DB: %w", err)
  108. }
  109. converted := convertToBuild(res)
  110. stackConf.parsed.Build = &converted
  111. }
  112. // only include build and push steps if an image is not already specified
  113. if stackConf.parsed.Build.Image == nil {
  114. bi, pi, builder, err := createV1BuildResourcesFromPorterYaml(stackConf)
  115. if err != nil {
  116. return nil, "", err
  117. }
  118. resources = append(resources, bi, pi)
  119. // also excluding use of pre-deploy with pre-built imges
  120. preDeploy, cmd, err := createPreDeployResource(client,
  121. stackConf.parsed.Release,
  122. stackConf.stackName,
  123. bi.Name,
  124. pi.Name,
  125. stackConf.projectID,
  126. stackConf.clusterID,
  127. stackConf.parsed.Env,
  128. )
  129. if err != nil {
  130. return nil, "", err
  131. }
  132. if preDeploy != nil {
  133. color.New(color.FgYellow).Printf("Found pre-deploy command to run before deploying apps: %s \n", cmd)
  134. resources = append(resources, preDeploy)
  135. } else {
  136. color.New(color.FgYellow).Printf("No pre-deploy command found in porter.yaml or helm. \n")
  137. }
  138. return resources, builder, nil
  139. }
  140. return resources, "", nil
  141. }
  142. func createStackConf(client *api.Client, app *Application, namespace string, stackName string, projectID uint, clusterID uint) (*StackConf, error) {
  143. err := config.ValidateCLIEnvironment()
  144. if err != nil {
  145. errMsg := composePreviewMessage("porter CLI is not configured correctly", Error)
  146. return nil, fmt.Errorf("%s: %w", errMsg, err)
  147. }
  148. releaseEnvVars := getEnvFromRelease(client, stackName, projectID, clusterID)
  149. if releaseEnvVars != nil {
  150. color.New(color.FgYellow).Printf("Reading build env from release\n")
  151. app.Env = mergeStringMaps(app.Env, releaseEnvVars)
  152. }
  153. return &StackConf{
  154. apiClient: client,
  155. parsed: app,
  156. stackName: stackName,
  157. projectID: projectID,
  158. clusterID: clusterID,
  159. namespace: namespace,
  160. }, nil
  161. }
  162. func createV1BuildResourcesFromPorterYaml(stackConf *StackConf) (*switchboardTypes.Resource, *switchboardTypes.Resource, string, error) {
  163. bi, err := stackConf.parsed.Build.getV1BuildImage(stackConf.stackName, stackConf.parsed.Env, stackConf.namespace)
  164. if err != nil {
  165. return nil, nil, "", err
  166. }
  167. pi, err := stackConf.parsed.Build.getV1PushImage(stackConf.stackName, stackConf.namespace)
  168. if err != nil {
  169. return nil, nil, "", err
  170. }
  171. return bi, pi, stackConf.parsed.Build.GetBuilder(), nil
  172. }
  173. func convertToBuild(porterApp *types.PorterApp) Build {
  174. var context *string
  175. if porterApp.BuildContext != "" {
  176. context = &porterApp.BuildContext
  177. }
  178. var method *string
  179. var m string
  180. if porterApp.RepoName == "" {
  181. m = "registry"
  182. method = &m
  183. } else if porterApp.Dockerfile == "" {
  184. m = "pack"
  185. method = &m
  186. } else {
  187. m = "docker"
  188. method = &m
  189. }
  190. var builder *string
  191. if porterApp.Builder != "" {
  192. builder = &porterApp.Builder
  193. }
  194. var buildpacks []*string
  195. if porterApp.Buildpacks != "" {
  196. bpSlice := strings.Split(porterApp.Buildpacks, ",")
  197. buildpacks = make([]*string, len(bpSlice))
  198. for i, bp := range bpSlice {
  199. temp := bp
  200. buildpacks[i] = &temp
  201. }
  202. }
  203. var dockerfile *string
  204. if porterApp.Dockerfile != "" {
  205. dockerfile = &porterApp.Dockerfile
  206. }
  207. var image *string
  208. if porterApp.ImageRepoURI != "" {
  209. image = &porterApp.ImageRepoURI
  210. }
  211. return Build{
  212. Context: context,
  213. Method: method,
  214. Builder: builder,
  215. Buildpacks: buildpacks,
  216. Dockerfile: dockerfile,
  217. Image: image,
  218. }
  219. }
  220. func getEnvFromRelease(client *api.Client, stackName string, projectID uint, clusterID uint) map[string]string {
  221. var envVarsStringMap map[string]string
  222. namespace := fmt.Sprintf("porter-stack-%s", stackName)
  223. release, err := client.GetRelease(
  224. context.Background(),
  225. projectID,
  226. clusterID,
  227. namespace,
  228. stackName,
  229. )
  230. if err == nil && release != nil {
  231. for key, val := range release.Config {
  232. if key != "global" && isMapStringInterface(val) {
  233. appConfig := val.(map[string]interface{})
  234. if appConfig != nil {
  235. if container, ok := appConfig["container"]; ok {
  236. if containerMap, ok := container.(map[string]interface{}); ok {
  237. if env, ok := containerMap["env"]; ok {
  238. if envMap, ok := env.(map[string]interface{}); ok {
  239. if normal, ok := envMap["normal"]; ok {
  240. if normalMap, ok := normal.(map[string]interface{}); ok {
  241. convertedMap, err := toStringMap(normalMap)
  242. if err == nil && len(convertedMap) > 0 {
  243. envVarsStringMap = convertedMap
  244. break
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. return envVarsStringMap
  257. }
  258. func isMapStringInterface(val interface{}) bool {
  259. _, ok := val.(map[string]interface{})
  260. return ok
  261. }
  262. func toStringMap(m map[string]interface{}) (map[string]string, error) {
  263. result := make(map[string]string)
  264. for k, v := range m {
  265. strVal, ok := v.(string)
  266. if !ok {
  267. return nil, fmt.Errorf("value for key %q is not a string", k)
  268. }
  269. result[k] = strVal
  270. }
  271. return result, nil
  272. }
  273. func mergeStringMaps(base, override map[string]string) map[string]string {
  274. result := make(map[string]string)
  275. if base == nil && override == nil {
  276. return result
  277. }
  278. for k, v := range base {
  279. result[k] = v
  280. }
  281. for k, v := range override {
  282. result[k] = v
  283. }
  284. return result
  285. }