pack.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package pack
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net/url"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. packclient "github.com/buildpacks/pack/pkg/client"
  11. githubApi "github.com/google/go-github/v41/github"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/cli/cmd/docker"
  14. "github.com/porter-dev/porter/cli/cmd/github"
  15. "k8s.io/client-go/util/homedir"
  16. "github.com/docker/docker/client"
  17. )
  18. type Agent struct{}
  19. func (a *Agent) Build(opts *docker.BuildOpts, buildConfig *types.BuildConfig, dc client.CommonAPIClient, cacheImage string) error {
  20. //initialize a pack client
  21. logger := newPackLogger()
  22. client, err := packclient.NewClient(packclient.WithLogger(logger), packclient.WithDockerClient(dc))
  23. if err != nil {
  24. return err
  25. }
  26. absPath, err := filepath.Abs(opts.BuildContext)
  27. if err != nil {
  28. return err
  29. }
  30. buildOpts := packclient.BuildOptions{
  31. RelativeBaseDir: filepath.Dir(absPath),
  32. Image: fmt.Sprintf("%s:%s", opts.ImageRepo, opts.Tag),
  33. Builder: "paketobuildpacks/builder:full",
  34. AppPath: opts.BuildContext,
  35. Env: opts.Env,
  36. CacheImage: cacheImage,
  37. Publish: true,
  38. }
  39. if buildConfig != nil {
  40. buildOpts.Builder = buildConfig.Builder
  41. for i := range buildConfig.Buildpacks {
  42. bp := buildConfig.Buildpacks[i]
  43. if bp == "" {
  44. continue
  45. }
  46. u, err := url.Parse(bp)
  47. if err == nil && u.Scheme != "" {
  48. // could be a git repository containing the buildpack
  49. if !strings.HasSuffix(u.Path, ".zip") && u.Host != "github.com" && u.Host != "www.github.com" {
  50. return fmt.Errorf("please provide either a github.com URL or a ZIP file URL")
  51. }
  52. urlPaths := strings.Split(u.Path[1:], "/")
  53. dstDir := filepath.Join(homedir.HomeDir(), ".porter")
  54. bpCustomName := regexp.MustCompile("/|-").ReplaceAllString(u.Path[1:], "_")
  55. var zipFileName string
  56. if strings.HasSuffix(bpCustomName, ".zip") {
  57. zipFileName = bpCustomName
  58. } else {
  59. zipFileName = fmt.Sprintf("%s.zip", bpCustomName)
  60. }
  61. downloader := &github.ZIPDownloader{
  62. ZipFolderDest: dstDir,
  63. AssetFolderDest: dstDir,
  64. ZipName: zipFileName,
  65. RemoveAfterDownload: true,
  66. }
  67. if zipFileName != bpCustomName {
  68. // try to download the repo ZIP from github
  69. githubClient := githubApi.NewClient(nil)
  70. rel, _, err := githubClient.Repositories.GetLatestRelease(
  71. context.Background(),
  72. urlPaths[0],
  73. urlPaths[1],
  74. )
  75. if err == nil {
  76. bp = rel.GetZipballURL()
  77. } else {
  78. // default to the current default branch
  79. repo, _, err := githubClient.Repositories.Get(
  80. context.Background(),
  81. urlPaths[0],
  82. urlPaths[1],
  83. )
  84. if err != nil {
  85. return fmt.Errorf("could not fetch git repo details")
  86. }
  87. bp = fmt.Sprintf("%s/archive/refs/heads/%s.zip", bp, repo.GetDefaultBranch())
  88. }
  89. }
  90. err = downloader.DownloadToFile(bp)
  91. if err != nil {
  92. return err
  93. }
  94. err = downloader.UnzipToDir()
  95. if err != nil {
  96. return err
  97. }
  98. dstFiles, err := ioutil.ReadDir(dstDir)
  99. if err != nil {
  100. return err
  101. }
  102. var bpRealName string
  103. for _, info := range dstFiles {
  104. if info.Mode().IsDir() && strings.Contains(info.Name(), urlPaths[1]) {
  105. bpRealName = filepath.Join(dstDir, info.Name())
  106. }
  107. }
  108. buildOpts.Buildpacks = append(buildOpts.Buildpacks, bpRealName)
  109. } else {
  110. buildOpts.Buildpacks = append(buildOpts.Buildpacks, bp)
  111. }
  112. }
  113. // FIXME: use all the config vars
  114. }
  115. if len(buildOpts.Buildpacks) > 0 && strings.HasPrefix(buildOpts.Builder, "heroku") {
  116. buildOpts.Buildpacks = append(buildOpts.Buildpacks, "heroku/procfile")
  117. }
  118. return client.Build(context.Background(), buildOpts)
  119. }