Skip to content

Commit

Permalink
FAB-10832 GenerateDockerBuild to accept path, code
Browse files Browse the repository at this point in the history
Continuing a series of removing the chaincode spec dependencies from the
platforms package.

Change-Id: I23f108d895078db80d9b6ff1f733d27a71d5f548
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Jul 10, 2018
1 parent b2db32a commit fe3bbe2
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 54 deletions.
4 changes: 2 additions & 2 deletions core/chaincode/platforms/car/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func (carPlatform *Platform) GenerateDockerfile() (string, error) {
return dockerFileContents, nil
}

func (carPlatform *Platform) GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec, tw *tar.Writer) error {
func (carPlatform *Platform) GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error {

// Bundle the .car file into a tar stream so it may be transferred to the builder container
codepackage, output := io.Pipe()
go func() {
tw := tar.NewWriter(output)

err := cutil.WriteBytesToPackage("codepackage.car", cds.CodePackage, tw)
err := cutil.WriteBytesToPackage("codepackage.car", code, tw)

tw.Close()
output.CloseWithError(err)
Expand Down
20 changes: 9 additions & 11 deletions core/chaincode/platforms/golang/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func pathExists(path string) (bool, error) {
return true, err
}

func decodeUrl(spec *pb.ChaincodeSpec) (string, error) {
func decodeUrl(path string) (string, 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:]
if strings.HasPrefix(path, "http://") {
urlLocation = path[7:]
} else if strings.HasPrefix(path, "https://") {
urlLocation = path[8:]
} else {
urlLocation = spec.ChaincodeId.Path
urlLocation = path
}

if len(urlLocation) < 2 {
Expand Down Expand Up @@ -498,10 +498,8 @@ func getLDFlagsOpts() string {
return staticLDFlagsOpts
}

func (goPlatform *Platform) GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec, tw *tar.Writer) error {
spec := cds.ChaincodeSpec

pkgname, err := decodeUrl(spec)
func (goPlatform *Platform) GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error {
pkgname, err := decodeUrl(path)
if err != nil {
return fmt.Errorf("could not decode url: %s", err)
}
Expand All @@ -516,7 +514,7 @@ func (goPlatform *Platform) GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec,
}
logger.Infof("building chaincode with tags: %s", gotags)

codepackage := bytes.NewReader(cds.CodePackage)
codepackage := bytes.NewReader(code)
binpackage := bytes.NewBuffer(nil)
err = util.DockerBuild(util.DockerBuildOptions{
Cmd: fmt.Sprintf("GOPATH=/chaincode/input:$GOPATH go build -tags \"%s\" %s -o /chaincode/output/chaincode %s", gotags, ldflagsOpt, pkgname),
Expand Down
37 changes: 13 additions & 24 deletions core/chaincode/platforms/golang/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,37 +195,26 @@ func Test_DeploymentPayloadWithStateDBArtifacts(t *testing.T) {
}

func Test_decodeUrl(t *testing.T) {
cs := &pb.ChaincodeSpec{
ChaincodeId: &pb.ChaincodeID{
Name: "Test Chaincode",
Path: "http://github.com/hyperledger/fabric/examples/chaincode/go/map",
},
}

if _, err := decodeUrl(cs); err != nil {
path := "http://github.com/hyperledger/fabric/examples/chaincode/go/map"
if _, err := decodeUrl(path); err != nil {
t.Fail()
t.Logf("Error to decodeUrl unsuccessfully with valid path: %s, %s", cs.ChaincodeId.Path, err)
t.Logf("Error to decodeUrl unsuccessfully with valid path: %s, %s", path, err)
}

cs.ChaincodeId.Path = ""

if _, err := decodeUrl(cs); err == nil {
path = ""
if _, err := decodeUrl(path); err == nil {
t.Fail()
t.Logf("Error to decodeUrl successfully with invalid path: %s", cs.ChaincodeId.Path)
t.Logf("Error to decodeUrl successfully with invalid path: %s", path)
}

cs.ChaincodeId.Path = "/"

if _, err := decodeUrl(cs); err == nil {
t.Fail()
t.Logf("Error to decodeUrl successfully with invalid path: %s", cs.ChaincodeId.Path)
path = "/"
if _, err := decodeUrl(path); err == nil {
t.Fatalf("Error to decodeUrl successfully with invalid path: %s", path)
}

cs.ChaincodeId.Path = "http:///"

if _, err := decodeUrl(cs); err == nil {
t.Fail()
t.Logf("Error to decodeUrl successfully with invalid path: %s", cs.ChaincodeId.Path)
path = "http:///"
if _, err := decodeUrl(path); err == nil {
t.Fatalf("Error to decodeUrl successfully with invalid path: %s", path)
}
}

Expand Down Expand Up @@ -361,7 +350,7 @@ func TestGenerateDockerBuild(t *testing.T) {
if _, err = platform.GenerateDockerfile(); err != nil {
t.Errorf("could not generate docker file for a valid spec: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
}
err = platform.GenerateDockerBuild(cds, tw)
err = platform.GenerateDockerBuild(cds.Path(), cds.Bytes(), tw)
if err = testerr(err, tst.SuccessExpected); err != nil {
t.Errorf("Error validating chaincode spec: %s, %s", cds.ChaincodeSpec.ChaincodeId.Path, err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/platforms/java/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestGenerateDockerBuild(t *testing.T) {
CodePackage: []byte{}}
tw := tar.NewWriter(gzip.NewWriter(bytes.NewBuffer(nil)))

err := platform.GenerateDockerBuild(cds, tw)
err := platform.GenerateDockerBuild(cds.Path(), cds.Bytes(), tw)
assert.NoError(t, err)
}

Expand Down
4 changes: 2 additions & 2 deletions core/chaincode/platforms/java/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (javaPlatform *Platform) GenerateDockerfile() (string, error) {
return dockerFileContents, nil
}

func (javaPlatform *Platform) GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec, tw *tar.Writer) error {
return cutil.WriteBytesToPackage("codepackage.tgz", cds.CodePackage, tw)
func (javaPlatform *Platform) GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error {
return cutil.WriteBytesToPackage("codepackage.tgz", code, tw)
}

//GetMetadataProvider fetches metadata provider given deployment spec
Expand Down
25 changes: 16 additions & 9 deletions core/chaincode/platforms/mock/platform.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core/chaincode/platforms/node/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ func (nodePlatform *Platform) GenerateDockerfile() (string, error) {
return dockerFileContents, nil
}

func (nodePlatform *Platform) GenerateDockerBuild(cds *pb.ChaincodeDeploymentSpec, tw *tar.Writer) error {
func (nodePlatform *Platform) GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error {

codepackage := bytes.NewReader(cds.CodePackage)
codepackage := bytes.NewReader(code)
binpackage := bytes.NewBuffer(nil)
err := util.DockerBuild(util.DockerBuildOptions{
Cmd: fmt.Sprint("cp -R /chaincode/input/src/. /chaincode/output && cd /chaincode/output && npm install --production"),
Expand Down
2 changes: 1 addition & 1 deletion core/chaincode/platforms/node/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestGenerateDockerBuild(t *testing.T) {
gw := gzip.NewWriter(payload)
tw := tar.NewWriter(gw)

err = platform.GenerateDockerBuild(cds, tw)
err = platform.GenerateDockerBuild(cds.Path(), cds.Bytes(), tw)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/chaincode/platforms/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Platform interface {
ValidateCodePackage(code []byte) error
GetDeploymentPayload(path string) ([]byte, error)
GenerateDockerfile() (string, error)
GenerateDockerBuild(spec *pb.ChaincodeDeploymentSpec, tw *tar.Writer) error
GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error
GetMetadataProvider(spec *pb.ChaincodeDeploymentSpec) ccmetadata.MetadataProvider
}

Expand Down Expand Up @@ -161,7 +161,7 @@ func (r *Registry) StreamDockerBuild(cds *pb.ChaincodeDeploymentSpec, inputFiles
// ----------------------------------------------------------------------------------------------------
// Now give the platform an opportunity to contribute its own context to the build
// ----------------------------------------------------------------------------------------------------
err = platform.GenerateDockerBuild(cds, tw)
err = platform.GenerateDockerBuild(cds.Path(), cds.Bytes(), tw)
if err != nil {
return fmt.Errorf("Failed to generate platform-specific docker build: %s", err)
}
Expand Down
9 changes: 9 additions & 0 deletions protos/peer/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ func (cs *ChaincodeSpec) Path() string {
return cs.ChaincodeId.Path
}

// Path implements the platforms.PathDescriber interface
func (cds *ChaincodeDeploymentSpec) Path() string {
if cds.ChaincodeSpec == nil {
return ""
}

return cds.ChaincodeSpec.Path()
}

// Bytes implements the platforms.CodePackage interface
func (cds *ChaincodeDeploymentSpec) Bytes() []byte {
return cds.CodePackage
Expand Down

0 comments on commit fe3bbe2

Please sign in to comment.