podman

Форк
0
104 строки · 3.2 Кб
1
package util
2

3
import (
4
	"fmt"
5
	"io"
6
	"os"
7
	"path/filepath"
8

9
	"github.com/containers/buildah/define"
10
	"github.com/containers/common/libimage"
11
	lplatform "github.com/containers/common/libimage/platform"
12
	"github.com/containers/image/v5/types"
13
	"github.com/containers/storage"
14
	"github.com/containers/storage/pkg/archive"
15
	"github.com/containers/storage/pkg/chrootarchive"
16
	"github.com/containers/storage/pkg/unshare"
17
	v1 "github.com/opencontainers/image-spec/specs-go/v1"
18
)
19

20
// LookupImage returns *Image to corresponding imagename or id
21
func LookupImage(ctx *types.SystemContext, store storage.Store, image string) (*libimage.Image, error) {
22
	systemContext := ctx
23
	if systemContext == nil {
24
		systemContext = &types.SystemContext{}
25
	}
26
	runtime, err := libimage.RuntimeFromStore(store, &libimage.RuntimeOptions{SystemContext: systemContext})
27
	if err != nil {
28
		return nil, err
29
	}
30
	localImage, _, err := runtime.LookupImage(image, nil)
31
	if err != nil {
32
		return nil, err
33
	}
34
	return localImage, nil
35
}
36

37
// NormalizePlatform validates and translate the platform to the canonical value.
38
//
39
// For example, if "Aarch64" is encountered, we change it to "arm64" or if
40
// "x86_64" is encountered, it becomes "amd64".
41
//
42
// Wrapper around libimage.NormalizePlatform to return and consume
43
// v1.Platform instead of independent os, arch and variant.
44
func NormalizePlatform(platform v1.Platform) v1.Platform {
45
	os, arch, variant := lplatform.Normalize(platform.OS, platform.Architecture, platform.Variant)
46
	return v1.Platform{
47
		OS:           os,
48
		Architecture: arch,
49
		Variant:      variant,
50
	}
51
}
52

53
// ExportFromReader reads bytes from given reader and exports to external tar, directory or stdout.
54
func ExportFromReader(input io.Reader, opts define.BuildOutputOption) error {
55
	var err error
56
	if !filepath.IsAbs(opts.Path) {
57
		opts.Path, err = filepath.Abs(opts.Path)
58
		if err != nil {
59
			return err
60
		}
61
	}
62
	if opts.IsDir {
63
		// In order to keep this feature as close as possible to
64
		// buildkit it was decided to preserve ownership when
65
		// invoked as root since caller already has access to artifacts
66
		// therefore we can preserve ownership as is, however for rootless users
67
		// ownership has to be changed so exported artifacts can still
68
		// be accessible by unprivileged users.
69
		// See: https://github.com/containers/buildah/pull/3823#discussion_r829376633
70
		noLChown := false
71
		if unshare.IsRootless() {
72
			noLChown = true
73
		}
74

75
		err = os.MkdirAll(opts.Path, 0700)
76
		if err != nil {
77
			return fmt.Errorf("failed while creating the destination path %q: %w", opts.Path, err)
78
		}
79

80
		err = chrootarchive.Untar(input, opts.Path, &archive.TarOptions{NoLchown: noLChown})
81
		if err != nil {
82
			return fmt.Errorf("failed while performing untar at %q: %w", opts.Path, err)
83
		}
84
	} else {
85
		outFile := os.Stdout
86
		if !opts.IsStdout {
87
			outFile, err = os.Create(opts.Path)
88
			if err != nil {
89
				return fmt.Errorf("failed while creating destination tar at %q: %w", opts.Path, err)
90
			}
91
			defer outFile.Close()
92
		}
93
		_, err = io.Copy(outFile, input)
94
		if err != nil {
95
			return fmt.Errorf("failed while performing copy to %q: %w", opts.Path, err)
96
		}
97
	}
98
	return nil
99
}
100

101
func SetHas(m map[string]struct{}, k string) bool {
102
	_, ok := m[k]
103
	return ok
104
}
105

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.