/
dockerload.go
189 lines (156 loc) · 4.91 KB
/
dockerload.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package dockerclient provides helper methods for creating a synthesized
// docker load TAR stream and loading it into the local Docker daemon.
package dockerclient
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"time"
logrus "github.com/Sirupsen/logrus"
"github.com/docker/distribution/configuration"
"github.com/docker/distribution/context"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/version"
"github.com/docker/docker/reference"
"github.com/fsouza/go-dockerclient"
"github.com/docker/distribution/registry/handlers"
"github.com/docker/distribution/registry/listener"
"github.com/docker/distribution/registry/storage/driver/factory"
)
// V1LayerInfo holds information derived from a V1 history JSON blob.
type V1LayerInfo struct {
ID string `json:"id"`
}
// GetLayerInfo returns the parsed V1 layer information for the given layer.
func GetLayerInfo(layerHistory schema1.History) V1LayerInfo {
layerInfo := V1LayerInfo{}
err := json.Unmarshal([]byte(layerHistory.V1Compatibility), &layerInfo)
if err != nil {
log.Fatalf("Could not unmarshal V1 compatibility information")
}
return layerInfo
}
// DockerLoadTar performs a `docker load` of a TAR containing the V1 docker load format.
func DockerLoadTar(reader io.Reader) error {
client, err := newDockerClient()
if err != nil {
return fmt.Errorf("Could not connect to Docker: %v", err)
}
opts := docker.LoadImageOptions{reader}
lerr := client.LoadImage(opts)
if lerr != nil {
return fmt.Errorf("Could not perform docker-load: %v", lerr)
}
return nil
}
// DockerLoad performs a `docker load` of the given image with its manifest and layerPaths.
func DockerLoad(image reference.Named, manifest *schema1.SignedManifest, layerPaths map[string]string, localIp string) error {
if !isLocalDockerDaemon() && localIp == "localhost" {
return errors.New("The `--local-ip` flag is required for non-local Docker daemon")
}
go func() {
err := runRegistry(image, manifest, layerPaths)
if err != nil {
log.Fatalf("Error running local registry: %v", err)
}
}()
// Connect to Docker.
log.Println("Connecting to docker")
client, err := newDockerClient()
if err != nil {
return fmt.Errorf("Could not connect to Docker: %v", err)
}
// Wait a bit for the registry to start.
time.Sleep(2 * time.Second)
// Conduct a pull of the image.
log.Println("Pulling image")
var tagName = "latest"
if tagged, ok := image.(reference.NamedTagged); ok {
tagName = tagged.Tag()
}
w := newPullProgressDisplay(tagName, len(layerPaths))
defer w.Done()
localRegistry := fmt.Sprintf("%s:5000", localIp)
localRepository := fmt.Sprintf("%s/%s", localRegistry, image.RemoteName())
opts := docker.PullImageOptions{
Repository: localRepository,
Registry: localRegistry,
Tag: tagName,
OutputStream: w,
RawJSONStream: true,
}
auth := docker.AuthConfiguration{}
perr := client.PullImage(opts, auth)
if perr != nil {
return fmt.Errorf("Error pulling image into Docker: %v", perr)
}
// Tag the image to the name expected.
tagOpts := docker.TagImageOptions{
Repo: image.FullName(),
Tag: tagName,
Force: true,
}
localName := fmt.Sprintf("%s:%s", localRepository, tagName)
terr := client.TagImage(localName, tagOpts)
if terr != nil {
return fmt.Errorf("Error re-tagging image in Docker: %v", terr)
}
// Untag the image with its temporary name.
rerr := client.RemoveImage(localName)
if rerr != nil {
return fmt.Errorf("Error removing older tag in Docker: %v", rerr)
}
return nil
}
func runRegistry(image reference.Named, manifest *schema1.SignedManifest, layerPaths map[string]string) error {
factory.Register("localserve", &localServeDriverFactory{
image: image,
manifest: manifest,
layerPaths: layerPaths,
})
buf := bytes.NewBufferString(`
version: 0.1
log:
level: error
formatter: text
http:
addr: localhost:5000
storage:
localserve:
compatibility:
schema1:
disablesignaturestore: true
`)
logrus.SetLevel(logrus.PanicLevel)
ctx := context.WithVersion(context.Background(), version.Version)
config, err := configuration.Parse(buf)
if err != nil {
panic(err)
}
handler := handlers.NewApp(ctx, config)
server := &http.Server{
Handler: handler,
}
ln, err := listener.NewListener(config.HTTP.Net, config.HTTP.Addr)
if err != nil {
return err
}
return server.Serve(ln)
}