/
push.go
78 lines (66 loc) · 1.88 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
package docker
import (
"encoding/base64"
"encoding/json"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/pkg/errors"
"github.com/rai-project/config"
"github.com/rai-project/model"
"github.com/rai-project/utils"
)
func (c *Client) ImagePush(name0 string, pushOpts model.Push) error {
decrypt := func(s string) string {
if strings.HasPrefix(s, utils.CryptoHeader) && config.App.Secret != "" {
if val, err := utils.DecryptStringBase64(config.App.Secret, s); err == nil {
return val
}
}
return s
}
name, err := parseImageName(name0)
if err != nil {
return errors.Wrapf(err, "unable to parse the image name %v", name0)
}
log.WithField("image_name", name).Debug("publishing to docker repository")
username := decrypt(pushOpts.Credentials.Username)
password := decrypt(pushOpts.Credentials.Password)
email := ""
if strings.Contains(username, "@") {
email = username
username = ""
}
authOk, err := c.Client.RegistryLogin(c.options.context, types.AuthConfig{
Username: username,
Password: password,
Email: email,
})
if err != nil {
return errors.Wrapf(err, "unable to login registry using username = %s", username)
}
if authOk.Status == "" {
return errors.Wrapf(err, "unable to login registry because of invalid status code")
}
auth := types.AuthConfig{
Username: username,
Password: password,
Email: email,
IdentityToken: authOk.IdentityToken,
}
encodedJSON, err := json.Marshal(auth)
if err != nil {
return errors.Wrap(err, "unable to marshal auth to json")
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
reader, err := c.Client.ImagePush(c.options.context, name, types.ImagePushOptions{RegistryAuth: authStr})
if err != nil {
return err
}
defer reader.Close()
return jsonmessage.DisplayJSONMessagesToStream(
reader,
c.options.stdout,
nil,
)
}