Last active
September 11, 2016 10:08
-
-
Save jasimmk/ac36aa13c20a97036f34919656d1e5b0 to your computer and use it in GitHub Desktop.
Automate installation of Odoo9 and Postgres 9.5 in singlestep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -u | |
# Single command odoo 9 installation. For Ubuntu 16.04 | |
# | |
# TODO: If source folder exists, we will not be cloning odoo repo | |
# TODO: Install this only on test enviroments. Script installs odoo and postgres | |
# Server. Please don't attempt on production systems unless you know how to | |
# fix the script | |
# | |
# MIT License | |
# Copyright (c) 2016 Jasim Muhammed | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# Based on | |
# http://www.schenkels.nl/2015/09/odoo-v9-install-script-ubuntu-14-04/ | |
# http://openies.com/blog/install-openerp-odoo-9-on-ubuntu-server-14-04-lts/ | |
PY27=$(which python2.7) | |
BASE_DIR=$(cd $(dirname "${0}") && pwd -P); | |
# Default configuations | |
ODOO_USER="odoo" | |
ODOO_ADMIN_PASSWD="odoo_admin" | |
ODOO_ADMIN_USER="admin" | |
ODOO_HOST="127.0.0.1" | |
ODOO_PORT="8069" | |
ODOO_NAME="odoo-server" | |
ODOO_CONFIG_FILE="/etc/${ODOO_NAME}.conf" | |
ODOO_HOME_DIR="/opt/${ODOO_USER}" | |
ODOO_APP_DIR="${ODOO_HOME_DIR}/src" | |
ODOO_ADDONS_DIR="${ODOO_HOME_DIR}/addons" | |
ODOO_VENV_DIR="${ODOO_HOME_DIR}/venv" | |
ODOO_VENV_PY27="${ODOO_VENV_DIR}/bin/python" | |
ODOO_VENV_PIP="${ODOO_VENV_DIR}/bin/pip" | |
ODOO_LOGS_DIR="${ODOO_HOME_DIR}/logs" | |
ODOO_LOG_FILE="${ODOO_LOGS_DIR}/odoo-server.log" | |
ODOO_SERVICE_NAME="${ODOO_NAME}.service" | |
ODOO_SERVICE_FILE="/lib/systemd/system/${ODOO_SERVICE_NAME}" | |
ODOO_SERVICE_PID="/run/odoo/${ODOO_NAME}.pid" | |
ODOO_PROFILE_FILE="$ODOO_HOME_DIR/.bashrc" | |
ODOO_REQFILE_URL="https://raw.githubusercontent.com/odoo/odoo/9.0/requirements.txt" | |
ODOO_REPO_URL="https://www.github.com/odoo/odoo" | |
ODOO_REPO_BRANCH="9.0" | |
POSTGRES_HOST="127.0.0.1" | |
POSTGRES_PORT="5432" | |
POSTGRES_USER="postgres" | |
POSTGRES_PASSWD="odoo_passwd" | |
POSTGRES_SERVICE_NAME="postgresql.service" | |
POSTGRES_CREATE_CMD="createuser --createdb --username postgres --no-createrole --no-superuser --no-password ${ODOO_USER}" | |
POSTGRES_CHANGE_PASSWD_SQL="ALTER USER ${ODOO_USER} WITH PASSWORD '${POSTGRES_PASSWD}'" | |
POSTGERS_CHECK_USER_SQL="SELECT 1 FROM pg_roles WHERE rolname='${ODOO_USER}'" | |
POSTGRES_REPO_KEY_URL="https://www.postgresql.org/media/keys/ACCC4CF8.asc" | |
# Coloring codes | |
RES_COL=60 | |
MOVE_TO_COL="echo -en \\033[${RES_COL}G" | |
SETCOLOR_SUCCESS="echo -en \\033[1;32m" | |
SETCOLOR_FAILURE="echo -en \\033[1;31m" | |
SETCOLOR_NORMAL="echo -en \\033[0;39m" | |
# WKTMLPDF_PACKAGE_FILE="wkhtmltox-0.12.2.1_linux-trusty-amd64.deb" | |
# WKTMLPDF_URL="http://download.gna.org/wkhtmltopdf/0.12/0.12.2.1/${WKTMLPDF_PACKAGE_FILE}" | |
# General Functions | |
OUT=0 | |
reset_out() { | |
OUT=0 | |
} | |
count_resonse() { | |
OUT=$((OUT + $?)) | |
} | |
# Function to print the SUCCESS status | |
echo_success() { | |
$MOVE_TO_COL | |
echo -n "[" | |
$SETCOLOR_SUCCESS | |
echo -n $" OK " | |
$SETCOLOR_NORMAL | |
echo -n "]" | |
echo -ne "\n" | |
return 0 | |
} | |
# Function to print the FAILED status message | |
echo_failure() { | |
$MOVE_TO_COL | |
echo -n "[" | |
$SETCOLOR_FAILURE | |
echo -n $"FAILED" | |
$SETCOLOR_NORMAL | |
echo -n "]" | |
echo -ne "\n" | |
return 1 | |
} | |
tab_print() { | |
$SETCOLOR_SUCCESS | |
echo -n "${1}" | |
$MOVE_TO_COL | |
echo -n ":" | |
echo -n " " | |
echo -n "${2}" | |
$SETCOLOR_NORMAL | |
echo -ne "\n" | |
} | |
# Function to start certain set of tasks and resets counter | |
task_start() { | |
echo -e "${1}" | |
reset_out | |
} | |
task_end() { | |
if [ $OUT -eq 0 ];then | |
echo_success | |
else | |
echo_failure | |
fi | |
} | |
task_start "\n---- Checking root access ----" | |
{ | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root" 1>&2 | |
echo_failure | |
exit 1 | |
fi | |
} | |
task_end | |
task_start "\n---- Set locales ----" | |
{ | |
sudo locale-gen --purge en_US.UTF-8 | |
cat > /etc/default/locale << LOCALE_MSG | |
LANG=en_US.UTF-8 | |
LANGUAGE= | |
LC_CTYPE="en_US.UTF-8" | |
LC_NUMERIC="en_US.UTF-8" | |
LC_TIME="en_US.UTF-8" | |
LC_COLLATE="en_US.UTF-8" | |
LC_MONETARY="en_US.UTF-8" | |
LC_MESSAGES="en_US.UTF-8" | |
LC_PAPER="en_US.UTF-8" | |
LC_NAME="en_US.UTF-8" | |
LC_ADDRESS="en_US.UTF-8" | |
LC_TELEPHONE="en_US.UTF-8" | |
LC_MEASUREMENT="en_US.UTF-8" | |
LC_IDENTIFICATION="en_US.UTF-8" | |
LC_ALL=en_US.UTF-8 | |
LOCALE_MSG | |
count_resonse | |
} | |
task_end | |
task_start "\n---- Setting up postgresql repositories ----" | |
{ | |
echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" > /etc/apt/sources.list.d/postgres_server.list | |
count_resonse | |
wget --quiet -O - "${POSTGRES_REPO_KEY_URL}" | sudo apt-key add - | |
count_resonse | |
} | |
task_end | |
task_start "\n---- Installing and Updating necessary packages ----" | |
{ | |
apt-get update | |
count_resonse | |
apt-get install git postgresql-9.5 postgresql-server-dev-9.5 python-pip python-dev \ | |
python-virtualenv libevent-dev gcc libjpeg-dev libxml2-dev libxslt-dev node-less libldap2-dev \ | |
libssl-dev libsasl2-dev wkhtmltopdf -y | |
count_resonse | |
if [ $(id -u "${ODOO_USER}" 2>/dev/null || echo -1) -ge 0 ]; then | |
echo "* User ${ODOO_USER} already exists" | |
else | |
adduser --disabled-password --home="${ODOO_HOME_DIR}" --gecos "" "${ODOO_USER}" | |
fi | |
count_resonse | |
} | |
task_end | |
## sudo -H -u "${ODOO_USER}" bash -c 'echo "I am $USER, with uid $UID"' | |
task_start "\n---- Starting postgres service ----" | |
{ | |
systemctl restart "${POSTGRES_SERVICE_NAME}" | |
count_resonse | |
} | |
task_end | |
task_start "\n---- Setting up postgres users ----" | |
{ | |
cd /tmp || echo -e "Failed to navigate to temp" | |
sudo -H -u "${POSTGRES_USER}" psql postgres -tAc "${POSTGERS_CHECK_USER_SQL}"| grep -q 1 | |
if [ $? -eq 0 ];then | |
echo "* Postgres user ${ODOO_USER} already exists" | |
else | |
sudo -H -u "${POSTGRES_USER}" ${POSTGRES_CREATE_CMD} | |
fi | |
count_resonse | |
sudo -H -u "${POSTGRES_USER}" psql -c "${POSTGRES_CHANGE_PASSWD_SQL}" | |
count_resonse | |
cd "${BASE_DIR}" || echo -e "Failed to navigate to ${BASE_DIR}" | |
} | |
task_end | |
task_start "\n---- Setting Odoo log files ----" | |
{ | |
# Logs & Source files | |
sudo -H -u "${ODOO_USER}" mkdir -p "${ODOO_LOGS_DIR}" | |
count_resonse | |
sudo -H -u "${ODOO_USER}" mkdir -p "${ODOO_ADDONS_DIR}" | |
count_resonse | |
sudo -H -u "${ODOO_USER}" touch "${ODOO_LOG_FILE}" | |
count_resonse | |
} | |
task_end | |
# Setup virtual env if does not exist | |
task_start "\n---- Setting Odoo virtual environment ----" | |
{ | |
if [ ! -f "${ODOO_VENV_PY27}" ]; then | |
sudo -H -u "${ODOO_USER}" virtualenv -qp "${PY27}" "${ODOO_VENV_DIR}" | |
count_resonse | |
sudo -H -u "${ODOO_USER}" "${ODOO_VENV_PIP}" install --upgrade pip | |
count_resonse | |
fi | |
sudo -H -u "${ODOO_USER}" touch "${ODOO_PROFILE_FILE}" | |
count_resonse | |
if ! grep -q '##ODOO_VENV' "${ODOO_PROFILE_FILE}"; then | |
echo -e "* Installing Odoo virtual env to ${ODOO_PROFILE_FILE}" | |
echo -e "##ODOO_VENV\nsource \"${ODOO_VENV_DIR}/bin/activate\"\ncd ${ODOO_HOME_DIR}" >> "${ODOO_PROFILE_FILE}" | |
fi | |
count_resonse | |
} | |
task_end | |
# Setting up requirements and Wkhtmltopdf | |
task_start "\n---- Setting up requirements.txt ----" | |
{ | |
cd /tmp | |
# Wheel version of pillow doesn't have support for jpeg decoding: decoder jpeg not available | |
sudo -H -u "${ODOO_USER}" "${ODOO_VENV_PIP}" install --no-binary :all: Pillow==2.7.0 | |
wget -O requirements.txt "${ODOO_REQFILE_URL}" && sudo -H -u "${ODOO_USER}" "${ODOO_VENV_PIP}" install --no-binary :all: -r requirements.txt | |
count_resonse | |
# cd /tmp && wget -O $WKTMLPDF_PACKAGE_FILE $WKTMLPDF_URL && dpkg -i --force $WKTMLPDF_PACKAGE_FILE | |
# count_resonse | |
} | |
task_end | |
# Git clone | |
task_start "\n---- Checking out odoo files from Repo ----" | |
{ | |
if [ -d "${ODOO_APP_DIR}" ]; then | |
echo "* Odoo git directory already exists" | |
else | |
sudo -H -u "${ODOO_USER}" git clone "${ODOO_REPO_URL}" --depth 1 --branch "${ODOO_REPO_BRANCH}" --single-branch "${ODOO_APP_DIR}" | |
fi | |
count_resonse | |
} | |
task_end | |
# Setting up /etc/odoo-server.conf | |
task_start "\n---- Setting up Odoo Configurations ----" | |
{ | |
cp $ODOO_APP_DIR/debian/openerp-server.conf $ODOO_CONFIG_FILE | |
count_resonse | |
chown "${ODOO_USER}:${ODOO_USER}" ${ODOO_CONFIG_FILE} | |
count_resonse | |
chmod 640 ${ODOO_CONFIG_FILE} | |
count_resonse | |
echo -e "* Change server config file" | |
echo -e "** Remove unwanted lines" | |
sed -i "/db_password/d" ${ODOO_CONFIG_FILE} | |
sed -i "/db_user/d" ${ODOO_CONFIG_FILE} | |
sed -i "/db_host/d" ${ODOO_CONFIG_FILE} | |
sed -i "/admin_passwd/d" ${ODOO_CONFIG_FILE} | |
sed -i "/addons_path/d" ${ODOO_CONFIG_FILE} | |
count_resonse | |
echo -e "** Add correct lines" | |
{ | |
echo "db_user = ${ODOO_USER}" | |
echo "db_host = ${POSTGRES_HOST}" | |
echo "db_password = ${POSTGRES_PASSWD}" | |
echo "admin_passwd = ${ODOO_ADMIN_PASSWD}" | |
echo "logfile = ${ODOO_LOG_FILE}" | |
echo "addons_path=${ODOO_APP_DIR}/addons,${ODOO_ADDONS_DIR}" | |
} >> "${ODOO_CONFIG_FILE}" | |
count_resonse | |
} | |
task_end | |
# Adding ODOO as a deamon (initscript) | |
task_start "\n---- Setting up Odoo service and starting ----" | |
{ | |
sudo touch "${ODOO_SERVICE_FILE}" | |
count_resonse | |
sudo chmod 0700 "${ODOO_SERVICE_FILE}" | |
count_resonse | |
echo -e "* Create systemd unit file" | |
{ | |
echo "[Unit]" | |
echo "Description=ODOO Application Server" | |
echo "Requires=${POSTGRES_SERVICE_NAME}" | |
echo "After=${POSTGRES_SERVICE_NAME}" | |
echo "[Install]" | |
echo "Alias=${ODOO_SERVICE_NAME}" | |
echo "[Service]" | |
echo "Type=simple" | |
echo "PermissionsStartOnly=true" | |
echo "User=${ODOO_USER}" | |
echo "Group=${ODOO_USER}" | |
echo "SyslogIdentifier=${ODOO_NAME}" | |
echo "PIDFile=/run/odoo/${ODOO_NAME}.pid" | |
echo "ExecStartPre=/usr/bin/install -d -m755 -o ${ODOO_USER} -g ${ODOO_USER} /run/odoo" | |
echo "ExecStart=$ODOO_VENV_PY27 ${ODOO_APP_DIR}/openerp-server -c $ODOO_CONFIG_FILE --pid=${ODOO_SERVICE_PID}" | |
echo "ExecStop=/bin/kill \$(/bin/cat ${ODOO_SERVICE_PID})" | |
echo "[Install]" | |
echo "WantedBy=multi-user.target" | |
} > "${ODOO_SERVICE_FILE}" | |
count_resonse | |
echo -e "* Enabling Systemd File" | |
sudo systemctl enable "${ODOO_SERVICE_FILE}" | |
count_resonse | |
echo -e "-- Starting ODOO Server --" | |
sudo systemctl start "${ODOO_SERVICE_NAME}" | |
count_resonse | |
} | |
task_end | |
# Tasks summary | |
task_start "\n---- Installation Summary ----" | |
{ | |
tab_print "Odoo URL" "${ODOO_HOST}:${ODOO_PORT}" | |
tab_print "Odoo admin user" "${ODOO_ADMIN_USER}" | |
tab_print "Odoo admin password" "${ODOO_ADMIN_PASSWD}" | |
tab_print "Postgres URL" "${POSTGRES_HOST}:${POSTGRES_PORT}" | |
tab_print "Odoo db user" "${ODOO_USER}" | |
tab_print "Odoo db password" "${POSTGRES_PASSWD}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment