#!/usr/bin/env bash # Based on script from https://github.com/jackdb/pg-app-dev-vm/blob/master/Vagrant-setup/bootstrap.sh set -e curl https://raw.githubusercontent.com/IgnisDa/printr/main/get-printr.sh | sh -s -- --yes printr -f bold "Setting configuration variables" # Edit the following to change the name of the database user that will be created: APP_DB_USER="{{cookiecutter.postgres_admin_user}}" APP_DB_PASS="{{cookiecutter.postgres_password}}" # Edit the following to change the name of the database that is created (defaults to the user name) APP_DB_NAME="{{cookiecutter.postgres_database_name}}" # Edit the following to change the version of PostgreSQL that is installed PG_VERSION=13 printr -f bold "Configuration variables complete" print_db_usage () { printr -c cyan -f bold "Admin access to postgres user via VM:" printr -c cyan -f bold " vagrant ssh" printr -c cyan -f bold " sudo su - postgres" echo "" printr -c cyan -f bold "psql access to app database user via VM:" printr -c cyan -f bold " vagrant ssh" printr -c cyan -f bold " sudo su - postgres" printr -c cyan -f bold " PGUSER=$APP_DB_USER PGPASSWORD=$APP_DB_PASS psql -h localhost $APP_DB_NAME" } export DEBIAN_FRONTEND=noninteractive PROVISIONED_ON=/etc/vm_provision_on_timestamp if [ -f "$PROVISIONED_ON" ] then echo "VM was already provisioned at: $(cat $PROVISIONED_ON)" echo "To run system updates manually login via 'vagrant ssh' and run 'apt-get update" echo "" print_db_usage exit fi printr -f bold "Adding apt-keys and installing environment packages" PG_REPO_APT_SOURCE=/etc/apt/sources.list.d/pgdg.list if [ ! -f "$PG_REPO_APT_SOURCE" ] then # Add PG apt repo: echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > "$PG_REPO_APT_SOURCE" # Add PGDG repo key: wget --quiet -O - https://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | apt-key add - fi # Update package list apt-get update curl -fsSL https://get.docker.com | sh sudo usermod -aG docker vagrant # Install postgresql, python3.8 and fish shell (required for development) apt-get -y install "postgresql-$PG_VERSION" "postgresql-contrib-$PG_VERSION" "bat" "fish" "python3" "python3-pip" "exa" "neovim" printr -c yellow -f bold "Environment packages installed successfully!" PG_CONF="/etc/postgresql/$PG_VERSION/main/postgresql.conf" PG_HBA="/etc/postgresql/$PG_VERSION/main/pg_hba.conf" PG_DIR="/var/lib/postgresql/$PG_VERSION/main" # Edit postgresql.conf to change listen address to '*': sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" "$PG_CONF" # Append to pg_hba.conf to add password auth: echo "host all all all md5" >> "$PG_HBA" # Explicitly set default client_encoding echo "client_encoding = utf8" >> "$PG_CONF" # Restart so that all new config is loaded: service postgresql restart printr -c cyan -f bold "Creating database with the given parameters" cat << EOF | su - postgres -c psql -- Create the database user: CREATE ROLE "$APP_DB_USER" WITH LOGIN CREATEDB ENCRYPTED PASSWORD '$APP_DB_PASS'; -- Create the database: CREATE DATABASE "$APP_DB_NAME" WITH OWNER="$APP_DB_USER" LC_COLLATE="en_US.utf8" LC_CTYPE="en_US.utf8" ENCODING="UTF8" TEMPLATE=template0; -- Grant necessary privileges GRANT ALL PRIVILEGES ON DATABASE "$APP_DB_NAME" TO "$APP_DB_USER"; EOF printr -c yellow "Database creation complete!" echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf sudo sysctl -p # Tag the provision time: date > "$PROVISIONED_ON" timedatectl set-ntp true timedatectl set-timezone Asia/Kolkata printr -c cyan -f bold "Installing global python packages" pip3 install poetry print_db_usage echo "" printr -c blue -f bold "Successfully created {{cookiecutter.project_name}} development virtual machine"