bdr.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/sh
  2. #
  3. # This doesn't seem to be currently in use (2017.04.25).
  4. #
  5. #move to script directory so all relative paths work
  6. cd "$(dirname "$0")"
  7. #includes
  8. . ../config.sh
  9. #set the date
  10. now=$(date +%Y-%m-%d)
  11. #set the database password
  12. if [ .$database_password = .'random' ]; then
  13. database_password=$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | base64 | sed 's/[=\+//]//g')
  14. fi
  15. #show this server's addresses
  16. server_address=$(hostname -I);
  17. echo "This Server Address: $server_address"
  18. #nodes addresses
  19. read -p "Enter all Node IP Addresses: " nodes
  20. #request the domain and email
  21. read -p 'Create Group (true/false): ' group_create
  22. if [ .$group_create = .true ]; then
  23. read -p 'Enter this Nodes Address: ' node_1;
  24. else
  25. read -p 'Join using node already in group: ' node_1;
  26. read -p 'Enter this Nodes Address: ' node_2;
  27. fi
  28. #settings summary
  29. echo "-----------------------------";
  30. echo " Summary";
  31. echo "-----------------------------";
  32. echo "Create Group: $group_create";
  33. echo "All Node IP Addresses: $nodes";
  34. if [ .$group_create = .true ]; then
  35. echo "This Nodes Address: $node_1";
  36. else
  37. echo "Join using node in group: $node_1;"
  38. echo "This Node Address: $node_2";
  39. fi
  40. echo "";
  41. #verify
  42. read -p 'Is the information correct (y/n): ' verified
  43. if [ .$verified != ."y" ]; then
  44. echo "Goodbye";
  45. exit 0;
  46. fi
  47. #iptables rules
  48. for node in $nodes; do
  49. iptables -A INPUT -j ACCEPT -p tcp --dport 5432 -s ${node}/32
  50. iptables -A INPUT -j ACCEPT -p tcp --dport 8080 -s ${node}/32
  51. done
  52. apt-get remove iptables-persistent -y
  53. echo iptables-persistent iptables-persistent/autosave_v4 boolean true | debconf-set-selections
  54. echo iptables-persistent iptables-persistent/autosave_v6 boolean true | debconf-set-selections
  55. apt-get install -y iptables-persistent
  56. #setup ssl
  57. sed -i /etc/postgresql/9.4/main/postgresql.conf -e s:'snakeoil.key:snakeoil-postgres.key:'
  58. cp /etc/ssl/private/ssl-cert-snakeoil.key /etc/ssl/private/ssl-cert-snakeoil-postgres.key
  59. chown postgres:postgres /etc/ssl/private/ssl-cert-snakeoil-postgres.key
  60. chmod 600 /etc/ssl/private/ssl-cert-snakeoil-postgres.key
  61. #postgresql.conf - append settings
  62. cp /etc/postgresql/9.4/main/postgresql.conf /etc/postgresql/9.4/main/postgresql.conf-$now
  63. cat ../postgresql/postgresql.conf > /etc/postgresql/9.4/main/postgresql.conf
  64. #pg_hba.conf - append settings
  65. cp /etc/postgresql/9.4/main/pg_hba.conf /etc/postgresql/9.4/main/pg_hba.conf-$now
  66. cat ../postgresql/pg_hba.conf > /etc/postgresql/9.4/main/pg_hba.conf
  67. #chmod 640 /etc/postgresql/9.4/main/pg_hba.conf
  68. #chown -R postgres:postgres /etc/postgresql/9.4/main
  69. for node in $nodes; do
  70. echo "hostssl all all ${node}/32 trust" >> /etc/postgresql/9.4/main/pg_hba.conf
  71. echo "hostssl replication postgres ${node}/32 trust" >> /etc/postgresql/9.4/main/pg_hba.conf
  72. done
  73. #reload configuration
  74. systemctl daemon-reload
  75. #restart postgres
  76. systemctl restart postgresql
  77. #set the working directory
  78. cwd=$(pwd)
  79. cd /tmp
  80. #add the database users and databases
  81. sudo -u postgres psql -c "CREATE DATABASE fusionpbx;";
  82. sudo -u postgres psql -c "CREATE DATABASE freeswitch;";
  83. #add the users and grant permissions
  84. sudo -u postgres psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$database_password';"
  85. sudo -u postgres psql -c "CREATE ROLE freeswitch WITH SUPERUSER LOGIN PASSWORD '$database_password';"
  86. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
  87. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"
  88. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to freeswitch;"
  89. #add the postgres extensions
  90. sudo -u postgres psql -d fusionpbx -c "CREATE EXTENSION btree_gist;";
  91. sudo -u postgres psql -d fusionpbx -c "CREATE EXTENSION bdr;";
  92. sudo -u postgres psql -d freeswitch -c "CREATE EXTENSION btree_gist;";
  93. sudo -u postgres psql -d freeswitch -c "CREATE EXTENSION bdr;";
  94. #add master nodes
  95. if [ .$group_create = .true ]; then
  96. #add first node
  97. sudo -u postgres psql -d fusionpbx -c "SELECT bdr.bdr_group_create(local_node_name := '$node_1', node_external_dsn := 'host=$node_1 port=5432 dbname=fusionpbx connect_timeout=10 keepalives_idle=5 keepalives_interval=1 sslmode=require');";
  98. sudo -u postgres psql -d freeswitch -c "SELECT bdr.bdr_group_create(local_node_name := '$node_1', node_external_dsn := 'host=$node_1 port=5432 dbname=freeswitch connect_timeout=10 keepalives_idle=5 keepalives_interval=1 sslmode=require');";
  99. else
  100. #add additional master nodes
  101. sudo -u postgres psql -d fusionpbx -c "SELECT bdr.bdr_group_join(local_node_name := '$node_2', node_external_dsn := 'host=$node_2 port=5432 dbname=fusionpbx connect_timeout=10 keepalives_idle=5 keepalives_interval=1', join_using_dsn := 'host=$node_1 port=5432 dbname=fusionpbx connect_timeout=10 keepalives_idle=5 keepalives_interval=1 sslmode=require');";
  102. sudo -u postgres psql -d freeswitch -c "SELECT bdr.bdr_group_join(local_node_name := '$node_2', node_external_dsn := 'host=$node_2 port=5432 dbname=freeswitch connect_timeout=10 keepalives_idle=5 keepalives_interval=1', join_using_dsn := 'host=$node_1 port=5432 dbname=freeswitch connect_timeout=10 keepalives_idle=5 keepalives_interval=1 sslmode=require');";
  103. fi
  104. #load the freeswitch database
  105. #sudo -u postgres psql -d freeswitch -f /var/www/fusionpbx/resources/install/sql/switch.sql -L /tmp/switch-sql.log
  106. #sleeping
  107. if [ .$group_create = .false ]; then
  108. echo "Sleeping for 15 seconds";
  109. for i in `seq 1 15`; do
  110. echo $i
  111. sleep 1
  112. done
  113. fi
  114. #add extension pgcrypto
  115. if [ .$group_create = .false ]; then
  116. sudo -u postgres psql -d freeswitch -c "CREATE EXTENSION pgcrypto;";
  117. fi
  118. #message to user
  119. echo "Completed"