Skip to content
Open
78 changes: 59 additions & 19 deletions usr/local/bin/deploy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
readonly ARGS="$@"
readonly DEPLOY_USER="www-data"

clone_repo_at_tag() {
local repo="$1"
Expand All @@ -9,12 +10,23 @@ clone_repo_at_tag() {

echo ">>> cloning $tag..."
git clone --depth 1 -b $tag https://github.com/$repo.git $repo_dirname &> /dev/null
if [ $? -ne 0 ]
then
echo "*** clone failed"
exit 1
fi
build $repo_dirname
}

diff_deps() {
echo ">>> diffing dependencies"
local repo_dirname="$1"
diff -B <(cat jsbin/package.json | json dependencies) <(cat $repo_dirname/package.json | json dependencies)
if [ $? -ne 0 ]
then
echo "*** package diff failed"
exit 1
fi
}

build() {
Expand All @@ -26,34 +38,58 @@ build() {
echo ">>> copying node modules from v$(json -f ../jsbin/package.json version)..."
cp -R ../jsbin/node_modules .

if [ $? -ne 0 ]
then
echo "*** copy previous version of modules failed"
exit 1
fi

if [ -z "$d" ]; then
echo ">>> no update required"
else
echo ">>> updating npm modules"
npm install &> /dev/null

if [ $? -ne 0 ]
then
echo "*** npm install failed"
exit 1
fi
fi


echo ">>> running build..."
grunt build &> /dev/null

if [ $? -ne 0 ]
then
echo "*** grunt build failed"
exit 1
fi
}

symlink_repo_to_name() {
local repodir="$1"
local project_name="$2"

echo ">>> symlinking $repodir"
ln -nfs $repodir $project_name
ln -nfs /WWW/$repodir /WWW/$project_name
}

get_latest_tag() {
local repo="$1"
curl -s https://api.github.com/repos/$repo/tags | json 0.name
}

main() {
restart_jsbin() {
echo ">>> restarting..."
sudo restart jsbin
}

update_repo_and_symlinks() {
local cwd=$(pwd)
local args="$@"

cd /WWW/
local current_version=$(json -f jsbin/package.json version)
# read the arg first, or get it from the environment value
Expand All @@ -65,25 +101,29 @@ main() {
cd $cwd

symlink_repo_to_name "$project_name-$tag" $project_name
echo ">>> deploy complete"
}

restart() {
echo ">>> restarting..."
sudo restart jsbin
run_as_deploy_user() {
echo "*** launching deploy as $DEPLOY_USER"
sudo -Hu "$DEPLOY_USER" -- sh -c "$0 $ARGS"
if [ $? -ne 0 ]
then
echo "*** deploy failed"
exit 1
fi
}

if [ "$USER" != "www-data" ]
then
echo "*** launching deploy as www-data"
sudo -u www-data -- sh -c "/usr/local/bin/deploy $ARGS"
if [ $? -ne 0 ]; then
echo "*** deploy failed"
exit 1;
main() {
if [ "$USER" != $DEPLOY_USER ]
then
run_as_deploy_user
restart_jsbin
echo ">>> deploy complete"
exit 0
else
restart
update_repo_and_symlinks $ARGS
exit 0
fi
else
main $ARGS
echo "*** exiting from www-data"
fi
}

main $ARGS