2021-09-14 16:14:50 +02:00
|
|
|
// Package cnbutils provides utility functions to interact with Buildpacks
|
|
|
|
package cnbutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-10-01 10:05:15 +02:00
|
|
|
"os"
|
2021-09-14 16:14:50 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2022-03-30 13:58:16 +02:00
|
|
|
"github.com/pkg/errors"
|
2021-09-14 16:14:50 +02:00
|
|
|
)
|
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
const bpCacheDir = "/tmp/buildpacks_cache"
|
|
|
|
|
2021-09-14 16:14:50 +02:00
|
|
|
type BuildPackMetadata struct {
|
2021-10-01 13:30:06 +02:00
|
|
|
ID string `toml:"id,omitempty" json:"id,omitempty" yaml:"id,omitempty"`
|
|
|
|
Name string `toml:"name,omitempty" json:"name,omitempty" yaml:"name,omitempty"`
|
|
|
|
Version string `toml:"version,omitempty" json:"version,omitempty" yaml:"version,omitempty"`
|
|
|
|
Description string `toml:"description,omitempty" json:"description,omitempty" yaml:"description,omitempty"`
|
|
|
|
Homepage string `toml:"homepage,omitempty" json:"homepage,omitempty" yaml:"homepage,omitempty"`
|
|
|
|
Keywords []string `toml:"keywords,omitempty" json:"keywords,omitempty" yaml:"keywords,omitempty"`
|
|
|
|
Licenses []License `toml:"licenses,omitempty" json:"licenses,omitempty" yaml:"licenses,omitempty"`
|
2021-09-14 16:14:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type License struct {
|
2021-10-01 13:30:06 +02:00
|
|
|
Type string `toml:"type" json:"type"`
|
|
|
|
URI string `toml:"uri" json:"uri"`
|
2021-09-14 16:14:50 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 10:05:15 +02:00
|
|
|
func DownloadBuildpacks(path string, bpacks []string, dockerCreds string, utils BuildUtils) (Order, error) {
|
|
|
|
|
|
|
|
if dockerCreds != "" {
|
|
|
|
os.Setenv("DOCKER_CONFIG", filepath.Dir(dockerCreds))
|
|
|
|
}
|
|
|
|
|
2021-10-01 13:30:06 +02:00
|
|
|
var orderEntry OrderEntry
|
|
|
|
order := Order{
|
|
|
|
Utils: utils,
|
|
|
|
}
|
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
err := utils.MkdirAll(bpCacheDir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return Order{}, errors.Wrap(err, "failed to create temp directory for buildpack cache")
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:14:50 +02:00
|
|
|
for _, bpack := range bpacks {
|
|
|
|
var bpackMeta BuildPackMetadata
|
2022-03-30 13:58:16 +02:00
|
|
|
imageInfo, err := utils.GetRemoteImageInfo(bpack)
|
2021-09-14 16:14:50 +02:00
|
|
|
if err != nil {
|
2022-03-30 13:58:16 +02:00
|
|
|
return Order{}, errors.Wrap(err, "failed to get remote image info of buildpack")
|
2021-09-14 16:14:50 +02:00
|
|
|
}
|
2022-03-30 13:58:16 +02:00
|
|
|
hash, err := imageInfo.Digest()
|
|
|
|
if err != nil {
|
|
|
|
return Order{}, errors.Wrap(err, "failed to get image digest")
|
|
|
|
}
|
|
|
|
cacheDir := filepath.Join(bpCacheDir, hash.String())
|
2021-09-14 16:14:50 +02:00
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
cacheExists, err := utils.DirExists(cacheDir)
|
2021-09-14 16:14:50 +02:00
|
|
|
if err != nil {
|
2022-03-30 13:58:16 +02:00
|
|
|
return Order{}, errors.Wrapf(err, "failed to check if cache dir '%s' exists", cacheDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cacheExists {
|
|
|
|
log.Entry().Infof("Using cached buildpack '%s'", bpack)
|
|
|
|
} else {
|
|
|
|
err := utils.MkdirAll(cacheDir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return Order{}, errors.Wrap(err, "failed to create temp directory for buildpack cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Entry().Infof("Downloading buildpack '%s' to %s", bpack, cacheDir)
|
|
|
|
img, err := utils.DownloadImageContent(bpack, cacheDir)
|
|
|
|
if err != nil {
|
|
|
|
return Order{}, errors.Wrapf(err, "failed download buildpack image '%s'", bpack)
|
|
|
|
}
|
|
|
|
imageInfo = img
|
2021-09-14 16:14:50 +02:00
|
|
|
}
|
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
imgConf, err := imageInfo.ConfigFile()
|
2021-09-14 16:14:50 +02:00
|
|
|
if err != nil {
|
2022-03-30 13:58:16 +02:00
|
|
|
return Order{}, errors.Wrapf(err, "failed to read '%s' image config", bpack)
|
2021-09-14 16:14:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(imgConf.Config.Labels["io.buildpacks.buildpackage.metadata"]), &bpackMeta)
|
|
|
|
if err != nil {
|
2022-03-30 13:58:16 +02:00
|
|
|
return Order{}, errors.Wrapf(err, "failed unmarshal '%s' image label", bpack)
|
2021-09-14 16:14:50 +02:00
|
|
|
}
|
|
|
|
log.Entry().Debugf("Buildpack metadata: '%v'", bpackMeta)
|
2021-10-01 13:30:06 +02:00
|
|
|
orderEntry.Group = append(orderEntry.Group, bpackMeta)
|
2021-09-14 16:14:50 +02:00
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
err = CopyProject(filepath.Join(cacheDir, "cnb/buildpacks"), path, nil, nil, utils)
|
2021-09-14 16:14:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return Order{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 13:30:06 +02:00
|
|
|
order.Order = []OrderEntry{orderEntry}
|
2021-09-14 16:14:50 +02:00
|
|
|
|
|
|
|
return order, nil
|
|
|
|
}
|