Permalink
Please sign in to comment.
Showing
with
174 additions
and 8 deletions.
- +1 −4 .gitignore
- +2 −1 package.json
- +92 −0 scripts/build-deb
- +8 −0 scripts/debian/control
- +37 −0 scripts/debian/postinst
- +14 −0 scripts/debian/prerm
- +20 −0 scripts/debian/systemd.service
- +0 −3 etc/init/bitcored.conf → scripts/debian/upstart.conf
| @@ -5,7 +5,8 @@ | ||
| "author": "BitPay <[email protected]>", | ||
| "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", | ||
| @@ -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" |
| @@ -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 }} |
| @@ -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 |
| @@ -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 |
| @@ -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 |
0 comments on commit
f9e5121