2021-10-19 15:06:48 +00:00
|
|
|
// Copyright © 2020-2021 Ettore Di Giacinto <mudler@mocaccino.org>
|
2021-01-19 16:23:14 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-03-11 16:04:26 +00:00
|
|
|
"encoding/json"
|
2021-01-19 16:23:14 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-03-11 16:04:26 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2021-01-19 16:23:14 +00:00
|
|
|
"github.com/docker/go-units"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
luetTypes "github.com/mudler/luet/pkg/api/core/types"
|
2021-10-19 15:06:48 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/types/artifact"
|
2021-10-20 22:13:02 +00:00
|
|
|
|
2021-06-01 14:43:31 +00:00
|
|
|
"github.com/mudler/luet/pkg/helpers/docker"
|
|
|
|
fileHelper "github.com/mudler/luet/pkg/helpers/file"
|
2021-01-19 16:23:14 +00:00
|
|
|
)
|
|
|
|
|
2021-03-16 10:34:36 +00:00
|
|
|
const (
|
|
|
|
errImageDownloadMsg = "failed downloading image %s: %s"
|
|
|
|
)
|
|
|
|
|
2021-01-19 16:23:14 +00:00
|
|
|
type DockerClient struct {
|
|
|
|
RepoData RepoData
|
2021-03-11 16:04:26 +00:00
|
|
|
auth *types.AuthConfig
|
2021-10-19 15:06:48 +00:00
|
|
|
Cache *artifact.ArtifactCache
|
2021-10-20 22:13:02 +00:00
|
|
|
context *luetTypes.Context
|
2021-01-19 16:23:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
func NewDockerClient(r RepoData, ctx *luetTypes.Context) *DockerClient {
|
2021-03-11 16:04:26 +00:00
|
|
|
auth := &types.AuthConfig{}
|
|
|
|
|
|
|
|
dat, _ := json.Marshal(r.Authentication)
|
|
|
|
json.Unmarshal(dat, auth)
|
|
|
|
|
2021-10-19 15:06:48 +00:00
|
|
|
return &DockerClient{RepoData: r, auth: auth,
|
2021-10-20 22:13:02 +00:00
|
|
|
Cache: artifact.NewCache(ctx.Config.GetSystem().GetSystemPkgsCacheDirPath()),
|
|
|
|
context: ctx,
|
2021-10-19 15:06:48 +00:00
|
|
|
}
|
2021-01-19 16:23:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
func (c *DockerClient) DownloadArtifact(a *artifact.PackageArtifact) (*artifact.PackageArtifact, error) {
|
2021-01-19 16:23:14 +00:00
|
|
|
//var u *url.URL = nil
|
|
|
|
var err error
|
2021-02-22 10:54:09 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Spinner()
|
|
|
|
defer c.context.SpinnerStop()
|
2021-02-22 10:54:09 +00:00
|
|
|
|
2021-10-19 15:06:48 +00:00
|
|
|
resultingArtifact := a.ShallowCopy()
|
2021-04-12 17:00:36 +00:00
|
|
|
artifactName := path.Base(a.Path)
|
2021-10-19 15:06:48 +00:00
|
|
|
|
|
|
|
downloaded := false
|
2021-01-19 16:23:14 +00:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// Files are in URI/packagename:version (GetPackageImageName() method)
|
|
|
|
// use downloadAndExtract .. and egenrate an archive to consume. Checksum should be already checked while downloading the image
|
|
|
|
// with the above functions, because Docker images already contain such metadata
|
|
|
|
// - Check how verification is done when calling DownloadArtifact outside, similarly we need to check DownloadFile, and how verification
|
|
|
|
// is done in such cases (see repository.go)
|
|
|
|
|
|
|
|
// Check if file is already in cache
|
2021-10-19 15:06:48 +00:00
|
|
|
fileName, err := c.Cache.Get(a)
|
|
|
|
// Check if file is already in cache
|
|
|
|
if err == nil {
|
2021-04-12 17:00:36 +00:00
|
|
|
resultingArtifact = a
|
2021-10-19 15:06:48 +00:00
|
|
|
resultingArtifact.Path = fileName
|
2021-04-12 17:00:36 +00:00
|
|
|
resultingArtifact.Checksums = artifact.Checksums{}
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Debug("Use artifact", artifactName, "from cache.")
|
2021-01-19 16:23:14 +00:00
|
|
|
} else {
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
temp, err := c.context.Config.GetSystem().TempDir("image")
|
2021-01-19 16:23:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(temp)
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
tempArtifact, err := c.context.Config.GetSystem().TempFile("artifact")
|
2021-10-19 15:06:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempArtifact.Name())
|
2021-01-19 16:23:14 +00:00
|
|
|
for _, uri := range c.RepoData.Urls {
|
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
imageName := fmt.Sprintf("%s:%s", uri, a.CompileSpec.GetPackage().ImageID())
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Info("Downloading image", imageName)
|
2021-01-19 16:23:14 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
contentstore, err := c.context.Config.GetSystem().TempDir("contentstore")
|
2021-03-09 08:22:37 +00:00
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Warning("Cannot create contentstore", err.Error())
|
2021-03-09 08:22:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:23:14 +00:00
|
|
|
// imageName := fmt.Sprintf("%s/%s", uri, artifact.GetCompileSpec().GetPackage().GetPackageImageName())
|
2021-06-01 14:43:31 +00:00
|
|
|
info, err := docker.DownloadAndExtractDockerImage(contentstore, imageName, temp, c.auth, c.RepoData.Verify)
|
2021-01-19 16:23:14 +00:00
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Warning(fmt.Sprintf(errImageDownloadMsg, imageName, err.Error()))
|
2021-01-19 16:23:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-09 08:22:37 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Info(fmt.Sprintf("Pulled: %s", info.Target.Digest))
|
|
|
|
c.context.Info(fmt.Sprintf("Size: %s", units.BytesSize(float64(info.Target.Size))))
|
|
|
|
c.context.Debug("\nCompressing result ", filepath.Join(temp), "to", tempArtifact.Name())
|
2021-01-19 16:23:14 +00:00
|
|
|
|
2021-01-20 11:31:36 +00:00
|
|
|
// We discard checksum, that are checked while during pull and unpack
|
2021-10-19 15:06:48 +00:00
|
|
|
resultingArtifact.Checksums = artifact.Checksums{}
|
|
|
|
resultingArtifact.Path = tempArtifact.Name() // First set to cache file
|
|
|
|
err = resultingArtifact.Compress(temp, 1)
|
2021-01-19 16:23:14 +00:00
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Error(fmt.Sprintf("Failed compressing package %s: %s", imageName, err.Error()))
|
2021-01-19 16:23:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-19 15:06:48 +00:00
|
|
|
_, _, err = c.Cache.Put(resultingArtifact)
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Error(fmt.Sprintf("Failed storing package %s from cache: %s", imageName, err.Error()))
|
2021-10-19 15:06:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName, err := c.Cache.Get(resultingArtifact)
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Error(fmt.Sprintf("Failed getting package %s from cache: %s", imageName, err.Error()))
|
2021-10-19 15:06:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resultingArtifact.Path = fileName // Cache is persistent. tempArtifact is not
|
|
|
|
|
|
|
|
downloaded = true
|
2021-01-19 16:23:14 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-10-19 15:06:48 +00:00
|
|
|
if !downloaded {
|
|
|
|
return nil, errors.Wrap(err, "no image available from repositories")
|
2021-01-19 16:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultingArtifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DockerClient) DownloadFile(name string) (string, error) {
|
|
|
|
var file *os.File = nil
|
|
|
|
var err error
|
2021-04-21 15:59:56 +00:00
|
|
|
var temp, contentstore string
|
2021-01-19 16:23:14 +00:00
|
|
|
// Files should be in URI/repository:<file>
|
|
|
|
ok := false
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
temp, err = c.context.Config.GetSystem().TempDir("tree")
|
2021-01-19 16:23:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uri := range c.RepoData.Urls {
|
2021-10-20 22:13:02 +00:00
|
|
|
file, err = c.context.Config.GetSystem().TempFile("DockerClient")
|
2021-01-19 16:23:14 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
contentstore, err = c.context.Config.GetSystem().TempDir("contentstore")
|
2021-03-09 08:22:37 +00:00
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Warning("Cannot create contentstore", err.Error())
|
2021-03-09 08:22:37 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-01-19 16:23:14 +00:00
|
|
|
|
2021-06-01 14:43:31 +00:00
|
|
|
imageName := fmt.Sprintf("%s:%s", uri, docker.StripInvalidStringsFromImage(name))
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Info("Downloading", imageName)
|
2021-03-09 08:22:37 +00:00
|
|
|
|
2021-06-16 21:29:23 +00:00
|
|
|
info, err := docker.DownloadAndExtractDockerImage(contentstore, imageName, temp, c.auth, c.RepoData.Verify)
|
2021-01-19 16:23:14 +00:00
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Warning(fmt.Sprintf(errImageDownloadMsg, imageName, err.Error()))
|
2021-01-19 16:23:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Info(fmt.Sprintf("Pulled: %s", info.Target.Digest))
|
|
|
|
c.context.Info(fmt.Sprintf("Size: %s", units.BytesSize(float64(info.Target.Size))))
|
2021-03-09 08:22:37 +00:00
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
c.context.Debug("\nCopying file ", filepath.Join(temp, name), "to", file.Name())
|
2021-06-01 14:43:31 +00:00
|
|
|
err = fileHelper.CopyFile(filepath.Join(temp, name), file.Name())
|
2021-01-19 16:23:14 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.Name(), err
|
|
|
|
}
|