forked from hyperledger-archives/fabric
/
vm.go
executable file
·383 lines (332 loc) · 11.5 KB
/
vm.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 container
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"golang.org/x/net/context"
"github.com/spf13/viper"
"github.com/fsouza/go-dockerclient"
"github.com/op/go-logging"
pb "github.com/openblockchain/obc-peer/protos"
)
func newDockerClient() (client *docker.Client, err error) {
//QQ: is this ok using config properties here so deep ? ie, should we read these in main and stow them away ?
endpoint := viper.GetString("vm.endpoint")
vmLogger.Info("Creating VM with endpoint: %s", endpoint)
if viper.GetBool("vm.docker.tls.enabled") {
cert := viper.GetString("vm.docker.tls.cert.file")
key := viper.GetString("vm.docker.tls.key.file")
ca := viper.GetString("vm.docker.tls.ca.file")
client, err = docker.NewTLSClient(endpoint, cert, key, ca)
} else {
client, err = docker.NewClient(endpoint)
}
return
}
// VM implemenation of VM management functionality.
type VM struct {
Client *docker.Client
}
// NewVM creates a new VM instance.
func NewVM() (*VM, error) {
client, err := newDockerClient()
if err != nil {
return nil, err
}
VM := &VM{Client: client}
return VM, nil
}
var vmLogger = logging.MustGetLogger("container")
// ListImages list the images available
func (vm *VM) ListImages(context context.Context) error {
imgs, err := vm.Client.ListImages(docker.ListImagesOptions{All: false})
if err != nil {
return err
}
for _, img := range imgs {
fmt.Println("ID: ", img.ID)
fmt.Println("RepoTags: ", img.RepoTags)
fmt.Println("Created: ", img.Created)
fmt.Println("Size: ", img.Size)
fmt.Println("VirtualSize: ", img.VirtualSize)
fmt.Println("ParentId: ", img.ParentID)
}
return nil
}
// BuildChaincodeContainer builds the container for the supplied chaincode specification
func (vm *VM) BuildChaincodeContainer(spec *pb.ChaincodeSpec) ([]byte, error) {
chaincodePkgBytes, err := GetChaincodePackageBytes(spec)
if err != nil {
return nil, fmt.Errorf("Error getting chaincode package bytes: %s", err)
}
err = vm.buildChaincodeContainerUsingDockerfilePackageBytes(spec, chaincodePkgBytes)
if err != nil {
return nil, fmt.Errorf("Error building Chaincode container: %s", err)
}
return chaincodePkgBytes, nil
}
// GetChaincodePackageBytes creates bytes for docker container generation using the supplied chaincode specification
func GetChaincodePackageBytes(spec *pb.ChaincodeSpec) ([]byte, error) {
if spec == nil || spec.ChaincodeID == nil {
return nil, fmt.Errorf("invalid chaincode spec")
}
if spec.ChaincodeID.Name != "" {
return nil, fmt.Errorf("chaincode name exists")
}
inputbuf := bytes.NewBuffer(nil)
gw := gzip.NewWriter(inputbuf)
tw := tar.NewWriter(gw)
var err error
spec.ChaincodeID.Name, err = generateHashcode(spec, tw)
if err != nil {
tw.Close()
gw.Close()
return nil, fmt.Errorf("Error generating hashcode: %s", err)
}
err = writeChaincodePackage(spec, tw)
if err != nil {
tw.Close()
gw.Close()
return nil, fmt.Errorf("Error writing chaincode package: %s", err)
}
tw.Close()
gw.Close()
chaincodePkgBytes := inputbuf.Bytes()
return chaincodePkgBytes, nil
}
// Builds the Chaincode image using the supplied Dockerfile package contents
func (vm *VM) buildChaincodeContainerUsingDockerfilePackageBytes(spec *pb.ChaincodeSpec, code []byte) error {
outputbuf := bytes.NewBuffer(nil)
vmName := spec.ChaincodeID.Name
inputbuf := bytes.NewReader(code)
opts := docker.BuildImageOptions{
Name: vmName,
Pull: true,
InputStream: inputbuf,
OutputStream: outputbuf,
}
if err := vm.Client.BuildImage(opts); err != nil {
vmLogger.Debug(fmt.Sprintf("Failed Chaincode docker build:\n%s\n", outputbuf.String()))
return fmt.Errorf("Error building Chaincode container: %s", err)
}
return nil
}
// BuildPeerContainer builds the image for the Peer to be used in development network
func (vm *VM) BuildPeerContainer() error {
//inputbuf, err := vm.GetPeerPackageBytes()
inputbuf, err := vm.getPackageBytes(vm.writePeerPackage)
if err != nil {
return fmt.Errorf("Error building Peer container: %s", err)
}
outputbuf := bytes.NewBuffer(nil)
opts := docker.BuildImageOptions{
Name: "openchain-peer",
Pull: true,
InputStream: inputbuf,
OutputStream: outputbuf,
}
if err := vm.Client.BuildImage(opts); err != nil {
vmLogger.Debug(fmt.Sprintf("Failed Peer docker build:\n%s\n", outputbuf.String()))
return fmt.Errorf("Error building Peer container: %s\n", err)
}
return nil
}
// BuildObccaContainer builds the image for the obcca to be used in development network
func (vm *VM) BuildObccaContainer() error {
inputbuf, err := vm.getPackageBytes(vm.writeObccaPackage)
if err != nil {
return fmt.Errorf("Error building obcca container: %s", err)
}
outputbuf := bytes.NewBuffer(nil)
opts := docker.BuildImageOptions{
Name: "obcca",
Pull: true,
InputStream: inputbuf,
OutputStream: outputbuf,
}
if err := vm.Client.BuildImage(opts); err != nil {
vmLogger.Debug(fmt.Sprintf("Failed obcca docker build:\n%s\n", outputbuf.String()))
return fmt.Errorf("Error building obcca container: %s\n", err)
}
return nil
}
// GetPeerPackageBytes returns the gzipped tar image used for docker build of Peer
func (vm *VM) GetPeerPackageBytes() (io.Reader, error) {
inputbuf := bytes.NewBuffer(nil)
gw := gzip.NewWriter(inputbuf)
tr := tar.NewWriter(gw)
// Get the Tar contents for the image
err := vm.writePeerPackage(tr)
tr.Close()
gw.Close()
if err != nil {
return nil, fmt.Errorf("Error getting Peer package: %s", err)
}
return inputbuf, nil
}
//type tarWriter func()
func (vm *VM) getPackageBytes(writerFunc func(*tar.Writer) error) (io.Reader, error) {
inputbuf := bytes.NewBuffer(nil)
gw := gzip.NewWriter(inputbuf)
tr := tar.NewWriter(gw)
// Get the Tar contents for the image
err := writerFunc(tr)
tr.Close()
gw.Close()
if err != nil {
return nil, fmt.Errorf("Error getting package bytes: %s", err)
}
return inputbuf, nil
}
//tw is expected to have the chaincode in it from GenerateHashcode. This method
//will just package rest of the bytes
func writeChaincodePackage(spec *pb.ChaincodeSpec, tw *tar.Writer) error {
var urlLocation string
if strings.HasPrefix(spec.ChaincodeID.Path, "http://") {
urlLocation = spec.ChaincodeID.Path[7:]
} else if strings.HasPrefix(spec.ChaincodeID.Path, "https://") {
urlLocation = spec.ChaincodeID.Path[8:]
} else {
urlLocation = spec.ChaincodeID.Path
}
if urlLocation == "" {
return fmt.Errorf("empty url location")
}
if strings.LastIndex(urlLocation, "/") == len(urlLocation)-1 {
urlLocation = urlLocation[:len(urlLocation)-1]
}
toks := strings.Split(urlLocation, "/")
if toks == nil || len(toks) == 0 {
return fmt.Errorf("cannot get path components from %s", urlLocation)
}
chaincodeGoName := toks[len(toks)-1]
if chaincodeGoName == "" {
return fmt.Errorf("could not get chaincode name from path %s", urlLocation)
}
//let the executable's name be chaincode ID's name
newRunLine := fmt.Sprintf("RUN go install %s && cp src/github.com/openblockchain/obc-peer/openchain.yaml $GOPATH/bin && mv $GOPATH/bin/%s $GOPATH/bin/%s", urlLocation, chaincodeGoName, spec.ChaincodeID.Name)
dockerFileContents := fmt.Sprintf("%s\n%s", viper.GetString("chaincode.golang.Dockerfile"), newRunLine)
dockerFileSize := int64(len([]byte(dockerFileContents)))
//Make headers identical by using zero time
var zeroTime time.Time
tw.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize, ModTime: zeroTime, AccessTime: zeroTime, ChangeTime: zeroTime})
tw.Write([]byte(dockerFileContents))
err := writeGopathSrc(tw, urlLocation)
if err != nil {
return fmt.Errorf("Error writing Chaincode package contents: %s", err)
}
return nil
}
func (vm *VM) writePeerPackage(tw *tar.Writer) error {
startTime := time.Now()
dockerFileContents := viper.GetString("peer.Dockerfile")
dockerFileSize := int64(len([]byte(dockerFileContents)))
tw.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize, ModTime: startTime, AccessTime: startTime, ChangeTime: startTime})
tw.Write([]byte(dockerFileContents))
err := writeGopathSrc(tw, "")
if err != nil {
return fmt.Errorf("Error writing Peer package contents: %s", err)
}
return nil
}
func (vm *VM) writeObccaPackage(tw *tar.Writer) error {
startTime := time.Now()
dockerFileContents := viper.GetString("peer.Dockerfile")
dockerFileContents = dockerFileContents + "RUN cd obc-ca && go install\n"
dockerFileSize := int64(len([]byte(dockerFileContents)))
tw.WriteHeader(&tar.Header{Name: "Dockerfile", Size: dockerFileSize, ModTime: startTime, AccessTime: startTime, ChangeTime: startTime})
tw.Write([]byte(dockerFileContents))
err := writeGopathSrc(tw, "")
if err != nil {
return fmt.Errorf("Error writing obcca package contents: %s", err)
}
return nil
}
func writeGopathSrc(tw *tar.Writer, excludeDir string) error {
gopath := os.Getenv("GOPATH")
if strings.LastIndex(gopath, "/") == len(gopath)-1 {
gopath = gopath[:len(gopath)]
}
rootDirectory := fmt.Sprintf("%s%s%s", os.Getenv("GOPATH"), string(os.PathSeparator), "src")
vmLogger.Info("rootDirectory = %s", rootDirectory)
//append "/" if necessary
if excludeDir != "" && strings.LastIndex(excludeDir, "/") < len(excludeDir)-1 {
excludeDir = excludeDir + "/"
}
rootDirLen := len(rootDirectory)
walkFn := func(path string, info os.FileInfo, err error) error {
// If path includes .git, ignore
if strings.Contains(path, ".git") {
return nil
}
if info.Mode().IsDir() {
return nil
}
//exclude any files with excludeDir prefix. They should already be in the tar
if excludeDir != "" && strings.Index(path, excludeDir) == rootDirLen+1 { //1 for "/"
return nil
}
// Because of scoping we can reference the external rootDirectory variable
newPath := fmt.Sprintf("src%s", path[rootDirLen:])
//newPath := path[len(rootDirectory):]
if len(newPath) == 0 {
return nil
}
fr, err := os.Open(path)
if err != nil {
return err
}
defer fr.Close()
h, err := tar.FileInfoHeader(info, newPath)
if err != nil {
vmLogger.Error(fmt.Sprintf("Error getting FileInfoHeader: %s", err))
return err
}
//Let's take the variance out of the tar, make headers identical everywhere by using zero time
var zeroTime time.Time
h.AccessTime = zeroTime
h.ModTime = zeroTime
h.ChangeTime = zeroTime
h.Name = newPath
if err = tw.WriteHeader(h); err != nil {
vmLogger.Error(fmt.Sprintf("Error writing header: %s", err))
return err
}
if _, err := io.Copy(tw, fr); err != nil {
return err
}
return nil
}
if err := filepath.Walk(rootDirectory, walkFn); err != nil {
vmLogger.Info("Error walking rootDirectory: %s", err)
return err
}
// Write the tar file out
if err := tw.Close(); err != nil {
return err
}
//ioutil.WriteFile("/tmp/chaincode_deployment.tar", inputbuf.Bytes(), 0644)
return nil
}