This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
registry_docker.go
159 lines (134 loc) · 4.53 KB
/
registry_docker.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
149
150
151
152
153
154
155
156
157
158
159
package docker
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"os"
"github.com/docker/cli/cli/config"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/registry"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
wpdockerclient "github.com/hashicorp/waypoint/builtin/docker/client"
)
func (r *Registry) pushWithDocker(
ctx context.Context,
log hclog.Logger,
ui terminal.UI,
source *Image,
target *Image,
authConfig *Auth,
) error {
stdout, _, err := ui.OutputWriters()
if err != nil {
return status.Errorf(codes.FailedPrecondition, "unable to create output for logs:%s", err)
}
sg := ui.StepGroup()
defer sg.Wait()
step := sg.Add("Initializing Docker client...")
defer func() { step.Abort() }()
cli, err := wpdockerclient.NewClientWithOpts(client.FromEnv)
if err != nil {
return status.Errorf(codes.FailedPrecondition, "unable to create Docker client:%s", err)
}
cli.NegotiateAPIVersion(ctx)
step.Update("Tagging Docker image: %s => %s:%s", source.Name(), r.config.Image, r.config.Tag)
err = cli.ImageTag(ctx, source.Name(), target.Name())
if err != nil {
return status.Errorf(codes.Internal, "unable to tag image:%s", err)
}
step.Done()
if r.config.Local {
return nil
}
ref, err := reference.ParseNormalizedNamed(target.Name())
if err != nil {
return status.Errorf(codes.Internal, "unable to parse image name: %s", err)
}
var encodedAuth = ""
if r.config.EncodedAuth != "" {
encodedAuth = r.config.EncodedAuth
} else if encodedAuth == "" && r.config.Password != "" {
// If there was no explicit encoded auth but there is a password, make the username+password
// into an encoded auth string.
var authConfig types.AuthConfig
authConfig.Username = r.config.Username
authConfig.Password = r.config.Password
buf, err := json.Marshal(authConfig)
if err != nil {
return status.Errorf(codes.Internal, "unable to generate authentication info for registry: %s", err)
}
encodedAuth = base64.URLEncoding.EncodeToString(buf)
} else if r.config.Auth == nil && r.config.EncodedAuth == "" {
// Resolve the Repository name from fqn to RepositoryInfo
repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil {
return status.Errorf(codes.Internal, "unable to parse repository info from image name: %s", err)
}
var server string
if repoInfo.Index.Official {
info, err := cli.Info(ctx)
if err != nil || info.IndexServerAddress == "" {
server = registry.IndexServer
} else {
server = info.IndexServerAddress
}
} else {
server = repoInfo.Index.Name
}
var errBuf bytes.Buffer
cf := config.LoadDefaultConfigFile(&errBuf)
if errBuf.Len() > 0 {
// NOTE(mitchellh): I don't know why we ignore this, but we always have.
log.Warn("error loading Docker config file", "err", err)
}
authConfig, _ := cf.GetAuthConfig(server)
buf, err := json.Marshal(authConfig)
if err != nil {
return status.Errorf(codes.Internal, "unable to generate authentication info for registry: %s", err)
}
encodedAuth = base64.URLEncoding.EncodeToString(buf)
} else if (*r.config.Auth != Auth{}) {
if authConfig.Hostname != "" {
return status.Errorf(codes.InvalidArgument, "hostname not supported for registry")
}
authBytes, err := json.Marshal(types.AuthConfig{
Username: authConfig.Username,
Password: authConfig.Password,
Email: authConfig.Email,
Auth: authConfig.Auth,
ServerAddress: authConfig.ServerAddress,
IdentityToken: authConfig.IdentityToken,
RegistryToken: authConfig.RegistryToken,
})
if err != nil {
return status.Errorf(codes.Internal, "failed to marshal auth info to json: %s", err)
}
encodedAuth = base64.URLEncoding.EncodeToString(authBytes)
}
step = sg.Add("Pushing Docker image...")
options := types.ImagePushOptions{
RegistryAuth: encodedAuth,
}
responseBody, err := cli.ImagePush(ctx, reference.FamiliarString(ref), options)
if err != nil {
return status.Errorf(codes.Internal, "unable to push image to registry: %s", err)
}
defer responseBody.Close()
var termFd uintptr
if f, ok := stdout.(*os.File); ok {
termFd = f.Fd()
}
err = jsonmessage.DisplayJSONMessagesStream(responseBody, step.TermOutput(), termFd, true, nil)
if err != nil {
return status.Errorf(codes.Internal, "unable to stream Docker logs to terminal: %s", err)
}
step.Done()
return nil
}