pack.go 4.0 KB

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