This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
/
registry.go
148 lines (134 loc) · 4.43 KB
/
registry.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package packager
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/docker/app/internal"
"github.com/docker/app/pkg/resto"
"github.com/docker/app/types"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type imageComponents struct {
Name string
Repository string
Tag string
}
func splitImageName(repotag string) (*imageComponents, error) {
named, err := reference.ParseNormalizedNamed(repotag)
if err != nil {
return nil, errors.Wrap(err, "failed to parse image name")
}
res := &imageComponents{
Repository: named.Name(),
}
res.Name = res.Repository[strings.LastIndex(res.Repository, "/")+1:]
if tagged, ok := named.(reference.Tagged); ok {
res.Tag = tagged.Tag()
}
return res, nil
}
// Pull loads an app from a registry and returns the extracted dir name
func Pull(repotag string, outputDir string) (string, error) {
imgRef, err := splitImageName(repotag)
if err != nil {
return "", errors.Wrapf(err, "origin %q is not a valid image name", repotag)
}
payload, err := resto.PullConfigMulti(context.Background(), repotag, resto.RegistryOptions{})
if err != nil {
return "", err
}
appDir := filepath.Join(outputDir, internal.DirNameFromAppName(imgRef.Name))
if err := os.Mkdir(appDir, 0755); err != nil {
return "", errors.Wrap(err, "failed to create output application directory")
}
if err := ExtractImagePayloadToDiskFiles(appDir, payload); err != nil {
return "", err
}
return appDir, nil
}
// ExtractImagePayloadToDiskFiles extracts all the files out of the image payload and onto disk
// creating all necessary folders in between.
func ExtractImagePayloadToDiskFiles(appDir string, payload map[string]string) error {
for localFilepath, filedata := range payload {
fileBytes := []byte(filedata)
// Deal with windows/linux slashes
convertedFilepath := filepath.FromSlash(localFilepath)
// Check we aren't doing ./../../../ etc in the path
fullFilepath := filepath.Join(appDir, convertedFilepath)
if _, err := filepath.Rel(appDir, fullFilepath); err != nil {
log.Warnf("dropping image entry %q with unexpected path outside of app dir", localFilepath)
continue
}
// Create the directories for any nested files
basepath := filepath.Dir(fullFilepath)
if err := os.MkdirAll(basepath, os.ModePerm); err != nil {
return errors.Wrapf(err, "failed to create directories for file: %s", fullFilepath)
}
if err := ioutil.WriteFile(fullFilepath, fileBytes, 0644); err != nil {
return errors.Wrapf(err, "failed to write output file: %s", fullFilepath)
}
}
return nil
}
// Push pushes an app to a registry. Returns the image digest.
func Push(app *types.App, namespace, tag, repo string) (string, error) {
payload, err := createPayload(app)
if err != nil {
return "", errors.Wrap(err, "failed to read external file while creating payload for push")
}
imageName := createImageName(app, namespace, tag, repo)
return resto.PushConfigMulti(context.Background(), payload, imageName, resto.RegistryOptions{}, nil)
}
func createImageName(app *types.App, namespace, tag, repo string) string {
if namespace == "" || tag == "" {
metadata := app.Metadata()
if namespace == "" {
namespace = metadata.Namespace
}
if tag == "" {
tag = metadata.Version
}
}
if repo == "" {
repo = internal.AppNameFromDir(app.Name) + internal.AppExtension
}
if namespace != "" && namespace[len(namespace)-1] != '/' {
namespace += "/"
}
return namespace + repo + ":" + tag
}
func createPayload(app *types.App) (map[string]string, error) {
payload := map[string]string{
internal.MetadataFileName: string(app.MetadataRaw()),
internal.ComposeFileName: string(app.Composes()[0]),
internal.SettingsFileName: string(app.SettingsRaw()[0]),
}
if err := readAttachments(payload, app.Path, app.Attachments()); err != nil {
return nil, err
}
return payload, nil
}
func readAttachments(payload map[string]string, parentDirPath string, files []types.Attachment) error {
var errs []string
for _, file := range files {
// Convert to local OS filepath slash syntax
fullFilePath := filepath.Join(parentDirPath, filepath.FromSlash(file.Path()))
filedata, err := ioutil.ReadFile(fullFilePath)
if err != nil {
errs = append(errs, err.Error())
continue
}
payload[file.Path()] = string(filedata)
}
return newErrGroup(errs)
}
func newErrGroup(errs []string) error {
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}