def save_model(self, request, obj, form, change):
"""
Automatically detect/populate several fields before saving instance
Since we're defining what is (hopefully) an existing structure,
we should be able to auto-detect several elements from the database
itself. There can also a backend monitor on each server that will
keep these values updated, but bootstrapping is always best.
Autodetected fields:
* is_online
* master
* version
"""
# First, check the online status. We want this to be as fresh as
# possible, so we might as well grab it now.
obj.is_online = False
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
check = sock.connect_ex((obj.server.hostname, obj.herd.db_port))
if check == 0:
obj.is_online = True
# Then, since herds are organized such that each herd follows a single
# primary node, we can auto-declare that this is a replica or not.
# If we search and find a primary for this herd, that instance will
# become our master.
util = PGUtility(obj)
obj.master = util.get_herd_primary()
obj.version = util.get_version()
if obj.master and not obj.version:
obj.version = obj.master.version
# Save now that we've hijacked everything.
obj.save()
# Attempt to initialize the instance. This only works if the instance
# doesn't already exist. It's also optional, so don't derail the save
# just because it didn't fully work.
try:
util.init_missing()
except Exception, e:
self.message_user(request, "Instance init: %s" % str(e),
messages.WARNING
)
评论列表
文章目录