Commit 90a7f4a7 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

slapos: Update slapgrid to use new API

parent 8652fba3
...@@ -423,6 +423,7 @@ class Partition(object): ...@@ -423,6 +423,7 @@ class Partition(object):
instance_storage_home='', instance_storage_home='',
ipv4_global_network='', ipv4_global_network='',
buildout_debug=False, buildout_debug=False,
api_backward_compatibility=False,
): ):
"""Initialisation of class parameters""" """Initialisation of class parameters"""
self.buildout = buildout self.buildout = buildout
...@@ -443,6 +444,7 @@ class Partition(object): ...@@ -443,6 +444,7 @@ class Partition(object):
self.software_release_url = software_release_url self.software_release_url = software_release_url
self.instance_storage_home = instance_storage_home self.instance_storage_home = instance_storage_home
self.ipv4_global_network = ipv4_global_network self.ipv4_global_network = ipv4_global_network
self.api_backward_compatibility = api_backward_compatibility
self.key_file = '' self.key_file = ''
self.cert_file = '' self.cert_file = ''
...@@ -489,16 +491,20 @@ class Partition(object): ...@@ -489,16 +491,20 @@ class Partition(object):
# Certificate files are unset, skip. # Certificate files are unset, skip.
return return
try: if self.api_backward_compatibility:
partition_certificate = self.computer_partition.getCertificate() try:
except NotFoundError: partition_certificate = self.computer_partition["slap_partition"].getCertificate()
raise NotFoundError('Partition %s is not known by SlapOS Master.' % self.computer_partition["X509"] = {}
self.partition_id) self.computer_partition["X509"]["certificate"] = partition_certificate["certificate"]
self.computer_partition["X509"]["key"] = partition_certificate["key"]
except NotFoundError:
raise NotFoundError('Partition %s is not known by SlapOS Master.' %
self.partition_id)
uid, gid = self.getUserGroupId() uid, gid = self.getUserGroupId()
for name, path in [('certificate', self.cert_file), ('key', self.key_file)]: for name, path in [('certificate', self.cert_file), ('key', self.key_file)]:
new_content = partition_certificate[name] new_content = self.computer_partition["X509"][name]
old_content = None old_content = None
if os.path.exists(path): if os.path.exists(path):
old_content = open(path).read() old_content = open(path).read()
...@@ -582,7 +588,7 @@ class Partition(object): ...@@ -582,7 +588,7 @@ class Partition(object):
installs the software partition with the help of buildout installs the software partition with the help of buildout
""" """
self.logger.info("Installing Computer Partition %s..." self.logger.info("Installing Computer Partition %s..."
% self.computer_partition.getId()) % self.computer_partition.get("compute_partition_id"))
self.check_free_space() self.check_free_space()
...@@ -731,7 +737,7 @@ class Partition(object): ...@@ -731,7 +737,7 @@ class Partition(object):
if os.path.exists(self.supervisord_partition_configuration_path): if os.path.exists(self.supervisord_partition_configuration_path):
os.unlink(self.supervisord_partition_configuration_path) os.unlink(self.supervisord_partition_configuration_path)
else: else:
partition_id = self.computer_partition.getId() partition_id = self.computer_partition.get("compute_partition_id")
group_partition_template = bytes2str(pkg_resources.resource_string(__name__, group_partition_template = bytes2str(pkg_resources.resource_string(__name__,
'templates/group_partition_supervisord.conf.in')) 'templates/group_partition_supervisord.conf.in'))
self.supervisor_configuration_group = group_partition_template % { self.supervisor_configuration_group = group_partition_template % {
...@@ -766,22 +772,22 @@ class Partition(object): ...@@ -766,22 +772,22 @@ class Partition(object):
"""Asks supervisord to start the instance. If this instance is not """Asks supervisord to start the instance. If this instance is not
installed, we install it. installed, we install it.
""" """
partition_id = self.computer_partition.getId() partition_id = self.computer_partition.get("compute_partition_id")
try: try:
with self.getSupervisorRPC() as supervisor: with self.getSupervisorRPC() as supervisor:
supervisor.startProcessGroup(partition_id, False) supervisor.startProcessGroup(partition_id, False)
except xmlrpclib.Fault as exc: except xmlrpclib.Fault as exc:
if exc.faultString.startswith('BAD_NAME:'): if exc.faultString.startswith('BAD_NAME:'):
self.logger.info("Nothing to start on %s..." % self.logger.info("Nothing to start on %s..." %
self.computer_partition.getId()) self.computer_partition.get("compute_partition_id"))
else: else:
raise raise
else: else:
self.logger.info("Requested start of %s..." % self.computer_partition.getId()) self.logger.info("Requested start of %s..." % self.computer_partition.get("compute_partition_id"))
def stop(self): def stop(self):
"""Asks supervisord to stop the instance.""" """Asks supervisord to stop the instance."""
partition_id = self.computer_partition.getId() partition_id = self.computer_partition.get("compute_partition_id")
try: try:
with self.getSupervisorRPC() as supervisor: with self.getSupervisorRPC() as supervisor:
supervisor.stopProcessGroup(partition_id, False) supervisor.stopProcessGroup(partition_id, False)
...@@ -791,13 +797,13 @@ class Partition(object): ...@@ -791,13 +797,13 @@ class Partition(object):
else: else:
raise raise
else: else:
self.logger.info("Requested stop of %s..." % self.computer_partition.getId()) self.logger.info("Requested stop of %s..." % self.computer_partition.get("compute_partition_id"))
def destroy(self): def destroy(self):
"""Destroys the partition and makes it available for subsequent use." """Destroys the partition and makes it available for subsequent use."
""" """
self.logger.info("Destroying Computer Partition %s..." self.logger.info("Destroying Computer Partition %s..."
% self.computer_partition.getId()) % self.computer_partition.get("compute_partition_id"))
self.createRetentionLockDate() self.createRetentionLockDate()
if not self.checkRetentionIsAuthorized(): if not self.checkRetentionIsAuthorized():
......
This diff is collapsed.
...@@ -82,9 +82,8 @@ class Manager(object): ...@@ -82,9 +82,8 @@ class Manager(object):
# Get partitions IPv6 address # Get partitions IPv6 address
computer_partition = partition.computer_partition computer_partition = partition.computer_partition
parameter_dict = computer_partition.getInstanceParameterDict()
partition_ip_list = parameter_dict['ip_list'] + parameter_dict.get( partition_ip_list = computer_partition['ip_list'] + computer_partition.get(
'full_ip_list', []) 'full_ip_list', [])
partition_ip_list = [tup[1] for tup in partition_ip_list] partition_ip_list = [tup[1] for tup in partition_ip_list]
......
...@@ -741,6 +741,36 @@ class SlapConnectionHelper(ConnectionHelper): ...@@ -741,6 +741,36 @@ class SlapConnectionHelper(ConnectionHelper):
return loads(xml) return loads(xml)
# https://stackoverflow.com/a/33571117
def _byteify(data, ignore_dicts = False):
if isinstance(data, str):
return data
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [ _byteify(item, ignore_dicts=True) for item in data ]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.items() # changed to .items() for python 2.7/3
}
# python 3 compatible duck-typing
# if this is a unicode string, return its string representation
if str(type(data)) == "<type 'unicode'>":
return data.encode('utf-8')
# if it's anything else, return it in its original form
return data
def json_loads_byteified(json_text):
return _byteify(
json.loads(json_text, object_hook=_byteify),
ignore_dicts=True
)
class JioAPIConnectionHelper(ConnectionHelper): class JioAPIConnectionHelper(ConnectionHelper):
def apiCall(self, path, data): def apiCall(self, path, data):
...@@ -749,7 +779,7 @@ class JioAPIConnectionHelper(ConnectionHelper): ...@@ -749,7 +779,7 @@ class JioAPIConnectionHelper(ConnectionHelper):
data=json.dumps(data), data=json.dumps(data),
headers={'Content-type': 'application/json'}, headers={'Content-type': 'application/json'},
expect_json_error=True) expect_json_error=True)
return req.json() return json_loads_byteified(req.text)
def get(self, data): def get(self, data):
return self.apiCall(path="get/", return self.apiCall(path="get/",
......
This diff is collapsed.
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