Fixed: OpenStack API now supports very long instance names
authorpeusterm <manuel.peuster@uni-paderborn.de>
Fri, 23 Jun 2017 13:00:21 +0000 (15:00 +0200)
committerpeusterm <manuel.peuster@uni-paderborn.de>
Fri, 23 Jun 2017 13:00:21 +0000 (15:00 +0200)
src/emuvim/api/openstack/compute.py

index fd9b1ac..09957b7 100755 (executable)
@@ -480,9 +480,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):
@@ -499,12 +506,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.