Skip to content

Commit

Permalink
make install script more generic + more type safety (#7135)
Browse files Browse the repository at this point in the history
# Description & Issue number it closes 
<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. -->

the current install script is very ubuntu-specific. this is my attempt
to make it more flexible since many users might want to use this
project.
  • Loading branch information
DAcodedBEAT authored Sep 8, 2024
2 parents 1aa2e8e + a6d3cf0 commit 8c01e53
Show file tree
Hide file tree
Showing 20 changed files with 211 additions and 133 deletions.
176 changes: 176 additions & 0 deletions install/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#!/usr/bin/env sh

# Error on unset variable or parameter and exit
set -u

DATABASE_NAME="$1"
DATABASE_USERNAME="$2"
DATABASE_PASSWORD="$3"

# Determine if the script is run as root
if [ "$(id -u)" -eq 0 ]; then
SUDO=""
else
SUDO="sudo"
fi

# Function to check if the OS is Ubuntu
check_os() {
# Check if /etc/os-release exists
if [ -f /etc/os-release ]; then
# Extract the ID value from /etc/os-release
os_id=$(grep '^ID=' /etc/os-release | cut -d= -f2 | tr -d '"')

# Print a warning to stderr if the OS is not Ubuntu
if [ "$os_id" != "ubuntu" ]; then
echo "Warning: This script isn't regularly tested on non-Ubuntu installations." >&2
fi
else
echo "Error: /etc/os-release file not found. Cannot determine the OS." >&2
fi
}

check_os

# Function to run a command and exit if it fails
run_or_exit() {
"$@"
status=$?
if [ $status -ne 0 ]; then
echo "Command failed: $*" >&2
exit 1
fi
}

# Function to download files using curl or wget
download_file() {
URL="$1"
OUTPUT="$2"

if command -v curl >/dev/null 2>&1; then
run_or_exit curl -L -o "$OUTPUT" "$URL"
elif command -v wget >/dev/null 2>&1; then
run_or_exit wget -O "$OUTPUT" "$URL"
else
echo "Error: Neither curl nor wget is available." >&2
exit 1
fi
}

# Package installation function based on available package managers
if command -v apt-get >/dev/null 2>&1; then
install_packages() {
run_or_exit $SUDO apt-get update
run_or_exit $SUDO apt-get install -y "$@"
}
elif command -v dnf >/dev/null 2>&1; then
install_packages() {
run_or_exit $SUDO dnf install -y "$@"
}
elif command -v yum >/dev/null 2>&1; then
install_packages() {
run_or_exit $SUDO yum install -y "$@"
}
else
echo "Error: No supported package manager found." >&2
exit 1
fi

# Service management function based on available service managers
if command -v systemctl >/dev/null 2>&1; then
enable_and_start_service() {
run_or_exit $SUDO systemctl enable "$1"
run_or_exit $SUDO systemctl start "$1"
}

restart_service() {
run_or_exit $SUDO systemctl restart "$1"
}
else
echo "Error: No supported service manager found." >&2
exit 1
fi

# Install required packages
install_packages apache2 curl gawk libapache2-mod-php mariadb-client mariadb-server \
php php-bcmath php-cli php-curl php-dev php-gd php-intl php-mbstring php-mysql \
php-soap php-xml php-zip unzip

# Common logic for all distributions
cd /tmp

# Get the latest version of ChurchCRM
if command -v curl >/dev/null 2>&1; then
VERSION_CMD="curl -Is https://github.com/ChurchCRM/CRM/releases/latest | awk -F\/ '/^location:/ {sub(/\r$/, \"\", \$NF); print \$NF}'"
elif command -v wget >/dev/null 2>&1; then
VERSION_CMD="wget --spider --server-response https://github.com/ChurchCRM/CRM/releases/latest 2>&1 | awk -F\/ '/^ Location:/ {sub(/\r$/, \"\", \$NF); print \$NF}'"
else
echo "Error: Neither curl nor wget is available." >&2
exit 1
fi

VERSION=$(eval "$VERSION_CMD")
DOWNLOAD_URL="https://github.com/ChurchCRM/CRM/releases/download/$VERSION/ChurchCRM-$VERSION.zip"
download_file "$DOWNLOAD_URL" "ChurchCRM-$VERSION.zip"
run_or_exit unzip "ChurchCRM-$VERSION.zip" && rm "ChurchCRM-$VERSION.zip"
run_or_exit $SUDO chown -R www-data:www-data churchcrm
run_or_exit $SUDO mv churchcrm /var/www/html/

enable_and_start_service apache2
enable_and_start_service mariadb

## Creating the database
run_or_exit $SUDO mariadb -uroot -p -e "CREATE DATABASE ${DATABASE_NAME} /*\!40100 DEFAULT CHARACTER SET utf8 */;
CREATE USER ${DATABASE_USERNAME}@'localhost' IDENTIFIED BY '${DATABASE_PASSWORD}';
GRANT ALL ON ${DATABASE_NAME}.* TO '${DATABASE_USERNAME}'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;"

echo "Please make sure to secure your database server:"
echo " $SUDO mysql_secure_installation"

PHP_CONF_D_PATH="/etc/php/conf.d/churchcrm.ini"
PHP_VERSION=$(php -r 'echo phpversion();' | cut -d '.' -f 1,2)

if [ "$PHP_VERSION" = "8.3" ]; then
PHP_CONF_D_PATH="/etc/php/8.3/apache2/conf.d/99-churchcrm.ini"
fi

# Set-up the required PHP configuration
run_or_exit $SUDO tee "$PHP_CONF_D_PATH" << 'TXT'
file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
TXT

# Set-up the required Apache configuration
run_or_exit $SUDO tee /etc/apache2/sites-available/churchcrm.conf << 'TXT'
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/churchcrm/
ServerName ChurchCRM
<Directory /var/www/html/churchcrm/>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
TXT

# Enable apache rewrite module
run_or_exit $SUDO a2enmod rewrite

# Disable the default apache site and enable ChurchCRM
run_or_exit $SUDO a2dissite 000-default.conf
run_or_exit $SUDO a2ensite churchcrm.conf

# Restart apache to load new configuration
restart_service apache2.service
98 changes: 0 additions & 98 deletions install/ubuntu.sh

This file was deleted.

2 changes: 1 addition & 1 deletion src/ChurchCRM/Reports/ChurchInfoReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function stripPhone($phone)
if (mb_substr($phone, 0, 1) == '-') {
$phone = mb_substr($phone, 1, strlen($phone) - 1);
}
if (strlen($phone) == 7) {
if (strlen($phone) === 7) {
// Fix the missing -
$phone = mb_substr($phone, 0, 3) . '-' . mb_substr($phone, 3, 4);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ChurchCRM/Reports/PDF_Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public function sGetHeadString($rsCustomFields, $aHead): string

$sHeadStr = '';

if (strlen($per_LastName) && (strtolower($per_LastName) != strtolower($this->sLastName))) {
if (!empty($per_LastName) && (strtolower($per_LastName) != strtolower($this->sLastName))) {
$bDifferentLastName = true;
} else {
$bDifferentLastName = false;
Expand Down Expand Up @@ -421,7 +421,7 @@ public function sGetHeadString($rsCustomFields, $aHead): string

// If there is no additional information for either head or spouse, there is no
// need to print the name in the sublist, they are already are in the heading.
if (strlen($sHeadStr) == $iTempLen) {
if (strlen($sHeadStr) === $iTempLen) {
return '';
} else {
return $sHeadStr;
Expand Down
2 changes: 1 addition & 1 deletion src/ChurchCRM/Service/GroupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function deleteGroupRole(string $groupID, string $groupRoleID): array
public function addGroupRole(string $groupID, string $groupRoleName): string
{
requireUserGroupMembership('bManageGroups');
if (strlen($groupRoleName) == 0) {
if (strlen($groupRoleName) === 0) {
throw new \Exception('New field name cannot be blank');
} else {
// Check for a duplicate option name
Expand Down
2 changes: 1 addition & 1 deletion src/ChurchCRM/dto/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function removeGroup($GroupID): void

public static function hasPeople(): bool
{
return array_key_exists('aPeopleCart', $_SESSION) && count($_SESSION['aPeopleCart']) != 0;
return array_key_exists('aPeopleCart', $_SESSION) && count($_SESSION['aPeopleCart']) !== 0;
}

public static function countPeople(): int
Expand Down
2 changes: 1 addition & 1 deletion src/DonatedItemReplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$row = mysqli_fetch_array($rsItem);
$startItem = $row[0];

if (strlen($startItem) == 2) { // replicated items will sort better if they have a two-digit number
if (strlen($startItem) === 2) { // replicated items will sort better if they have a two-digit number
$letter = mb_substr($startItem, 0, 1);
$number = mb_substr($startItem, 1, 1);
$startItem = $letter . '0' . $number;
Expand Down
2 changes: 1 addition & 1 deletion src/DonationFundEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$donation->setName(InputUtils::filterString($_POST[$iFieldID . 'name']));
$donation->setDescription(InputUtils::legacyFilterInput($_POST[$iFieldID . 'desc']));
$donation->setActive($_POST[$iFieldID . 'active'] == 1);
if (strlen($donation->getName()) == 0) {
if (strlen($donation->getName()) === 0) {
$aNameErrors[$iFieldID] = true;
$bErrorFlag &= $aNameErrors[$iFieldID];
}
Expand Down
2 changes: 1 addition & 1 deletion src/FamilyCustomFieldsEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
for ($iFieldID = 1; $iFieldID <= $numRows; $iFieldID++) {
$aNameFields[$iFieldID] = InputUtils::legacyFilterInput($_POST[$iFieldID . 'name']);

if (strlen($aNameFields[$iFieldID]) == 0) {
if (strlen($aNameFields[$iFieldID]) === 0) {
$aNameErrors[$iFieldID] = true;
$bErrorFlag = true;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/FamilyEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@

// Make sure first names were entered if editing existing family
if ($iFamilyID > 0) {
if (strlen($aFirstNames[$iCount]) == 0) {
if (strlen($aFirstNames[$iCount]) === 0) {
$aFirstNameError[$iCount] = gettext('First name must be entered');
$bErrorFlag = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/GroupPropsFormEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
for ($iPropID = 1; $iPropID <= $numRows; $iPropID++) {
$aNameFields[$iPropID] = InputUtils::legacyFilterInput($_POST[$iPropID . 'name']);

if (strlen($aNameFields[$iPropID]) == 0) {
if (strlen($aNameFields[$iPropID]) === 0) {
$aNameErrors[$iPropID] = true;
$bErrorFlag = true;
} else {
Expand Down Expand Up @@ -114,7 +114,7 @@
$newFieldName = InputUtils::legacyFilterInput($_POST['newFieldName']);
$newFieldDesc = InputUtils::legacyFilterInput($_POST['newFieldDesc']);

if (strlen($newFieldName) == 0) {
if (strlen($newFieldName) === 0) {
$bNewNameError = true;
} else {
$sSQL = 'SELECT prop_Name FROM groupprop_master WHERE grp_ID = ' . $iGroupID;
Expand Down
2 changes: 1 addition & 1 deletion src/GroupView.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
extract($aRow);

//If the property doesn't already exist for this Person, write the <OPTION> tag
if (strlen(strstr($sAssignedProperties, ',' . $pro_ID . ',')) == 0) {
if (strlen(strstr($sAssignedProperties, ',' . $pro_ID . ',')) === 0) {
echo '<option value="' . $pro_ID . '">' . $pro_Name . '</option>';
}
}
Expand Down
Loading

0 comments on commit 8c01e53

Please sign in to comment.