mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Add an option to use AWS instance address in a PostgreSQL connection string.
When PostgreSQL is running inside the docker container, it should provide the address of the host it's running on and not the address from inside of the container to the outside world.
This commit is contained in:
+13
-1
@@ -7,6 +7,7 @@ from helpers.etcd import Etcd
|
||||
from helpers.postgresql import Postgresql
|
||||
from helpers.ha import Ha
|
||||
|
||||
INSTANCE_METADATA_URL = "http://169.254.169.254/latest/meta-data/"
|
||||
|
||||
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)
|
||||
|
||||
@@ -14,8 +15,19 @@ f = open(sys.argv[1], "r")
|
||||
config = yaml.load(f.read())
|
||||
f.close()
|
||||
|
||||
if config.get('aws_use_host_address', False):
|
||||
# get host address of the AWS host via a call to
|
||||
# http://169.254.169.254/latest/meta-data/local-ipv4
|
||||
try:
|
||||
aws_host_address = urllib2.urlopen(INSTANCE_METADATA_URL+"/local-ipv4").read()
|
||||
except (urllib2.HTTPError, urllib2.URLError) as e:
|
||||
logging.error("Error retrieiving IPv4 address from AWS instance: {0}".format(e))
|
||||
aws_host_address = None
|
||||
else:
|
||||
aws_host_address = None
|
||||
|
||||
etcd = Etcd(config["etcd"])
|
||||
postgresql = Postgresql(config["postgresql"])
|
||||
postgresql = Postgresql(config["postgresql"], aws_host_address)
|
||||
ha = Ha(postgresql, etcd)
|
||||
|
||||
# stop postgresql on script exit
|
||||
|
||||
@@ -9,7 +9,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class Postgresql:
|
||||
|
||||
def __init__(self, config):
|
||||
def __init__(self, config, aws_host_address=None):
|
||||
self.name = config["name"]
|
||||
self.host, self.port = config["listen"].split(":")
|
||||
self.data_dir = config["data_dir"]
|
||||
@@ -18,7 +18,8 @@ class Postgresql:
|
||||
self.config = config
|
||||
|
||||
self.cursor_holder = None
|
||||
self.connection_string = "postgres://%s:%s@%s:%s/postgres" % (self.replication["username"], self.replication["password"], self.host, self.port)
|
||||
connection_host = aws_host_address or self.host
|
||||
self.connection_string = "postgres://%s:%s@%s:%s/postgres" % (self.replication["username"], self.replication["password"], connection_host, self.port)
|
||||
|
||||
self.conn = None
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
loop_wait: 10
|
||||
aws_use_host_address: "on"
|
||||
etcd:
|
||||
scope: batman
|
||||
ttl: 30
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
loop_wait: 10
|
||||
aws_use_host_address: "on"
|
||||
etcd:
|
||||
scope: batman
|
||||
ttl: 30
|
||||
|
||||
Reference in New Issue
Block a user