usefulfiles/scripts/obrand_install.sh
2016-04-15 14:52:35 +01:00

185 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
NVM="$HOME/.nvm"
NVM_VERSION="5.7.0"
DEV="$HOME/dev"
LIVE="$HOME/live"
STAGING="$HOME/staging"
SERVER="O-BrandServer"
FRONT="O-Brand"
SWAP="/swapfile"
DBSETUP="obrand-admin-server/dbrebuild.sh"
PKG_MANAGER=$( command -v yum || command -v apt-get ) || echo "Neither yum nor apt-get found"
PKG_DEVICE=$(basename $PKG_MANAGER)
UPDATE=no
DATABASE=no
NGINX=no
fancy_echo() {
local fmt="$1"; shift
# shellcheck disable=SC2059
printf "\n$fmt\n" "$@"
}
for i in "$@"
do
case $i in
-u|--update)
UPDATE=YES
shift # past argument=value
;;
-d|--database)
DATABASE=YES
shift # past argument=value
;;
-n|--nginx)
NGINX=YES
shift # past argument=value
;;
--default)
DEFAULT=YES
shift # past argument with no value
;;
*)
# unknown option
;;
esac
done
fancy_echo "Installing Obrand Admin Server ..."
## Update initial packages
fancy_echo Cleaning...
if [ ! -d "$DEV" ]; then
mkdir -p $DEV
fi
if [ ! -d "$LIVE" ]; then
mkdir -p $LIVE
fi
if [ ! -d "$STAGING" ]; then
rm -rf $STAGING
mkdir -p $STAGING
else
mkdir -p $STAGING
fi
if [[ $UPDATE = "yes" ]]; then
if [ $(basename $PKG_MANAGER) = "yum" ]; then
sudo yum --assumeyes upgrade
sudo yum --assumeyes install git make automake gcc gcc-c++ kernel-devel nginx htop screen wget curl postgresql postgresql-contrib
else
sudo apt-get -y -q --force-yes update
sudo apt-get -y -q --force-yes upgrade
sudo apt-get --assume-yes install build-essential git nginx htop screen wget curl postgresql postgresql-contrib
fi
fi
# set up and install swap file
if [[ ! -f "$SWAP" ]]; then
sudo fallocate -l 1G $SWAP
sudo chmod 600 $SWAP
sudo mkswap $SWAP
sudo swapon $SWAP
fi
# setup node and npm
if [ ! -d "$NVM" ]; then
# Will enter here if $DIRECTORY exists, even if it contains spaces
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
export NVM_DIR=$NVM
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
source $HOME/.bashrc
source $NVM/nvm.sh
fi
nvm install $NVM_VERSION
# get source and build
# Install global packages.
fancy_echo "Installing global packages ..."
npm set progress=false
npm install -g gulp bower pm2 npm-check npm-install-missing ember-cli phantomjs-prebuilt
if [ ! -d "$LIVE" ]; then
mkdir $LIVE
fi
if [ ! -d "$DEV" ]; then
mkdir $DEV
fi
cd $DEV
# Get latest version of Obrand Admin
if [ ! -d "$DEV/$FRONT/.git" ]; then
git clone https://censis.visualstudio.com/DefaultCollection/_git/O-Brand
else
cd $DEV/$FRONT
git pull origin master
fi
if [ ! -d "$DEV/$SERVER/.git" ]; then
git clone https://censis.visualstudio.com/DefaultCollection/O-Brand/_git/O-BrandServer
else
cd $DEV/$SERVER
git pull origin master
fi
# Get latest version of Obrand Admin Server
cd $DEV/$FRONT
npm install . && npm-install-missing
bower install
ember build --environment production
cd $DEV/$SERVER
npm install . && npm-install-missing
npm install git+http://gitlab.silvrtree.co.uk/martind2000/md-utils.git
npm install git+http://gitlab.silvrtree.co.uk/martind2000/node-validator.git
rsync -uav --exclude .git $DEV/$SERVER/ $STAGING
cd $DEV/$FRONT/dist
cp -ra . $STAGING/wwwroot
if [[ $DATABASE = "yes" ]]; then
echo Setting up Database server...
# sudo su - postgres -c "$DEV/$DBSETUP"
# node dbconfig.js
fi
echo Copying...
rsync -uav --exclude .git $STAGING/ $LIVE
# Configure nginx
if [[ $NGINX = "yes" ]]; then
sudo fuser -k 80/tcp
sudo wget https://dl.dropboxusercontent.com/u/233909/obrand/obrand.nginx -O /etc/nginx/sites-available/obrand && sudo ln -s /etc/nginx/sites-available/obrand /etc/nginx/sites-enabled/obrand
sudo rm /etc/nginx/sites-enabled/default
npm set progress=true
sudo /etc/init.d/nginx restart
fi