From cdf3a73c7d0b33002db7919a897d076f99a678f2 Mon Sep 17 00:00:00 2001 From: Ravin Perera <33562092+ravinsp@users.noreply.github.com> Date: Thu, 22 Jul 2021 12:38:02 +0530 Subject: [PATCH] Added ncc build to nodejs contracts. (#334) --- dev-setup.sh | 8 +++++++- examples/nodejs_contract/.gitignore | 3 ++- examples/nodejs_contract/package.json | 4 ++++ test/local-cluster/cluster-create.sh | 13 +++++++++---- test/vm-cluster/cluster.sh | 11 +++++++---- test/vm-cluster/setup-hp.sh | 16 +++------------- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/dev-setup.sh b/dev-setup.sh index a03f3434..4a40ca3e 100755 --- a/dev-setup.sh +++ b/dev-setup.sh @@ -64,7 +64,7 @@ cmake .. sudo make install popd > /dev/null 2>&1 popd > /dev/null 2>&1 -rm v1.0.3.tar.gz && rm -r readerwriterqueue-1.0.3 +rm v1.0.3.tar.gz && sudo rm -r readerwriterqueue-1.0.3 # Concurrent queue wget https://github.com/cameron314/concurrentqueue/archive/1.0.2.tar.gz @@ -92,6 +92,12 @@ sudo apt-get install -y \ # jq command (needed for remote cluster scripts) sudo apt-get install -y jq +# NodeJs +curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash - +sudo apt-get install -y nodejs +# ncc build utility for nodejs compiled builds. +npm i -g @vercel/ncc + # Update linker library cache. sudo ldconfig diff --git a/examples/nodejs_contract/.gitignore b/examples/nodejs_contract/.gitignore index b512c09d..76add878 100644 --- a/examples/nodejs_contract/.gitignore +++ b/examples/nodejs_contract/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +dist \ No newline at end of file diff --git a/examples/nodejs_contract/package.json b/examples/nodejs_contract/package.json index 6c7801db..22af9324 100644 --- a/examples/nodejs_contract/package.json +++ b/examples/nodejs_contract/package.json @@ -1,4 +1,8 @@ { + "scripts": { + "build-echo": "ncc build echo_contract.js -o dist/echo-contract", + "build-file": "ncc build file_contract.js -o dist/file-contract" + }, "dependencies": { "bson": "4.0.4" } diff --git a/test/local-cluster/cluster-create.sh b/test/local-cluster/cluster-create.sh index 53e662bb..834a185b 100755 --- a/test/local-cluster/cluster-create.sh +++ b/test/local-cluster/cluster-create.sh @@ -31,16 +31,21 @@ elif [ "$CONTRACT" = "nodefile" ]; then # nodejs file contract (uses BSON protoc echo "Using nodejs file contract." pushd $hpcore/examples/nodejs_contract/ > /dev/null 2>&1 npm install + npm run build-file popd > /dev/null 2>&1 - copyfiles="$hpcore/examples/nodejs_contract/{node_modules,package.json,hp-contract-lib.js,file_contract.js}" + copyfiles="$hpcore/examples/nodejs_contract/dist/file-contract/index.js" binary="/usr/bin/node" - binargs="file_contract.js" + binargs="index.js" else # nodejs echo contract (default) echo "Using nodejs echo contract." - copyfiles="$hpcore/examples/nodejs_contract/{package.json,hp-contract-lib.js,echo_contract.js}" + pushd $hpcore/examples/nodejs_contract/ > /dev/null 2>&1 + npm install + npm run build-echo + popd > /dev/null 2>&1 + copyfiles="$hpcore/examples/nodejs_contract/dist/echo-contract/index.js" binary="/usr/bin/node" - binargs="echo_contract.js" + binargs="index.js" fi if [ "$loglevel" = "" ]; then diff --git a/test/vm-cluster/cluster.sh b/test/vm-cluster/cluster.sh index ba458008..fca84925 100755 --- a/test/vm-cluster/cluster.sh +++ b/test/vm-cluster/cluster.sh @@ -337,12 +337,15 @@ if [ $mode = "new" ] || [ $mode = "updatebin" ]; then strip $hpcore/build/hpcore strip $hpcore/build/appbill cp $hpcore/build/{hpcore,hpfs,hpws} $hpfiles/bin/ - cp $hpcore/examples/nodejs_contract/{package.json,echo_contract.js,hp-contract-lib.js} \ - $hpfiles/nodejs_contract/ + pushd $hpcore/examples/nodejs_contract/ > /dev/null 2>&1 + npm install + npm run build-echo + popd > /dev/null 2>&1 + cp $hpcore/examples/nodejs_contract/dist/echo-contract/index.js $hpfiles/nodejs_contract/ fi if [ $mode = "new" ]; then - cp ../bin/{libfuse3.so.3,libblake3.so,fusermount3,hpws,hpfs} $hpfiles/bin/ + cp ../bin/{libblake3.so,hpws,hpfs} $hpfiles/bin/ cp ./setup-hp.sh $hpfiles/ fi @@ -438,7 +441,7 @@ if [ $mode = "new" ] || [ $mode = "reconfig" ]; then # Merge json contents to produce final config. echo "$(cat ./cfg/node$n.cfg)" \ - '{"contract": {"id": "3c349abe-4d70-4f50-9fa6-018f1f2530ab", "bin_path": "/usr/bin/node", "bin_args": "'$basedir'/'$hpfiles'/nodejs_contract/echo_contract.js", "unl": '${myunl}'}}'\ + '{"contract": {"id": "3c349abe-4d70-4f50-9fa6-018f1f2530ab", "bin_path": "/usr/bin/node", "bin_args": "'$basedir'/'$hpfiles'/nodejs_contract/index.js", "unl": '${myunl}'}}'\ '{"mesh": {"known_peers": '${mypeers}'}}'\ $contconfig \ | jq --slurp 'reduce .[] as $item ({}; . * $item)' > ./cfg/node$n-merged.cfg diff --git a/test/vm-cluster/setup-hp.sh b/test/vm-cluster/setup-hp.sh index f9f3fcfa..cdcd23a8 100755 --- a/test/vm-cluster/setup-hp.sh +++ b/test/vm-cluster/setup-hp.sh @@ -15,10 +15,6 @@ then sudo swapon /swapfile fi -# Getting updates if any -echo "Checking for updates..." -sudo apt-get update - if [ -x "$(command -v node)" ]; then echo "NodeJs already installed." else @@ -29,14 +25,13 @@ else sudo apt-get install -y nodejs fi -if [ -x "$(command -v fusermount3)" ]; then +if [ -x "$(command -v fusermount)" ]; then echo "FUSE already installed." else echo "Installing FUSE and other shared libraries..." - sudo apt-get -y install libgomp1 libssl-dev - sudo cp $basedir/$hpfiles/bin/{libfuse3.so.3,libblake3.so} /usr/local/lib/ + sudo apt-get -y install libgomp1 libssl-dev fuse3 + sudo cp $basedir/$hpfiles/bin/libblake3.so /usr/local/lib/ sudo ldconfig - sudo cp $basedir/$hpfiles/bin/fusermount3 /usr/local/bin/ fi if [ -x "$(command -v sqlite3)" ]; then @@ -59,11 +54,6 @@ if [ -f $basedir/$hpfiles/ssl/tlscert.pem ]; then fi if [ $mode = "new" ] || [ $mode = "reconfig" ]; then - # npm install to support nodejs contract - pushd $basedir/$hpfiles/nodejs_contract > /dev/null 2>&1 - npm install - popd > /dev/null 2>&1 - # Create getpid script (gets process ids belonging to this contract dir) echo "pids=\$(pidof \$*) && [ ! -z \"\$pids\" ] && ps -fp \$pids | grep -w $contdir | awk '{print \$2}' | tr '\n' ' '" > $contdir/getpid.sh sudo chmod +x $contdir/getpid.sh