View raw

1 #!/usr/bin/env bash 2 3 set -eEuo pipefail 4 5 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 6 readonly SCRIPT_DIR 7 readonly PASSWD_FILE="/etc/mosquitto/passwd" 8 readonly BROKER_SERVICE_NAME="mosquitto.service" 9 readonly HUB_STATUS_SERVICE_NAME="fapg-daq-hub-status.service" 10 readonly HUB_STATUS_SERVICE_FILE="/etc/systemd/system/${HUB_STATUS_SERVICE_NAME}" 11 readonly REPO_DIR="/opt/fapg/fapg-daq" 12 readonly HUB_STATUS_BIN="${REPO_DIR}/roles/daq-hub/bin/fapg-daq-hub-status" 13 readonly ENV_DIR="/etc/fapg-daq" 14 readonly HUB_STATUS_ENV_FILE="${ENV_DIR}/hub-status.env" 15 readonly RUN_USER="fapg-daq" 16 readonly RUN_GROUP="fapg-daq" 17 readonly MQTT_HOST="127.0.0.1" 18 readonly MQTT_PORT="1883" 19 readonly MQTT_USERNAME="fapg_hub" 20 readonly HUB_STATUS_INTERVAL="5" 21 HUB_MQTT_PASSWORD="" 22 23 function die() { 24 echo "ERROR: $*" >&2 25 exit 1 26 } 27 28 function require_root() { 29 if [[ "${EUID}" -ne 0 ]]; then 30 die "run as root, e.g. sudo $0" 31 fi 32 } 33 34 function passwd_has_user() { 35 local user="$1" 36 37 [[ -f "${PASSWD_FILE}" ]] || return 1 38 grep -qE "^${user}:" "${PASSWD_FILE}" 39 } 40 41 function ensure_mosquitto_password() { 42 local user="$1" 43 44 if passwd_has_user "${user}"; then 45 echo "Mosquitto password for ${user} already exists; leaving it unchanged." 46 return 47 fi 48 49 echo "Enter password for ${user}" 50 if [[ -f "${PASSWD_FILE}" ]]; then 51 mosquitto_passwd "${PASSWD_FILE}" "${user}" 52 else 53 mosquitto_passwd -c "${PASSWD_FILE}" "${user}" 54 fi 55 } 56 57 function read_hub_mqtt_password() { 58 if [[ -n "${HUB_MQTT_PASSWORD:-}" ]]; then 59 return 60 fi 61 62 read -rsp "MQTT password for user '${MQTT_USERNAME}': " HUB_MQTT_PASSWORD 63 echo 64 65 [[ -n "${HUB_MQTT_PASSWORD:-}" ]] || die "MQTT password is required." 66 } 67 68 function ensure_hub_mosquitto_password() { 69 if passwd_has_user "${MQTT_USERNAME}"; then 70 echo "Mosquitto password for ${MQTT_USERNAME} already exists; leaving it unchanged." 71 return 72 fi 73 74 read_hub_mqtt_password 75 76 if [[ -f "${PASSWD_FILE}" ]]; then 77 mosquitto_passwd -b "${PASSWD_FILE}" "${MQTT_USERNAME}" "${HUB_MQTT_PASSWORD}" 78 else 79 mosquitto_passwd -b -c "${PASSWD_FILE}" "${MQTT_USERNAME}" "${HUB_MQTT_PASSWORD}" 80 fi 81 } 82 83 function install_os_dependencies() { 84 apt update && apt install -y \ 85 perl \ 86 mosquitto \ 87 mosquitto-clients \ 88 libnet-mqtt-simple-perl 89 } 90 91 function create_runtime_user() { 92 if ! getent group "${RUN_GROUP}" > /dev/null; then 93 addgroup --system "${RUN_GROUP}" 94 fi 95 96 if ! id "${RUN_USER}" > /dev/null 2>&1; then 97 adduser \ 98 --system \ 99 --ingroup "${RUN_GROUP}" \ 100 --home /var/lib/fapg-daq \ 101 --no-create-home \ 102 --disabled-login \ 103 "${RUN_USER}" 104 fi 105 } 106 107 function check_repo_layout() { 108 [[ -d "${REPO_DIR}" ]] || die "Repo not found at ${REPO_DIR}." 109 [[ -x "${HUB_STATUS_BIN}" ]] || die "${HUB_STATUS_BIN} does not exist or is not executable." 110 } 111 112 function write_hub_status_env_file() { 113 mkdir -p "${ENV_DIR}" 114 115 if [[ -f "${HUB_STATUS_ENV_FILE}" ]]; then 116 echo "${HUB_STATUS_ENV_FILE} already exists; leaving it unchanged." 117 chown root:"${RUN_GROUP}" "${HUB_STATUS_ENV_FILE}" 118 chmod 0640 "${HUB_STATUS_ENV_FILE}" 119 return 120 fi 121 122 read_hub_mqtt_password 123 124 cat > "${HUB_STATUS_ENV_FILE}" <<EOF 125 MQTT_SIMPLE_ALLOW_INSECURE_LOGIN=1 126 127 MQTT_HOST=${MQTT_HOST} 128 MQTT_PORT=${MQTT_PORT} 129 MQTT_USERNAME=${MQTT_USERNAME} 130 MQTT_PASSWORD=${HUB_MQTT_PASSWORD} 131 132 HUB_STATUS_INTERVAL=${HUB_STATUS_INTERVAL} 133 EOF 134 135 chown root:"${RUN_GROUP}" "${HUB_STATUS_ENV_FILE}" 136 chmod 0640 "${HUB_STATUS_ENV_FILE}" 137 } 138 139 function install_systemd_services() { 140 install -d -m 0755 -o root -g root /etc/systemd/system 141 142 install -m 0644 -o root -g root \ 143 "${SCRIPT_DIR}/etc/systemd/system/fapg-daq-hub-status.service" \ 144 "${HUB_STATUS_SERVICE_FILE}" 145 } 146 147 require_root 148 install_os_dependencies 149 create_runtime_user 150 check_repo_layout 151 152 # Install files 153 154 install -d -m 0755 -o root -g root /etc/mosquitto/conf.d 155 156 install -m 0640 -o mosquitto -g mosquitto \ 157 "${SCRIPT_DIR}/etc/mosquitto/acl" \ 158 /etc/mosquitto/acl 159 160 install -m 0644 -o root -g root \ 161 "${SCRIPT_DIR}/etc/mosquitto/conf.d/fapg-home.conf" \ 162 /etc/mosquitto/conf.d/fapg-home.conf 163 164 # Password management 165 166 ensure_mosquitto_password fapg_zero 167 ensure_hub_mosquitto_password 168 ensure_mosquitto_password fapg_vps 169 170 chown mosquitto:mosquitto "${PASSWD_FILE}" 171 chmod 0640 "${PASSWD_FILE}" 172 173 # System service activation 174 175 write_hub_status_env_file 176 install_systemd_services 177 178 systemctl daemon-reload 179 systemctl enable "${BROKER_SERVICE_NAME}" "${HUB_STATUS_SERVICE_NAME}" 180 systemctl restart "${BROKER_SERVICE_NAME}" 181 systemctl restart "${HUB_STATUS_SERVICE_NAME}" 182 183 systemctl --no-pager --full status "${BROKER_SERVICE_NAME}" 184 systemctl --no-pager --full status "${HUB_STATUS_SERVICE_NAME}" 185