diff --git a/.gitignore b/.gitignore index efe72ac..61b58ad 100644 --- a/.gitignore +++ b/.gitignore @@ -15,9 +15,6 @@ bower_components report .DS_Store +build -bitcore.js -bitcore.min.js -bitcore.js.sig -bitcore.min.js.sig tests.js diff --git a/package.json b/package.json index df833fc..cb44232 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "author": "BitPay ", "main": "index.js", "scripts": { - "test": "./node_modules/.bin/mocha test/** --recursive" + "test": "./node_modules/.bin/mocha test/** --recursive", + "build-deb": "./scripts/build-deb" }, "bin": { "bitcore": "./bin/bitcore", diff --git a/scripts/build-deb b/scripts/build-deb new file mode 100755 index 0000000..8f203b4 --- /dev/null +++ b/scripts/build-deb @@ -0,0 +1,92 @@ +#!/bin/bash +set -e +set -o pipefail + +log_title() { + local code="\033[" + local color="${code}1;34m" + [ -z "$text" ] && local text="$color$1${code}0m" + echo -e "\n$text" +} + +root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/.." + +package_arch="amd64" +package_version=$(jq -r ".version" "${root_dir}/package.json") +package_maintainer=$(jq -r ".author" "${root_dir}/package.json") +package_description=$(jq -r ".description" "${root_dir}/package.json") + +deb_dir="${root_dir}/build/bitcore_${package_version}_${package_arch}" + +log_title "Making Debian package:\n" +echo -e " Name: bitcore" +echo -e " Version: ${package_version}" +echo -e " Maintainer: ${package_maintainer}" +echo -e " Description: ${package_description}" +echo -e "" + +if [ -e "$deb_dir" ]; then rm -rf "$deb_dir"; fi + +escape() { + sed -e 's/[]\/$*.^|[]/\\&/g' -e 's/&/\\&/g' <<< "$@" +} + +replace_vars() { + declare -r file="$1" + declare -r target_file="$2" + + sed < "$file" \ + -e "s/{{ deb_package_version }}/$(escape $package_version)/g" \ + -e "s/{{ deb_package_description }}/$(escape $package_description)/g" \ + -e "s/{{ deb_package_maintainer }}/$(escape $package_maintainer)/g" \ + -e "s/{{ deb_package_arch }}/$(escape $package_arch)/g" \ + > "$target_file" +} + +log_title "Setting up Debian package:" +mkdir -vp "$deb_dir/DEBIAN" \ + "$deb_dir/etc/bitcore" \ + "$deb_dir/usr/opt/bitcore" \ + "$deb_dir/usr/bin" + +mkdir -vp "$deb_dir/etc/init" +mkdir -vp "$deb_dir/etc/systemd/system" +replace_vars "${root_dir}/scripts/debian/control" "$deb_dir/DEBIAN/control" +replace_vars "${root_dir}/scripts/debian/postinst" "$deb_dir/DEBIAN/postinst" +replace_vars "${root_dir}/scripts/debian/prerm" "$deb_dir/DEBIAN/prerm" +replace_vars "${root_dir}/scripts/debian/upstart.conf" "$deb_dir/etc/init/bitcore.conf" +replace_vars "${root_dir}/scripts/debian/systemd.service" "$deb_dir/etc/systemd/system/bitcore.service" +chmod -vR 0755 "$deb_dir/DEBIAN/" + +log_title "Copying Bitcore" + +app_dir="$deb_dir/usr/opt/bitcore/" + +rsync -vr "${root_dir}/bin" "${app_dir}" +chmod -vR 0755 "${app_dir}/bin/bitcore" "${app_dir}/bin/bitcored" +cp -v "${root_dir}/package.json" "${app_dir}" +cp -v "${root_dir}/README.md" "${app_dir}" +cp -v "${root_dir}/index.js" "${app_dir}" +pushd "${deb_dir}/usr/bin" +ln -vs "../opt/bitcore/bin/bitcore" +ln -vs "../opt/bitcore/bin/bitcored" +popd + +log_title "Installing Bitcore Modules" +pushd "${app_dir}" +VERIFY_BITCOIN_DOWNLOAD=1 npm install --production +echo "Cleanup Node.js addon binaries before packaging:" +find "${app_dir}" -type f -name '*.node' -print -delete +find "${app_dir}" -type f -name '*.o' -print -delete +echo "Cleanup intermediate files:" +rm -v "${deb_dir}/usr/opt/bitcore/node_modules/bitcore-node/bin/bitcoin-0.12.0-linux64.tar.gz" +npm shrinkwrap --dev +popd + +log_title "Building Debian package" +dpkg-deb -Z gzip --verbose --build "$deb_dir" + +log_title "Signing Debian package" +dpkg-sig --sign builder "${deb_dir}.deb" + +echo -e "Success.\n" diff --git a/scripts/debian/control b/scripts/debian/control new file mode 100644 index 0000000..8ad6825 --- /dev/null +++ b/scripts/debian/control @@ -0,0 +1,8 @@ +Package: bitcore +Version: {{ deb_package_version }} +Section: base +Priority: optional +Architecture: {{ deb_package_arch }} +Depends: nodejs, nodejs-legacy, npm, build-essential, libzmq3-dev +Maintainer: {{ deb_package_maintainer }} +Description: {{ deb_package_description }} diff --git a/scripts/debian/postinst b/scripts/debian/postinst new file mode 100644 index 0000000..e1271c4 --- /dev/null +++ b/scripts/debian/postinst @@ -0,0 +1,37 @@ +#!/bin/bash +set -e +set -o pipefail + +# add group +if ! getent group | grep -q "^bitcore:" ; then + echo "Creating system group: bitcore" + groupadd --system bitcore +fi + +# add user +if ! getent passwd | grep -q "^bitcore:"; then + echo "Creating bitcore system user" + useradd --gid "bitcore" --system -m bitcore +fi + +# build nodejs addons +cd "/usr/opt/bitcore" +SKIP_BITCOIN_DOWNLOAD=1 npm rebuild + +# setup log directory +mkdir -p "/var/log/bitcore" +chown -R bitcore:bitcore "/var/log/bitcore" + +# start bitcore +if hash service 2> /dev/null; then + service bitcore start || echo "bitcore could not be registered or started" +elif hash start 2> /dev/null; then + start bitcore || echo "bitcore could not be registered or started" +elif hash systemctl 2> /dev/null; then + { + systemctl enable "bitcore.service" && \ + systemctl start "bitcore.service" + } || echo "bitcore could not be registered or started" +else + echo 'Your system does not appear to use upstart or systemd, so bitcore could not be started' +fi diff --git a/scripts/debian/prerm b/scripts/debian/prerm new file mode 100644 index 0000000..23bbee6 --- /dev/null +++ b/scripts/debian/prerm @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e +set -o pipefail + +if hash service 2> /dev/null; then + service bitcore stop || echo "bitcore wasn't running!" +elif hash stop 2> /dev/null; then + stop "$service_name" || echo "bitcore wasn't running!" +elif hash systemctl 2> /dev/null; then + systemctl disable "bitcore.service" || echo "bitcore wasn't running!" +else + echo "Your system does not appear to use upstart or systemd, so bitcore could not be stopped" +fi diff --git a/scripts/debian/systemd.service b/scripts/debian/systemd.service new file mode 100644 index 0000000..dc83ed6 --- /dev/null +++ b/scripts/debian/systemd.service @@ -0,0 +1,20 @@ +[Unit] +Description={{ deb_package_description }} +Requires=network.target + +[Service] +Type=simple +WorkingDirectory=/usr/opt/bitcore +ExecStart=/usr/opt/bitcore/bin/bitcored +ExecReload=/bin/kill -HUP $MAINPID +Restart=on-failure +RestartSec=15 +User=bitcore +ExecStartPre=/bin/mkdir -p /run/bitcore +ExecStartPre=/bin/chown bitcore:bitcore /run/bitcore +ExecStartPre=/bin/chmod 755 /run/bitcore +PermissionsStartOnly=true +TimeoutStopSec=300 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/etc/init/bitcored.conf b/scripts/debian/upstart.conf similarity index 70% rename from etc/init/bitcored.conf rename to scripts/debian/upstart.conf index 99adce8..657f0ab 100644 --- a/etc/init/bitcored.conf +++ b/scripts/debian/upstart.conf @@ -18,9 +18,6 @@ setgid bitcore # home dir of the bitcore daemon user env HOME=/home/bitcore -# path should include the correct version of node -env PATH=/home/bitcore/.nvm/versions/node/v0.12.7/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin - respawn respawn limit 5 15