From 288e7bde061cb0e00f56cadbd31a8deddad1d68b Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Thu, 7 Aug 2025 08:28:12 +0200 Subject: [PATCH] initial commit --- README.md | 102 +++++++++++++++++++ install.sh | 289 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 391 insertions(+) create mode 100644 README.md create mode 100755 install.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f78b38 --- /dev/null +++ b/README.md @@ -0,0 +1,102 @@ +# Reflect installer +Run this script from a git repository to automatically install and configure Reflect as a git submodule to your project. + +> [!IMPORTANT] +> The script will only run in environments with Bash and `coreutils` installed (for now). + +# Prerequisite +A MariaDB or MySQL server with at least `SELECT` rights granted to a database table. (`SELECT`, `INSERT`, `UPDATE`, and `DELETE` rights to this table are required if you wish to manage your Reflect configuration through REST) + +# Get started +1. Create a git repository for your project. +2. Create a new empty MariaDB/MySQL database table. +3. Run this command from the root directory of your local repository. + +```sh +curl -fsSL https://codeberg.org/reflect/install/raw/branch/master/install.sh | bash +``` + +# Arguments +You can pass optional named arguments to this script for headless installation of Reflect. + +## `--overwrite` - Overwrite existing configuration files +```sh +./install.sh --example=n +``` +Pass `--overwrite=y` to overwrite all existing Reflect configuration files. + +## Endpoints + +### `--dir` - Installation directory +```sh +./install.sh --dir="/path/to/project" +``` +Pass a `--dir` argument with a value of a directory Reflect should consider its [`root_path`](https://reflect.vlw.se/docs/Reference/Env#root_path). + +### `--endpoints` - Overwrite configuration files +```sh +./install.sh --endpoints="api" +``` +Pass an `--endpoints` argument with a value of a directory relative to [`root_path`](https://reflect.vlw.se/docs/Reference/Env#root_path) where Reflect endpoint directories are stored. + +### Database + +### `--db` - Reflect database table +```sh +./install.sh --db="reflect" +``` +Pass a `--db` argument with a value of a database table that Reflect should use. + +### `--host` - Reflect database hostname +```sh +./install.sh --host="/path/to/project" +``` +Pass a `--host` argument with a value of a fully qualified hostname that Reflect should use when connecting to you MariaDB/MySQL server. + +### `--user` - Reflect database user +```sh +./install.sh --user="www-data" +``` +Pass a `--user` argument with a value of a username that Reflect should use when connecting to you MariaDB/MySQL server. + +### `--password` - Reflect database user +```sh +./install.sh --password="mypassword" +``` +Pass a `--password` argument with a value of a password that Reflect should use when connecting to you MariaDB/MySQL server. + +```sh +./install.sh --password=null +``` +Pass "null" to this argument to disable password authentication + +### Database seeding + +### `--db` - Reflect database table +```sh +./install.sh --db="reflect" +``` +Pass a `--db` argument with a value of a database table that Reflect should use. + +### `--seed_host` - Reflect database hostname +```sh +./install.sh --seed_host="/path/to/project" +``` +Pass a `--host` argument with a value of a fully qualified hostname that will be used by this script to seed the database. + +### `--seed_user` - Reflect database user +```sh +./install.sh --seed_user="www-data" +``` +Pass a `--user` argument with a value of a username that will be used by this script to seed the database. + +### `--seed_password` - Reflect database user +```sh +./install.sh --seed_password="mypassword" +``` +Pass a `--password` argument with a value of a password that will be used by this script to seed the database. + +```sh +./install.sh --seed_password=null +``` +Pass "null" to this argument to disable password authentication \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..11f7b95 --- /dev/null +++ b/install.sh @@ -0,0 +1,289 @@ +#!/bin/bash + +# Initialize variables +db="" +cwd="" +dir="" +host="" +user="" +seed="" +password="" +seed_host="" +seed_user="" +overwrite="" +endpoints="" +seed_password="" + +echo_err() { + echo "!! -> $1" +} + +# Bail out from an error with a message asking the user to report the incident +exit_report() { + echo_err "Please report this error at: https://codeberg.org/reflect/install/issues" + exit 1 +} + +# Make sure we have all the system packages we need to proceed with the installation +check_sys_depend() { + local valid=true + local packages=("git" "composer" "mysql") + + for package in "${packages[@]}" ; do + if ! dpkg -l | grep -qw "ii ${package}" ; then + echo_err "Package '${package}' is not installed." + valid=false + fi + done + + # Bail out if any required package is missing + if [ "$valid" = false ] ; then + exit 1 + fi +} + +install_reflect() { + if ! [ -d ".git" ] ; then + echo_err "Installation aborted: '${cwd}' is not a git repository" + exit 1 + fi + + if ! [ -d "reflect" ] ; then + git submodule add https://codeberg.org/reflect/reflect + fi + + # Update submodules + git submodule update --init --recursive + + # Install dependencies with composer + (cd reflect && composer install --classmap-authoritative) + + # Bail out if composer didn't create a vendor folder + if ! [ -d "reflect/vendor" ] ; then + echo_err "Something went wrong with the installation." + exit_report + fi +} + +configure_reflect() { + if ! [ -d "reflect" ] ; then + echo_err "Reflect is not installed." + exit_report + fi + + # A configuration file already exists + if [[ -f "reflect/.env.ini" ]] ; then + if ! [[ -n "$overwrite" ]]; then + echo "A Reflect configuration file already exists at: ${cwd}/reflect/.env.ini" + read -p "Do you want to overwrite this file? (y/n): " overwrite ]: " seed_password > reflect/.env.ini + done +} + +main() { + # Get the current working directory + cwd=$(pwd) + + check_sys_depend + install_reflect + configure_reflect + + echo + echo "Reflect has been sucessfully installed." + echo "Thank you for choosing Reflect :)" + echo + + exit 0 +} + +# Loop through all arguments +for arg in "$@"; do + case $arg in + --db=*) + db="${arg#*=}" + ;; + + --dir=*) + dir="${arg#*=}" + ;; + + --host=*) + host="${arg#*=}" + ;; + + --user=*) + user="${arg#*=}" + ;; + + --seed=*) + seed="${arg#*=}" + ;; + + --seed-host=*) + seed_host="${arg#*=}" + ;; + + --seed-user=*) + seed_user="${arg#*=}" + ;; + + --password=*) + password="${arg#*=}" + ;; + + --overwrite=*) + overwrite="${arg#*=}" + ;; + + --endpoints=*) + endpoints="${arg#*=}" + ;; + + --seed-password=*) + seed_password="${arg#*=}" + ;; + + *) + echo_err "Unknown argument: $arg" + ;; + esac +done + +# Start installation in passed directory +if [[ -n "$dir" ]]; then + if ! [ -d "$dir" ]; then + echo_err "Installation aborted: '$dir' is not a directory" + exit 1 + fi + + cd $dir + cwd=$(pwd) + main +fi + +# Prompt the user for confirmation +echo +echo "You are currently in: $(pwd)" +read -p "Do you want to proceed with the installation in this directory? (y/n): " choice