#!/bin/bash # ------------------------------- # Global Counters # ------------------------------- SUCCESS=0 FAIL=0 increment_success() { SUCCESS=$((SUCCESS+1)); } increment_fail() { FAIL=$((FAIL+1)); } # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo "========================================" echo " Service Management " echo "========================================" # Services that need stop + disable STOP_AND_DISABLE_SERVICES=( "okta_tcp.service" "di_modbus.service" "matrix.service" ) # Services that need disable only DISABLE_ONLY_SERVICES=( "connman.service" ) handle_stop_and_disable() { SERVICE=$1 echo "----------------------------------------" echo "Processing: $SERVICE" # Check if service exists if ! systemctl list-unit-files | grep -q "^$SERVICE"; then echo -e "${RED}ERROR: $SERVICE not found.${NC}" increment_fail return fi echo -e "${GREEN}Service exists.${NC}" # Stop if running if systemctl is-active --quiet $SERVICE; then echo -e "${YELLOW}Service is RUNNING. Stopping...${NC}" if systemctl stop $SERVICE; then echo -e "${GREEN}Stopped successfully.${NC}" increment_success else echo -e "${RED}Failed to stop.${NC}" increment_fail return fi else echo -e "${GREEN}Service is not running.${NC}" increment_success fi # Disable if enabled if systemctl is-enabled --quiet $SERVICE; then echo -e "${YELLOW}Service is ENABLED. Disabling...${NC}" if systemctl disable $SERVICE; then echo -e "${GREEN}Disabled successfully.${NC}" increment_success else echo -e "${RED}Failed to disable.${NC}" increment_fail return fi else echo -e "${GREEN}Service already disabled.${NC}" increment_success fi } handle_disable_only() { SERVICE=$1 echo "----------------------------------------" echo "Processing: $SERVICE" # Check if service exists if ! systemctl list-unit-files | grep -q "^$SERVICE"; then echo -e "${RED}ERROR: $SERVICE not found.${NC}" increment_fail return fi echo -e "${GREEN}Service exists.${NC}" # Disable if enabled if systemctl is-enabled --quiet $SERVICE; then echo -e "${YELLOW}Service is ENABLED. Disabling...${NC}" if systemctl disable $SERVICE; then echo -e "${GREEN}Disabled successfully.${NC}" increment_success else echo -e "${RED}Failed to disable.${NC}" increment_fail return fi else echo -e "${GREEN}Service already disabled.${NC}" increment_success fi } # ------------------------------- # Process Services # ------------------------------- for svc in "${STOP_AND_DISABLE_SERVICES[@]}"; do handle_stop_and_disable "$svc" done for svc in "${DISABLE_ONLY_SERVICES[@]}"; do handle_disable_only "$svc" done # ------------------------------- # DNS Configuration # ------------------------------- DNS_TARGET="/run/systemd/resolve/stub-resolv.conf" RESOLV_FILE="/etc/resolv.conf" CONNMAN_RESOLV="/etc/resolv-conf.connman" echo "========================================" echo " Checking DNS configuration... $SUCCESS " if [ -L "$RESOLV_FILE" ] && [ "$(readlink -f $RESOLV_FILE)" = "$DNS_TARGET" ]; then echo -e "${GREEN}DNS already correctly configured.${NC}" increment_success else echo -e "${YELLOW}Fixing DNS configuration...${NC}" rm -f "$RESOLV_FILE" ln -s "$DNS_TARGET" "$RESOLV_FILE" if [ $? -eq 0 ]; then echo -e "${GREEN}DNS configuration updated successfully.${NC}" increment_success else echo -e "${RED}Failed to update DNS configuration.${NC}" increment_fail fi fi # Remove connman resolv file if exists if [ -f "$CONNMAN_RESOLV" ]; then echo -e "${YELLOW}Removing $CONNMAN_RESOLV...${NC}" rm -f "$CONNMAN_RESOLV" && \ echo -e "${GREEN}Removed successfully.${NC}" || \ echo -e "${RED}Failed to remove.${NC}" else echo -e "${GREEN}$CONNMAN_RESOLV not present.${NC}" increment_success fi # ------------------------------- # Runtime Permission # ------------------------------- check_and_fix_permission() { local FILE="$1" echo "----------------------------------------" echo "Checking: $FILE" if [ -f "$FILE" ]; then if [ -x "$FILE" ]; then echo -e "${GREEN}Already executable.${NC}" increment_success else echo -e "${YELLOW}Setting execute permission...${NC}" if chmod +x "$FILE"; then echo -e "${GREEN}Permission updated successfully.${NC}" increment_success else echo -e "${RED}Failed to update permission.${NC}" increment_fail fi fi else echo -e "${RED}File not found: $FILE${NC}" increment_fail fi } FILES=( "/home/root/DigiRuntimeARM64" "/home/root/DigiDeviceDiscovery" "/home/root/ha_manager" "/home/root/rtc_init.sh" ) echo "========================================" echo " Checking runtime permission... " for file in "${FILES[@]}"; do check_and_fix_permission "$file" done # ------------------------------- # Logrotate Configuration # ------------------------------- echo "========================================" echo " Checking logrotate configuration... " LOGROTATE_CONF="/etc/logrotate.conf" RSYSLOG_FILE="/etc/logrotate.d/rsyslog" # 1️⃣ Check and comment 'dateext' if not commented if [ -f "$LOGROTATE_CONF" ]; then if grep -q "^[[:space:]]*dateext" "$LOGROTATE_CONF"; then echo -e "${YELLOW}'dateext' is active. Commenting it...${NC}" sed -i 's/^[[:space:]]*dateext/#dateext/' "$LOGROTATE_CONF" if [ $? -eq 0 ]; then echo -e "${GREEN}'dateext' commented successfully.${NC}" increment_success else echo -e "${RED}Failed to modify $LOGROTATE_CONF.${NC}" increment_fail fi else echo -e "${GREEN}'dateext' already commented or not present.${NC}" increment_success fi else echo -e "${RED}$LOGROTATE_CONF not found.${NC}" increment_fail fi # 2️⃣ Check rsyslog logrotate file (whitespace-safe comparison) EXPECTED_CONTENT="/var/log/syslog { size 5M rotate 3 missingok notifempty compress copytruncate }" normalize_content() { echo "$1" | sed ' s/^[[:space:]]*//; s/[[:space:]]*$//; /^$/d; s/[[:space:]]\+/ /g ' } if [ -f "$RSYSLOG_FILE" ]; then echo -e "${YELLOW}rsyslog file exists. Verifying content...${NC}" NORMALIZED_CURRENT=$(normalize_content "$(cat "$RSYSLOG_FILE")") NORMALIZED_EXPECTED=$(normalize_content "$EXPECTED_CONTENT") if [ "$NORMALIZED_CURRENT" = "$NORMALIZED_EXPECTED" ]; then echo -e "${GREEN}rsyslog logrotate config already correct.${NC}" increment_success else echo -e "${YELLOW}Content differs. Updating rsyslog configuration...${NC}" echo "$EXPECTED_CONTENT" > "$RSYSLOG_FILE" if [ $? -eq 0 ]; then echo -e "${GREEN}rsyslog configuration updated successfully.${NC}" increment_success else echo -e "${RED}Failed to update rsyslog configuration.${NC}" increment_fail fi fi else echo -e "${YELLOW}rsyslog logrotate file not found. Creating...${NC}" echo "$EXPECTED_CONTENT" > "$RSYSLOG_FILE" if [ $? -eq 0 ]; then echo -e "${GREEN}rsyslog configuration created successfully.${NC}" increment_success else echo -e "${RED}Failed to create rsyslog configuration.${NC}" increment_fail fi fi # ------------------------------- # Cron Daily Modification # ------------------------------- echo "========================================" echo " Checking /etc/crontab configuration... " CRONTAB_FILE="/etc/crontab" DESIRED_CRON="*/5 * * * * root cd / && run-parts /etc/cron.daily" if [ -f "$CRONTAB_FILE" ]; then # If already correct → do nothing if grep -Fxq "$DESIRED_CRON" "$CRONTAB_FILE"; then echo -e "${GREEN}Cron.daily already configured correctly.${NC}" increment_success else echo -e "${YELLOW}Updating cron.daily schedule (preserving order)...${NC}" # Replace any existing cron.daily line (commented or not) sed -i \ '/run-parts \/etc\/cron.daily/c\'"$DESIRED_CRON" \ "$CRONTAB_FILE" if [ $? -eq 0 ]; then echo -e "${GREEN}Cron.daily updated successfully in same position.${NC}" increment_success else echo -e "${RED}Failed to update /etc/crontab.${NC}" increment_fail fi fi else echo -e "${RED}/etc/crontab not found.${NC}" increment_fail fi # ------------------------------- # Cron @reboot Modification # ------------------------------- echo "========================================" echo " Checking RTC @reboot cron configuration... " CRONTAB_FILE="/etc/crontab" RTC_CRON="@reboot root /home/root/rtc_init.sh" if [ -f "$CRONTAB_FILE" ]; then # Check if already exists if grep -Fq "$RTC_CRON" "$CRONTAB_FILE"; then echo -e "${GREEN}RTC @reboot cron already configured.${NC}" increment_success else echo -e "${YELLOW}Adding RTC @reboot cron entry...${NC}" # Append safely at end (recommended for @reboot jobs) echo "$RTC_CRON" >> "$CRONTAB_FILE" if [ $? -eq 0 ]; then echo -e "${GREEN}RTC @reboot cron added successfully.${NC}" increment_success else echo -e "${RED}Failed to update RTC cron.${NC}" increment_fail fi fi else echo -e "${RED}/etc/crontab not found.${NC}" increment_fail fi # ------------------------------- # Comment Out cron_jobs in /etc/cron.d/ # ------------------------------- echo "==========================================" echo " Checking /etc/cron.d/cron_jobs & comment." CRON_JOBS_FILE="/etc/cron.d/cron_jobs" if [ -f "$CRON_JOBS_FILE" ]; then # Check if any uncommented line exists if grep -q '^[^#[:space:]]' "$CRON_JOBS_FILE"; then echo -e "${YELLOW}Commenting out active cron entries...${NC}" # Prepend # to uncommented lines sed -i 's/^[^#]/#&/' "$CRON_JOBS_FILE" if [ $? -eq 0 ]; then echo -e "${GREEN}All cron_jobs lines commented successfully.${NC}" increment_success else echo -e "${RED}Failed to comment cron_jobs entries.${NC}" increment_fail fi else echo -e "${GREEN}All lines already commented in cron_jobs.${NC}" increment_success fi else echo -e "${RED}$CRON_JOBS_FILE not found.${NC}" increment_fail fi # ------------------------------- # File move - all .service and lighttpd # ------------------------------- echo "==========================================" echo " Moving service file and lighttpd also NTPD and rsync lib........." # -------- PATHS -------- PHP_INI_DEST="/usr/share/matrix-gui-2.0/" SYSTEMD_DEST="/etc/systemd/system" LIGHTTPD_DEST="/etc/lighttpd" NTPD_DEST="/etc" RSYNC_DEST="/usr/bin/" SERVICE_FILES=( "/home/root/digirtu.service" "/home/root/digiUDP.service" "/home/root/digiRTUfailover.service" ) PHP_INI_SOURCE="/home/root/php.ini" LIGHTTPD_SOURCE="/home/root/lighttpd.conf" NTPD_SOURCE="/home/root/ntp.conf" RSYNC_SOURCE="/home/root/rsync" # -------- FUNCTION TO MOVE FILE -------- move_file() { local src="$1" local dest_dir="$2" local filename filename=$(basename "$src") local dest="$dest_dir/$filename" if [[ -f "$src" ]]; then if [[ -f "$dest" ]]; then echo -e "${YELLOW}$filename already exists at destination. Overwriting...${NC}" else echo -e "${GREEN}Moving $filename to $dest_dir${NC}" fi mv -f "$src" "$dest" echo -e "${GREEN}$filename moved successfully.${NC}" else if [[ -f "$dest" ]]; then echo -e "${GREEN}$filename already present at destination. No changes needed.${NC}" else echo -e "${RED}$filename not found in source or destination!${NC}" fi fi } # -------- FUNCTION TO MOVE DIRECTORY -------- move_directory() { local src="$1" local dest_dir="$2" local dirname dirname=$(basename "$src") local dest="$dest_dir/$dirname" if [[ -d "$src" ]]; then if [[ -e "$dest" ]]; then echo -e "${YELLOW}$dirname already exists at destination. Replacing...${NC}" rm -rf "$dest" else echo -e "${GREEN}Moving $dirname to $dest_dir${NC}" fi mv "$src" "$dest" echo -e "${GREEN}$dirname moved successfully.${NC}" else if [[ -e "$dest" ]]; then echo -e "${GREEN}$dirname already present at destination. No changes needed.${NC}" else echo -e "${RED}$dirname not found in source or destination!${NC}" fi fi } # -------- PROCESS SERVICE FILES -------- for file in "${SERVICE_FILES[@]}"; do move_file "$file" "$SYSTEMD_DEST" done # -------- PROCESS PHP_INI FILES -------- move_file "$PHP_INI_SOURCE" "$PHP_INI_DEST" # -------- PROCESS LIGHTTPD FILES -------- move_file "$LIGHTTPD_SOURCE" "$LIGHTTPD_DEST" # -------- PROCESS ntp.conf FILES -------- move_file "$NTPD_SOURCE" "$NTPD_DEST" # -------- PROCESS rsync FILES -------- move_file "$RSYNC_SOURCE" "$RSYNC_DEST" # ------- check and set the permission for rsync check_and_fix_permission "/usr/bin/rsync" # -------- PROCESS LIGHTTPD DIRECTORY -------- #move_directory "$LIGHTTPD_SOURCE" "$LIGHTTPD_DEST" # -------- RELOAD SYSTEMD IF SERVICES EXIST -------- if ls "$SYSTEMD_DEST"/digi*.service >/dev/null 2>&1; then echo -e "${GREEN}Reloading systemd daemon...${NC}" systemctl daemon-reload echo -e "${GREEN}Systemd reloaded successfully.${NC}" fi #--------- Restart and check the Lighttpd --------- systemctl restart lighttpd echo -e "${GREEN}Lighttpd reloaded successfully.${NC}" echo -e "${GREEN}Deployment completed successfully.${NC}" # ------------------------------- # Execution Summary # ------------------------------- echo "========================================" echo -e "${GREEN}Execution Summary:${NC}" echo "----------------------------------------" echo -e "Total Successful Steps : ${GREEN}$SUCCESS${NC}" echo -e "Total Failed Steps : ${RED}$FAIL${NC}" echo "========================================" echo "" echo "========================================" echo -e "${GREEN}All tasks completed.${NC}" echo "========================================"