pack.go 3.4 KB

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