| 1 | #!/usr/bin/env bash |
| 2 | # ========================================================================== |
| 3 | # _ _ _ ____ ____ _____ |
| 4 | # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ |
| 5 | # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| |
| 6 | # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | |
| 7 | # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| |
| 8 | # |
| 9 | # --- High-Performance Connectivity Tracer (HiPerConTracer) --- |
| 10 | # https://www.nntb.no/~dreibh/hipercontracer/ |
| 11 | # ========================================================================== |
| 12 | # |
| 13 | # High-Performance Connectivity Tracer (HiPerConTracer) |
| 14 | # Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 | # |
| 16 | # This program is free software: you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License as published by |
| 18 | # the Free Software Foundation, either version 3 of the License, or |
| 19 | # (at your option) any later version. |
| 20 | # |
| 21 | # This program is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | # GNU General Public License for more details. |
| 25 | # |
| 26 | # You should have received a copy of the GNU General Public License |
| 27 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | # |
| 29 | # Contact: [email protected] |
| 30 | |
| 31 | # Bash options: |
| 32 | set -eu |
| 33 | |
| 34 | |
| 35 | if [ $# -lt 2 ] ; then |
| 36 | echo >&2 "Usage: $0 mariadb|mongodb|mysql|postgresql users.conf [--force-removal-without-confirmation]" |
| 37 | exit 1 |
| 38 | fi |
| 39 | if [ ! -e "$2" ] ; then |
| 40 | echo >&2 "ERROR: Use configuration $2 does not exist!" |
| 41 | fi |
| 42 | |
| 43 | DOMAIN="test.local" |
| 44 | DATABASE="just_a_test_database" |
| 45 | MAINTAINER_PASSWORD="!maintainer!" |
| 46 | IMPORTER_PASSWORD="!importer!" |
| 47 | RESEARCHER_PASSWORD="!researcher!" |
| 48 | CONFIG="$2" |
| 49 | if [ ! -e "${CONFIG}" ] ; then |
| 50 | echo >&2 "ERROR: Use configuration $2 does not exist!" |
| 51 | fi |
| 52 | if [ $# -gt 2 ] ; then |
| 53 | OPTION="$3" |
| 54 | else |
| 55 | OPTION="" |
| 56 | fi |
| 57 | # shellcheck disable=SC1090 |
| 58 | . "${CONFIG}" |
| 59 | |
| 60 | if [ "${ROOT_PASSWORD}" == "!root!" ] || \ |
| 61 | [ "${MAINTAINER_PASSWORD}" == "!maintainer!" ] || \ |
| 62 | [ "${IMPORTER_PASSWORD}" == "!importer!" ] || \ |
| 63 | [ "${RESEARCHER_PASSWORD}" == "!researcher!" ] ; then |
| 64 | echo >&2 "DO NOT USE THE EXAMPLE PASSWORDS!" |
| 65 | exit 1 |
| 66 | fi |
| 67 | |
| 68 | if [ "${OPTION}" != "--force-removal-without-confirmation" ] ; then |
| 69 | echo -n "WARNING: THIS SCRIPT WILL ERASE THE EXISTING DATABASES. To proceed, type \"I am sure!\": " |
| 70 | read -er agree |
| 71 | if [ "$agree" != "I am sure!" ] ; then |
| 72 | echo "Exiting." |
| 73 | exit |
| 74 | fi |
| 75 | fi |
| 76 | |
| 77 | |
| 78 | # ###### MySQL/MariaDB ###################################################### |
| 79 | if [ "$1" == "mysql" ] || [ "$1" == "mariadb" ] ; then |
| 80 | # Documentation: https://www.digitalocean.com/community/tutorials/how-to-install-mariadb-on-ubuntu-22-04 |
| 81 | server="mariadb" |
| 82 | |
| 83 | echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Preparing admin user for MySQL/MariaDB ...\x1b[0m" |
| 84 | |
| 85 | # ====== Create database ================================================= |
| 86 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating database ...\x1b[0m" |
| 87 | sed -e "s%\${DATABASE}%${DATABASE}%" \ |
| 88 | <../SQL/mariadb-database.sql | \ |
| 89 | tee | \ |
| 90 | mysql \ |
| 91 | --host="${server}.${DOMAIN}" \ |
| 92 | --port 3306 \ |
| 93 | --protocol=tcp \ |
| 94 | --ssl \ |
| 95 | --ssl-verify-server-cert \ |
| 96 | --ssl-ca=/etc/ssl/TestLevel1/certs/TestLevel1.crt \ |
| 97 | --ssl-crl=/etc/ssl/TestGlobal.crl \ |
| 98 | --user=root \ |
| 99 | --password="${ROOT_PASSWORD}" |
| 100 | |
| 101 | # ====== Create schema, views, functions and procedures ================== |
| 102 | for item in functions procedures schema views ; do |
| 103 | if [ -e ../SQL/mariadb-${item}.sql ] ; then |
| 104 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating ${item} ...\x1b[0m" |
| 105 | mysql \ |
| 106 | --host="${server}.${DOMAIN}" \ |
| 107 | --port 3306 \ |
| 108 | --protocol=tcp \ |
| 109 | --ssl \ |
| 110 | --ssl-verify-server-cert \ |
| 111 | --ssl-ca=/etc/ssl/TestLevel1/certs/TestLevel1.crt \ |
| 112 | --ssl-crl=/etc/ssl/TestGlobal.crl \ |
| 113 | --user=root \ |
| 114 | --password="${ROOT_PASSWORD}" \ |
| 115 | --database="${DATABASE}" \ |
| 116 | <../SQL/mariadb-${item}.sql |
| 117 | fi |
| 118 | done |
| 119 | |
| 120 | # ====== Create users and roles ========================================== |
| 121 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating users and roles ...\x1b[0m" |
| 122 | sed -e "s%\${DATABASE}%${DATABASE}%" \ |
| 123 | -e "s%\${ROOT_PASSWORD}%${ROOT_PASSWORD}%" \ |
| 124 | -e "s%\${MAINTAINER_PASSWORD}%${MAINTAINER_PASSWORD}%" \ |
| 125 | -e "s%\${RESEARCHER_PASSWORD}%${RESEARCHER_PASSWORD}%" \ |
| 126 | -e "s%\${IMPORTER_PASSWORD}%${IMPORTER_PASSWORD}%" \ |
| 127 | <../SQL/mariadb-users.sql | \ |
| 128 | tee | \ |
| 129 | mysql \ |
| 130 | --host="${server}.${DOMAIN}" \ |
| 131 | --port 3306 \ |
| 132 | --protocol=tcp \ |
| 133 | --ssl \ |
| 134 | --ssl-verify-server-cert \ |
| 135 | --ssl-ca=/etc/ssl/TestLevel1/certs/TestLevel1.crt \ |
| 136 | --ssl-crl=/etc/ssl/TestGlobal.crl \ |
| 137 | --user=root \ |
| 138 | --password="${ROOT_PASSWORD}" \ |
| 139 | --database="${DATABASE}" |
| 140 | |
| 141 | |
| 142 | # ###### PostgreSQL ######################################################### |
| 143 | elif [ "$1" == "postgresql" ] ; then |
| 144 | # Documentation: https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-22-04-quickstart |
| 145 | server="postgresql" |
| 146 | |
| 147 | echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Preparing admin user for PostgreSQL ...\x1b[0m" |
| 148 | |
| 149 | # ====== Create database ================================================= |
| 150 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating database ...\x1b[0m" |
| 151 | sed -e "s%\${DATABASE}%${DATABASE}%" \ |
| 152 | <../SQL/postgresql-database.sql | \ |
| 153 | tee | \ |
| 154 | sudo -u postgres psql |
| 155 | |
| 156 | # ====== Create schema, views, functions and procedures ================== |
| 157 | for item in functions procedures schema views ; do |
| 158 | if [ -e ../SQL/postgresql-${item}.sql ] ; then |
| 159 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating ${item} ...\x1b[0m" |
| 160 | # shellcheck disable=SC2024 |
| 161 | sudo -u postgres psql -d "${DATABASE}" <../SQL/postgresql-${item}.sql |
| 162 | fi |
| 163 | done |
| 164 | |
| 165 | # ====== Create users and roles ========================================== |
| 166 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating users and roles ...\x1b[0m" |
| 167 | sed -e "s%\${DATABASE}%${DATABASE}%" \ |
| 168 | -e "s%\${ROOT_PASSWORD}%${ROOT_PASSWORD}%" \ |
| 169 | -e "s%\${MAINTAINER_PASSWORD}%${MAINTAINER_PASSWORD}%" \ |
| 170 | -e "s%\${RESEARCHER_PASSWORD}%${RESEARCHER_PASSWORD}%" \ |
| 171 | -e "s%\${IMPORTER_PASSWORD}%${IMPORTER_PASSWORD}%" \ |
| 172 | <../SQL/postgresql-users.sql | \ |
| 173 | tee | \ |
| 174 | sudo -u postgres psql -d "${DATABASE}" |
| 175 | |
| 176 | |
| 177 | # ###### MongoDB ############################################################ |
| 178 | elif [ "$1" == "mongodb" ] ; then |
| 179 | # Documentation: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/ |
| 180 | server="mongodb" |
| 181 | |
| 182 | echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Preparing admin user for MongoDB ...\x1b[0m" |
| 183 | |
| 184 | # ====== Create database ================================================= |
| 185 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating database ...\x1b[0m" |
| 186 | sed -e "s%\${DATABASE}%${DATABASE}%" \ |
| 187 | <../NoSQL/mongodb-database.ms | \ |
| 188 | tee | \ |
| 189 | mongosh \ |
| 190 | "mongodb://${server}.${DOMAIN}:27017" \ |
| 191 | --tls \ |
| 192 | --tlsDisabledProtocols TLS1_0,TLS1_1,TLS1_2 \ |
| 193 | --tlsCAFile /etc/ssl/TestLevel1/certs/TestLevel1.crt \ |
| 194 | --username root --password "${ROOT_PASSWORD}" \ |
| 195 | --quiet |
| 196 | |
| 197 | # --tlsCertificateKeyFile /etc/ssl/[email protected]/[email protected]+key.pem \ |
| 198 | |
| 199 | # ====== Create schema =================================================== |
| 200 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating schema ...\x1b[0m" |
| 201 | sed -e "s%\${DATABASE}%${DATABASE}%" \ |
| 202 | <../NoSQL/mongodb-schema.ms | \ |
| 203 | tee | \ |
| 204 | mongosh \ |
| 205 | "mongodb://${server}.${DOMAIN}:27017" \ |
| 206 | --tls \ |
| 207 | --tlsDisabledProtocols TLS1_0,TLS1_1,TLS1_2 \ |
| 208 | --tlsCAFile /etc/ssl/TestLevel1/certs/TestLevel1.crt \ |
| 209 | --username root --password "${ROOT_PASSWORD}" \ |
| 210 | --quiet |
| 211 | |
| 212 | # ====== Create users and roles ========================================== |
| 213 | echo -e "\x1b[33m$(date +"%F %H:%M:%S"): Creating user and roles ...\x1b[0m" |
| 214 | sed -e "s%\${DATABASE}%${DATABASE}%" \ |
| 215 | -e "s%\${MAINTAINER_PASSWORD}%${MAINTAINER_PASSWORD}%" \ |
| 216 | -e "s%\${RESEARCHER_PASSWORD}%${RESEARCHER_PASSWORD}%" \ |
| 217 | -e "s%\${IMPORTER_PASSWORD}%${IMPORTER_PASSWORD}%" \ |
| 218 | <../NoSQL/mongodb-users.ms | \ |
| 219 | tee | \ |
| 220 | mongosh \ |
| 221 | "mongodb://${server}.${DOMAIN}:27017" \ |
| 222 | --tls \ |
| 223 | --tlsDisabledProtocols TLS1_0,TLS1_1,TLS1_2 \ |
| 224 | --tlsCAFile /etc/ssl/TestLevel1/certs/TestLevel1.crt \ |
| 225 | --username root --password "${ROOT_PASSWORD}" \ |
| 226 | --quiet |
| 227 | echo "" |
| 228 | |
| 229 | |
| 230 | # ###### Error ############################################################## |
| 231 | else |
| 232 | echo >&2 "ERROR: Unsupported database: $1" |
| 233 | exit 1 |
| 234 | fi |