Commit a999ce10 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin Committed by Titouan Soulard

slapos: Update slapgrid to use new API

parent 9b720767
...@@ -433,6 +433,7 @@ class Partition(object): ...@@ -433,6 +433,7 @@ class Partition(object):
ipv4_global_network='', ipv4_global_network='',
buildout_debug=False, buildout_debug=False,
partition_timeout=None, partition_timeout=None,
api_backward_compatibility=False,
): ):
"""Initialisation of class parameters""" """Initialisation of class parameters"""
self.buildout = buildout self.buildout = buildout
...@@ -455,6 +456,7 @@ class Partition(object): ...@@ -455,6 +456,7 @@ class Partition(object):
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.partition_timeout = partition_timeout self.partition_timeout = partition_timeout
self.api_backward_compatibility = api_backward_compatibility
self.key_file = '' self.key_file = ''
self.cert_file = '' self.cert_file = ''
...@@ -501,16 +503,20 @@ class Partition(object): ...@@ -501,16 +503,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):
with open(path) as f: with open(path) as f:
...@@ -566,7 +572,7 @@ class Partition(object): ...@@ -566,7 +572,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.partition_id) % self.computer_partition.get("compute_partition_id"))
self.check_free_space() self.check_free_space()
...@@ -713,6 +719,14 @@ class Partition(object): ...@@ -713,6 +719,14 @@ class Partition(object):
self.logger.warning('No runners nor services found for partition %r' % self.logger.warning('No runners nor services found for partition %r' %
self.partition_id) self.partition_id)
else: else:
partition_id = self.computer_partition.get("compute_partition_id")
group_partition_template = bytes2str(pkg_resources.resource_string(__name__,
'templates/group_partition_supervisord.conf.in'))
self.supervisor_configuration_group = group_partition_template % {
'instance_id': partition_id,
'program_list': ','.join(['_'.join([partition_id, runner])
for runner in runner_list + service_list])
}
# Same method to add to service and run # Same method to add to service and run
self.addServicesToGroup(runner_list, self.run_path) self.addServicesToGroup(runner_list, self.run_path)
self.addServicesToGroup( self.addServicesToGroup(
...@@ -784,22 +798,22 @@ class Partition(object): ...@@ -784,22 +798,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.
""" """
self.updateSupervisorConfiguration() partition_id = self.computer_partition.get("compute_partition_id")
partition_id = self.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..." % partition_id) self.logger.info("Nothing to start on %s..." %
self.computer_partition.get("compute_partition_id"))
else: else:
raise raise
else: else:
self.logger.info("Requested start of %s..." % partition_id) 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.partition_id partition_id = self.computer_partition.get("compute_partition_id")
filename = partition_id + '.conf' filename = partition_id + '.conf'
filepath = os.path.join( filepath = os.path.join(
self.supervisord_partition_configuration_dir, filename) self.supervisord_partition_configuration_dir, filename)
...@@ -810,13 +824,13 @@ class Partition(object): ...@@ -810,13 +824,13 @@ class Partition(object):
raise raise
else: else:
self.updateSupervisor() self.updateSupervisor()
self.logger.info("Requested stop of %s..." % partition_id) 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.partition_id) % 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]
......
...@@ -762,6 +762,36 @@ class SlapConnectionHelper(ConnectionHelper): ...@@ -762,6 +762,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):
...@@ -770,7 +800,7 @@ class JioAPIConnectionHelper(ConnectionHelper): ...@@ -770,7 +800,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