From 3f2f5653a81e404c68df5cc6807ba7e82a061361 Mon Sep 17 00:00:00 2001 From: peusterm Date: Fri, 23 Jun 2017 15:00:21 +0200 Subject: [PATCH] Fixed: OpenStack API now supports very long instance names --- src/emuvim/api/openstack/compute.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/emuvim/api/openstack/compute.py b/src/emuvim/api/openstack/compute.py index 6d4d864..38796cf 100755 --- a/src/emuvim/api/openstack/compute.py +++ b/src/emuvim/api/openstack/compute.py @@ -486,9 +486,16 @@ class OpenstackCompute(object): if name_or_id in self.computeUnits: return self.computeUnits[name_or_id] + if self._shorten_server_name(name_or_id) in self.computeUnits: + return self.computeUnits[name_or_id] + for server in self.computeUnits.values(): if server.name == name_or_id or server.template_name == name_or_id or server.full_name == name_or_id: return server + if (server.name == self._shorten_server_name(name_or_id) + or server.template_name == self._shorten_server_name(name_or_id) + or server.full_name == self._shorten_server_name(name_or_id)): + return server return None def create_server(self, name, stack_operation=False): @@ -505,12 +512,25 @@ class OpenstackCompute(object): """ if self.find_server_by_name_or_id(name) is not None and not stack_operation: raise Exception("Server with name %s already exists." % name) - server = Server(name) + safe_name = self._shorten_server_name(name) + server = Server(safe_name) server.id = str(uuid.uuid4()) if not stack_operation: self.computeUnits[server.id] = server return server + def _shorten_server_name(self, name, char_limit=64): + """ + Docker does not like too long instance names. + This function provides a shorter name if needed + """ + LOG.debug("Long server name: {}".format(name)) + if len(name) > char_limit: + # construct a short name + name = name[-char_limit:].strip("-_ .") + LOG.debug("Short server name: {}".format(name)) + return name + def delete_server(self, server): """ Deletes the given server from the stack dictionary and the computeUnits dictionary. -- 2.25.1