apply.go 9.0 KB

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