Skip to content

Commit a73f332

Browse files
author
Simon Stone
committed
[FAB-16714] Update Node.js chaincode build/start cmds
Instead of the "cp -R && npm install" hardcoded into the peer, update the Node.js chaincode build command to a script that is now included in the nodeenv image: /chaincode/build.sh Instead of the "npm start" hardcoded into the peer, update the Node.js chaincode start command to a script that is now included in the nodeenv image: /chaincode/start.sh This aligns Node.js chaincode with Java chaincode, and allows the Node.js chaincode maintainers to manage the build/start processing without having to submit CRs to core Fabric to change the peer code. Signed-off-by: Simon Stone <sstone1@uk.ibm.com> Change-Id: I4d5c9a2623247ab182fdec07747775b6e6980025
1 parent 810b723 commit a73f332

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

core/chaincode/platforms/node/platform.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,18 @@ func (p *Platform) GenerateDockerfile() (string, error) {
174174
return dockerFileContents, nil
175175
}
176176

177+
var buildScript = `
178+
set -e
179+
if [ -x /chaincode/build.sh ]; then
180+
/chaincode/build.sh
181+
else
182+
cp -R /chaincode/input/src/. /chaincode/output && cd /chaincode/output && npm install --production
183+
fi
184+
`
185+
177186
func (p *Platform) DockerBuildOptions(path string) (util.DockerBuildOptions, error) {
178187
return util.DockerBuildOptions{
179188
Image: util.GetDockerImageFromConfig("chaincode.node.runtime"),
180-
Cmd: fmt.Sprint("cp -R /chaincode/input/src/. /chaincode/output && cd /chaincode/output && npm install --production"),
189+
Cmd: buildScript,
181190
}, nil
182191
}

core/chaincode/platforms/node/platform_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,22 @@ func TestGenerateDockerfile(t *testing.T) {
131131
}
132132
}
133133

134+
var expectedBuildScript = `
135+
set -e
136+
if [ -x /chaincode/build.sh ]; then
137+
/chaincode/build.sh
138+
else
139+
cp -R /chaincode/input/src/. /chaincode/output && cd /chaincode/output && npm install --production
140+
fi
141+
`
142+
134143
func TestGenerateBuildOptions(t *testing.T) {
135144
opts, err := platform.DockerBuildOptions("pathname")
136145
assert.NoError(t, err)
137146

138147
expectedOpts := util.DockerBuildOptions{
139148
Image: "hyperledger/fabric-nodeenv:latest",
140-
Cmd: "cp -R /chaincode/input/src/. /chaincode/output && cd /chaincode/output && npm install --production",
149+
Cmd: expectedBuildScript,
141150
}
142151
assert.Equal(t, expectedOpts, opts)
143152
}

core/container/dockercontroller/dockercontroller.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ func (vm *DockerVM) Build(ccid string, metadata *persistence.ChaincodePackageMet
206206
}, nil
207207
}
208208

209+
// In order to support starting chaincode containers built with Fabric v1.4 and earlier,
210+
// we must check for the precense of the start.sh script for Node.js chaincode before
211+
// attempting to call it.
212+
var nodeStartScript = `
213+
set -e
214+
if [ -x /chaincode/start.sh ]; then
215+
/chaincode/start.sh --peer.address %[1]s
216+
else
217+
cd /usr/local/src
218+
npm start -- --peer.address %[1]s
219+
fi
220+
`
221+
209222
func (vm *DockerVM) GetArgs(ccType string, peerAddress string) ([]string, error) {
210223
// language specific arguments, possibly should be pushed back into platforms, but were simply
211224
// ported from the container_runtime chaincode component
@@ -215,7 +228,7 @@ func (vm *DockerVM) GetArgs(ccType string, peerAddress string) ([]string, error)
215228
case pb.ChaincodeSpec_JAVA.String():
216229
return []string{"/root/chaincode-java/start", "--peerAddress", peerAddress}, nil
217230
case pb.ChaincodeSpec_NODE.String():
218-
return []string{"/bin/sh", "-c", fmt.Sprintf("cd /usr/local/src; npm start -- --peer.address %s", peerAddress)}, nil
231+
return []string{"/bin/sh", "-c", fmt.Sprintf(nodeStartScript, peerAddress)}, nil
219232
default:
220233
return nil, errors.Errorf("unknown chaincodeType: %s", ccType)
221234
}

core/container/dockercontroller/dockercontroller_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestIntegrationPath(t *testing.T) {
6363
DockerVM: &dc,
6464
}, instance)
6565

66-
err = dc.Start(ccid, "NODE", &ccintf.PeerConnection{
66+
err = dc.Start(ccid, "GOLANG", &ccintf.PeerConnection{
6767
Address: "peer-address",
6868
})
6969
require.NoError(t, err)
@@ -72,6 +72,16 @@ func TestIntegrationPath(t *testing.T) {
7272
require.NoError(t, err)
7373
}
7474

75+
var expectedNodeStartScript = `
76+
set -e
77+
if [ -x /chaincode/start.sh ]; then
78+
/chaincode/start.sh --peer.address peer-address
79+
else
80+
cd /usr/local/src
81+
npm start -- --peer.address peer-address
82+
fi
83+
`
84+
7585
func TestGetArgs(t *testing.T) {
7686
tests := []struct {
7787
name string
@@ -81,7 +91,7 @@ func TestGetArgs(t *testing.T) {
8191
}{
8292
{"golang-chaincode", pb.ChaincodeSpec_GOLANG, []string{"chaincode", "-peer.address=peer-address"}, ""},
8393
{"java-chaincode", pb.ChaincodeSpec_JAVA, []string{"/root/chaincode-java/start", "--peerAddress", "peer-address"}, ""},
84-
{"node-chaincode", pb.ChaincodeSpec_NODE, []string{"/bin/sh", "-c", "cd /usr/local/src; npm start -- --peer.address peer-address"}, ""},
94+
{"node-chaincode", pb.ChaincodeSpec_NODE, []string{"/bin/sh", "-c", expectedNodeStartScript}, ""},
8595
{"unknown-chaincode", pb.ChaincodeSpec_Type(999), []string{}, "unknown chaincodeType: 999"},
8696
}
8797
for _, tc := range tests {
@@ -491,6 +501,7 @@ type InMemBuilder struct{}
491501
func (imb InMemBuilder) Build() (io.Reader, error) {
492502
buf := &bytes.Buffer{}
493503
fmt.Fprintln(buf, "FROM busybox:latest")
504+
fmt.Fprintln(buf, `RUN ln -s /bin/true /bin/chaincode`)
494505
fmt.Fprintln(buf, `CMD ["tail", "-f", "/dev/null"]`)
495506

496507
startTime := time.Now()

0 commit comments

Comments
 (0)