-
Notifications
You must be signed in to change notification settings - Fork 68
/
docker.go
141 lines (108 loc) · 3.26 KB
/
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
package docker
import (
"context"
"encoding/base64"
"encoding/json"
"os"
"os/exec"
"github.com/docker/docker/registry"
"github.com/docker/docker/pkg/jsonmessage"
clicommand "github.com/docker/cli/cli/command"
cliconfig "github.com/docker/cli/cli/config"
"github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"github.com/pkg/errors"
)
const (
// Docker is the docker command.
Docker = "docker"
)
type loginOptions struct {
serverAddress string
user string
password string
passwordStdin bool
}
// Exec executes a docker command
func Exec(args ...string) error {
_, lookErr := exec.LookPath(Docker)
if lookErr != nil {
return errors.Wrap(lookErr, "failed to find the docker binary")
}
cmd := exec.Command(Docker, args...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
if cmdErr := cmd.Run(); cmdErr != nil {
return errors.Wrapf(cmdErr, "failed to execute cmd")
}
return nil
}
// ExecPush does push image to registry using native docker client,
// instead of using `docker push` in bash
func ExecPush(serverAddress, token, image string) error {
configFile := cliconfig.LoadDefaultConfigFile(os.Stderr)
authConfig, err := configFile.GetAuthConfig(serverAddress)
// TODO: rethink how to reuse creds store
authConfig.Password = token
if err != nil {
return errors.Errorf("Error reading credentials: %v", err)
}
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
buf, err := json.Marshal(authConfig)
if err != nil {
return err
}
encodedAuth := base64.URLEncoding.EncodeToString(buf)
responseBody, err := cli.ImagePush(ctx, image, types.ImagePushOptions{RegistryAuth: encodedAuth})
if err != nil {
return err
}
defer responseBody.Close()
out := clicommand.NewOutStream(os.Stdout)
return jsonmessage.DisplayJSONMessagesToStream(responseBody, out, nil)
return nil
}
// ExecLogin executes a docker login similar to docker login command
func ExecLogin(serverAddress, username, token string) error {
var response registrytypes.AuthenticateOKBody
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
// Remove http|https from serverAddress
serverAddress = registry.ConvertToHostname(serverAddress)
authConfig := &types.AuthConfig{
ServerAddress: serverAddress,
Username: username,
RegistryToken: token,
}
response, _ = cli.RegistryLogin(ctx, types.AuthConfig(*authConfig))
configFile := cliconfig.LoadDefaultConfigFile(os.Stderr)
creds := configFile.GetCredentialsStore(serverAddress)
if err := creds.Store(*authConfig); err != nil {
return errors.Errorf("Error saving credentials: %v", err)
}
if response.Status != "" {
return errors.Errorf("Error saving credentials: %v", response.Status)
}
return nil
}
// AirflowCommand is the main method of interaction with Airflow
func AirflowCommand(id string, airflowCommand string) string {
cmd := exec.Command("docker", "exec", "-it", id, "bash", "-c", airflowCommand)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
_ = errors.Wrapf(err, "error encountered")
}
stringOut := string(out)
return stringOut
}