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

Sunzi run #22

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/templates/create/recipes/sunzi.sh
Expand Up @@ -45,3 +45,38 @@ function sunzi.install() {
return 0
fi
}

# Simple idempotent solution for running bash functions.
#
# Example:
#
# function setup_nodejs() {
# git clone https://github.com/joyent/node.git /opt/node
# # do stuff...
# }
#
# # will only run the function once
# # regardless of how many times sunzi deploy is executed
# sunzi.run "setup_nodejs"
#
# # force to run every time
# sunzi.run "setup_nodejs" force
#
function sunzi.run() {
completed_dir=/etc/sunzi_run
if [ ! -d $completed_dir ]; then
mkdir $completed_dir
fi

completed_file="$completed_dir/$1"

if [ "$2" == "force" ]; then
rm -fv "$completed_file"
fi

if [ ! -f "$completed_file" ]; then
$1
touch "$completed_file"
fi
}