-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathregistry.go
71 lines (62 loc) · 1.78 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
package internal
import (
"context"
"encoding/base64"
"encoding/json"
"io"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/packethost/pkg/log"
"github.com/pkg/errors"
)
// RegistryConnDetails are the connection details for accessing a Docker
// registry and logging activities
type RegistryConnDetails struct {
registry,
user,
pwd string
logger log.Logger
}
// NewRegistryConnDetails creates a new RegistryConnDetails
func NewRegistryConnDetails(registry, user, pwd string, logger log.Logger) *RegistryConnDetails {
return &RegistryConnDetails{
registry: registry,
user: user,
pwd: pwd,
logger: logger,
}
}
// NewClient uses the RegistryConnDetails to create a new Docker Client
func (r *RegistryConnDetails) NewClient() (*client.Client, error) {
if r.registry == "" {
return nil, errors.New("required DOCKER_REGISTRY")
}
c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, errors.Wrap(err, "DOCKER CLIENT")
}
return c, nil
}
// pullImage outputs to stdout the contents of the requested image (relative to the registry)
func (r *RegistryConnDetails) pullImage(ctx context.Context, cli *client.Client, image string) error {
authConfig := types.AuthConfig{
Username: r.user,
Password: r.pwd,
ServerAddress: r.registry,
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return errors.Wrap(err, "DOCKER AUTH")
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
out, err := cli.ImagePull(ctx, r.registry+"/"+image, types.ImagePullOptions{RegistryAuth: authStr})
if err != nil {
return errors.Wrap(err, "DOCKER PULL")
}
defer out.Close()
if _, err := io.Copy(os.Stdout, out); err != nil {
return err
}
return nil
}