Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 83e4682

Browse files
committed
Vagrant box for running postgres locally
1 parent 92221be commit 83e4682

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Integrates with [Sleepingpill](https://github.com/javaBin/sleepingPillCore) whic
1010
* Build backend: `cd backend && mvn clean install`
1111
* Build frontend: `cd frontend && npm install`
1212
* Build nodeproxy: `cd scripts/proxy && npm install`
13+
* Start local postgres: `cd localpostgres && vagrant up`
1314
* Run the app: `./scripts/start.sh`
1415

1516
This starts a screen. To escape from the screen: `ctrl-x`, `q`, `y`.

localpostgres/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vagrant
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/sh -e
2+
3+
# Edit the following to change the name of the database user that will be created:
4+
APP_DB_USER=submit
5+
APP_DB_PASS=password
6+
7+
# Edit the following to change the name of the database that is created (defaults to the user name)
8+
APP_DB_NAME=submit
9+
10+
# Edit the following to change the version of PostgreSQL that is installed
11+
PG_VERSION=9.6
12+
13+
###########################################################
14+
# Changes below this line are probably not necessary
15+
###########################################################
16+
print_db_usage () {
17+
echo "Your PostgreSQL database has been setup and can be accessed on your local machine on the forwarded port (default: 15432)"
18+
echo " Host: localhost"
19+
echo " Port: 15432"
20+
echo " Database: $APP_DB_NAME"
21+
echo " Username: $APP_DB_USER"
22+
echo " Password: $APP_DB_PASS"
23+
echo ""
24+
echo "Admin access to postgres user via VM:"
25+
echo " vagrant ssh"
26+
echo " sudo su - postgres"
27+
echo ""
28+
echo "psql access to app database user via VM:"
29+
echo " vagrant ssh"
30+
echo " sudo su - postgres"
31+
echo " PGUSER=$APP_DB_USER PGPASSWORD=$APP_DB_PASS psql -h localhost $APP_DB_NAME"
32+
echo ""
33+
echo "Env variable for application development:"
34+
echo " DATABASE_URL=postgresql://$APP_DB_USER:$APP_DB_PASS@localhost:15432/$APP_DB_NAME"
35+
echo ""
36+
echo "Local command to access the database via psql:"
37+
echo " PGUSER=$APP_DB_USER PGPASSWORD=$APP_DB_PASS psql -h localhost -p 15432 $APP_DB_NAME"
38+
}
39+
40+
export DEBIAN_FRONTEND=noninteractive
41+
42+
PROVISIONED_ON=/etc/vm_provision_on_timestamp
43+
if [ -f "$PROVISIONED_ON" ]
44+
then
45+
echo "VM was already provisioned at: $(cat $PROVISIONED_ON)"
46+
echo "To run system updates manually login via 'vagrant ssh' and run 'apt-get update && apt-get upgrade'"
47+
echo ""
48+
print_db_usage
49+
exit
50+
fi
51+
52+
PG_REPO_APT_SOURCE=/etc/apt/sources.list.d/pgdg.list
53+
if [ ! -f "$PG_REPO_APT_SOURCE" ]
54+
then
55+
# Add PG apt repo:
56+
echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" > "$PG_REPO_APT_SOURCE"
57+
58+
# Add PGDG repo key:
59+
wget --quiet -O - https://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | apt-key add -
60+
fi
61+
62+
# Update package list and upgrade all packages
63+
apt-get update
64+
apt-get -y upgrade
65+
66+
apt-get -y install "postgresql-$PG_VERSION" "postgresql-contrib-$PG_VERSION"
67+
68+
PG_CONF="/etc/postgresql/$PG_VERSION/main/postgresql.conf"
69+
PG_HBA="/etc/postgresql/$PG_VERSION/main/pg_hba.conf"
70+
PG_DIR="/var/lib/postgresql/$PG_VERSION/main"
71+
72+
# Edit postgresql.conf to change listen address to '*':
73+
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" "$PG_CONF"
74+
75+
# Append to pg_hba.conf to add password auth:
76+
echo "host all all all md5" >> "$PG_HBA"
77+
78+
# Explicitly set default client_encoding
79+
echo "client_encoding = utf8" >> "$PG_CONF"
80+
81+
# Restart so that all new config is loaded:
82+
service postgresql restart
83+
84+
cat << EOF | su - postgres -c psql
85+
-- Create the database user:
86+
CREATE USER $APP_DB_USER WITH PASSWORD '$APP_DB_PASS';
87+
88+
-- Create the database:
89+
CREATE DATABASE $APP_DB_NAME WITH OWNER=$APP_DB_USER
90+
LC_COLLATE='en_US.utf8'
91+
LC_CTYPE='en_US.utf8'
92+
ENCODING='UTF8'
93+
TEMPLATE=template0;
94+
EOF
95+
96+
# Tag the provision time:
97+
date > "$PROVISIONED_ON"
98+
99+
echo "Successfully created PostgreSQL dev virtual machine."
100+
echo ""
101+
print_db_usage

localpostgres/Vagrantfile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
$script = <<SCRIPT
5+
echo I am provisioning...
6+
date > /etc/vagrant_provisioned_at
7+
SCRIPT
8+
9+
Vagrant.configure("2") do |config|
10+
config.vm.provision "shell", inline: $script
11+
end
12+
13+
Vagrant::Config.run do |config|
14+
config.vm.box = "ubuntu/trusty64"
15+
config.vm.box_url = "https://atlas.hashicorp.com/ubuntu/boxes/trusty64"
16+
config.vm.host_name = "postgresql"
17+
18+
config.vm.share_folder "bootstrap", "/mnt/bootstrap", ".", :create => true
19+
config.vm.provision :shell, :path => "Vagrant-setup/bootstrap.sh"
20+
21+
# PostgreSQL Server port forwarding
22+
config.vm.forward_port 5432, 15432
23+
end

0 commit comments

Comments
 (0)