apply.go 9.2 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. 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. var req *types.CreateOrUpdatePorterAppEventRequest
  48. if os.Getenv("GITHUB_RUN_ID") != "" {
  49. req = &types.CreateOrUpdatePorterAppEventRequest{
  50. Status: "PROGRESSING",
  51. Type: types.PorterAppEventType_Build,
  52. TypeExternalSource: "GITHUB",
  53. Metadata: map[string]any{
  54. "action_run_id": os.Getenv("GITHUB_RUN_ID"),
  55. "org": os.Getenv("GITHUB_REPOSITORY_OWNER"),
  56. },
  57. }
  58. repoNameSplit := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")
  59. if len(repoNameSplit) != 2 {
  60. return "", fmt.Errorf("unable to parse GITHUB_REPOSITORY")
  61. }
  62. req.Metadata["repo"] = repoNameSplit[1]
  63. actionRunID := os.Getenv("GITHUB_RUN_ID")
  64. if actionRunID != "" {
  65. arid, err := strconv.Atoi(actionRunID)
  66. if err != nil {
  67. return "", fmt.Errorf("unable to parse GITHUB_RUN_ID as int: %w", err)
  68. }
  69. req.Metadata["action_run_id"] = arid
  70. }
  71. repoOwnerAccountID := os.Getenv("GITHUB_REPOSITORY_OWNER_ID")
  72. if repoOwnerAccountID != "" {
  73. arid, err := strconv.Atoi(repoOwnerAccountID)
  74. if err != nil {
  75. return "", fmt.Errorf("unable to parse GITHUB_REPOSITORY_OWNER_ID as int: %w", err)
  76. }
  77. req.Metadata["github_account_id"] = arid
  78. }
  79. } else {
  80. req = &types.CreateOrUpdatePorterAppEventRequest{
  81. Status: "PROGRESSING",
  82. Type: types.PorterAppEventType_Build,
  83. TypeExternalSource: "GITHUB",
  84. Metadata: map[string]any{},
  85. }
  86. }
  87. ctx := context.Background()
  88. event, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
  89. if err != nil {
  90. return "", fmt.Errorf("unable to create porter app build event: %w", err)
  91. }
  92. return event.ID, nil
  93. }
  94. func createV1BuildResources(client *api.Client, app *Application, stackName string, projectID uint, clusterID uint) ([]*switchboardTypes.Resource, string, error) {
  95. var builder string
  96. resources := make([]*switchboardTypes.Resource, 0)
  97. stackConf, err := createStackConf(client, app, stackName, projectID, clusterID)
  98. if err != nil {
  99. return nil, "", err
  100. }
  101. var bi, pi *switchboardTypes.Resource
  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. }
  139. return resources, builder, nil
  140. }
  141. func createStackConf(client *api.Client, app *Application, stackName string, projectID uint, clusterID uint) (*StackConf, error) {
  142. err := config.ValidateCLIEnvironment()
  143. if err != nil {
  144. errMsg := composePreviewMessage("porter CLI is not configured correctly", Error)
  145. return nil, fmt.Errorf("%s: %w", errMsg, err)
  146. }
  147. releaseEnvVars := getEnvFromRelease(client, stackName, projectID, clusterID)
  148. if releaseEnvVars != nil {
  149. color.New(color.FgYellow).Printf("Reading build env from release\n")
  150. app.Env = mergeStringMaps(app.Env, releaseEnvVars)
  151. }
  152. return &StackConf{
  153. apiClient: client,
  154. parsed: app,
  155. stackName: stackName,
  156. projectID: projectID,
  157. clusterID: clusterID,
  158. namespace: fmt.Sprintf("porter-stack-%s", stackName),
  159. }, nil
  160. }
  161. func createV1BuildResourcesFromPorterYaml(stackConf *StackConf) (*switchboardTypes.Resource, *switchboardTypes.Resource, string, error) {
  162. bi, err := stackConf.parsed.Build.getV1BuildImage(stackConf.stackName, stackConf.parsed.Env, stackConf.namespace)
  163. if err != nil {
  164. return nil, nil, "", err
  165. }
  166. pi, err := stackConf.parsed.Build.getV1PushImage(stackConf.stackName, stackConf.namespace)
  167. if err != nil {
  168. return nil, nil, "", err
  169. }
  170. return bi, pi, stackConf.parsed.Build.GetBuilder(), nil
  171. }
  172. func convertToBuild(porterApp *types.PorterApp) Build {
  173. var context *string
  174. if porterApp.BuildContext != "" {
  175. context = &porterApp.BuildContext
  176. }
  177. var method *string
  178. var m string
  179. if porterApp.RepoName == "" {
  180. m = "registry"
  181. method = &m
  182. } else if porterApp.Dockerfile == "" {
  183. m = "pack"
  184. method = &m
  185. } else {
  186. m = "docker"
  187. method = &m
  188. }
  189. var builder *string
  190. if porterApp.Builder != "" {
  191. builder = &porterApp.Builder
  192. }
  193. var buildpacks []*string
  194. if porterApp.Buildpacks != "" {
  195. bpSlice := strings.Split(porterApp.Buildpacks, ",")
  196. buildpacks = make([]*string, len(bpSlice))
  197. for i, bp := range bpSlice {
  198. temp := bp
  199. buildpacks[i] = &temp
  200. }
  201. }
  202. var dockerfile *string
  203. if porterApp.Dockerfile != "" {
  204. dockerfile = &porterApp.Dockerfile
  205. }
  206. var image *string
  207. if porterApp.ImageRepoURI != "" {
  208. image = &porterApp.ImageRepoURI
  209. }
  210. return Build{
  211. Context: context,
  212. Method: method,
  213. Builder: builder,
  214. Buildpacks: buildpacks,
  215. Dockerfile: dockerfile,
  216. Image: image,
  217. }
  218. }
  219. func getEnvFromRelease(client *api.Client, stackName string, projectID uint, clusterID uint) map[string]string {
  220. var envVarsStringMap map[string]string
  221. namespace := fmt.Sprintf("porter-stack-%s", stackName)
  222. release, err := client.GetRelease(
  223. context.Background(),
  224. projectID,
  225. clusterID,
  226. namespace,
  227. stackName,
  228. )
  229. if err == nil && release != nil {
  230. for key, val := range release.Config {
  231. if key != "global" && isMapStringInterface(val) {
  232. appConfig := val.(map[string]interface{})
  233. if appConfig != nil {
  234. if container, ok := appConfig["container"]; ok {
  235. if containerMap, ok := container.(map[string]interface{}); ok {
  236. if env, ok := containerMap["env"]; ok {
  237. if envMap, ok := env.(map[string]interface{}); ok {
  238. if normal, ok := envMap["normal"]; ok {
  239. if normalMap, ok := normal.(map[string]interface{}); ok {
  240. convertedMap, err := toStringMap(normalMap)
  241. if err == nil && len(convertedMap) > 0 {
  242. envVarsStringMap = convertedMap
  243. break
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. return envVarsStringMap
  256. }
  257. func isMapStringInterface(val interface{}) bool {
  258. _, ok := val.(map[string]interface{})
  259. return ok
  260. }
  261. func toStringMap(m map[string]interface{}) (map[string]string, error) {
  262. result := make(map[string]string)
  263. for k, v := range m {
  264. strVal, ok := v.(string)
  265. if !ok {
  266. return nil, fmt.Errorf("value for key %q is not a string", k)
  267. }
  268. result[k] = strVal
  269. }
  270. return result, nil
  271. }
  272. func mergeStringMaps(base, override map[string]string) map[string]string {
  273. result := make(map[string]string)
  274. if base == nil && override == nil {
  275. return result
  276. }
  277. for k, v := range base {
  278. result[k] = v
  279. }
  280. for k, v := range override {
  281. result[k] = v
  282. }
  283. return result
  284. }