|
1 | 1 | #!/bin/bash
|
2 |
| -# __ |
3 |
| -# _____ ____ _/ |_ ____ ____ _______ |
4 |
| -# / \ _/ __ \ \ __\_/ __ \ / _ \ \_ __ \ |
5 |
| -# | Y Y \\ ___/ | | \ ___/ ( <_> ) | | \/ |
6 |
| -# |__|_| / \___ > |__| \___ > \____/ |__| |
7 |
| -# \/ \/ \/ |
| 2 | + |
| 3 | + |
| 4 | +################################################################################ |
8 | 5 | #
|
9 |
| -# .___ |
10 |
| -# __| _/ __ __ _____ ______ |
11 |
| -# / __ | | | \ / \ \____ \ |
12 |
| -# / /_/ | | | /| Y Y \| |_> > |
13 |
| -# \____ | |____/ |__|_| /| __/ |
14 |
| -# \/ \/ |__| |
| 6 | +# Name : Meteor app deployer |
| 7 | +# Version : 1.0.0 |
| 8 | +# Last edit : 22/02/2015 |
| 9 | +# Description : Building and deploying a meteor app onto a unix server |
| 10 | +# through SSH |
| 11 | +# Author(s) : Grabcode (@grabthecode) |
15 | 12 | #
|
| 13 | +# Exit codes : |
| 14 | +# 0 : No error |
| 15 | +# 1 : Missing mandatory parameter |
| 16 | +# |
| 17 | +################################################################################ |
| 18 | + |
| 19 | +### Functions declaration |
16 | 20 |
|
17 |
| -METEOR_DOMAIN="$1" |
18 |
| -DUMP_DIR="${2:-dump}" |
| 21 | +# Usage info |
| 22 | +show_help() { |
| 23 | +cat << EOF |
| 24 | +Usage: ${0##*/} --ssh username@hostname --app app_name [-v] [-r remote_user] |
| 25 | +Deploy meteor app to given host. |
| 26 | + -a | --app meteor application name |
| 27 | + -s | --ssh remote server ssh connection details |
| 28 | + -r remote user name launching the app |
| 29 | + -u remote upstart service managing the meteor app |
| 30 | + -h | --help pring usage |
| 31 | + -v verbose mode |
| 32 | +EOF |
| 33 | +} |
19 | 34 |
|
20 |
| -if [[ "$METEOR_DOMAIN" == "" ]] |
21 |
| -then |
22 |
| - echo "You need to supply your meteor app name and the path to the dump dir" |
23 |
| - echo "e.g. ./meteor-deploy.sh app <path>" |
| 35 | +if [ $# -eq 0 ]; then |
| 36 | + show_help |
24 | 37 | exit 1
|
25 | 38 | fi
|
26 | 39 |
|
27 |
| -./meteor-dump.sh "$1" "$DUMP_DIR" |
28 |
| -if [[ "$?" == "1" ]] |
29 |
| -then |
30 |
| - echo "meteor-dump.sh failed. Please try to execute it directly." |
31 |
| - echo "e.g. ./meteor-dump.sh $METEOR_DOMAIN $DUMP_DIR" |
| 40 | +# Log helper |
| 41 | +log() { |
| 42 | + echo "`date` :: $1" >&2 |
| 43 | +} |
| 44 | + |
| 45 | +# Param parsing helper |
| 46 | +process_arg (){ |
| 47 | + if [ "$1" != '' ]; then |
| 48 | + echo $1 |
| 49 | + else |
| 50 | + log 'ERROR: Must specify a non-empty argument.' |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +# Parsing parameters |
| 56 | +while [ "$#" -gt 0 ]; do |
| 57 | + case $1 in |
| 58 | + -h|-\?|--help) # Call a "show_help" function to display a synopsis, then exit. |
| 59 | + show_help |
| 60 | + exit |
| 61 | + ;; |
| 62 | + -a|--app) |
| 63 | + APP=$(process_arg $2) |
| 64 | + if [ $? -ne 0 ]; then |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + shift 2 |
| 68 | + continue |
| 69 | + ;; |
| 70 | + -s|--ssh) |
| 71 | + HOST=$(process_arg $2) |
| 72 | + if [ $? -ne 0 ]; then |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + shift 2 |
| 76 | + continue |
| 77 | + ;; |
| 78 | + -r) |
| 79 | + REMOTE_USER=$(process_arg $2) |
| 80 | + if [ $? -ne 0 ]; then |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + shift 2 |
| 84 | + continue |
| 85 | + ;; |
| 86 | + -u) |
| 87 | + REMOTE_SERVICE=$(process_arg $2) |
| 88 | + if [ $? -ne 0 ]; then |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | + shift 2 |
| 92 | + continue |
| 93 | + ;; |
| 94 | + -v) |
| 95 | + VERBOSE="${i#*=}" |
| 96 | + ;; |
| 97 | + --) # End of all options. |
| 98 | + shift |
| 99 | + break |
| 100 | + ;; |
| 101 | + -?*) |
| 102 | + log 'WARN: Unknown option (ignored): %s\n' "$1" >&2 |
| 103 | + ;; |
| 104 | + *) # Default case: If no more options then break out of the loop. |
| 105 | + break |
| 106 | + esac |
| 107 | + |
| 108 | + shift |
| 109 | +done |
| 110 | + |
| 111 | +# Check mandatory parameters |
| 112 | +if [ ! "$APP" ] || [ ! "$HOST" ]; then |
| 113 | + log 'ERROR: missing mandatory parameter. See --help.' >&2 |
32 | 114 | exit 1
|
33 | 115 | fi
|
34 | 116 |
|
35 |
| -meteor deploy "$1" |
| 117 | +APP_TAR="$APP".tar.gz |
36 | 118 |
|
37 |
| -./meteor-restore.sh "$1" "$DUMP_DIR" |
38 |
| -if [[ "$?" == "1" ]] |
39 |
| -then |
40 |
| - echo "meteor-restore.sh failed. Please try to execute it directly." |
41 |
| - echo "e.g. ./meteor-restore.sh $METEOR_DOMAIN $DUMP_DIR" |
42 |
| - exit 1 |
| 119 | +#Default REMOTE_USER to APP |
| 120 | +if [ ! "$REMOTE_USER" ]; then |
| 121 | + REMOTE_USER="$APP" |
| 122 | +fi |
| 123 | +REMOTE_USERHOME=/home/"$REMOTE_USER" |
| 124 | + |
| 125 | +#Default REMOTE_SERVICE to APP |
| 126 | +if [ ! "$REMOTE_SERVICE" ]; then |
| 127 | + REMOTE_SERVICE="$APP" |
43 | 128 | fi
|
44 | 129 |
|
| 130 | + |
| 131 | +### Variables definition |
| 132 | +log " *** Settings ***" |
| 133 | +log "APP [$APP]" |
| 134 | +log "HOST [$HOST]" |
| 135 | +log "REMOTE_USER [$REMOTE_USER]" |
| 136 | +log "REMOTE_USERHOME [$REMOTE_USERHOME]" |
| 137 | +log "REMOTE_SERVICE [$REMOTE_SERVICE]" |
| 138 | + |
| 139 | + |
| 140 | +#todo: warning if branch is not master, if git detected |
| 141 | +log " *** STARTING DEPLOYMENT ***" |
| 142 | + |
| 143 | +# todo: add git tags |
| 144 | +#Valid architectures include os.osx.x86_64, os.linux.x86_64, and os.linux.x86_32. |
| 145 | +log "Building $APP" |
| 146 | +meteor build . --architecture os.linux.x86_64 #todo: get architecture based on uname |
| 147 | +log "Uploading $APP_TAR to $HOST" |
| 148 | +scp $APP_TAR $HOST:~ |
| 149 | +log "Removing $APP_TAR" |
| 150 | +rm $APP_TAR |
| 151 | + |
| 152 | + |
| 153 | +### Remote commands - assumptions: upstart service named "$APP" |
| 154 | +log "Connection to $HOST" |
| 155 | +ssh -T $HOST <<EOF |
| 156 | +$(typeset -f) #getting log defined |
| 157 | +
|
| 158 | +log "Moving $APP_TAR to $REMOTE_USERHOME" |
| 159 | +sudo mv $APP_TAR $REMOTE_USERHOME |
| 160 | +
|
| 161 | +log "Transferring permissions to $REMOTE_USER" |
| 162 | +sudo chown $REMOTE_USER:$REMOTE_USER $REMOTE_USERHOME -R |
| 163 | +
|
| 164 | +log "Log in as $REMOTE_USER" |
| 165 | +sudo su $REMOTE_USER |
| 166 | +
|
| 167 | +$(typeset -f) #getting log defined |
| 168 | +
|
| 169 | +log "Unpacking $APP_TAR" |
| 170 | +cd $REMOTE_USERHOME |
| 171 | +tar -zxf $APP_TAR |
| 172 | +rm $APP_TAR |
| 173 | +
|
| 174 | +log "Installing dependancies" |
| 175 | +cd bundle/programs/server/ |
| 176 | +npm install |
| 177 | +
|
| 178 | +log "Logout from $REMOTE_USER" |
| 179 | +exit |
| 180 | +
|
| 181 | +log "Restarting upstart service $APP" |
| 182 | +sudo service $APP restart |
| 183 | +exit |
| 184 | +
|
| 185 | +EOF |
| 186 | +log "Closing ssh connection" |
| 187 | + |
| 188 | +log " *** DEPLOYMENT OVER ***" |
45 | 189 | exit 0
|
0 commit comments