pack.go 3.7 KB

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