2022-02-09 09:59:50 +00:00
|
|
|
#!/bin/bash
|
|
|
|
### BEGIN INIT INFO
|
|
|
|
# Provides: ruling.digital
|
|
|
|
# Required-Start: $network
|
|
|
|
# Required-Stop:
|
|
|
|
# Default-Start: 2 3 4 5
|
|
|
|
# Default-Stop: 0 1 6
|
|
|
|
# Short-Description: Start orbit daemon at boot time
|
|
|
|
# Description: Start orbit daemon at boot time
|
|
|
|
### END INIT INFO
|
|
|
|
ORBIT_USER=rulingcom
|
|
|
|
|
|
|
|
if [ -f "/home/$ORBIT_USER/.rvm/scripts/rvm" ]; then
|
|
|
|
source /home/$ORBIT_USER/.rvm/scripts/rvm
|
|
|
|
rvm use default
|
|
|
|
else
|
|
|
|
if [ -f "/etc/profile.d/rvm.sh" ]; then
|
|
|
|
source /etc/profile.d/rvm.sh
|
|
|
|
elif [ "$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`
|
2022-08-24 09:22:55 +00:00
|
|
|
ORBIT_GIT='http://ruling.digital/git'
|
2022-08-24 06:55:16 +00:00
|
|
|
ORBIT_BUILT_IN_EXT='http://gitlab.tp.rulingcom.com/core/default-modules/-/raw/orbit/built_in_extensions.rb'
|
|
|
|
ORBIT_DEFAULT_THEME='http://gitlab.tp.rulingcom.com/themes/default-theme.git'
|
|
|
|
NGINX_ORBIT_SITES='/etc/nginx/orbit_sites'
|
2022-02-09 09:59:50 +00:00
|
|
|
|
|
|
|
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' | grep -v '#' | xargs | awk '{print $2}'`;
|
|
|
|
ORBIT_ROOT=${ORBIT_ROOT//"/public;"/""};
|
|
|
|
if [ ! -d $ORBIT_ROOT ]; then echo "Site folder $ORBIT_ROOT not fount"; fi
|
|
|
|
}
|
|
|
|
|
|
|
|
start_orbit () {
|
|
|
|
sig 0 $ORBIT_ROOT && echo >&2 "$ORBIT_ROOT Already running";
|
|
|
|
echo "Starting $ORBIT_ROOT";
|
|
|
|
sudo su -l $ORBIT_USER -c "cd $ORBIT_ROOT && bundle exec unicorn_rails -D -E $RAILS_ENV -c config/unicorn.rb";
|
|
|
|
}
|
2022-07-23 04:22:13 +00:00
|
|
|
bundle_orbit () {
|
|
|
|
args="$1"
|
|
|
|
if [[ -z $args ]]; then
|
|
|
|
args="install"
|
|
|
|
fi
|
|
|
|
sudo su -l $ORBIT_USER -c "cd $ORBIT_ROOT && bundle $args";
|
|
|
|
}
|
2022-02-09 09:59:50 +00:00
|
|
|
case $1 in
|
2022-07-23 04:22:13 +00:00
|
|
|
install)
|
|
|
|
if [ ! -z "$2" ]; then
|
|
|
|
get_orbit_root $2
|
|
|
|
bundle_orbit
|
|
|
|
else
|
|
|
|
closed_sites="$(cat /home/$ORBIT_USER/closed_sites.txt)"
|
|
|
|
for APP in `ls -1 $NGINX_ORBIT_SITES`; do
|
|
|
|
get_orbit_root $APP;
|
|
|
|
if [[ ! "$closed_sites" =~ "$ORBIT_ROOT" ]]; then
|
|
|
|
bundle_orbit;
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0;
|
|
|
|
;;
|
2022-02-09 09:59:50 +00:00
|
|
|
start)
|
|
|
|
if [ ! -z "$2" ]; then
|
|
|
|
get_orbit_root $2
|
|
|
|
start_orbit
|
|
|
|
else
|
2022-07-23 04:22:13 +00:00
|
|
|
closed_sites="$(cat /home/$ORBIT_USER/closed_sites.txt)"
|
2022-02-09 09:59:50 +00:00
|
|
|
for APP in `ls -1 $NGINX_ORBIT_SITES`; do
|
|
|
|
get_orbit_root $APP;
|
2022-07-23 04:22:13 +00:00
|
|
|
if [[ "$closed_sites" =~ "$ORBIT_ROOT" ]]; then
|
|
|
|
echo "Stopping $ORBIT_ROOT";
|
|
|
|
sig QUIT && continue;
|
|
|
|
else
|
|
|
|
start_orbit;
|
|
|
|
fi
|
2022-02-09 09:59:50 +00:00
|
|
|
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
|
2022-07-23 04:22:13 +00:00
|
|
|
closed_sites="$(cat /home/$ORBIT_USER/closed_sites.txt)"
|
2022-02-09 09:59:50 +00:00
|
|
|
for APP in `ls -1 $NGINX_ORBIT_SITES`; do
|
|
|
|
get_orbit_root $APP;
|
2022-07-23 04:22:13 +00:00
|
|
|
if [[ "$closed_sites" =~ "$ORBIT_ROOT" ]]; then
|
|
|
|
echo "Stopping $ORBIT_ROOT";
|
|
|
|
sig QUIT && continue;
|
|
|
|
else
|
|
|
|
sig USR2 && echo "$ORBIT_ROOT reloaded OK" && continue;
|
|
|
|
echo >&2 "Couldn't reload $ORBIT_ROOT, starting instead";
|
|
|
|
start_orbit;
|
|
|
|
fi
|
2022-02-09 09:59:50 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
exit 0;
|
|
|
|
;;
|
|
|
|
create)
|
2022-08-24 08:09:20 +00:00
|
|
|
d="$(dirname $2)"
|
|
|
|
site_name="$(basename $2)"
|
|
|
|
if [[ $d != '.' ]]; then
|
2022-08-25 01:39:48 +00:00
|
|
|
if [[ "${d:0:1}" == "/" ]]; then
|
|
|
|
ORBIT_SITES="$d"
|
|
|
|
else
|
|
|
|
ORBIT_SITES="$ORBIT_SITES/$d"
|
|
|
|
fi
|
2022-08-24 08:09:20 +00:00
|
|
|
fi
|
|
|
|
test -s "$NGINX_ORBIT_SITES/$site_name" && echo "Site $site_name already exist." && exit 0
|
|
|
|
test -s "$ORBIT_SITES/$site_name" && echo "File $ORBIT_SITES/$site_name already exist." && exit 0
|
2022-02-09 09:59:50 +00:00
|
|
|
|
|
|
|
while true; do
|
|
|
|
read -p "nginx server name: " SERVER_NAME
|
|
|
|
if [ ! -z "$SERVER_NAME" ]; then break; fi
|
|
|
|
done
|
2022-08-24 06:55:16 +00:00
|
|
|
|
|
|
|
read -p "port number: " PORT
|
|
|
|
if [ -z "$PORT" ]; then
|
|
|
|
PORT="80";
|
|
|
|
fi
|
2022-02-09 09:59:50 +00:00
|
|
|
|
|
|
|
while true; do
|
|
|
|
read -p "database name: " DATABASE
|
|
|
|
if [ ! -z "$DATABASE" ]; then break; fi
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "-----------------------------------------------------"
|
2022-08-24 08:09:20 +00:00
|
|
|
echo "Path: $ORBIT_SITES/$site_name"
|
2022-02-09 09:59:50 +00:00
|
|
|
echo "Database: $DATABASE"
|
|
|
|
echo http://$SERVER_NAME:$PORT
|
|
|
|
echo "-----------------------------------------------------"
|
|
|
|
|
|
|
|
while true; do
|
2022-08-24 08:09:20 +00:00
|
|
|
read -p "Create Orbit $site_name? (y/n) " CONFIRM
|
2022-02-09 09:59:50 +00:00
|
|
|
case "$CONFIRM" in
|
|
|
|
y|Y ) break;;
|
|
|
|
n|N ) exit 0;;
|
|
|
|
* ) echo "(y/n)";;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
cd ~
|
2022-08-24 09:22:55 +00:00
|
|
|
sudo wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/nginx4-5.conf -O $NGINX_ORBIT_SITES/$site_name
|
2022-08-24 08:09:20 +00:00
|
|
|
sudo perl -pi -e "s/{{ORBIT}}/$site_name/g" $NGINX_ORBIT_SITES/$site_name
|
|
|
|
sudo perl -pi -e "s#{{ORBIT_SITES}}#${ORBIT_SITES}#g" $NGINX_ORBIT_SITES/$site_name
|
|
|
|
sudo perl -pi -e "s/{{PORT}}/$PORT/g" $NGINX_ORBIT_SITES/$site_name
|
|
|
|
sudo perl -pi -e "s/{{SERVER_NAME}}/$SERVER_NAME/g" $NGINX_ORBIT_SITES/$site_name
|
2022-02-09 09:59:50 +00:00
|
|
|
sudo service nginx reload
|
2022-08-24 09:22:55 +00:00
|
|
|
sudo su -l $ORBIT_USER -c "mkdir -p $ORBIT_SITES"
|
2022-08-24 08:09:20 +00:00
|
|
|
sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES && git clone $ORBIT_GIT $site_name"
|
|
|
|
sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES/$site_name && wget $ORBIT_BUILT_IN_EXT && git clone $ORBIT_DEFAULT_THEME app/templates/default-theme && bundle install"
|
|
|
|
sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES/$site_name && perl -pi -e \"s/orbit_4_5/$DATABASE/g\" config/mongoid.yml"
|
|
|
|
# sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES/$site_name && bundle exec rake assets:precompile RAILS_ENV=production"
|
|
|
|
sudo su -l $ORBIT_USER -c "cd $ORBIT_SITES/$site_name && bundle exec unicorn_rails -c config/unicorn.rb -D -E $RAILS_ENV"
|
2022-02-09 09:59:50 +00:00
|
|
|
|
|
|
|
echo "-----------------------------------------------------"
|
2022-08-24 08:09:20 +00:00
|
|
|
echo "$site_name is ready"
|
|
|
|
echo "Path: $ORBIT_SITES/$site_name"
|
2022-09-15 08:11:13 +00:00
|
|
|
local_ip=`ip route get 8.8.8.8|xargs|awk '{print $7}'`;
|
2022-08-24 06:55:16 +00:00
|
|
|
echo http://$local_ip:$PORT
|
2022-02-09 09:59:50 +00:00
|
|
|
echo "-----------------------------------------------------"
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
setup)
|
2022-08-24 08:09:20 +00:00
|
|
|
wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/install_orbit_environment.sh -O install_orbit_environment.sh
|
|
|
|
source install_orbit_environment.sh
|
2022-02-09 09:59:50 +00:00
|
|
|
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 <start|stop|restart|force-stop|create|setup>"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|