diff --git a/README.md b/README.md index 1cb62df..f1cf46b 100644 --- a/README.md +++ b/README.md @@ -201,4 +201,240 @@ $ sudo logrotate -d /etc/logrotate.conf # 確認系統是否有使用 /etc/logro endscript } +``` + +### 三、定期壓縮與刪除 log +```sh +# 開機網站自動起始 +$ sudo vim /etc/init.d/orbit # 內容建附註 orbit + +``` + +```txt +# 附註 orbit +#!/bin/bash + +ORBIT_USER=rulingcom + +if [ -f "/home/$ORBIT_USER/.rvm/scripts/rvm" ]; then + source /home/$ORBIT_USER/.rvm/scripts/rvm + rvm use default +else + if [ "$1" != "setup" ]; then + echo "" + echo "-----------------------------------------------------" + echo "Orbit server dependencies is missing. Please run setup first." + echo "command: service orbit setup" + echo "-----------------------------------------------------" + echo "" + exit 0; + fi +fi + +ORBIT_SITES=`pwd` +ORBIT_GIT=http://gitlab.tp.rulingcom.com/saurabh/orbit4-5.git +NGINX_ORBIT_SITES=/etc/nginx/orbit_sites + +RAILS_ENV=production + +sig () { + test -s "$ORBIT_ROOT/tmp/pids/unicorn.pid" && kill -$1 `cat $ORBIT_ROOT/tmp/pids/unicorn.pid` +} + +get_orbit_root () { + if [ ! -f $NGINX_ORBIT_SITES/$1 ]; then echo "Site $1 not found" && exit 0; fi + ORBIT_ROOT=`cat $NGINX_ORBIT_SITES/$1 | grep 'root'`; + ORBIT_ROOT=${ORBIT_ROOT//"/public;"/""}; + ORBIT_ROOT=${ORBIT_ROOT//"root"/""}; + ORBIT_ROOT=${ORBIT_ROOT//" "/""}; + if [ ! -d $ORBIT_ROOT ]; then echo "Site folder $ORBIT_ROOT not fount" && exit 0; fi +} + +start_orbit () { + sig 0 $ORBIT_ROOT && echo >&2 "$ORBIT_ROOT Already running" && continue; + if [ ! -d "$ORBIT_ROOT/public/assets" ]; then + sudo su -l $ORBIT_USER -c "cd $ORBIT_ROOT && git fetch origin && git pull origin master && bundle" + sudo su -l $ORBIT_USER -c "cd $ORBIT_ROOT && bundle exec rake assets:precompile RAILS_ENV=production" + fi + echo "Starting $ORBIT_ROOT"; + sudo su -l $ORBIT_USER -c "cd $ORBIT_ROOT && bundle exec unicorn_rails -D -E $RAILS_ENV -c $ORBIT_ROOT/config/unicorn.rb"; +} + +case $1 in +start) + if [ ! -z "$2" ]; then + get_orbit_root $2 + start_orbit + else + for APP in `ls -1 $NGINX_ORBIT_SITES`; do + get_orbit_root $APP; + start_orbit; + done + fi + + exit 0; + ;; +stop) + while [ `whoami` != "root" ]; do + if [ ! -z "$2" ]; then + read -p "Stop Orbit $2? (y/n) " CONFIRM + else + read -p "Stop All Orbits? (y/n) " CONFIRM + fi + case "$CONFIRM" in + y|Y ) break;; + n|N ) exit 0;; + * ) echo "(y/n)";; + esac + done + if [ ! -z "$2" ]; then + get_orbit_root $2 + echo "Stopping $ORBIT_ROOT" + sig QUIT + else + for APP in `ls -1 $NGINX_ORBIT_SITES`; do + get_orbit_root $APP; + echo "Stopping $ORBIT_ROOT"; + sig QUIT && continue; + done + fi + exit 0; + ;; +force-stop) + while true; do + if [ ! -z "$2" ]; then + read -p "Stop Orbit $2? (y/n) " CONFIRM + else + read -p "Stop All Orbits? (y/n) " CONFIRM + fi + case "$CONFIRM" in + y|Y ) break;; + n|N ) exit 0;; + * ) echo "(y/n)";; + esac + done + if [ ! -z "$2" ]; then + get_orbit_root $2 + echo "Stopping $ORBIT_ROOT" + sig TERM + else + for APP in `ls -1 $NGINX_ORBIT_SITES`; do + get_orbit_root $APP; + echo "Stopping $ORBIT_ROOT"; + sig TERM && continue; + done + fi + exit 0; + ;; +restart|reload) + if [ ! -z "$2" ]; then + get_orbit_root $2 + sig USR2 && echo "$ORBIT_ROOT reloaded OK" && exit 0 + echo >&2 "Couldn't reload $ORBIT_ROOT, starting instead" + start_orbit + else + for APP in `ls -1 $NGINX_ORBIT_SITES`; do + get_orbit_root $APP; + sig USR2 && echo "$ORBIT_ROOT reloaded OK" && continue; + echo >&2 "Couldn't reload $ORBIT_ROOT, starting instead"; + start_orbit; + done + fi + exit 0; + ;; +create) + if [ "$0" == "/etc/init.d/orbit" ]; then + echo "Don't use service to create." + echo "Use: orbit create $2" + exit 0 + fi + test -s "$NGINX_ORBIT_SITES/$2" && echo "Site $2 already exist." && exit 0 + test -s "$ORBIT_SITES/$2" && echo "File $ORBIT_SITES/$2 already exist." && exit 0 + + while true; do + read -p "nginx server name: " SERVER_NAME + if [ ! -z "$SERVER_NAME" ]; then break; fi + done + + while true; do + read -p "port number: " PORT + if [ ! -z "$PORT" ]; then break; fi + done + + while true; do + read -p "database name: " DATABASE + if [ ! -z "$DATABASE" ]; then break; fi + done + + echo "-----------------------------------------------------" + echo "Path: $ORBIT_SITES/$2" + echo "Database: $DATABASE" + echo http://$SERVER_NAME:$PORT + echo "-----------------------------------------------------" + + while true; do + read -p "Create Orbit $2? (y/n) " CONFIRM + case "$CONFIRM" in + y|Y ) break;; + n|N ) exit 0;; + * ) echo "(y/n)";; + esac + done + + cd ~ + sudo wget http://installer.tp.rulingcom.com/nginx4-5.conf + sudo cp nginx4-5.conf $NGINX_ORBIT_SITES/$2 + sudo rm nginx4-5.conf + sudo perl -pi -e "s/{{ORBIT}}/$2/g" $NGINX_ORBIT_SITES/$2 + sudo perl -pi -e "s#{{ORBIT_SITES}}#${ORBIT_SITES}#g" $NGINX_ORBIT_SITES/$2 + sudo perl -pi -e "s/{{PORT}}/$PORT/g" $NGINX_ORBIT_SITES/$2 + sudo perl -pi -e "s/{{SERVER_NAME}}/$SERVER_NAME/g" $NGINX_ORBIT_SITES/$2 + sudo service nginx reload + + sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES && git clone $ORBIT_GIT $2 && cd $2 && bundle" + sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES/$2 && perl -pi -e \"s/orbit_4_5/$DATABASE/g\" config/mongoid.yml" + sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES/$2 && bundle exec rake assets:precompile RAILS_ENV=production" + sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES/$2 && bundle exec unicorn_rails -D -E $RAILS_ENV -c $ORBIT_SITES/$2/config/unicorn.rb" + sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES/$2 && bundle exec rake orbit_cron:install" + + echo "-----------------------------------------------------" + echo "$2 is ready" + echo "Path: $ORBIT_SITES/$2" + echo http://`ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`:$PORT + echo "-----------------------------------------------------" + + exit 0 + ;; +setup) + sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 + sudo echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list + + sudo apt-get update + sudo apt-get -y upgrade + sudo apt-get install -y nginx git-core mongodb-org imagemagick curl openssh-server nano + + sudo mkdir $NGINX_ORBIT_SITES + sudo perl -pi -e "s/sites-enabled/orbit_sites/g" /etc/nginx/nginx.conf + sudo rm -r /etc/nginx/sites-* + + sudo apt-get --no-install-recommends --yes install gawk g++ gcc make libc6-dev libreadline6-dev zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 autoconf libgdbm-dev libncurses5-dev automake libtool bison pkg-config libffi-dev + + sudo su -l $ORBIT_USER -c "\curl -sSL https://get.rvm.io | bash -s stable" + sudo su -l $ORBIT_USER -c "rvm install 2.1 --default" + echo "gem: --no-ri --no-rdoc" > /home/$ORBIT_USER/.gemrc + source /home/$ORBIT_USER/.rvm/scripts/rvm + + echo "" + echo "-----------------------------------------------------" + echo "System is ready. You can start creating Orbit servers." + echo "command: orbit create Orbit_Folder_Name" + echo "-----------------------------------------------------" + echo "" + exit 0 + ;; +*) + echo >&2 "Usage $0 " + exit 1 + ;; +esac ``` \ No newline at end of file