2020-05-04 20:29:38 +00:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
# Description: Install and manage a Chatwoot installation.
|
2022-05-25 07:00:00 +00:00
|
|
|
|
# OS: Ubuntu 20.04 LTS
|
2022-06-22 18:09:01 +00:00
|
|
|
|
# Script Version: 2.0.7
|
2021-02-17 14:33:43 +00:00
|
|
|
|
# Run this script as root
|
2020-05-04 20:29:38 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
set -eu -o errexit -o pipefail -o noclobber -o nounset
|
|
|
|
|
|
|
|
|
|
# -allow a command to fail with !’s side effect on errexit
|
|
|
|
|
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
|
|
|
|
|
! getopt --test > /dev/null
|
|
|
|
|
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
|
|
|
|
|
echo '`getopt --test` failed in this environment.'
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Global variables
|
|
|
|
|
# option --output/-o requires 1 argument
|
|
|
|
|
LONGOPTS=console,debug,help,install:,logs:,restart,ssl,upgrade,webserver,version
|
|
|
|
|
OPTIONS=cdhi:l:rsuwv
|
|
|
|
|
CWCTL_VERSION="2.0.7"
|
2022-05-27 12:03:24 +00:00
|
|
|
|
pg_pass=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 15 ; echo '')
|
2022-06-22 18:09:01 +00:00
|
|
|
|
|
|
|
|
|
# if user does not specify an option
|
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
|
echo "No options specified. Use --help to learn more."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# -regarding ! and PIPESTATUS see above
|
|
|
|
|
# -temporarily store output to be able to check for errors
|
|
|
|
|
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
|
|
|
|
|
# -pass arguments only via -- "$@" to separate them correctly
|
|
|
|
|
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
|
|
|
|
|
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
|
|
|
|
|
# e.g. return value is 1
|
|
|
|
|
# then getopt has complained about wrong arguments to stdout
|
|
|
|
|
exit 2
|
|
|
|
|
fi
|
|
|
|
|
# read getopt’s output this way to handle the quoting right:
|
|
|
|
|
eval set -- "$PARSED"
|
|
|
|
|
|
|
|
|
|
c=n d=n h=n i=n l=n r=n s=n u=n w=n v=n BRANCH=master SERVICE=web
|
|
|
|
|
# Iterate options in order and nicely split until we see --
|
|
|
|
|
while true; do
|
|
|
|
|
case "$1" in
|
|
|
|
|
-c|--console)
|
|
|
|
|
c=y
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
-d|--debug)
|
|
|
|
|
d=y
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
-h|--help)
|
|
|
|
|
h=y
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
-i|--install)
|
|
|
|
|
i=y
|
|
|
|
|
BRANCH="$2"
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
-l|--logs)
|
|
|
|
|
l=y
|
|
|
|
|
SERVICE="$2"
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
-r|--restart)
|
|
|
|
|
r=y
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
-s|--ssl)
|
|
|
|
|
s=y
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
-u|--upgrade)
|
|
|
|
|
u=y
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
-w|--webserver)
|
|
|
|
|
w=y
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
-v|--version)
|
|
|
|
|
v=y
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
--)
|
|
|
|
|
shift
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo "Invalid option(s) specified. Use help(-h) to learn more."
|
|
|
|
|
exit 3
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# log if debug flag set
|
|
|
|
|
if [ "$d" == "y" ]; then
|
|
|
|
|
echo "console: $c, debug: $d, help: $h, install: $i, BRANCH: $BRANCH, \
|
|
|
|
|
logs: $l, SERVICE: $SERVICE, ssl: $s, upgrade: $u, webserver: $w"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# exit if script is not run as root
|
|
|
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
|
|
|
echo 'This needs to be run as root.' >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
trap exit_handler EXIT
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Invoked upon EXIT signal from bash
|
|
|
|
|
# Upon non-zero exit, notifies the user to check log file.
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function exit_handler() {
|
|
|
|
|
if [ "$?" -ne 0 ]; then
|
|
|
|
|
echo -en "\nSome error has occured. Check '/var/log/chatwoot-setup.log' for details.\n"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
2021-12-14 12:56:57 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Read user input related to domain setup
|
|
|
|
|
# Globals:
|
|
|
|
|
# domain_name
|
|
|
|
|
# le_email
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function get_domain_info() {
|
2022-06-22 18:09:01 +00:00
|
|
|
|
read -rp 'Enter the domain/subdomain for Chatwoot (e.g., chatwoot.domain.com): ' domain_name
|
|
|
|
|
read -rp 'Enter an email address for LetsEncrypt to send reminders when your SSL certificate is up for renewal: ' le_email
|
2022-05-25 07:00:00 +00:00
|
|
|
|
cat << EOF
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
This script will generate SSL certificates via LetsEncrypt and
|
|
|
|
|
serve Chatwoot at https://$domain_name.
|
|
|
|
|
Proceed further once you have pointed your DNS to the IP of the instance.
|
2020-05-04 20:29:38 +00:00
|
|
|
|
|
|
|
|
|
EOF
|
2022-05-25 07:00:00 +00:00
|
|
|
|
read -rp 'Do you wish to proceed? (yes or no): ' exit_true
|
2022-06-22 18:09:01 +00:00
|
|
|
|
if [ "$exit_true" == "no" ]; then
|
2022-05-25 07:00:00 +00:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
2020-05-04 20:29:38 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Install common dependencies
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function install_dependencies() {
|
|
|
|
|
apt update && apt upgrade -y
|
|
|
|
|
apt install -y curl
|
|
|
|
|
curl -sL https://deb.nodesource.com/setup_12.x | bash -
|
|
|
|
|
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
|
|
|
|
|
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
|
|
|
|
|
apt update
|
2020-05-04 20:29:38 +00:00
|
|
|
|
|
2022-05-25 07:00:00 +00:00
|
|
|
|
apt install -y \
|
|
|
|
|
git software-properties-common imagemagick libpq-dev \
|
|
|
|
|
libxml2-dev libxslt1-dev file g++ gcc autoconf build-essential \
|
|
|
|
|
libssl-dev libyaml-dev libreadline-dev gnupg2 \
|
|
|
|
|
postgresql-client redis-tools \
|
|
|
|
|
nodejs yarn patch ruby-dev zlib1g-dev liblzma-dev \
|
|
|
|
|
libgmp-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev sudo
|
|
|
|
|
}
|
2020-05-04 20:29:38 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Install postgres and redis
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function install_databases() {
|
|
|
|
|
apt install -y postgresql postgresql-contrib redis-server
|
|
|
|
|
}
|
2020-05-04 20:29:38 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Install nginx and cerbot for LetsEncrypt
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function install_webserver() {
|
|
|
|
|
apt install -y nginx nginx-full certbot python3-certbot-nginx
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Create chatwoot linux user
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function create_cw_user() {
|
|
|
|
|
if ! id -u "chatwoot"; then
|
|
|
|
|
adduser --disabled-login --gecos "" chatwoot
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Install rvm(ruby version manager)
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function configure_rvm() {
|
2022-06-22 18:09:01 +00:00
|
|
|
|
create_cw_user
|
2022-05-25 07:00:00 +00:00
|
|
|
|
|
|
|
|
|
gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
|
|
|
|
|
gpg2 --keyserver hkp://keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
|
|
|
|
|
curl -sSL https://get.rvm.io | bash -s stable
|
|
|
|
|
adduser chatwoot rvm
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Save the pgpass used to setup postgres
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function save_pgpass() {
|
|
|
|
|
mkdir -p /opt/chatwoot/config
|
|
|
|
|
file="/opt/chatwoot/config/.pg_pass"
|
|
|
|
|
if ! test -f "$file"; then
|
|
|
|
|
echo $pg_pass > /opt/chatwoot/config/.pg_pass
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Get the pgpass used to setup postgres if installation fails midway
|
|
|
|
|
# and needs to be re-run
|
|
|
|
|
# Globals:
|
|
|
|
|
# pg_pass
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function get_pgpass() {
|
|
|
|
|
file="/opt/chatwoot/config/.pg_pass"
|
|
|
|
|
if test -f "$file"; then
|
|
|
|
|
pg_pass=$(cat $file)
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Configure postgres to create chatwoot db user.
|
|
|
|
|
# Enable postgres and redis systemd services.
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function configure_db() {
|
2022-06-22 18:09:01 +00:00
|
|
|
|
save_pgpass
|
|
|
|
|
get_pgpass
|
|
|
|
|
sudo -i -u postgres psql << EOF
|
|
|
|
|
\set pass `echo $pg_pass`
|
|
|
|
|
CREATE USER chatwoot CREATEDB;
|
|
|
|
|
ALTER USER chatwoot PASSWORD :'pass';
|
|
|
|
|
ALTER ROLE chatwoot SUPERUSER;
|
|
|
|
|
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
|
|
|
|
|
DROP DATABASE template1;
|
|
|
|
|
CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
|
|
|
|
|
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
|
|
|
|
|
\c template1
|
|
|
|
|
VACUUM FREEZE;
|
2022-05-25 07:00:00 +00:00
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
systemctl enable redis-server.service
|
|
|
|
|
systemctl enable postgresql
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Install Chatwoot
|
|
|
|
|
# This includes setting up ruby, cloning repo and installing dependencies.
|
|
|
|
|
# Globals:
|
|
|
|
|
# pg_pass
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function setup_chatwoot() {
|
2022-06-22 18:09:01 +00:00
|
|
|
|
local secret=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 63 ; echo '')
|
|
|
|
|
local RAILS_ENV=production
|
|
|
|
|
get_pgpass
|
2022-05-25 07:00:00 +00:00
|
|
|
|
|
|
|
|
|
sudo -i -u chatwoot << EOF
|
|
|
|
|
rvm --version
|
|
|
|
|
rvm autolibs disable
|
2022-05-27 12:03:24 +00:00
|
|
|
|
rvm install "ruby-3.0.4"
|
|
|
|
|
rvm use 3.0.4 --default
|
2022-05-25 07:00:00 +00:00
|
|
|
|
|
|
|
|
|
git clone https://github.com/chatwoot/chatwoot.git
|
|
|
|
|
cd chatwoot
|
|
|
|
|
git checkout "$BRANCH"
|
|
|
|
|
bundle
|
|
|
|
|
yarn
|
|
|
|
|
|
|
|
|
|
cp .env.example .env
|
|
|
|
|
sed -i -e "/SECRET_KEY_BASE/ s/=.*/=$secret/" .env
|
|
|
|
|
sed -i -e '/REDIS_URL/ s/=.*/=redis:\/\/localhost:6379/' .env
|
|
|
|
|
sed -i -e '/POSTGRES_HOST/ s/=.*/=localhost/' .env
|
|
|
|
|
sed -i -e '/POSTGRES_USERNAME/ s/=.*/=chatwoot/' .env
|
|
|
|
|
sed -i -e "/POSTGRES_PASSWORD/ s/=.*/=$pg_pass/" .env
|
|
|
|
|
sed -i -e '/RAILS_ENV/ s/=.*/=$RAILS_ENV/' .env
|
|
|
|
|
echo -en "\nINSTALLATION_ENV=linux_script" >> ".env"
|
|
|
|
|
|
|
|
|
|
rake assets:precompile RAILS_ENV=production
|
2021-12-14 12:56:57 +00:00
|
|
|
|
EOF
|
2022-05-25 07:00:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Run database migrations.
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function run_db_migrations(){
|
|
|
|
|
sudo -i -u chatwoot << EOF
|
|
|
|
|
cd chatwoot
|
2022-06-22 18:09:01 +00:00
|
|
|
|
RAILS_ENV=production bundle exec rails db:chatwoot_prepare
|
2020-05-04 20:29:38 +00:00
|
|
|
|
EOF
|
2022-05-25 07:00:00 +00:00
|
|
|
|
}
|
2020-05-04 20:29:38 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Setup Chatwoot systemd services and cwctl CLI
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function configure_systemd_services() {
|
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/chatwoot-web.1.service /etc/systemd/system/chatwoot-web.1.service
|
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/chatwoot-worker.1.service /etc/systemd/system/chatwoot-worker.1.service
|
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/chatwoot.target /etc/systemd/system/chatwoot.target
|
2021-12-14 12:56:57 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/chatwoot /etc/sudoers.d/chatwoot
|
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/setup_20.04.sh /usr/local/bin/cwctl
|
|
|
|
|
chmod +x /usr/local/bin/cwctl
|
|
|
|
|
|
2022-05-25 07:00:00 +00:00
|
|
|
|
systemctl enable chatwoot.target
|
|
|
|
|
systemctl start chatwoot.target
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Fetch and install SSL certificates from LetsEncrypt
|
|
|
|
|
# Modify the nginx config and restart nginx.
|
|
|
|
|
# Also modifies FRONTEND_URL in .env file.
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# domain_name
|
|
|
|
|
# le_email
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function setup_ssl() {
|
2022-06-22 18:09:01 +00:00
|
|
|
|
if [ "$d" == "y" ]; then
|
|
|
|
|
echo "debug: setting up ssl"
|
|
|
|
|
echo "debug: domain: $domain_name"
|
|
|
|
|
echo "debug: letsencrypt email: $le_email"
|
|
|
|
|
fi
|
2022-05-25 07:00:00 +00:00
|
|
|
|
curl https://ssl-config.mozilla.org/ffdhe4096.txt >> /etc/ssl/dhparam
|
|
|
|
|
wget https://raw.githubusercontent.com/chatwoot/chatwoot/develop/deployment/nginx_chatwoot.conf
|
|
|
|
|
cp nginx_chatwoot.conf /etc/nginx/sites-available/nginx_chatwoot.conf
|
|
|
|
|
certbot certonly --non-interactive --agree-tos --nginx -m "$le_email" -d "$domain_name"
|
|
|
|
|
sed -i "s/chatwoot.domain.com/$domain_name/g" /etc/nginx/sites-available/nginx_chatwoot.conf
|
|
|
|
|
ln -s /etc/nginx/sites-available/nginx_chatwoot.conf /etc/nginx/sites-enabled/nginx_chatwoot.conf
|
|
|
|
|
systemctl restart nginx
|
|
|
|
|
sudo -i -u chatwoot << EOF
|
|
|
|
|
cd chatwoot
|
|
|
|
|
sed -i "s/http:\/\/0.0.0.0:3000/https:\/\/$domain_name/g" .env
|
2021-02-17 14:33:43 +00:00
|
|
|
|
EOF
|
2022-05-25 07:00:00 +00:00
|
|
|
|
systemctl restart chatwoot.target
|
|
|
|
|
}
|
2021-12-14 12:56:57 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Setup logging
|
|
|
|
|
# Globals:
|
|
|
|
|
# LOG_FILE
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
2022-05-25 07:00:00 +00:00
|
|
|
|
function setup_logging() {
|
|
|
|
|
touch /var/log/chatwoot-setup.log
|
|
|
|
|
LOG_FILE="/var/log/chatwoot-setup.log"
|
|
|
|
|
}
|
2021-12-14 12:56:57 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
function ssl_success_message() {
|
|
|
|
|
cat << EOF
|
2022-05-25 07:00:00 +00:00
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
***************************************************************************
|
|
|
|
|
Woot! Woot!! Chatwoot server installation is complete.
|
|
|
|
|
The server will be accessible at https://$domain_name
|
|
|
|
|
|
|
|
|
|
Join the community at https://chatwoot.com/community
|
|
|
|
|
***************************************************************************
|
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cwctl_message() {
|
|
|
|
|
echo $'\U0001F680 Try out the all new Chatwoot CLI tool to manage your installation.'
|
|
|
|
|
echo $'\U0001F680 Type "cwctl --help" to learn more.'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# This function handles the installation(-i/--install)
|
|
|
|
|
# Globals:
|
|
|
|
|
# CW_VERSION
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function get_cw_version() {
|
|
|
|
|
CW_VERSION=$(curl -s https://app.chatwoot.com/api | python3 -c 'import sys,json;data=json.loads(sys.stdin.read()); print(data["version"])')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# This function handles the installation(-i/--install)
|
|
|
|
|
# Globals:
|
|
|
|
|
# configure_webserver
|
|
|
|
|
# install_pg_redis
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function install() {
|
|
|
|
|
get_cw_version
|
2022-05-25 07:00:00 +00:00
|
|
|
|
cat << EOF
|
|
|
|
|
|
|
|
|
|
***************************************************************************
|
2022-06-22 18:09:01 +00:00
|
|
|
|
Chatwoot Installation (v$CW_VERSION)
|
2022-05-25 07:00:00 +00:00
|
|
|
|
***************************************************************************
|
|
|
|
|
|
2022-06-02 14:21:50 +00:00
|
|
|
|
For more verbose logs, open up a second terminal and follow along using,
|
2022-06-22 18:09:01 +00:00
|
|
|
|
'tail -f /var/log/chatwoot-setup.log'.
|
2022-05-25 07:00:00 +00:00
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
sleep 3
|
|
|
|
|
read -rp 'Would you like to configure a domain and SSL for Chatwoot?(yes or no): ' configure_webserver
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
if [ "$configure_webserver" == "yes" ]; then
|
2022-05-25 07:00:00 +00:00
|
|
|
|
get_domain_info
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo -en "\n"
|
|
|
|
|
read -rp 'Would you like to install Postgres and Redis? (Answer no if you plan to use external services): ' install_pg_redis
|
|
|
|
|
|
2022-06-02 14:21:50 +00:00
|
|
|
|
echo -en "\n➥ 1/9 Installing dependencies. This takes a while.\n"
|
2022-05-25 07:00:00 +00:00
|
|
|
|
install_dependencies &>> "${LOG_FILE}"
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
if [ "$install_pg_redis" != "no" ]; then
|
|
|
|
|
echo "➥ 2/9 Installing databases."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
install_databases &>> "${LOG_FILE}"
|
2022-06-22 18:09:01 +00:00
|
|
|
|
else
|
|
|
|
|
echo "➥ 2/9 Skipping Postgres and Redis installation."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
if [ "$configure_webserver" == "yes" ]; then
|
|
|
|
|
echo "➥ 3/9 Installing webserver."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
install_webserver &>> "${LOG_FILE}"
|
2022-06-22 18:09:01 +00:00
|
|
|
|
else
|
|
|
|
|
echo "➥ 3/9 Skipping webserver installation."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "➥ 4/9 Setting up Ruby"
|
|
|
|
|
configure_rvm &>> "${LOG_FILE}"
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
if [ "$install_pg_redis" != "no" ]; then
|
|
|
|
|
echo "➥ 5/9 Setting up the database."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
configure_db &>> "${LOG_FILE}"
|
2022-06-22 18:09:01 +00:00
|
|
|
|
else
|
|
|
|
|
echo "➥ 5/9 Skipping database setup."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
echo "➥ 6/9 Installing Chatwoot. This takes a long while."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
setup_chatwoot &>> "${LOG_FILE}"
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
if [ "$install_pg_redis" != "no" ]; then
|
|
|
|
|
echo "➥ 7/9 Running database migrations."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
run_db_migrations &>> "${LOG_FILE}"
|
2022-06-22 18:09:01 +00:00
|
|
|
|
else
|
|
|
|
|
echo "➥ 7/9 Skipping database migrations."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
echo "➥ 8/9 Setting up systemd services."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
configure_systemd_services &>> "${LOG_FILE}"
|
|
|
|
|
|
|
|
|
|
public_ip=$(curl http://checkip.amazonaws.com -s)
|
2022-05-27 12:03:24 +00:00
|
|
|
|
|
2022-05-25 07:00:00 +00:00
|
|
|
|
if [ "$configure_webserver" != "yes" ]
|
|
|
|
|
then
|
|
|
|
|
cat << EOF
|
2022-06-22 18:09:01 +00:00
|
|
|
|
➥ 9/9 Skipping SSL/TLS setup.
|
2022-05-25 07:00:00 +00:00
|
|
|
|
|
|
|
|
|
***************************************************************************
|
|
|
|
|
Woot! Woot!! Chatwoot server installation is complete.
|
|
|
|
|
The server will be accessible at http://$public_ip:3000
|
|
|
|
|
|
2022-05-27 12:03:24 +00:00
|
|
|
|
To configure a domain and SSL certificate, follow the guide at
|
2022-05-25 07:00:00 +00:00
|
|
|
|
https://www.chatwoot.com/docs/deployment/deploy-chatwoot-in-linux-vm
|
|
|
|
|
|
|
|
|
|
Join the community at https://chatwoot.com/community
|
|
|
|
|
***************************************************************************
|
2022-06-22 18:09:01 +00:00
|
|
|
|
|
2022-05-25 07:00:00 +00:00
|
|
|
|
EOF
|
2022-06-22 18:09:01 +00:00
|
|
|
|
cwctl_message
|
2022-05-25 07:00:00 +00:00
|
|
|
|
else
|
2022-06-22 18:09:01 +00:00
|
|
|
|
echo "➥ 9/9 Setting up SSL/TLS."
|
2022-05-25 07:00:00 +00:00
|
|
|
|
setup_ssl &>> "${LOG_FILE}"
|
2022-06-22 18:09:01 +00:00
|
|
|
|
ssl_success_message
|
|
|
|
|
cwctl_message
|
2022-05-25 07:00:00 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$install_pg_redis" == "no" ]
|
|
|
|
|
then
|
|
|
|
|
cat <<EOF
|
2022-06-22 18:09:01 +00:00
|
|
|
|
|
2022-05-25 07:00:00 +00:00
|
|
|
|
***************************************************************************
|
|
|
|
|
The database migrations had not run as Postgres and Redis were not installed
|
|
|
|
|
as part of the installation process. After modifying the environment
|
|
|
|
|
variables (in the .env file) with your external database credentials, run
|
|
|
|
|
the database migrations using the below command.
|
2022-06-02 14:21:50 +00:00
|
|
|
|
'RAILS_ENV=production bundle exec rails db:chatwoot_prepare'.
|
2022-05-25 07:00:00 +00:00
|
|
|
|
***************************************************************************
|
2022-06-22 18:09:01 +00:00
|
|
|
|
|
2022-05-25 07:00:00 +00:00
|
|
|
|
EOF
|
2022-06-22 18:09:01 +00:00
|
|
|
|
cwctl_message
|
2022-05-25 07:00:00 +00:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-22 18:09:01 +00:00
|
|
|
|
##############################################################################
|
|
|
|
|
# Access ruby console (-c/--console)
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function get_console() {
|
|
|
|
|
sudo -i -u chatwoot bash -c " cd chatwoot && RAILS_ENV=production bundle exec rails c"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Prints the help message (-c/--console)
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function help() {
|
|
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
|
Usage: cwctl [OPTION]...
|
|
|
|
|
Install and manage your Chatwoot installation.
|
|
|
|
|
|
|
|
|
|
Example: cwctl -i master
|
|
|
|
|
Example: cwctl -l web
|
|
|
|
|
Example: cwctl --logs worker
|
|
|
|
|
Example: cwctl --upgrade
|
|
|
|
|
Example: cwctl -c
|
|
|
|
|
|
|
|
|
|
Installation/Upgrade:
|
|
|
|
|
-i, --install install Chatwoot with the git branch specified
|
|
|
|
|
-u, --upgrade upgrade Chatwoot to latest version
|
|
|
|
|
-s, --ssl fetch and install ssl certificates using LetsEncrypt
|
|
|
|
|
-w, --webserver install and configure Nginx webserver with SSL
|
|
|
|
|
|
|
|
|
|
Management:
|
|
|
|
|
-c, --console open ruby console
|
|
|
|
|
-l, --logs view logs from Chatwoot. Supported values include web/worker.
|
|
|
|
|
-r, --restart restart Chatwoot server
|
|
|
|
|
|
|
|
|
|
Miscellaneous:
|
|
|
|
|
-d, --debug show debug messages
|
|
|
|
|
-v, --version display version information and exit
|
|
|
|
|
-h, --help display this help text and exit
|
|
|
|
|
|
|
|
|
|
Exit status:
|
|
|
|
|
Returns 0 if successful; non-zero otherwise.
|
|
|
|
|
|
|
|
|
|
Report bugs at https://github.com/chatwoot/chatwoot/issues
|
|
|
|
|
Get help, https://chatwoot.com/community
|
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Get Chatwoot web/worker logs (-l/--logs)
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function get_logs() {
|
|
|
|
|
if [ "$SERVICE" == "worker" ]; then
|
|
|
|
|
journalctl -u chatwoot-worker.1.service -f
|
|
|
|
|
else
|
|
|
|
|
journalctl -u chatwoot-web.1.service -f
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Setup SSL (-s/--ssl)
|
|
|
|
|
# Installs nginx if not available.
|
|
|
|
|
# Globals:
|
|
|
|
|
# domain_name
|
|
|
|
|
# le_email
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function ssl() {
|
|
|
|
|
echo "Setting up ssl"
|
|
|
|
|
get_domain_info
|
|
|
|
|
if ! systemctl -q is-active nginx; then
|
|
|
|
|
install_webserver
|
|
|
|
|
fi
|
|
|
|
|
setup_ssl
|
|
|
|
|
ssl_success_message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Upgrade an existing installation to latest stable version(-u/--upgrade)
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function upgrade() {
|
|
|
|
|
get_cw_version
|
|
|
|
|
echo "Upgrading Chatwoot to v$CW_VERSION"
|
|
|
|
|
|
|
|
|
|
sudo -i -u chatwoot << "EOF"
|
|
|
|
|
|
|
|
|
|
# Navigate to the Chatwoot directory
|
|
|
|
|
cd chatwoot
|
|
|
|
|
|
|
|
|
|
# Pull the latest version of the master branch
|
|
|
|
|
git checkout master && git pull
|
|
|
|
|
|
|
|
|
|
# Ensure the ruby version is upto date
|
|
|
|
|
# Parse the latest ruby version
|
|
|
|
|
latest_ruby_version="$(cat '.ruby-version')"
|
|
|
|
|
rvm install "ruby-$latest_ruby_version"
|
|
|
|
|
rvm use "$latest_ruby_version" --default
|
|
|
|
|
|
|
|
|
|
# Update dependencies
|
|
|
|
|
bundle
|
|
|
|
|
yarn
|
|
|
|
|
|
|
|
|
|
# Recompile the assets
|
|
|
|
|
rake assets:precompile RAILS_ENV=production
|
|
|
|
|
|
|
|
|
|
# Migrate the database schema
|
|
|
|
|
RAILS_ENV=production bundle exec rake db:migrate
|
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
# Copy the updated targets
|
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/chatwoot-web.1.service /etc/systemd/system/chatwoot-web.1.service
|
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/chatwoot-worker.1.service /etc/systemd/system/chatwoot-worker.1.service
|
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/chatwoot.target /etc/systemd/system/chatwoot.target
|
|
|
|
|
|
|
|
|
|
cp /home/chatwoot/chatwoot/deployment/chatwoot /etc/sudoers.d/chatwoot
|
|
|
|
|
# TODO:(@vn) handle cwctl updates
|
|
|
|
|
|
|
|
|
|
systemctl daemon-reload
|
|
|
|
|
|
|
|
|
|
# Restart the chatwoot server
|
|
|
|
|
systemctl restart chatwoot.target
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Restart Chatwoot server (-r/--restart)
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function restart() {
|
|
|
|
|
systemctl restart chatwoot.target
|
|
|
|
|
systemctl status chatwoot.target
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Install nginx and setup SSL (-w/--webserver)
|
|
|
|
|
# Globals:
|
|
|
|
|
# domain_name
|
|
|
|
|
# le_email
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function webserver() {
|
|
|
|
|
echo "Installing nginx"
|
|
|
|
|
ssl
|
|
|
|
|
#TODO(@vn): allow installing nginx only without SSL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# Print cwctl version (-v/--version)
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function version() {
|
|
|
|
|
echo "cwctl v$CWCTL_VERSION alpha build"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
|
# main function that handles the control flow
|
|
|
|
|
# Globals:
|
|
|
|
|
# None
|
|
|
|
|
# Arguments:
|
|
|
|
|
# None
|
|
|
|
|
# Outputs:
|
|
|
|
|
# None
|
|
|
|
|
##############################################################################
|
|
|
|
|
function main() {
|
|
|
|
|
setup_logging
|
|
|
|
|
|
|
|
|
|
if [ "$c" == "y" ]; then
|
|
|
|
|
get_console
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$h" == "y" ]; then
|
|
|
|
|
help
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$i" == "y" ]; then
|
|
|
|
|
install
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$l" == "y" ]; then
|
|
|
|
|
get_logs
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$r" == "y" ]; then
|
|
|
|
|
restart
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$s" == "y" ]; then
|
|
|
|
|
ssl
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$u" == "y" ]; then
|
|
|
|
|
upgrade
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$w" == "y" ]; then
|
|
|
|
|
webserver
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "$v" == "y" ]; then
|
|
|
|
|
version
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 07:00:00 +00:00
|
|
|
|
main "$@"
|