Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ This script automatically installs EspoCRM as a Docker image with NGINX server a

```
wget https://github.com/espocrm/espocrm-installer/releases/latest/download/install.sh
sudo bash install.sh
bash install.sh
```

## Run with options

```
wget https://github.com/espocrm/espocrm-installer/releases/latest/download/install.sh
sudo bash install.sh -y --ssl --letsencrypt --domain=my-espocrm.com --email=email@my-domain.com
bash install.sh -y --ssl --letsencrypt --domain=my-espocrm.com --email=email@my-domain.com
```

## Run (only for development)

```
wget -N https://raw.githubusercontent.com/espocrm/espocrm-installer/master/install.sh
sudo bash install.sh
bash install.sh
```

## Documentation
Expand Down
203 changes: 148 additions & 55 deletions install.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# EspoCRM installer MASTER
#
Expand All @@ -8,6 +8,12 @@

set -e

function restoreBackup() {
if [ -n "$backupDirectory" ] && [ -d "$backupDirectory" ]; then
cp -rp "${backupDirectory}"/* "${data[homeDirectory]}"
fi
}

function printExitError() {
local message="$1"

Expand All @@ -29,14 +35,57 @@ printRedMessage() {
printf "${red}${message}${default}"
}

function restoreBackup() {
if [ -n "$backupDirectory" ] && [ -d "$backupDirectory" ]; then
cp -rp "${backupDirectory}"/* "${data[homeDirectory]}"
fi
function getOs() {
local osType="unknown"

case $(uname | tr '[:upper:]' '[:lower:]') in
linux*)
local linuxOs=$(getLinuxOs)

if [ -n "$linuxOs" ]; then
osType="$linuxOs"
fi
;;
darwin*)
osType="macOS"
;;
msys*)
osType="windows"
;;
esac

echo "$osType"
}

if ! [ $(id -u) = 0 ]; then
printExitError "This script should be run as root or with sudo."
REQUIRED_BASH_MAJOR=6
BASH_MAJOR="${BASH_VERSION%%.*}"

if [ "$BASH_MAJOR" -lt "$REQUIRED_BASH_MAJOR" ]; then
printRedMessage "Bash >= ${REQUIRED_BASH_MAJOR}.0 is required.\nCurrent Bash version: $BASH_VERSION"

if [[ "$(getOs)" == "macOS" ]]; then
echo
echo "Detected macOS."
echo "macOS ships with Bash 3.2 (too old)."

if ! command -v brew >/dev/null 2>&1; then
printExitError "Homebrew is required to install newer Bash.\nInstall Homebrew first:\n https://brew.sh"
fi

echo "Installing newer Bash via Homebrew..."
brew install bash

NEW_BASH="$(brew --prefix)/bin/bash"

if [ ! -x "$NEW_BASH" ]; then
printExitError "Bash installation failed."
fi

echo "Re-running script with: $NEW_BASH"
exec "$NEW_BASH" "$0" "$@"
else
printExitError "Please upgrade Bash and re-run this script."
fi
fi

# Pre installation modes:
Expand All @@ -60,7 +109,7 @@ declare -A data=(
[dbPassword]=$(openssl rand -hex 10)
[adminUsername]="admin"
[adminPassword]=$(openssl rand -hex 6)
[homeDirectory]="/var/www/espocrm"
[homeDirectory]="/Users/losobka/Projekty/espocrm-installer/instance"
[action]="main"
[backupPath]="SCRIPT_DIRECTORY/espocrm-backup"
)
Expand Down Expand Up @@ -177,28 +226,6 @@ function stopProcess() {
exit 0
}

function getOs() {
local osType="unknown"

case $(uname | tr '[:upper:]' '[:lower:]') in
linux*)
local linuxOs=$(getLinuxOs)

if [ -n "$linuxOs" ]; then
osType="$linuxOs"
fi
;;
darwin*)
osType="osx"
;;
msys*)
osType="windows"
;;
esac

echo "$osType"
}

function getLinuxOs() {
declare -a linuxOsList=(centos redhat fedora ubuntu debian mint)

Expand Down Expand Up @@ -230,10 +257,24 @@ function getHostname() {
}

function getServerIp() {
local serverIP=$(hostname -I | awk '{print $1}')
case $(getOs) in
macOS )
local serverIP=$(ifconfig | awk '/inet / && $2 != "127.0.0.1" {print $2}')
;;
* )
local serverIP=$(hostname -I | awk '{print $1}')
;;
esac

if [ -z "$serverIP" ] || [ "$(isIpValid $serverIP)" != true ]; then
serverIP=$(ip route get 1 | awk '{print $NF;exit}')
case $(getOs) in
macOS )
serverIP=$(route get default | awk '/interface:/{print $2}' | xargs ipconfig getifaddr)
;;
* )
serverIP=$(ip route get 1 | awk '{print $NF;exit}')
;;
esac
fi

if [ "$(isIpValid $serverIP)" = true ]; then
Expand All @@ -249,9 +290,22 @@ function getPublicIp() {
fi
}

grepAfterKey() {
local key="$1"

case "$(getOs)" in
macOS)
sed -n "s/^.*${key}: //p"
;;
*)
grep -oP "(?<=${key}: ).*"
;;
esac
}

function getActualInstalledMode() {
if [ -f "${data[homeDirectory]}/docker-compose.yml" ]; then
head -n 1 "${data[homeDirectory]}/docker-compose.yml" | grep -oP "(?<=MODE: ).*"
head -n 1 "${data[homeDirectory]}/docker-compose.yml" | grepAfterKey "(?<=MODE: ).*"
fi
}

Expand All @@ -268,7 +322,7 @@ function getYamlValue {
local category="$2"

if [ -f "${data[homeDirectory]}/docker-compose.yml" ]; then
sed -n "/${category}:/,/networks:/p" "${data[homeDirectory]}/docker-compose.yml" | grep -oP "(?<=${keyName}: ).*" | head -1
sed -n "/${category}:/,/networks:/p" "${data[homeDirectory]}/docker-compose.yml" | grepAfterKey $keyName | head -1
fi
}

Expand Down Expand Up @@ -325,12 +379,18 @@ function isPortInUse() {
return
fi

if (echo >"/dev/tcp/localhost/$port") &>/dev/null ; then
echo true
return
fi

echo false
case "$(getOs)" in
macOS | osx)
lsof -nP -iTCP:"$port" -sTCP:LISTEN >/dev/null 2>&1 && echo true || echo false
;;
*)
if (sudo echo >"/dev/tcp/localhost/$port") >/dev/null 2>&1; then
echo true
else
echo false
fi
;;
esac
}

function isEmailValidated() {
Expand Down Expand Up @@ -397,12 +457,14 @@ function checkFixSystemRequirements() {

case "$os" in
ubuntu | debian | mint )
apt-get update; \
apt-get install -y --no-install-recommends \
sudo apt-get update; \
sudo apt-get install -y --no-install-recommends \
curl \
unzip
;;

macOS )
brew install "${$missingLibs[@]}";;
* )
printExitError "Missing libraries: ${missingLibs[@]}. Please install them and try again."
;;
Expand Down Expand Up @@ -447,7 +509,7 @@ function cleanInstallation() {

backupActualInstallation

rm -rf "${data[homeDirectory]}"
# rm -rf "${data[homeDirectory]}"
}

function rebaseInstallation() {
Expand Down Expand Up @@ -801,14 +863,16 @@ function handleInstallationMode() {
}

function downloadSourceFiles() {
rm -rf ./espocrm-installer-master.zip ./espocrm-installer-master/

download https://github.com/espocrm/espocrm-installer/archive/refs/heads/master.zip "espocrm-installer-master.zip"
unzip -q "espocrm-installer-master.zip"

if [ ! -d "./espocrm-installer-master" ]; then
printExitError "Unable to load source files."
fi
# rm -rf ./espocrm-installer-master.zip ./espocrm-installer-master/
#
# download https://github.com/espocrm/espocrm-installer/archive/refs/heads/master.zip "espocrm-installer-master.zip"
# unzip -q "espocrm-installer-master.zip"
#
# if [ ! -d "./espocrm-installer-master" ]; then
# printExitError "Unable to load source files."
# fi
mkdir espocrm-installer-master
cp -R * espocrm-installer-master/
}

function prepareDocker() {
Expand All @@ -824,8 +888,10 @@ function prepareDocker() {
printExitError "Error: Unable to find \"docker-compose.yml\" file. Try to run the installation again."
fi

mv "./installation-modes/$mode/${data[server]}/docker-compose.yml" "${data[homeDirectory]}/docker-compose.yml"
mv "./installation-modes/$mode/${data[server]}"/* "${data[homeDirectory]}/data/${data[server]}"
mkdir -p "${data[homeDirectory]}/data/${data[server]}"
cp -R "./installation-modes/$mode/${data[server]}"/* "${data[homeDirectory]}/data/${data[server]}"
# mv "./installation-modes/$mode/${data[server]}/docker-compose.yml" "${data[homeDirectory]}/docker-compose.yml"
cp "./installation-modes/$mode/${data[server]}/docker-compose.yml" "${data[homeDirectory]}/docker-compose.yml"

# Copy helper commands
find "./commands" -type f | while read file; do
Expand All @@ -838,9 +904,34 @@ function prepareDocker() {
local configFile="${data[homeDirectory]}/data/espocrm/data/config.php"

if [ -f "$configFile" ]; then
sed -i "s#'siteUrl' => '.*'#'siteUrl' => '${data[url]}'#g" "$configFile"
replace "s#'siteUrl' => '.*'#'siteUrl' => '${data[url]}'#g" "$configFile"
fi
}
sedEscape() {
printf '%s' "$1" | sed 's/[\/&]/\\&/g'
}

replace() {
local pattern="$1"
local replacement="$2"
local file="$3"

if [ -z "$pattern" ] || [ -z "$replacement" ] || [ -z "$file" ]; then
printExitError "replace: usage: replace <pattern> <replacement> <file>" >&2
fi

case "$(getOs)" in
macOS)
pattern=$(sedEscape "$pattern")
replacement=$(sedEscape "$replacement")

sed -i '' "s#${pattern}#${replacement}#g" "$file"
;;
*)
sed -i "s#${pattern}#${replacement}#g" "$file"
;;
esac
}

runDockerDatabase() {
docker compose -f "${data[homeDirectory]}/docker-compose.yml" up -d espocrm-db || {
Expand Down Expand Up @@ -970,7 +1061,9 @@ function actionMain() {
ubuntu | debian | mint )
runShellScript "system-configuration/debian.sh"
;;

macOS )
runShellScript "system-configuration/macOS.sh"
;;
* )
printExitError "Your OS is not supported by the script. We recommend to use Ubuntu server."
;;
Expand Down
7 changes: 1 addition & 6 deletions installation-modes/http/init.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#!/bin/bash
#!/usr/bin/env bash

set -euo pipefail

if ! [ $(id -u) = 0 ]; then
printf "Error: this script must be run as root\n"
exit 1
fi

source installation-modes/utils.sh

cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
Expand Down
7 changes: 1 addition & 6 deletions installation-modes/letsencrypt/init.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#!/bin/bash
#!/usr/bin/env bash

set -euo pipefail

if ! [ $(id -u) = 0 ]; then
printf "Error: this script must be run as root\n"
exit 1
fi

source installation-modes/utils.sh

cp ./installation-modes/http/nginx/espocrm.conf ./installation-modes/letsencrypt/nginx/espocrm.conf
Expand Down
7 changes: 1 addition & 6 deletions installation-modes/ssl/init.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#!/bin/bash
#!/usr/bin/env bash

set -euo pipefail

if ! [ $(id -u) = 0 ]; then
printf "Error: this script must be run as root\n"
exit 1
fi

source installation-modes/utils.sh

cp ./installation-modes/http/nginx/espocrm.conf ./installation-modes/ssl/nginx/espocrm.conf
Expand Down
Loading