pack.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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(0600)
  37. file, err := os.OpenFile(filepath.Join(absPath, "Procfile"), os.O_RDONLY|os.O_CREATE, mode)
  38. if err != nil {
  39. return err
  40. }
  41. if err := file.Close(); err != nil {
  42. return err
  43. }
  44. buildOpts := packclient.BuildOptions{
  45. RelativeBaseDir: filepath.Dir(absPath),
  46. Image: fmt.Sprintf("%s:%s", opts.ImageRepo, opts.Tag),
  47. Builder: "paketobuildpacks/builder:full",
  48. AppPath: opts.BuildContext,
  49. Env: opts.Env,
  50. GroupID: 0,
  51. }
  52. if opts.UseCache {
  53. buildOpts.CacheImage = cacheImage
  54. buildOpts.Publish = true
  55. }
  56. if buildConfig != nil {
  57. buildOpts.Builder = buildConfig.Builder
  58. for i := range buildConfig.Buildpacks {
  59. bp := buildConfig.Buildpacks[i]
  60. if bp == "" {
  61. continue
  62. }
  63. u, err := url.Parse(bp)
  64. if err == nil && u.Scheme != "" {
  65. // could be a git repository containing the buildpack
  66. if !strings.HasSuffix(u.Path, ".zip") && u.Host != "github.com" && u.Host != "www.github.com" {
  67. return fmt.Errorf("please provide either a github.com URL or a ZIP file URL")
  68. }
  69. urlPaths := strings.Split(u.Path[1:], "/")
  70. dstDir := filepath.Join(homedir.HomeDir(), ".porter")
  71. bpCustomName := regexp.MustCompile("/|-").ReplaceAllString(u.Path[1:], "_")
  72. var zipFileName string
  73. if strings.HasSuffix(bpCustomName, ".zip") {
  74. zipFileName = bpCustomName
  75. } else {
  76. zipFileName = fmt.Sprintf("%s.zip", bpCustomName)
  77. }
  78. downloader := &github.ZIPDownloader{
  79. ZipFolderDest: dstDir,
  80. AssetFolderDest: dstDir,
  81. ZipName: zipFileName,
  82. RemoveAfterDownload: true,
  83. }
  84. if zipFileName != bpCustomName {
  85. // try to download the repo ZIP from github
  86. githubClient := githubApi.NewClient(nil)
  87. rel, _, err := githubClient.Repositories.GetLatestRelease(
  88. ctx,
  89. urlPaths[0],
  90. urlPaths[1],
  91. )
  92. if err == nil {
  93. bp = rel.GetZipballURL()
  94. } else {
  95. // default to the current default branch
  96. repo, _, err := githubClient.Repositories.Get(
  97. ctx,
  98. urlPaths[0],
  99. urlPaths[1],
  100. )
  101. if err != nil {
  102. return fmt.Errorf("could not fetch git repo details")
  103. }
  104. bp = fmt.Sprintf("%s/archive/refs/heads/%s.zip", bp, repo.GetDefaultBranch())
  105. }
  106. }
  107. err = downloader.DownloadToFile(bp)
  108. if err != nil {
  109. return err
  110. }
  111. err = downloader.UnzipToDir()
  112. if err != nil {
  113. return err
  114. }
  115. dstFiles, err := ioutil.ReadDir(dstDir)
  116. if err != nil {
  117. return err
  118. }
  119. var bpRealName string
  120. for _, info := range dstFiles {
  121. if info.Mode().IsDir() && strings.Contains(info.Name(), urlPaths[1]) {
  122. bpRealName = filepath.Join(dstDir, info.Name())
  123. }
  124. }
  125. buildOpts.Buildpacks = append(buildOpts.Buildpacks, bpRealName)
  126. } else {
  127. buildOpts.Buildpacks = append(buildOpts.Buildpacks, bp)
  128. }
  129. }
  130. // FIXME: use all the config vars
  131. }
  132. if len(buildOpts.Buildpacks) > 0 && strings.HasPrefix(buildOpts.Builder, "heroku") {
  133. buildOpts.Buildpacks = append(buildOpts.Buildpacks, "heroku/procfile@1.0.1")
  134. }
  135. return sharedPackClient.Build(ctx, buildOpts)
  136. }