Skip to content

Commit 115cb35

Browse files
committed
[FAB-14845] Extract Docker Client functions
This extracts docker from the lower layers of the platforms. Change-Id: Id361b069ea89325fdbbc6649273b314cd3ec9b84 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 8c3dd21 commit 115cb35

File tree

13 files changed

+175
-309
lines changed

13 files changed

+175
-309
lines changed

core/chaincode/platforms/golang/platform.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ func (p *Platform) GetDeploymentPayload(path string) ([]byte, error) {
371371
// from the filtered list
372372
// --------------------------------------------------------------------------------------
373373
for dep := range deps {
374-
375374
logger.Debugf("processing dep: %s", dep)
376375

377376
// Each dependency should either be in our GOPATH or GOROOT. We are not interested in packaging
@@ -428,15 +427,10 @@ func (p *Platform) GetDeploymentPayload(path string) ([]byte, error) {
428427
tw := tar.NewWriter(gw)
429428

430429
for _, file := range files {
431-
432-
// file.Path represents os localpath
433-
// file.Name represents tar packagepath
434-
435430
// If the file is metadata rather than golang code, remove the leading go code path, for example:
436431
// original file.Name: src/github.com/hyperledger/fabric/examples/chaincode/go/marbles02/META-INF/statedb/couchdb/indexes/indexOwner.json
437432
// updated file.Name: META-INF/statedb/couchdb/indexes/indexOwner.json
438433
if file.IsMetadata {
439-
440434
file.Name, err = filepath.Rel(filepath.Join("src", code.Pkg), file.Name)
441435
if err != nil {
442436
return nil, fmt.Errorf("This error was caused by bad packaging of the metadata. The file [%s] is marked as MetaFile, however not located under META-INF Error:[%s]", file.Name, err)
@@ -483,15 +477,11 @@ func (p *Platform) GetDeploymentPayload(path string) ([]byte, error) {
483477
}
484478

485479
func (p *Platform) GenerateDockerfile() (string, error) {
486-
487480
var buf []string
488-
489481
buf = append(buf, "FROM "+util.GetDockerfileFromConfig("chaincode.golang.runtime"))
490482
buf = append(buf, "ADD binpackage.tar /usr/local/bin")
491483

492-
dockerFileContents := strings.Join(buf, "\n")
493-
494-
return dockerFileContents, nil
484+
return strings.Join(buf, "\n"), nil
495485
}
496486

497487
const staticLDFlagsOpts = "-ldflags \"-linkmode external -extldflags '-static'\""
@@ -504,27 +494,16 @@ func getLDFlagsOpts() string {
504494
return staticLDFlagsOpts
505495
}
506496

507-
func (p *Platform) GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error {
497+
func (p *Platform) DockerBuildOptions(path string) (util.DockerBuildOptions, error) {
508498
pkgname, err := decodeUrl(path)
509499
if err != nil {
510-
return fmt.Errorf("could not decode url: %s", err)
500+
return util.DockerBuildOptions{}, fmt.Errorf("could not decode url: %s", err)
511501
}
512502

513503
ldflagsOpt := getLDFlagsOpts()
514-
logger.Infof("building chaincode with ldflagsOpt: '%s'", ldflagsOpt)
515-
516-
codepackage := bytes.NewReader(code)
517-
binpackage := bytes.NewBuffer(nil)
518-
err = util.DockerBuild(util.DockerBuildOptions{
519-
Cmd: fmt.Sprintf("GOPATH=/chaincode/input:$GOPATH go build %s -o /chaincode/output/chaincode %s", ldflagsOpt, pkgname),
520-
InputStream: codepackage,
521-
OutputStream: binpackage,
522-
})
523-
if err != nil {
524-
return err
525-
}
526-
527-
return cutil.WriteBytesToPackage("binpackage.tar", binpackage.Bytes(), tw)
504+
return util.DockerBuildOptions{
505+
Cmd: fmt.Sprintf("GOPATH=/chaincode/input:$GOPATH go build %s -o /chaincode/output/chaincode %s", ldflagsOpt, pkgname),
506+
}, nil
528507
}
529508

530509
// GetMetadataProvider fetches metadata provider given deployment spec

core/chaincode/platforms/golang/platform_test.go

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020
"time"
2121

22+
"github.com/hyperledger/fabric/core/chaincode/platforms/util"
2223
"github.com/hyperledger/fabric/core/config/configtest"
2324
pb "github.com/hyperledger/fabric/protos/peer"
2425
"github.com/spf13/viper"
@@ -301,61 +302,16 @@ func TestGetLDFlagsOpts(t *testing.T) {
301302
}
302303
}
303304

304-
//TestGenerateDockerBuild goes through the functions needed to do docker build
305-
func TestGenerateDockerBuild(t *testing.T) {
306-
defaultGopath := os.Getenv("GOPATH")
307-
testdataPath, err := filepath.Abs("testdata")
308-
require.NoError(t, err)
309-
310-
tests := []struct {
311-
gopath string
312-
spec spec
313-
}{
314-
{gopath: defaultGopath, spec: spec{CCName: "NoCode", Path: "path/to/nowhere", File: "/bin/warez", Mode: 0100400, SuccessExpected: false}},
315-
{gopath: defaultGopath, spec: spec{CCName: "invalidhttp", Path: "https://not/a/valid/path", SuccessExpected: false, RealGen: true}},
316-
{gopath: testdataPath, spec: spec{CCName: "noop", Path: "chaincodes/noop", SuccessExpected: true, RealGen: true}},
317-
{gopath: testdataPath, spec: spec{CCName: "noopBadPath", Path: "chaincodes/noop", File: "bad/path/to/chaincode.go", Mode: 0100400, SuccessExpected: false}},
318-
{gopath: testdataPath, spec: spec{CCName: "noopBadMode", Path: "chaincodes/noop", File: "chaincodes/noop/chaincode.go", Mode: 0100555, SuccessExpected: false}},
319-
{gopath: testdataPath, spec: spec{CCName: "AutoVendor", Path: "chaincodes/AutoVendor/chaincode", SuccessExpected: true, RealGen: true}},
320-
}
321-
305+
func TestDockerBuildOptions(t *testing.T) {
322306
platform := &Platform{}
323-
for _, test := range tests {
324-
tst := test.spec
325-
reset := updateGopath(t, test.gopath)
326-
327-
inputbuf := bytes.NewBuffer(nil)
328-
tw := tar.NewWriter(inputbuf)
329-
330-
var cds *pb.ChaincodeDeploymentSpec
331-
var err error
332-
if tst.RealGen {
333-
cds = &pb.ChaincodeDeploymentSpec{
334-
ChaincodeSpec: &pb.ChaincodeSpec{
335-
ChaincodeId: &pb.ChaincodeID{
336-
Name: tst.CCName,
337-
Path: tst.Path,
338-
Version: "0",
339-
},
340-
},
341-
}
342-
cds.CodePackage, err = platform.GetDeploymentPayload(tst.Path)
343-
if err = testerr(err, tst.SuccessExpected); err != nil {
344-
t.Errorf("test failed in GetDeploymentPayload: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
345-
}
346-
} else {
347-
cds, err = generateFakeCDS(tst.CCName, tst.Path, tst.File, tst.Mode)
348-
}
349307

350-
if _, err = platform.GenerateDockerfile(); err != nil {
351-
t.Errorf("could not generate docker file for a valid spec: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
352-
}
353-
err = platform.GenerateDockerBuild(cds.ChaincodeSpec.ChaincodeId.Path, cds.CodePackage, tw)
354-
if err = testerr(err, tst.SuccessExpected); err != nil {
355-
t.Errorf("Error validating chaincode spec: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
356-
}
357-
reset()
308+
opts, err := platform.DockerBuildOptions("the-path")
309+
assert.NoError(t, err, "unexpected error from DockerBuildOptions")
310+
311+
expectedOpts := util.DockerBuildOptions{
312+
Cmd: "GOPATH=/chaincode/input:$GOPATH go build -ldflags \"-linkmode external -extldflags '-static'\" -o /chaincode/output/chaincode the-path",
358313
}
314+
assert.Equal(t, expectedOpts, opts)
359315
}
360316

361317
func TestMain(m *testing.M) {

core/chaincode/platforms/java/platform.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,11 @@ func (p *Platform) GenerateDockerfile() (string, error) {
131131
return dockerFileContents, nil
132132
}
133133

134-
func (p *Platform) GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error {
135-
codepackage := bytes.NewReader(code)
136-
binpackage := bytes.NewBuffer(nil)
137-
buildOptions := util.DockerBuildOptions{
138-
Image: util.GetDockerfileFromConfig("chaincode.java.runtime"),
139-
Cmd: "./build.sh",
140-
InputStream: codepackage,
141-
OutputStream: binpackage,
142-
}
143-
logger.Debugf("Executing docker build %v, %v", buildOptions.Image, buildOptions.Cmd)
144-
err := util.DockerBuild(buildOptions)
145-
if err != nil {
146-
logger.Errorf("Can't build java chaincode %v", err)
147-
return err
148-
}
149-
150-
resultBytes := binpackage.Bytes()
151-
return cutil.WriteBytesToPackage("binpackage.tar", resultBytes, tw)
134+
func (p *Platform) DockerBuildOptions(path string) (util.DockerBuildOptions, error) {
135+
return util.DockerBuildOptions{
136+
Image: util.GetDockerfileFromConfig("chaincode.java.runtime"),
137+
Cmd: "./build.sh",
138+
}, nil
152139
}
153140

154141
// GetMetadataProvider fetches metadata provider given deployment spec

core/chaincode/platforms/java/platform_test.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,17 @@ func TestGenerateDockerfile(t *testing.T) {
159159
assert.Equal(t, dockerFileContents, dockerfile)
160160
}
161161

162-
func TestGenerateDockerBuild(t *testing.T) {
163-
t.Skip()
162+
func TestDockerBuildOptions(t *testing.T) {
164163
platform := java.Platform{}
165-
ccSpec := &pb.ChaincodeSpec{
166-
Type: pb.ChaincodeSpec_JAVA,
167-
ChaincodeId: &pb.ChaincodeID{Path: chaincodePathFolderGradle},
168-
Input: &pb.ChaincodeInput{Args: [][]byte{[]byte("init")}}}
169164

170-
cp, _ := platform.GetDeploymentPayload(ccSpec.ChaincodeId.Path)
165+
opts, err := platform.DockerBuildOptions("path")
166+
assert.NoError(t, err, "unexpected error from DockerBuildOptions")
171167

172-
cds := &pb.ChaincodeDeploymentSpec{
173-
ChaincodeSpec: ccSpec,
174-
CodePackage: cp}
175-
176-
payload := bytes.NewBuffer(nil)
177-
gw := gzip.NewWriter(payload)
178-
tw := tar.NewWriter(gw)
179-
180-
err := platform.GenerateDockerBuild(cds.ChaincodeSpec.ChaincodeId.Path, cds.CodePackage, tw)
181-
assert.NoError(t, err)
168+
expectedOpts := util.DockerBuildOptions{
169+
Image: "hyperledger/fabric-javaenv:latest",
170+
Cmd: "./build.sh",
171+
}
172+
assert.Equal(t, expectedOpts, opts)
182173
}
183174

184175
func generateMockPackegeBytes(fileName string, mode int64) ([]byte, error) {

core/chaincode/platforms/mock/platform.go

Lines changed: 58 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)