Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for hooks template, to implement custom lifecycle commands without overriding complete templates #76

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions node-deb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ while [ -n "$1" ]; do
template_executable="$value"
shift
;;
--template-hooks)
# HELPDOC: Override hooks template (default: 'node_deb.templates.hooks' from package.json then built-in)
zero_check "$value" "$param"
template_executable="$value"
shift
;;
--template-postinst)
# HELPDOC: Override maintainer script postinst template (default: 'node_deb.templates.postinst' from package.json then built-in)
zero_check "$value" "$param"
Expand Down Expand Up @@ -521,6 +527,16 @@ fi
: ${template_executable:="$node_deb_dir/templates/executable"}
log_debug "The executable template has been set to: $template_executable"

# Set hooks template
if [ -z "$template_hooks" ]; then
template_hooks=$(jq -r '.node_deb.templates.hooks' package.json)
if [[ "$template_hooks" == 'null' ]]; then
template_hooks=''
fi
fi
: ${template_hooks:="$node_deb_dir/templates/hooks"}
log_debug "The hooks template has been set to: $template_hooks"

# Set postinst template
if [ -z "$template_postinst" ]; then
template_postinst=$(jq -r '.node_deb.templates.postinst' package.json)
Expand Down Expand Up @@ -611,6 +627,7 @@ log_debug "The CLI entrypoint has been set to: $cli_entrypoint"


deb_dir="${package_name}_${package_version}_${architecture}"
hooks_path="usr/share/$package_name/scripts/hooks"

finish() {
if [ $no_delete_temp -ne 1 ]; then
Expand Down Expand Up @@ -693,6 +710,7 @@ replace_vars() {
-e "s/{{ node_deb_group }}/$(escape "$group")/g" \
-e "s/{{ node_deb_init }}/$(escape "$init")/g" \
-e "s/{{ node_deb_no_rebuild }}/$(escape "$no_rebuild")/g" \
-e "s/{{ node_deb_hooks }}/$(escape "/$hooks_path")/g" \
-e "s/{{ node_deb_version }}/$(escape "$node_deb_version")/g" \
-e "s/{{ install_strategy }}/$(escape "$install_strategy")/g" \
> "$target_file"
Expand All @@ -706,6 +724,7 @@ replace_vars "$template_postinst" "$deb_dir/DEBIAN/postinst" '0755'
replace_vars "$template_postrm" "$deb_dir/DEBIAN/postrm" '0755'
replace_vars "$template_prerm" "$deb_dir/DEBIAN/prerm" '0755'
replace_vars "$template_executable" "$deb_dir/usr/share/$package_name/bin/$executable_name" '0755'
replace_vars "$template_hooks" "$deb_dir/$hooks_path" '0755'
replace_vars "$template_default_variables" "$deb_dir/etc/default/$package_name" '0644'

if [ "$init" == 'auto' ] || [ "$init" == 'upstart' ]; then
Expand Down
13 changes: 13 additions & 0 deletions templates/hooks
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
# This file was autogenerated by node-deb {{ node_deb_version }}
set -e
set -o pipefail

case "$1" in
postinst)
;;
prerm)
;;
postrm)
;;
esac
2 changes: 2 additions & 0 deletions templates/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ dependency_install '{{ node_deb_package_name }}'
mkdir -p '/var/log/{{ node_deb_package_name }}'
chown -R '{{ node_deb_user }}:{{ node_deb_group }}' '/var/log/{{ node_deb_package_name }}'

{{node_deb_hooks}} "postinst $@"

if [[ "$init_type" != 'none' ]]; then
start_service '{{ node_deb_package_name }}'
fi
4 changes: 3 additions & 1 deletion templates/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
set -e
set -o pipefail

{{node_deb_hooks}} "postrm $@"

# Will return true even if deletion fails
delete_user() {
: "${1:?'User was not defined'}"
Expand Down Expand Up @@ -40,6 +42,6 @@ case "$1" in
rm -rf '/var/log/{{ node_deb_package_name }}'
;;
*)
echo "postinst called with unknown argument '$1'"
echo "postrm called with unknown argument '$1'"
;;
esac
2 changes: 2 additions & 0 deletions templates/prerm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
set -e
set -o pipefail

{{node_deb_hooks}} "prerm $@"

declare -r init_type='{{ node_deb_init }}'
declare -r service_name='{{ node_deb_package_name }}'

Expand Down