Skip to content

Instantly share code, notes, and snippets.

@mreferre
Last active March 19, 2018 23:21
Show Gist options
  • Save mreferre/cc118ff2309ecf7790e785418922d762 to your computer and use it in GitHub Desktop.
Save mreferre/cc118ff2309ecf7790e785418922d762 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Massimo Re Ferre' [email protected]
###########################################################
########### USER INPUTS ###########
###########################################################
# Variables AppServer component
RACK_ENV="${RACK_ENV:-'custom'}"
REDIS_SERVER_ENDPOINT="${REDIS_SERVER_ENDPOINT:-<here the IP or FQDN of the Redis endpoint>}"
YELB_DB_SERVER_ENDPOINT="${YELB_DB_SERVER_ENDPOINT:-<here the IP or FQDN of the Postgres endpoint>}"
# variables UI component
# note in most cases the YELB_APPSERVER_ENDPOINT would be window.location.host <-- NOOOOO (bisogna solo compilare angular con env PROD)!!!!
# it essentially points back to the same host (NGINX) that serves the html5/angular binaries and that also acts as a proxy to the yelb-appserver
# the true meaning of the YELB_APPSERVER_ENDPOINT variable is when you want to decouple the shipping of the html5/angular binaries (e.g off of S3)
# from the yelb-appserver end-point (e.g. API Gatway) in a serverless deployment
YELB_APPSERVER_ENDPOINT="${YELB_DB_SERVER_ENDPOINT:-<here the IP or FQDN of the AppServer>}"
###########################################################
########### END OF USER INPUTS ###########
###########################################################
###########################################################
## DO NOT TOUCH THESE UNLESS YOU KNOW WHAT YOU ARE DOING ##
###########################################################
NGINX_CONF=/etc/nginx/conf.d/default.conf
NGINX_MAIN=/etc/nginx/nginx.conf
LOG_OUTPUT="/yelb-setup.log"
###########################################################
########### ###########
###########################################################
logger() {
LOG_TYPE=$1
MSG=$2
COLOR_OFF="\x1b[39;49;00m"
case "${LOG_TYPE}" in
green)
# Green
COLOR_ON="\x1b[32;01m";;
blue)
# Blue
COLOR_ON="\x1b[36;01m";;
yellow)
# Yellow
COLOR_ON="\x1b[33;01m";;
red)
# Red
COLOR_ON="\x1b[31;01m";;
default)
# Default
COLOR_ON="${COLOR_OFF}";;
*)
# Default
COLOR_ON="${COLOR_OFF}";;
esac
TIME=$(date +%F" "%H:%M:%S)
echo -e "${COLOR_ON} ${TIME} -- ${MSG} ${COLOR_OFF}"
echo -e "${TIME} -- ${MSG}" >> "${LOG_OUTPUT}"
}
core() {
yum update -y
yum install -y git
git clone http://github.com/mreferre/yelb
}
postgres() {
yum install postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs
service postgresql initdb
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" /var/lib/pgsql9/data/postgresql.conf
sed -i "s/#port = 5432/port = 5432/" /var/lib/pgsql9/data/postgresql.conf
sed -i "s/peer/trust/" /var/lib/pgsql9/data/pg_hba.conf
service postgresql start
psql -v ON_ERROR_STOP=1 --username postgres <<-EOSQL
CREATE DATABASE yelbdatabase;
\connect yelbdatabase;
CREATE TABLE restaurants (
name char(30),
count integer,
PRIMARY KEY (name)
);
INSERT INTO restaurants (name, count) VALUES ('outback', 0);
INSERT INTO restaurants (name, count) VALUES ('bucadibeppo', 0);
INSERT INTO restaurants (name, count) VALUES ('chipotle', 0);
INSERT INTO restaurants (name, count) VALUES ('ihop', 0);
EOSQL
}
redis() {
yum -y install gcc gcc-c++ make jemalloc-devel
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
cp src/redis-server /usr/local/bin/
cp src/redis-cli /usr/local/bin/
cd utils
#the script below configures the redis service
REDIS_PORT=6379 \
REDIS_CONFIG_FILE=/etc/redis/6379.conf \
REDIS_LOG_FILE=/var/log/redis_6379.log \
REDIS_DATA_DIR=/var/lib/redis/6379 \
REDIS_EXECUTABLE=`command -v redis-server` ./utils/install_server.sh
}
npm() {
cd $HOME
cd yelb/yelb-ui
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
. ~/.nvm/nvm.sh
nvm install 6.11.5
nvm install --lts
npm install -g @angular/cli
}
ui() {
cd $HOME
cd yelb/yelb-ui
git clone https://github.com/vmware/clarity-seed.git
cd ./clarity-seed
git checkout -b f3250ee26ceb847f61bb167a90dc957edf6e7f43
cd ..
cp ./clarity-seed-newfiles/src/index.html ./clarity-seed/src/index.html
cp ./clarity-seed-newfiles/src/styles.css ./clarity-seed/src/styles.css
cp ./clarity-seed-newfiles/src/app/app* ./clarity-seed/src/app
cp ./clarity-seed-newfiles/src/environments/env* ./clarity-seed/src/environments
cp ./clarity-seed-newfiles/package.json ./clarity-seed/package.json
cp ./clarity-seed-newfiles/angular-cli.json ./clarity-seed/.angular-cli.json
rm -r ./clarity-seed/src/app/home
rm -r ./clarity-seed/src/app/about
# hack to replace a string with the actual app server endpoint in the proxy configuration
# this si due to the fact that angular environments can't work with system variables
# see here for more context: https://github.com/angular/angular-cli/issues/4419
sed -i "s/YELB_APPSERVER_ENDPOINT/$YELB_APPSERVER_ENDPOINT/" ./clarity-seed/src/environments/environment.custom.ts
# end of angular custom environment hack
cd ./clarity-seed/src
npm install
sudo mkdir /custom
sudo chmod 777 /custom
ng build --environment=custom --output-path=/custom/dist/
}
nginx() {
cd $HOME
cd yelb/yelb-ui
sudo yum install -y nginx
echo "server {" | sudo tee $NGINX_CONF > /dev/null
echo " listen 80;" | sudo tee -a $NGINX_CONF > /dev/null
echo " server_name localhost;" | sudo tee -a $NGINX_CONF > /dev/null
echo " location /api {" | sudo tee -a $NGINX_CONF > /dev/null
echo " proxy_pass http://"$YELB_APPSERVER_ENDPOINT":4567/api;" | sudo tee -a $NGINX_CONF > /dev/null
echo " }" | sudo tee -a $NGINX_CONF > /dev/null
echo "" | sudo tee -a $NGINX_CONF > /dev/null
echo " access_log /var/log/nginx/host.access.log main;" | sudo tee -a $NGINX_CONF > /dev/null
echo "" | sudo tee -a $NGINX_CONF > /dev/null
echo " location / {" | sudo tee -a $NGINX_CONF > /dev/null
echo " root /custom/dist;" | sudo tee -a $NGINX_CONF > /dev/null
echo " index index.html index.htm;" | sudo tee -a $NGINX_CONF > /dev/null
echo " }" | sudo tee -a $NGINX_CONF > /dev/null
echo "" | sudo tee -a $NGINX_CONF > /dev/null
echo " error_page 500 502 503 504 /50x.html;" | sudo tee -a $NGINX_CONF > /dev/null
echo " location = /50x.html {" | sudo tee -a $NGINX_CONF > /dev/null
echo " root "$HOME"/yelb/yelb-ui/clarity-seed/src/custom/dist;" | sudo tee -a $NGINX_CONF > /dev/null
echo " }" | sudo tee -a $NGINX_CONF > /dev/null
echo "}" | sudo tee -a $NGINX_CONF > /dev/null
sudo rm $NGINX_MAIN
echo "user nginx;" | sudo tee -a $NGINX_MAIN > /dev/null
echo "worker_processes 1;" | sudo tee -a $NGINX_MAIN > /dev/null
echo "" | sudo tee -a $NGINX_MAIN > /dev/null
echo "error_log /var/log/nginx/error.log warn;" | sudo tee -a $NGINX_MAIN > /dev/null
echo "pid /var/run/nginx.pid;" | sudo tee -a $NGINX_MAIN > /dev/null
echo "" | sudo tee -a $NGINX_MAIN > /dev/null
echo "events {" | sudo tee -a $NGINX_MAIN > /dev/null
echo " worker_connections 1024;" | sudo tee -a $NGINX_MAIN > /dev/null
echo "}" | sudo tee -a $NGINX_MAIN > /dev/null
echo "" | sudo tee -a $NGINX_MAIN > /dev/null
echo "http {" | sudo tee -a $NGINX_MAIN > /dev/null
echo " include /etc/nginx/mime.types;" | sudo tee -a $NGINX_MAIN > /dev/null
echo " default_type application/octet-stream;" | sudo tee -a $NGINX_MAIN > /dev/null
echo " log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '" | sudo tee -a $NGINX_MAIN > /dev/null
echo " '\$status \$body_bytes_sent "\$http_referer" '" | sudo tee -a $NGINX_MAIN > /dev/null
echo " '"\$http_user_agent" "\$http_x_forwarded_for"';" | sudo tee -a $NGINX_MAIN > /dev/null
echo " access_log /var/log/nginx/access.log main;" | sudo tee -a $NGINX_MAIN > /dev/null
echo " sendfile on;" | sudo tee -a $NGINX_MAIN > /dev/null
echo " keepalive_timeout 65;" | sudo tee -a $NGINX_MAIN > /dev/null
echo " include /etc/nginx/conf.d/*.conf;" | sudo tee -a $NGINX_MAIN > /dev/null
echo "}" | sudo tee -a $NGINX_MAIN > /dev/null
chkconfig nginx on
service nginx start
}
appserver() {
yum update -y
yum install -y ruby23
alternatives --set ruby /usr/bin/ruby2.3
yum install -y postgresql
yum install -y ruby23-devel
yum install -y gcc
yum install -y postgresql-devel
gem install pg --no-ri --no-rdoc
gem install redis --no-ri --no-rdoc
gem install sinatra --no-ri --no-rdoc
cd $HOME
cd ./yelb/yelb-appserver
ruby yelb-appserver.rb
}
###########################################################
########### MAIN ###########
###########################################################
# This execute all functions as parameters used to launch the script
for i in "$@"
do
"$i"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment