forked from estesp/manifest-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
130 lines (116 loc) · 3.67 KB
/
push.go
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
130
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/codegangsta/cli"
"github.com/estesp/manifest-tool/docker"
"github.com/estesp/manifest-tool/types"
"github.com/go-yaml/yaml"
"github.com/sirupsen/logrus"
"github.com/docker/distribution/manifest/manifestlist"
)
var pushCmd = cli.Command{
Name: "push",
Usage: "push a manifest list entry to a registry with provided image details",
Subcommands: []cli.Command{
{
Name: "from-spec",
Usage: "push a manifest list to a registry via a YAML spec",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "ignore-missing",
Usage: "only warn on missing images defined in YAML spec",
},
},
Action: func(c *cli.Context) {
filePath := c.Args().First()
a := getAuthInfo(c)
ignoreMissing := c.Bool("ignore-missing")
var yamlInput types.YAMLInput
filename, err := filepath.Abs(filePath)
if err != nil {
logrus.Fatalf(fmt.Sprintf("Can't resolve path to %q: %v", filePath, err))
}
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
logrus.Fatalf(fmt.Sprintf("Can't read YAML file %q: %v", filePath, err))
}
err = yaml.Unmarshal(yamlFile, &yamlInput)
if err != nil {
logrus.Fatalf(fmt.Sprintf("Can't unmarshal YAML file %q: %v", filePath, err))
}
digest, l, err := docker.PutManifestList(a, yamlInput, ignoreMissing, c.GlobalBool("insecure"))
if err != nil {
logrus.Fatal(err)
}
fmt.Printf("Digest: %s %d\n", digest, l)
},
},
{
Name: "from-args",
Usage: "push a manifest list to a registry via CLI arguments",
Flags: []cli.Flag{
cli.StringFlag{
Name: "platforms",
Usage: "comma-separated list of the platforms that images should be pushed for",
},
cli.StringFlag{
Name: "template",
Usage: "the pattern the source images have. OS and ARCH in that pattern will be replaced with the actual values from the platforms list",
},
cli.StringFlag{
Name: "target",
Usage: "the name of the manifest list image that is going to be produced",
},
cli.BoolFlag{
Name: "ignore-missing",
Usage: "only warn on missing images defined in platform list",
},
},
Action: func(c *cli.Context) {
a := getAuthInfo(c)
platforms := c.String("platforms")
templ := c.String("template")
target := c.String("target")
ignoreMissing := c.Bool("ignore-missing")
srcImages := []types.ManifestEntry{}
if len(platforms) == 0 || len(templ) == 0 || len(target) == 0 {
logrus.Fatalf("You must specify all three arguments --platforms, --template and --target")
}
platformList := strings.Split(platforms, ",")
for _, platform := range platformList {
osArchArr := strings.Split(platform, "/")
if len(osArchArr) != 2 {
logrus.Fatal("The --platforms argument must be a string slice where one value is of the form 'os/arch'")
}
os, arch := osArchArr[0], osArchArr[1]
srcImages = append(srcImages, types.ManifestEntry{
Image: strings.Replace(strings.Replace(templ, "ARCH", arch, 1), "OS", os, 1),
Platform: manifestlist.PlatformSpec{
OS: os,
Architecture: arch,
},
})
}
yamlInput := types.YAMLInput{
Image: target,
Manifests: srcImages,
}
digest, l, err := docker.PutManifestList(a, yamlInput, ignoreMissing, c.GlobalBool("insecure"))
if err != nil {
logrus.Fatal(err)
}
fmt.Printf("Digest: %s %d\n", digest, l)
},
},
},
}
func getAuthInfo(c *cli.Context) *types.AuthInfo {
return &types.AuthInfo{
Username: c.GlobalString("username"),
Password: c.GlobalString("password"),
DockerCfg: c.GlobalString("docker-cfg"),
}
}