Created
July 7, 2015 03:39
-
-
Save mdaniel/ec88b0f907da70c3f3bd to your computer and use it in GitHub Desktop.
Vagrantup pg_shard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby sw=2 sts=2: | |
# this controls which are implictly trusted by pg | |
my_subnet = "192.168.33.0/24" | |
host_by_ip = { | |
"192.168.33.10" => "pg-master", | |
"192.168.33.11" => "pg-slave-1", | |
"192.168.33.12" => "pg-slave-2", | |
} | |
hosts_txt = host_by_ip.map { |ip, hn| | |
"#{ip}\t#{hn}" | |
}.join("\n") | |
Vagrant.configure(2) do |config| | |
config.vm.box = "ubuntu/trusty64" | |
config.vm.box_check_update = true | |
host_by_ip.each do |ip, hostname| | |
config.vm.define hostname do |c| | |
# don't let vagrant do this, or it'll bind the hostname to 127.x | |
# c.vm.hostname = hostname | |
c.vm.network "private_network", ip: ip | |
c.vm.provider "virtualbox" do |vb| | |
vb.gui = false | |
vb.memory = "512" | |
end | |
c.vm.provision "shell", inline: <<SH | |
set -eu | |
set -x | |
cat >>/etc/hosts<<HOSTS | |
#{hosts_txt} | |
HOSTS | |
echo #{hostname} > /etc/hostname | |
hostname `cat /etc/hostname` | |
SH | |
c.vm.provision "shell" do |s| | |
s.path = "Vagrantfile.provision.sh" | |
s.args = "#{my_subnet}" | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -eu | |
pg_ver=9.4.1 | |
pg_home=/opt/pg-${pg_ver} | |
pg_owner=vagrant | |
PGDATA=/var/run/postgres | |
if [ -x "${pg_home}/bin/psql" ]; then | |
echo "Found existing PG install at \"$pg_home\"; exiting." >&2 | |
exit 0 | |
fi | |
my_subnet=${1:-} | |
if [ -z "$my_subnet" ]; then | |
echo "I require the subnet/cidr as the first arg" >&2 | |
exit 1 | |
fi | |
apt-get update | |
env DEBIAN_FRONTEND=noninteractive apt-get install -y \ | |
build-essential \ | |
git \ | |
libreadline-dev \ | |
libssl-dev \ | |
libxml2-dev \ | |
libxslt1-dev \ | |
python-dev \ | |
zlib1g-dev | |
cd /tmp | |
tar -xjf /vagrant/postgresql-${pg_ver}.tar.bz2 | |
mkdir /tmp/objdir | |
cd /tmp/objdir | |
../postgresql-${pg_ver}/configure \ | |
--prefix=${pg_home} \ | |
--disable-nls \ | |
--without-tcl \ | |
--without-perl \ | |
--with-python \ | |
--without-pam \ | |
--without-ldap \ | |
--without-bonjour \ | |
--with-openssl \ | |
--without-selinux \ | |
--with-libxml \ | |
--with-libxslt \ | |
--with-zlib | |
make >build.out 2>build.err | |
make install | |
PATH=${pg_home}/bin:$PATH | |
git clone file:///vagrant/.git pg_shard | |
cd pg_shard | |
make | |
make install | |
mkdir $PGDATA | |
chmod 0700 $PGDATA | |
chown -R $pg_owner $PGDATA | |
su -c "$pg_home/bin/initdb --encoding UTF-8 --locale=C $PGDATA" $pg_owner | |
sed -i.bak \ | |
-e "/listen_addresses/s/.*/listen_addresses = '*'/" \ | |
-e "/shared_preload_libraries/s/.*/shared_preload_libraries = 'pg_shard'/" \ | |
$PGDATA/postgresql.conf | |
if grep -q pg-master /etc/hostname; then | |
echo "Installing slave list ..." | |
awk '/pg-master/{next;} /pg-/{print $2" 5432"}' /etc/hosts | \ | |
sudo -u $pg_owner tee $PGDATA/pg_worker_list.conf >/dev/null | |
fi | |
echo "Patching pg_hba.conf" | |
cat >> $PGDATA/pg_hba.conf<<HBA | |
host\tall\tall\t${my_subnet}.0/24\ttrust | |
host\treplication\tall\t${my_subnet}.0/24\ttrust | |
HBA | |
chown -R $pg_owner $PGDATA/*.conf | |
echo 'Launching postgres ...' | |
su -c "${pg_home}/bin/pg_ctl -D $PGDATA -l $PGDATA/server.log -w start" $pg_owner | |
echo 'Good luck!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment