Advertisement
Guest User

setup erpnext v5.0 production

a guest
Mar 30th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.35 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4.  
  5.  
  6. ## Utils
  7.  
  8. print_msg() {
  9.     echo "Frappe password: $FRAPPE_USER_PASS"
  10.     echo "MariaDB root password: $MSQ_PASS"
  11.     echo "Administrator password: $ADMIN_PASS"
  12. }
  13.  
  14. get_passwd() {
  15.     echo `cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1`
  16. }
  17.  
  18. set_opts () {
  19.     OPTS=`getopt -o v --long verbose,mysql-root-password:,frappe-user:,bench-branch:,setup-production,skip-setup-bench,help -n 'parse-options' -- "$@"`
  20.      
  21.     if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
  22.      
  23.     eval set -- "$OPTS"
  24.      
  25.     VERBOSE=false
  26.     HELP=false
  27.     FRAPPE_USER=false
  28.     BENCH_BRANCH="master"
  29.     SETUP_PROD=false
  30.     SETUP_BENCH=true
  31.  
  32.     if [ -f ~/frappe_passwords.sh ]; then
  33.         source ~/frappe_passwords.sh
  34.     else
  35.         FRAPPE_USER_PASS=`get_passwd`
  36.         MSQ_PASS=`get_passwd`
  37.         ADMIN_PASS=`get_passwd`
  38.  
  39.         echo "FRAPPE_USER_PASS=$FRAPPE_USER_PASS" > ~/frappe_passwords.sh
  40.         echo "MSQ_PASS=$MSQ_PASS" >> ~/frappe_passwords.sh
  41.         echo "ADMIN_PASS=$ADMIN_PASS" >> ~/frappe_passwords.sh
  42.     fi
  43.      
  44.     while true; do
  45.     case "$1" in
  46.     -v | --verbose ) VERBOSE=true; shift ;;
  47.     -h | --help ) HELP=true; shift ;;
  48.     --mysql-root-password ) MSQ_PASS="$2"; shift; shift ;;
  49.     --frappe-user ) FRAPPE_USER="$2"; shift; shift ;;
  50.     --setup-production ) SETUP_PROD=true; shift;;
  51.     --bench-branch ) BENCH_BRANCH="$2"; shift;;
  52.     --skip-setup-bench ) SETUP_BENCH=false; shift;;
  53.     -- ) shift; break ;;
  54.     * ) break ;;
  55.     esac
  56.     done
  57. }
  58.  
  59. get_distro() {
  60.     ARCH=$(uname -m | sed 's/x86_/amd/;s/i[3-6]86/x86/')
  61.  
  62.     if [ $ARCH == "amd64" ]; then
  63.         T_ARCH="x86_64"
  64.         WK_ARCH="amd64"
  65.     else
  66.         T_ARCH="i386"
  67.         WK_ARCH="i386"
  68.     fi
  69.  
  70.     if [ -f /etc/redhat-release ]; then
  71.         OS="centos"
  72.         OS_VER=`cat /etc/redhat-release | sed 's/Linux\ //g' | cut -d" " -f3 | cut -d. -f1`
  73.  
  74.     elif [ -f /etc/lsb-release ]; then
  75.         . /etc/lsb-release
  76.         OS=$DISTRIB_ID
  77.         OS_VER=$DISTRIB_CODENAME
  78.  
  79.     elif [ -f /etc/debian_version ]; then
  80.         . /etc/os-release
  81.         OS="debian"  # XXX or Ubuntu??
  82.         OS_VER=$VERSION_ID
  83.     fi
  84.  
  85.     export OS=$OS
  86.     export OS_VER=$OS_VER
  87.     export ARCH=$ARCH
  88.     export T_ARCH=$T_ARCH
  89.     export WK_ARCH=$WK_ARCH
  90.     echo Installing for $OS $OS_VER $ARCH
  91.     echo "In case you encounter an error, you can post on https://discuss.frappe.io"
  92.     echo
  93. }
  94.  
  95. run_cmd() {
  96.     if $VERBOSE; then
  97.         "$@"
  98.     else
  99.         # $@
  100.         "$@" > /tmp/cmdoutput.txt 2>&1 || (cat /tmp/cmdoutput.txt && exit 1)
  101.     fi
  102. }
  103.  
  104. ## add repos
  105.  
  106. add_centos6_mariadb_repo() {
  107.     echo "
  108. [mariadb]
  109. name = MariaDB
  110. baseurl = http://yum.mariadb.org/5.5/centos$OS_VER-$ARCH
  111. gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
  112. gpgcheck=1
  113. " > /etc/yum.repos.d/mariadb.repo
  114. }
  115.  
  116.  
  117. add_ubuntu_mariadb_repo() {
  118.     run_cmd sudo apt-get update
  119.     run_cmd sudo apt-get install -y software-properties-common python-software-properties
  120.     run_cmd sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
  121.     run_cmd sudo add-apt-repository "deb http://ams2.mirrors.digitalocean.com/mariadb/repo/5.5/ubuntu $OS_VER main"
  122. }
  123.  
  124. add_debian_mariadb_repo() {
  125.     if [ $OS_VER == "7" ]; then
  126.         CODENAME="wheezy"
  127.    
  128.     elif [ $OS_VER == "6" ]; then
  129.         CODENAME="squeeze"
  130.     else
  131.         echo Unsupported Debian Version
  132.         exit 1
  133.     fi
  134.  
  135.     run_cmd sudo apt-get update
  136.     run_cmd sudo apt-get install -y python-software-properties
  137.     run_cmd sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
  138.     run_cmd sudo add-apt-repository "deb http://ams2.mirrors.digitalocean.com/mariadb/repo/5.5/debian $CODENAME main"
  139. }
  140.  
  141. add_ius_repo() {
  142.     if [ $OS_VER -eq "6" ]; then
  143.     wget http://dl.iuscommunity.org/pub/ius/stable/CentOS/$OS_VER/$T_ARCH/epel-release-6-5.noarch.rpm
  144.     wget http://dl.iuscommunity.org/pub/ius/stable/CentOS/$OS_VER/$T_ARCH/ius-release-1.0-13.ius.centos6.noarch.rpm
  145.     rpm --quiet -q epel-release || rpm -Uvh epel-release-6-5.noarch.rpm
  146.     rpm --quiet -q ius-release || rpm -Uvh ius-release-1.0-13.ius.centos6.noarch.rpm
  147.     fi
  148. }
  149.  
  150. add_epel_centos7() {
  151.     yum install -y epel-release
  152. }
  153.  
  154. add_maria_db_repo() {
  155.     if [ "$OS" == "Ubuntu" ] && [ $OS_VER == "utopic" ]; then
  156.         return
  157.     elif [ "$OS" == "centos" ]; then
  158.         echo Adding centos mariadb repo
  159.         add_centos6_mariadb_repo
  160.    
  161.     elif [ "$OS" == "debian" ]; then
  162.         echo Adding debian mariadb repo
  163.         add_debian_mariadb_repo
  164.  
  165.     elif [ "$OS" == "Ubuntu" ]; then
  166.         echo Adding ubuntu mariadb repo
  167.         add_ubuntu_mariadb_repo
  168.     else
  169.         echo Unsupported Distribution
  170.         exit 1
  171.     fi
  172. }
  173.  
  174. ## install
  175.  
  176. install_packages() {
  177.     if [ $OS == "centos" ]; then
  178.         run_cmd sudo yum install wget -y
  179.         run_cmd sudo yum groupinstall -y "Development tools"
  180.         if [ $OS_VER == "6" ]; then
  181.             run_cmd add_ius_repo
  182.             run_cmd sudo yum install -y git MariaDB-server MariaDB-client MariaDB-compat python-setuptools nginx zlib-devel bzip2-devel openssl-devel memcached postfix python27-devel python27 libxml2 libxml2-devel libxslt libxslt-devel redis MariaDB-devel libXrender libXext python27-setuptools cronie sudo which xorg-x11-fonts-Type1 xorg-x11-fonts-75dpi
  183.         elif [ $OS_VER == "7" ]; then
  184.             run_cmd add_epel_centos7
  185.             run_cmd sudo yum install -y git mariadb-server mariadb-devel python-setuptools nginx zlib-devel bzip2-devel openssl-devel memcached postfix python-devel libxml2 libxml2-devel libxslt libxslt-devel redis libXrender libXext supervisor cronie sudo which xorg-x11-fonts-75dpi xorg-x11-fonts-Type1
  186.         fi
  187.         echo "Installing wkhtmltopdf"
  188.         install_wkhtmltopdf_centos
  189.         run_cmd easy_install-2.7 -U pip
  190.    
  191.    
  192.     elif [ $OS == "debian" ] || [ $OS == "Ubuntu" ]; then
  193.         export DEBIAN_FRONTEND=noninteractive
  194.         setup_debconf
  195.         run_cmd sudo apt-get update
  196.         run_cmd sudo apt-get install python-dev python-setuptools build-essential python-mysqldb git memcached ntp vim screen htop mariadb-server mariadb-common libmariadbclient-dev  libxslt1.1 libxslt1-dev redis-server libssl-dev libcrypto++-dev postfix nginx supervisor python-pip fontconfig libxrender1 libxext6 xfonts-75dpi xfonts-base -y
  197.         echo "Installing wkhtmltopdf"
  198.         install_wkhtmltopdf_deb
  199.  
  200.     else
  201.         echo Unsupported Distribution
  202.         exit 1
  203.     fi
  204. }
  205.  
  206. install_wkhtmltopdf_centos () {
  207.  
  208.     if [[ $OS == "centos" && $OS_VER == "7" && $T_ARCH == "i386" ]]; then
  209.         echo "Cannot install wkhtmltodpdf. Skipping..."
  210.         return 0
  211.     fi
  212.     RPM="wkhtmltox-0.12.2.1_linux-$OS$OS_VER-$WK_ARCH.rpm"
  213.     run_cmd wget http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.2.1/$RPM
  214.     rpm --quiet -q wkhtmltox || run_cmd rpm -Uvh $RPM
  215. }
  216.  
  217. install_wkhtmltopdf_deb () {
  218.     if [[ $OS_VER == "utopic" ]]; then
  219.         echo "Cannot install wkhtmltodpdf. Skipping..."
  220.         return 0
  221.     fi
  222.     if [[ $OS == "debian" &&  $OS_VER == "7" ]]; then
  223.         WK_VER="wheezy"
  224.     else
  225.         WK_VER=$OS_VER
  226.     fi
  227.     run_cmd wget http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.2.1/wkhtmltox-0.12.2.1_linux-$WK_VER-$WK_ARCH.deb
  228.     run_cmd dpkg -i wkhtmltox-0.12.2.1_linux-$WK_VER-$WK_ARCH.deb
  229. }
  230.  
  231.  
  232. install_supervisor_centos6() {
  233.         run_cmd easy_install supervisor
  234.         curl -Ss https://raw.githubusercontent.com/pdvyas/supervisor-initscripts/master/redhat-init-jkoppe > /etc/init.d/supervisord
  235.         curl -Ss https://raw.githubusercontent.com/pdvyas/supervisor-initscripts/master/redhat-sysconfig-jkoppe > /etc/sysconfig/supervisord
  236.         curl -Ss https://raw.githubusercontent.com/pdvyas/supervisor-initscripts/master/supervisord.conf > /etc/supervisord.conf
  237.         run_cmd mkdir /etc/supervisor.d
  238.         run_cmd chmod +x /etc/init.d/supervisord
  239.         bash -c "service supervisord start || true"
  240. }
  241.  
  242. ### config
  243.  
  244. get_mariadb_password() {
  245.     get_password "MariaDB root" MSQ_PASS
  246. }
  247.  
  248. get_site_admin_password() {
  249.     get_password "Admin password" ADMIN_PASS
  250. }
  251.  
  252. get_password() {
  253.     if [ -z "$2" ]; then
  254.         read -t 1 -n 10000 discard  || true
  255.         echo
  256.         read -p "Enter $1 password to set:" -s TMP_PASS1
  257.         echo
  258.         read -p "Re enter $1 password to set:" -s TMP_PASS2
  259.         echo
  260.         if [ $TMP_PASS1 == $TMP_PASS2 ]; then
  261.             export $2=$TMP_PASS1
  262.         else
  263.             echo Passwords do not match
  264.             get_password $1 $2
  265.         fi
  266.     fi
  267. }
  268.  
  269. configure_mariadb_centos() {
  270.     # Required only for CentOS, Ubuntu and Debian will show dpkg configure screen to set the password
  271.     if [ $OS == "centos" ]; then
  272.         mysqladmin -u root password $MSQ_PASS
  273.     fi
  274. }
  275.  
  276. start_services_centos6() {
  277.     run_cmd service nginx start
  278.     run_cmd service mysql start
  279.     run_cmd service redis start
  280. }
  281.  
  282. configure_services_centos6() {
  283.     # bind memcached only on localhost
  284.     sed -i 's/OPTIONS=""/OPTIONS="-l 127.0.0.1"/g' /etc/sysconfig/memcached
  285.     run_cmd chkconfig --add supervisord
  286.     run_cmd chkconfig redis on
  287.     run_cmd chkconfig mysql on
  288.     run_cmd chkconfig nginx on
  289.     run_cmd chkconfig supervisord on
  290. }
  291.  
  292. configure_services_centos7() {
  293.     # bind memcached only on localhost
  294.     sed -i 's/OPTIONS=""/OPTIONS="-l 127.0.0.1"/g' /etc/sysconfig/memcached
  295.     run_cmd systemctl enable nginx
  296.     run_cmd systemctl enable mysql
  297.     run_cmd systemctl enable redis
  298.     run_cmd systemctl enable supervisord
  299.     run_cmd systemctl enable memcached
  300. }
  301.  
  302. start_services_centos7() {
  303.     run_cmd systemctl start nginx
  304.     run_cmd systemctl start mysql
  305.     run_cmd systemctl start redis
  306.     run_cmd systemctl start supervisord
  307.     run_cmd systemctl start memcached
  308. }
  309.  
  310. configure_mariadb() {
  311.     config="
  312. [mysqld]
  313. innodb-file-format=barracuda
  314. innodb-file-per-table=1
  315. innodb-large-prefix=1
  316. character-set-client-handshake = FALSE
  317. character-set-server = utf8mb4
  318. collation-server = utf8mb4_unicode_ci
  319.  
  320. [mysql]
  321. default-character-set = utf8mb4
  322. "
  323.     deb_cnf_path="/etc/mysql/conf.d/barracuda.cnf"
  324.     centos_cnf_path="/etc/my.cnf.d/barracuda.cnf"
  325.  
  326.     if [ $OS == "centos" ]; then
  327.  
  328.         echo "$config" > $centos_cnf_path
  329.         if [ $OS_VER == "6" ]; then
  330.             run_cmd sudo service mysql restart
  331.         elif [ $OS_VER == "7" ]; then
  332.             run_cmd sudo systemctl restart mysql
  333.         fi
  334.  
  335.     elif [ $OS == "debian" ] || [ $OS == "Ubuntu" ]; then
  336.         echo "$config" > $deb_cnf_path
  337.         sudo service mysql restart
  338.     fi
  339. }
  340.  
  341. setup_debconf() {
  342.     debconf-set-selections <<< "postfix postfix/mailname string `hostname`"
  343.     debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
  344.     debconf-set-selections <<< "mariadb-server-5.5 mysql-server/root_password password $MSQ_PASS"
  345.     debconf-set-selections <<< "mariadb-server-5.5 mysql-server/root_password_again password $MSQ_PASS"
  346. }
  347.  
  348. install_bench() {
  349.     run_cmd sudo su $FRAPPE_USER -c "cd /home/$FRAPPE_USER && git clone https://github.com/frappe/bench --branch v5.0 bench-repo"
  350.     if hash pip-2.7 &> /dev/null; then
  351.         PIP="pip-2.7"
  352.     elif hash pip2.7 &> /dev/null; then
  353.         PIP="pip2.7"
  354.     elif hash pip2 &> /dev/null; then
  355.         PIP="pip2"
  356.     elif hash pip &> /dev/null; then
  357.         PIP="pip"
  358.     else
  359.         echo PIP not installed
  360.         exit 1
  361.     fi
  362.     run_cmd sudo $PIP install -e /home/$FRAPPE_USER/bench-repo
  363. }
  364.  
  365. setup_bench() {
  366.     echo Installing frappe-bench
  367.     FRAPPE_BRANCH="v5.0"
  368.     ERPNEXT_APPS_JSON="https://raw.githubusercontent.com/frappe/bench/master/install_scripts/erpnext-apps.json"
  369.     if $SETUP_PROD; then
  370.         FRAPPE_BRANCH="v5.0"
  371.         ERPNEXT_APPS_JSON="file:///home/frappe/erpnext-apps-master.json"
  372.     fi
  373.        
  374.     run_cmd sudo su $FRAPPE_USER -c "cd /home/$FRAPPE_USER && bench init frappe-bench --frappe-branch $FRAPPE_BRANCH --apps_path $ERPNEXT_APPS_JSON"
  375.     echo Setting up first site
  376.     echo /home/$FRAPPE_USER/frappe-bench > /etc/frappe_bench_dir
  377.     run_cmd sudo su $FRAPPE_USER -c "cd /home/$FRAPPE_USER/frappe-bench && bench new-site erp1 --mariadb-root-password $MSQ_PASS --admin-password Password"
  378.     if [ "$FRAPPE_BRANCH" == "develop" ]; then
  379.         run_cmd sudo su $FRAPPE_USER -c "cd /home/$FRAPPE_USER/frappe-bench && bench install-app erpnext"
  380.     else
  381.         run_cmd sudo su $FRAPPE_USER -c "cd /home/$FRAPPE_USER/frappe-bench && bench frappe --install_app erpnext"
  382.     fi
  383.     run_cmd bash -c "cd /home/$FRAPPE_USER/frappe-bench && bench setup sudoers $FRAPPE_USER"
  384.     if $SETUP_PROD; then
  385.         run_cmd bash -c "cd /home/$FRAPPE_USER/frappe-bench && bench setup production $FRAPPE_USER"
  386.     fi
  387. }
  388.  
  389. add_user() {
  390. # Check if script is running as root and is not running as sudo. We want to skip
  391. # this step if the user is already running this script with sudo as a non root
  392. # user
  393.     if [ "$FRAPPE_USER" == "false" ]; then
  394.         if [[ $SUDO_UID -eq 0 ]] && [[ $EUID -eq 0 ]]; then
  395.             export FRAPPE_USER="frappe"
  396.         else
  397.             export FRAPPE_USER="$SUDO_USER"
  398.         fi
  399.     fi
  400.  
  401.     USER_EXISTS=`bash -c "id $FRAPPE_USER > /dev/null 2>&1  && echo true || (echo false && exit 0)"`
  402.  
  403.     if [ $USER_EXISTS == "false" ]; then
  404.         useradd -m -d /home/$FRAPPE_USER -s $SHELL $FRAPPE_USER
  405.         echo $FRAPPE_USER:$FRAPPE_USER_PASS | chpasswd
  406.         chmod o+x /home/$FRAPPE_USER
  407.         chmod o+r /home/$FRAPPE_USER
  408.     fi
  409. }
  410.  
  411. main() {
  412.     set_opts $@
  413.     get_distro
  414.     add_maria_db_repo
  415.     echo Installing packages for $OS\. This might take time...
  416.     install_packages
  417.     if [ $OS == "centos" ]; then
  418.         if [ $OS_VER == "6" ]; then
  419.             echo "Installing supervisor"
  420.             install_supervisor_centos6
  421.             echo "Configuring CentOS services"
  422.             configure_services_centos6
  423.             echo "Starting services"
  424.             start_services_centos6
  425.         elif [ $OS_VER == "7" ]; then
  426.             echo "Configuring CentOS services"
  427.             configure_services_centos7
  428.             echo "Starting services"
  429.             start_services_centos7
  430.         fi
  431.         configure_mariadb_centos
  432.     fi
  433.     configure_mariadb
  434.     echo "Adding frappe user"
  435.     add_user
  436.     install_bench
  437.     if $SETUP_BENCH; then
  438.         setup_bench
  439.     fi
  440.  
  441.     echo
  442.     RUNNING=""
  443.     if $SETUP_PROD; then
  444.         RUNNING=" and is running on port 80"
  445.     fi
  446.     echo "Frappe/ERPNext is installed successfully$RUNNING."
  447.     print_msg > ~/frappe_passwords.txt
  448.     print_msg
  449.     echo
  450.     echo "The passwords are also stored at ~/frappe_passwords.txt"
  451.     echo "You can remove this file after making a note of the passwords."
  452. }
  453.  
  454. main $@
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement