From fb7e17c9663593b9551a5203539e16434b429ac6 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 15:38:41 +0100 Subject: [PATCH 01/14] Move restart to bottom of main Also rename to restart_jsbin as to not conflict with global restart command --- usr/local/bin/deploy | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index 9eed384..2076fe2 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -50,6 +50,11 @@ get_latest_tag() { curl -s https://api.github.com/repos/$repo/tags | json 0.name } +restart_jsbin() { + echo ">>> restarting..." + sudo restart jsbin +} + main() { local cwd=$(pwd) local args="$@" @@ -65,14 +70,10 @@ main() { cd $cwd symlink_repo_to_name "$project_name-$tag" $project_name + restart_jsbin echo ">>> deploy complete" } -restart() { - echo ">>> restarting..." - sudo restart jsbin -} - if [ "$USER" != "www-data" ] then echo "*** launching deploy as www-data" @@ -80,8 +81,6 @@ then if [ $? -ne 0 ]; then echo "*** deploy failed" exit 1; - else - restart fi else main $ARGS From b079aae66295d3b3ecb0435fff975b15b593f524 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 15:40:39 +0100 Subject: [PATCH 02/14] Move user check into a function of its own We also don't call main from here anymore, this function will be called by main and if we need to switch users we rerun the script as correct user and exit when the sub process returns --- usr/local/bin/deploy | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index 2076fe2..1dcd244 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -50,6 +50,21 @@ get_latest_tag() { curl -s https://api.github.com/repos/$repo/tags | json 0.name } +check_user() { + 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 + else + exit 0 + fi + fi +} + restart_jsbin() { echo ">>> restarting..." sudo restart jsbin @@ -74,15 +89,3 @@ main() { echo ">>> deploy complete" } -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; - fi -else - main $ARGS - echo "*** exiting from www-data" -fi From b8f6c3d842c0e461fe63d84754f71896da90cbed Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 15:41:20 +0100 Subject: [PATCH 03/14] Exit from main with 0 if everything goes okay We also call main at the bottom of the file with the ARGS global --- usr/local/bin/deploy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index 1dcd244..c11a7cd 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -87,5 +87,7 @@ main() { symlink_repo_to_name "$project_name-$tag" $project_name restart_jsbin echo ">>> deploy complete" + exit 0 } +main $ARGS From e7e68effc6425b1ba9a77511848deacdf75507b5 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 15:41:39 +0100 Subject: [PATCH 04/14] Remove whitespace --- usr/local/bin/deploy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index c11a7cd..147ac1b 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -73,7 +73,7 @@ restart_jsbin() { main() { 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 From c20aa7716a4ed183205b19ca9c4ebee18e4ec20b Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 15:44:21 +0100 Subject: [PATCH 05/14] Call check_user() at top of main() --- usr/local/bin/deploy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index 147ac1b..9c39aa1 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -71,6 +71,8 @@ restart_jsbin() { } main() { + check_user + local cwd=$(pwd) local args="$@" From b2f62c55641e56f0489ee39a5af81fae0758c080 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 16:00:05 +0100 Subject: [PATCH 06/14] Remove check_user() --- usr/local/bin/deploy | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index 9c39aa1..b526fbc 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -50,21 +50,6 @@ get_latest_tag() { curl -s https://api.github.com/repos/$repo/tags | json 0.name } -check_user() { - 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 - else - exit 0 - fi - fi -} - restart_jsbin() { echo ">>> restarting..." sudo restart jsbin From 173144a254feb79763814a992d116e752c6c9f14 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 16:00:41 +0100 Subject: [PATCH 07/14] main() -> update_repo_and_symlinks() --- usr/local/bin/deploy | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index b526fbc..c8ca837 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -55,9 +55,7 @@ restart_jsbin() { sudo restart jsbin } -main() { - check_user - +update_repo_and_symlinks() { local cwd=$(pwd) local args="$@" @@ -72,8 +70,6 @@ main() { cd $cwd symlink_repo_to_name "$project_name-$tag" $project_name - restart_jsbin - echo ">>> deploy complete" exit 0 } From 2dcdd7d10836cfe28c02b034e603ca0e4d6da831 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 16:01:18 +0100 Subject: [PATCH 08/14] Move user switching logic into main Move the spawning of sub process into new function run_as_www() --- usr/local/bin/deploy | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index c8ca837..d1949f6 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -73,4 +73,26 @@ update_repo_and_symlinks() { exit 0 } +run_as_www() { + 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 + fi +} + +main() { + if [ "$USER" != "www-data" ] + then + run_as_www + restart_jsbin + echo ">>> deploy complete" + exit 0 + else + update_repo_and_symlinks + fi +} + main $ARGS From 4df797cc6049e92765d8cd4e7aeec47cc938a6d5 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Aug 2014 16:04:05 +0100 Subject: [PATCH 09/14] Move positive exit code to main() --- usr/local/bin/deploy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index d1949f6..49778f3 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -70,7 +70,6 @@ update_repo_and_symlinks() { cd $cwd symlink_repo_to_name "$project_name-$tag" $project_name - exit 0 } run_as_www() { @@ -92,6 +91,7 @@ main() { exit 0 else update_repo_and_symlinks + exit 0 fi } From 6f7a38a77780f66b7190708b5475d9b3d4bf6bcc Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Fri, 15 Aug 2014 14:21:11 +0100 Subject: [PATCH 10/14] Move deplot user into variable --- usr/local/bin/deploy | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index 49778f3..c549815 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -1,5 +1,6 @@ #!/bin/bash readonly ARGS="$@" +readonly DEPLOY_USER="www-data" clone_repo_at_tag() { local repo="$1" @@ -72,9 +73,9 @@ update_repo_and_symlinks() { symlink_repo_to_name "$project_name-$tag" $project_name } -run_as_www() { - echo "*** launching deploy as www-data" - sudo -u www-data -- sh -c "/usr/local/bin/deploy $ARGS" +run_as_deploy_user() { + echo "*** launching deploy as $DEPLOY_USER" + sudo -u "$DEPLOY_USER" -- sh -c "$0 $ARGS" if [ $? -ne 0 ] then echo "*** deploy failed" @@ -83,9 +84,9 @@ run_as_www() { } main() { - if [ "$USER" != "www-data" ] + if [ "$USER" != $DEPLOY_USER ] then - run_as_www + run_as_deploy_user restart_jsbin echo ">>> deploy complete" exit 0 From 688ebae832f352fb8b8cce19ea3f340655d512aa Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Fri, 15 Aug 2014 14:21:38 +0100 Subject: [PATCH 11/14] Pass to update_repo_and_symlinks() --- usr/local/bin/deploy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index c549815..a3c5405 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -91,7 +91,7 @@ main() { echo ">>> deploy complete" exit 0 else - update_repo_and_symlinks + update_repo_and_symlinks $ARGS exit 0 fi } From 0bbe0d97ac1b9833920b49dc2e1d64a393bc58fd Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Fri, 15 Aug 2014 14:22:09 +0100 Subject: [PATCH 12/14] Log more info and exit on errors --- usr/local/bin/deploy | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index a3c5405..5109c45 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -10,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() { @@ -27,6 +38,12 @@ 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 @@ -34,8 +51,20 @@ build() { npm install &> /dev/null fi + if [ $? -ne 0 ] + then + echo "*** npm install failed" + exit 1 + fi + echo ">>> running build..." grunt build &> /dev/null + + if [ $? -ne 0 ] + then + echo "*** grunt build failed" + exit 1 + fi } symlink_repo_to_name() { From f9589df93aca222280800f6824b25183d4eebf31 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Fri, 15 Aug 2014 14:22:24 +0100 Subject: [PATCH 13/14] Add full path to symlink command --- usr/local/bin/deploy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index 5109c45..3a29658 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -72,7 +72,7 @@ symlink_repo_to_name() { local project_name="$2" echo ">>> symlinking $repodir" - ln -nfs $repodir $project_name + ln -nfs /WWW/$repodir /WWW/$project_name } get_latest_tag() { From 0e99b1e118afdc4db6c9e5c188e58605ad3c439d Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Tue, 19 Aug 2014 13:07:56 +0100 Subject: [PATCH 14/14] Fixed npm install under www-data By using `sudo -H` which reads from the /var/www directory as $HOME, just make sure it has the right perms. --- usr/local/bin/deploy | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/usr/local/bin/deploy b/usr/local/bin/deploy index 3a29658..7a48c94 100755 --- a/usr/local/bin/deploy +++ b/usr/local/bin/deploy @@ -49,14 +49,15 @@ build() { else echo ">>> updating npm modules" npm install &> /dev/null - fi - if [ $? -ne 0 ] - then - echo "*** npm install failed" - exit 1 + if [ $? -ne 0 ] + then + echo "*** npm install failed" + exit 1 + fi fi + echo ">>> running build..." grunt build &> /dev/null @@ -104,7 +105,7 @@ update_repo_and_symlinks() { run_as_deploy_user() { echo "*** launching deploy as $DEPLOY_USER" - sudo -u "$DEPLOY_USER" -- sh -c "$0 $ARGS" + sudo -Hu "$DEPLOY_USER" -- sh -c "$0 $ARGS" if [ $? -ne 0 ] then echo "*** deploy failed"