-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (113 loc) · 3.48 KB
/
main.go
File metadata and controls
129 lines (113 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//go:build !windows
package main
import (
"context"
"flag"
"fmt"
"os"
imgcopy "go.podman.io/image/v5/copy"
"go.podman.io/image/v5/signature"
istorage "go.podman.io/image/v5/storage"
"go.podman.io/image/v5/transports/alltransports"
"go.podman.io/image/v5/types"
"go.podman.io/storage"
"go.podman.io/storage/pkg/reexec"
storagetypes "go.podman.io/storage/types"
jsonproxy "go.podman.io/common/pkg/json-proxy"
)
func main() {
if reexec.Init() {
return
}
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
sockfd := flag.Int("sockfd", -1, "socket file descriptor")
policyPath := flag.String("policy", "", "path to policy.json (default: system default)")
overrideArch := flag.String("override-arch", "", "override architecture for manifest list resolution")
graphRoot := flag.String("graph-root", "", "storage graph root")
runRoot := flag.String("run-root", "", "storage run root")
seedImage := flag.String("seed-image", "", "image to copy into local store")
flag.Parse()
if *sockfd < 0 {
return fmt.Errorf("usage: %s --sockfd <fd> [--policy <path>] [--override-arch <arch>] [--graph-root <path> --run-root <path> --seed-image <ref>]", os.Args[0])
}
if *graphRoot != "" {
ref, store, err := setupStore(*graphRoot, *runRoot, *seedImage)
if err != nil {
return fmt.Errorf("setting up store: %w", err)
}
defer func() {
_, _ = store.Shutdown(true)
}()
// Print the containers-storage:// reference for the test to read.
fmt.Fprintln(os.Stdout, ref)
}
manager, err := jsonproxy.NewManager(
jsonproxy.WithSystemContext(func() (*types.SystemContext, error) {
sc := &types.SystemContext{}
if *overrideArch != "" {
sc.ArchitectureChoice = *overrideArch
}
return sc, nil
}),
jsonproxy.WithPolicyContext(func() (*signature.PolicyContext, error) {
var policy *signature.Policy
var err error
if *policyPath != "" {
policy, err = signature.NewPolicyFromFile(*policyPath)
} else {
policy, err = signature.DefaultPolicy(nil)
}
if err != nil {
return nil, err
}
return signature.NewPolicyContext(policy)
}),
)
if err != nil {
return err
}
defer manager.Close()
return manager.Serve(context.Background(), *sockfd)
}
func setupStore(graphRoot, runRoot, seedImage string) (string, storage.Store, error) {
store, err := storage.GetStore(storagetypes.StoreOptions{
GraphRoot: graphRoot,
RunRoot: runRoot,
GraphDriverName: "overlay",
})
if err != nil {
return "", nil, fmt.Errorf("creating store: %w", err)
}
ctx := context.Background()
srcRef, err := alltransports.ParseImageName(seedImage)
if err != nil {
return "", nil, fmt.Errorf("parsing seed image %q: %w", seedImage, err)
}
destRef, err := istorage.Transport.ParseStoreReference(store, "testimage:latest")
if err != nil {
return "", nil, fmt.Errorf("creating store reference: %w", err)
}
policy, err := signature.DefaultPolicy(nil)
if err != nil {
return "", nil, fmt.Errorf("getting default policy: %w", err)
}
pc, err := signature.NewPolicyContext(policy)
if err != nil {
return "", nil, fmt.Errorf("creating policy context: %w", err)
}
defer func() {
if err := pc.Destroy(); err != nil {
fmt.Fprintf(os.Stderr, "warning: destroying policy context: %v\n", err)
}
}()
_, err = imgcopy.Image(ctx, pc, destRef, srcRef, nil)
if err != nil {
return "", nil, fmt.Errorf("copying seed image: %w", err)
}
return "containers-storage:" + destRef.StringWithinTransport(), store, nil
}