pack.go 3.7 KB

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