9120: search_for_now falls back to real driver methods when needed.
[arvados.git] / services / nodemanager / arvnodeman / computenode / driver / gce.py
index 280de98c96c898e1b4aa637c1c0f698cbd83e4c1..b853f00a6728693cce4b855021e18bb35c869087 100644 (file)
@@ -34,19 +34,18 @@ class ComputeNodeDriver(BaseComputeNodeDriver):
         super(ComputeNodeDriver, self).__init__(
             auth_kwargs, list_kwargs, create_kwargs,
             driver_class)
+        self._sizes_by_name = {sz.name: sz for sz in self.sizes.itervalues()}
+        self._disktype_links = {dt.name: self._object_link(dt)
+                                for dt in self.real.ex_list_disktypes()}
 
     @staticmethod
-    def _name_key(cloud_object):
-        return cloud_object.name
+    def _object_link(cloud_object):
+        return cloud_object.extra.get('selfLink')
 
     def _init_image(self, image_name):
         return 'image', self.search_for(
             image_name, 'list_images', self._name_key)
 
-    def _init_location(self, location_name):
-        return 'location', self.search_for(
-            location_name, 'list_locations', self._name_key)
-
     def _init_network(self, network_name):
         return 'ex_network', self.search_for(
             network_name, 'ex_list_networks', self._name_key)
@@ -61,28 +60,60 @@ class ComputeNodeDriver(BaseComputeNodeDriver):
             self.create_kwargs['ex_metadata']['sshKeys'] = (
                 'root:' + ssh_file.read().strip())
 
-    def list_sizes(self):
-        return super(ComputeNodeDriver, self).list_sizes(
-            self.create_kwargs['location'])
-
-    def arvados_create_kwargs(self, arvados_node):
-        cluster_id, _, node_id = arvados_node['uuid'].split('-')
-        result = {'name': 'compute-{}-{}'.format(node_id, cluster_id),
+    def create_cloud_name(self, arvados_node):
+        uuid_parts = arvados_node['uuid'].split('-', 2)
+        return 'compute-{parts[2]}-{parts[0]}'.format(parts=uuid_parts)
+
+    def arvados_create_kwargs(self, size, arvados_node):
+        name = self.create_cloud_name(arvados_node)
+        disks = [
+            {'autoDelete': True,
+             'boot': True,
+             'deviceName': name,
+             'initializeParams':
+                 {'diskName': name,
+                  'diskType': self._disktype_links['pd-standard'],
+                  'sourceImage': self._object_link(self.create_kwargs['image']),
+                  },
+             'type': 'PERSISTENT',
+             },
+            {'autoDelete': True,
+             'boot': False,
+             # Boot images rely on this device name to find the SSD.
+             # Any change must be coordinated in the image.
+             'deviceName': 'tmp',
+             'initializeParams':
+                 {'diskType': self._disktype_links['local-ssd'],
+                  },
+             'type': 'SCRATCH',
+             },
+            ]
+        result = {'name': name,
                   'ex_metadata': self.create_kwargs['ex_metadata'].copy(),
-                  'ex_tags': list(self.node_tags)}
-        result['ex_metadata']['arv-ping-url'] = self._make_ping_url(
-            arvados_node)
-        result['ex_metadata']['booted_at'] = time.strftime(ARVADOS_TIMEFMT,
-                                                           time.gmtime())
-        result['ex_metadata']['hostname'] = arvados_node_fqdn(arvados_node)
+                  'ex_tags': list(self.node_tags),
+                  'ex_disks_gce_struct': disks,
+                  }
+        result['ex_metadata'].update({
+                'arv-ping-url': self._make_ping_url(arvados_node),
+                'booted_at': time.strftime(ARVADOS_TIMEFMT, time.gmtime()),
+                'hostname': arvados_node_fqdn(arvados_node),
+                })
         return result
 
+
     def list_nodes(self):
         # The GCE libcloud driver only supports filtering node lists by zone.
         # Do our own filtering based on tag list.
-        return [node for node in
-                super(ComputeNodeDriver, self).list_nodes()
-                if self.node_tags.issubset(node.extra.get('tags', []))]
+        nodelist = [node for node in
+                    super(ComputeNodeDriver, self).list_nodes()
+                    if self.node_tags.issubset(node.extra.get('tags', []))]
+        # As of 0.18, the libcloud GCE driver sets node.size to the size's name.
+        # It's supposed to be the actual size object.  Check that it's not,
+        # and monkeypatch the results when that's the case.
+        if nodelist and not hasattr(nodelist[0].size, 'id'):
+            for node in nodelist:
+                node.size = self._sizes_by_name[node.size]
+        return nodelist
 
     @classmethod
     def _find_metadata(cls, metadata_items, key):
@@ -104,6 +135,10 @@ class ComputeNodeDriver(BaseComputeNodeDriver):
             raise
 
     def sync_node(self, cloud_node, arvados_node):
+        # We can't store the FQDN on the name attribute or anything like it,
+        # because (a) names are static throughout the node's life (so FQDN
+        # isn't available because we don't know it at node creation time) and
+        # (b) it can't contain dots.  Instead stash it in metadata.
         hostname = arvados_node_fqdn(arvados_node)
         metadata_req = cloud_node.extra['metadata'].copy()
         metadata_items = metadata_req.setdefault('items', [])
@@ -118,6 +153,12 @@ class ComputeNodeDriver(BaseComputeNodeDriver):
         if not response.success():
             raise Exception("setMetadata error: {}".format(response.error))
 
+    @classmethod
+    def node_fqdn(cls, node):
+        # See sync_node comment.
+        return cls._get_metadata(node.extra['metadata'].get('items', []),
+                                 'hostname', '')
+
     @classmethod
     def node_start_time(cls, node):
         try:
@@ -125,3 +166,7 @@ class ComputeNodeDriver(BaseComputeNodeDriver):
                     node.extra['metadata']['items'], 'booted_at'))
         except KeyError:
             return 0
+
+    @classmethod
+    def node_id(cls, node):
+        return node.id