apply.go 8.9 KB

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