|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e -o pipefail |
| 4 | + |
| 5 | +print_sep() { |
| 6 | + local cols line |
| 7 | + cols=${COLUMNS:-$(tput cols 2>/dev/null || echo 80)} |
| 8 | + printf -v line '%*s' "$cols" '' |
| 9 | + printf '%s\n' "${line// /-}" |
| 10 | +} |
| 11 | + |
| 12 | +echo "Starting TeleIRC installation script..." |
| 13 | +print_sep |
| 14 | + |
| 15 | +echo "Checking if systemd is the init system..." |
| 16 | +check_systemd_is_init() { |
| 17 | + if [ "$(basename "$(readlink /proc/1/exe)")" = "systemd" ]; then |
| 18 | + return 0 |
| 19 | + else |
| 20 | + return 1 |
| 21 | + fi |
| 22 | +} |
| 23 | +if ! check_systemd_is_init; then |
| 24 | + echo "Error: This script requires systemd as the init system." >&2 |
| 25 | + exit 1 |
| 26 | +else |
| 27 | + echo "systemd is the init system. Continuing..." |
| 28 | +fi |
| 29 | +print_sep |
| 30 | + |
| 31 | +echo "Checking if sudo and curl is installed..." |
| 32 | +if ! command -v sudo &> /dev/null; then |
| 33 | + echo "sudo could not be found. Please install sudo and re-run this script." >&2 |
| 34 | + exit 1 |
| 35 | +else |
| 36 | + echo "sudo is installed. Continuing..." |
| 37 | +fi |
| 38 | +if ! command -v curl &> /dev/null; then |
| 39 | + echo "curl could not be found. Please install curl and re-run this script." >&2 |
| 40 | + exit 1 |
| 41 | +else |
| 42 | + echo "curl is installed. Continuing..." |
| 43 | +fi |
| 44 | +print_sep |
| 45 | + |
| 46 | +echo "Fetching the latest TeleIRC version tag from Anitya API..." |
| 47 | +ANITYA_API_URL="https://release-monitoring.org/api/v2/versions/?project_id=386778" |
| 48 | +LATEST_VERSION=$(curl -sL "$ANITYA_API_URL" | sed -n 's/.*"latest_version":"\([^"]*\)".*/\1/p') |
| 49 | +if [[ -z "$LATEST_VERSION" ]]; then |
| 50 | + echo "Error: Could not retrieve or parse the latest version tag of TeleIRC from Anitya API." >&2 |
| 51 | + exit 1 |
| 52 | +fi |
| 53 | +VER=$LATEST_VERSION |
| 54 | +echo "Selecting TeleIRC version ${VER}..." |
| 55 | +print_sep |
| 56 | + |
| 57 | +echo "Creating temporary working directory..." |
| 58 | +TMP_DIR=$(mktemp -d) |
| 59 | +if [ ! -d "$TMP_DIR" ]; then |
| 60 | + echo "Error: Failed to create temporary directory." >&2 |
| 61 | + exit 1 |
| 62 | +else |
| 63 | + echo "Temporary directory created at ${TMP_DIR}." |
| 64 | +fi |
| 65 | +print_sep |
| 66 | + |
| 67 | +cleanup() { |
| 68 | + if [ -n "${TMP_DIR:-}" ] && [ -d "$TMP_DIR" ]; then |
| 69 | + echo "Cleaning up temporary files from ${TMP_DIR} ..." |
| 70 | + rm -rvf "$TMP_DIR" |
| 71 | + fi |
| 72 | +} |
| 73 | +trap cleanup EXIT INT |
| 74 | + |
| 75 | +GITHUB_RELEASE_URL="https://github.com/RITlug/teleirc/releases/download/v${VER}" |
| 76 | +GITHUB_RAW_URL="https://raw.githubusercontent.com/RITlug/teleirc/v${VER}" |
| 77 | +echo "Set GITHUB_RELEASE_URL to ${GITHUB_RELEASE_URL}" |
| 78 | +echo "Set GITHUB_RAW_URL to ${GITHUB_RAW_URL}" |
| 79 | +print_sep |
| 80 | + |
| 81 | +echo "Downloading TeleIRC deployment assets from GitHub..." |
| 82 | +curl --verbose --location --output "${TMP_DIR}"/teleirc "${GITHUB_RELEASE_URL}"/teleirc-${VER}-linux-x86_64 |
| 83 | +curl --verbose --location --output "${TMP_DIR}"/teleirc.sysusers "${GITHUB_RAW_URL}"/deployments/systemd/teleirc.sysusers |
| 84 | +curl --verbose --location --output "${TMP_DIR}"/teleirc.tmpfiles "${GITHUB_RAW_URL}"/deployments/systemd/teleirc.tmpfiles |
| 85 | +curl --verbose --location --output "${TMP_DIR}"/teleirc@.service "${GITHUB_RAW_URL}"/deployments/systemd/teleirc@.service |
| 86 | +curl --verbose --location --output "${TMP_DIR}"/teleirc.env "${GITHUB_RAW_URL}"/env.example |
| 87 | +print_sep |
| 88 | + |
| 89 | +if [ -z "$EDITOR" ]; then |
| 90 | + if command -v vim &> /dev/null; then |
| 91 | + EDITOR="vim" |
| 92 | + else |
| 93 | + EDITOR="nano" |
| 94 | + fi |
| 95 | +fi |
| 96 | +echo "Selected EDITOR as ${EDITOR} ..." |
| 97 | +print_sep |
| 98 | + |
| 99 | +echo "Please edit the TeleIRC environment file to configure your bridge settings..." |
| 100 | +$EDITOR "${TMP_DIR}"/teleirc.env |
| 101 | +print_sep |
| 102 | + |
| 103 | +echo "Please make sure you configured TeleIRC correctly in ${TMP_DIR}/teleirc.env before proceeding!" |
| 104 | +echo "Press any key to continue..." |
| 105 | +read -n 1 -s -r |
| 106 | +print_sep |
| 107 | + |
| 108 | +echo "Installing TeleIRC files and user..." |
| 109 | +sudo install -v -Dm755 -o root -g root "${TMP_DIR}"/teleirc /usr/local/bin/teleirc |
| 110 | +sudo install -v -Dm644 -o root -g root "${TMP_DIR}"/teleirc.sysusers /etc/sysusers.d/teleirc.conf |
| 111 | +sudo install -v -Dm644 -o root -g root "${TMP_DIR}"/teleirc.tmpfiles /etc/tmpfiles.d/teleirc.conf |
| 112 | +sudo install -v -Dm644 -o root -g root "${TMP_DIR}"/teleirc@.service /etc/systemd/system/teleirc@.service |
| 113 | +sudo systemd-sysusers /etc/sysusers.d/teleirc.conf |
| 114 | +sudo systemd-tmpfiles --create /etc/tmpfiles.d/teleirc.conf |
| 115 | +sudo install -v -Dm644 -o root -g root "${TMP_DIR}"/teleirc.env /etc/teleirc/bridge |
| 116 | +print_sep |
| 117 | + |
| 118 | +echo "Checking if SELinux is installed and enforced..." |
| 119 | +check_selinux_status() { |
| 120 | + if ! command -v getenforce &> /dev/null; then |
| 121 | + return 1 |
| 122 | + fi |
| 123 | + SELINUX_STATUS=$(getenforce) |
| 124 | + case "$SELINUX_STATUS" in |
| 125 | + "Enforcing") |
| 126 | + return 0 |
| 127 | + ;; |
| 128 | + *) |
| 129 | + return 1 |
| 130 | + ;; |
| 131 | + esac |
| 132 | +} |
| 133 | +if check_selinux_status; then |
| 134 | + echo "SELinux is enforced. Setting SELinux context for TeleIRC files..." |
| 135 | + sudo chcon --type bin_t --user system_u /usr/local/bin/teleirc |
| 136 | + sudo chcon --type etc_t --user system_u -R /etc/teleirc/ |
| 137 | + sudo chcon --type etc_t --user system_u /etc/sysusers.d/teleirc.conf |
| 138 | + sudo chcon --type etc_t --user system_u /etc/tmpfiles.d/teleirc.conf |
| 139 | + sudo chcon --type systemd_unit_file_t --user system_u /etc/systemd/system/teleirc@.service |
| 140 | +else |
| 141 | + echo "SELinux is not enforcing or not installed. We have no need to set it up." |
| 142 | +fi |
| 143 | +print_sep |
| 144 | + |
| 145 | +echo "Enabling TeleIRC systemd service unit..." |
| 146 | +sudo systemctl enable teleirc@bridge.service |
| 147 | +echo "Enabled TeleIRC systemd service unit." |
| 148 | +print_sep |
| 149 | + |
| 150 | +read -p "Do you want to start TeleIRC now? (y/n): " response |
| 151 | +if [[ "$response" =~ ^[Yy]$ ]]; then |
| 152 | + echo "Starting TeleIRC..." |
| 153 | + sudo systemctl start teleirc@bridge.service |
| 154 | +else |
| 155 | + echo "Skipping starting TeleIRC..." |
| 156 | +fi |
| 157 | +print_sep |
| 158 | + |
| 159 | +echo "Installation of TeleIRC version ${VER} is completed." |
0 commit comments