Cache is used if defined.

parent 16b5940f
...@@ -545,7 +545,7 @@ class ConnectionHelper: ...@@ -545,7 +545,7 @@ class ConnectionHelper:
ssl_error_message_connect_fail = "\nCouldn't authenticate computer. Please "\ ssl_error_message_connect_fail = "\nCouldn't authenticate computer. Please "\
"check that certificate and key exist and are valid. " "check that certificate and key exist and are valid. "
def __init__(self, connection_wrapper, host, path, key_file=None, def __init__(self, connection_wrapper, host, path, key_file=None,
cert_file=None, master_ca_file=None, timeout=None): cert_file=None, master_ca_file=None, timeout=None, cache_location=None):
self.connection_wrapper = connection_wrapper self.connection_wrapper = connection_wrapper
self.host = host self.host = host
self.path = path self.path = path
...@@ -553,18 +553,24 @@ class ConnectionHelper: ...@@ -553,18 +553,24 @@ class ConnectionHelper:
self.cert_file = cert_file self.cert_file = cert_file
self.master_ca_file = master_ca_file self.master_ca_file = master_ca_file
self.timeout = timeout self.timeout = timeout
self.cache_location = cache_location
def getComputerInformation(self, computer_id): def getComputerInformation(self, computer_id):
self.GET('/getComputerInformation?computer_id=%s' % computer_id) self.GET('/getComputerInformation?computer_id=%s' % computer_id)
return xml_marshaller.loads(self.response.read()) return xml_marshaller.loads(self.response.read())
def getFullComputerInformation(self, computer_id): def getFullComputerInformation(self, computer_id):
if os.path.exists("/opt/slapos/cache"): # Check if cache (file) is enabled and exists: return directly from cache
return xml_marshaller.loads(open("/opt/slapos/cache", "r").read()) if self.cache_location:
computer_cache_location = os.path.join(cache_location, 'computer.xml')
if self.cache_location and os.path.exists(computer_cache_location):
return xml_marshaller.loads(open(computer_cache_location, "r").read())
# Fetch up-to-date information if no valid cache or cache is not enabled
self.GET('/getFullComputerInformation?computer_id=%s' % computer_id) self.GET('/getFullComputerInformation?computer_id=%s' % computer_id)
if not os.path.exists("/opt/slapos/cache"): # If cache enabled: set it
if self.cache_location and not os.path.exists(computer_cache_location):
cache = self.response.read() cache = self.response.read()
open("/opt/slapos/cache", "w+").write(cache) open(computer_cache_location, "w+").write(cache)
return xml_marshaller.loads(cache) return xml_marshaller.loads(cache)
return xml_marshaller.loads(self.response.read()) return xml_marshaller.loads(self.response.read())
...@@ -645,6 +651,8 @@ class ConnectionHelper: ...@@ -645,6 +651,8 @@ class ConnectionHelper:
class slap: class slap:
zope.interface.implements(interface.slap) zope.interface.implements(interface.slap)
def __init__(self, cache_location=None):
self.cache_location = cache_location
def initializeConnection(self, slapgrid_uri, key_file=None, cert_file=None, def initializeConnection(self, slapgrid_uri, key_file=None, cert_file=None,
master_ca_file=None, timeout=60): master_ca_file=None, timeout=60):
...@@ -665,7 +673,8 @@ class slap: ...@@ -665,7 +673,8 @@ class slap:
raise AttributeError('Passed URL %r issue: there is no support for %r p' raise AttributeError('Passed URL %r issue: there is no support for %r p'
'rotocol' % (slapgrid_uri, scheme)) 'rotocol' % (slapgrid_uri, scheme))
self._connection_helper = ConnectionHelper(connection_wrapper, self._connection_helper = ConnectionHelper(connection_wrapper,
netloc, path, key_file, cert_file, master_ca_file, timeout) netloc, path, key_file, cert_file, master_ca_file, timeout,
cache_location)
def registerSoftwareRelease(self, software_release): def registerSoftwareRelease(self, software_release):
""" """
...@@ -688,16 +697,18 @@ class slap: ...@@ -688,16 +697,18 @@ class slap:
Registers connected representation of computer partition and Registers connected representation of computer partition and
returns Computer Partition class object returns Computer Partition class object
""" """
cache_id = "/tmp/cache_registerComputerPartition%s%s" % (computer_guid, partition_id) if self.cache_location:
if os.path.exists(cache_id): partition_cache_location = os.path.join(cache_location,
cache = open(cache_id, "r").read() 'partition%s%s.xml' % (computer_guid, partition_id))
if self.cache_location and os.path.exists(partition_cache_location):
cache = open(partition_cache_location, "r").read()
else: else:
self._connection_helper.GET('/registerComputerPartition?' \ self._connection_helper.GET('/registerComputerPartition?' \
'computer_reference=%s&computer_partition_reference=%s' % ( 'computer_reference=%s&computer_partition_reference=%s' % (
computer_guid, partition_id)) computer_guid, partition_id))
if not os.path.exists(cache_id): if self.cache_location and not os.path.exists(partition_cache_location):
cache = self._connection_helper.response.read() cache = self._connection_helper.response.read()
open(cache_id, "w+").write(cache) open(partition_cache_location, "w+").write(cache)
result = xml_marshaller.loads(cache) result = xml_marshaller.loads(cache)
# XXX: dirty hack to make computer partition usable. xml_marshaller is too # XXX: dirty hack to make computer partition usable. xml_marshaller is too
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment