3 from __future__ import absolute_import, print_function
8 import libcloud.compute.base as cloud_base
9 import libcloud.compute.providers as cloud_provider
10 import libcloud.compute.types as cloud_types
11 from libcloud.common.exceptions import BaseHTTPError
13 from . import BaseComputeNodeDriver
14 from .. import arvados_node_fqdn, arvados_timestamp, ARVADOS_TIMEFMT
16 class ComputeNodeDriver(BaseComputeNodeDriver):
18 DEFAULT_DRIVER = cloud_provider.get_driver(cloud_types.Provider.AZURE_ARM)
20 CLOUD_ERRORS = BaseComputeNodeDriver.CLOUD_ERRORS + (BaseHTTPError,)
22 def __init__(self, auth_kwargs, list_kwargs, create_kwargs,
23 driver_class=DEFAULT_DRIVER):
25 if not list_kwargs.get("ex_resource_group"):
26 raise Exception("Must include ex_resource_group in Cloud List configuration (list_kwargs)")
28 create_kwargs["ex_resource_group"] = list_kwargs["ex_resource_group"]
30 self.tags = {key[4:]: value
31 for key, value in create_kwargs.iteritems()
32 if key.startswith('tag_')}
33 # filter out tags from create_kwargs
34 create_kwargs = {key: value
35 for key, value in create_kwargs.iteritems()
36 if not key.startswith('tag_')}
37 super(ComputeNodeDriver, self).__init__(
38 auth_kwargs, list_kwargs, create_kwargs,
41 def arvados_create_kwargs(self, size, arvados_node):
42 cluster_id, _, node_id = arvados_node['uuid'].split('-')
43 name = 'compute-{}-{}'.format(node_id, cluster_id)
45 'booted_at': time.strftime(ARVADOS_TIMEFMT, time.gmtime()),
46 'arv-ping-url': self._make_ping_url(arvados_node)
48 tags.update(self.tags)
50 customdata = """#!/bin/sh
51 mkdir -p /var/tmp/arv-node-data/meta-data
52 echo %s > /var/tmp/arv-node-data/arv-ping-url
53 echo %s > /var/tmp/arv-node-data/meta-data/instance-id
54 echo %s > /var/tmp/arv-node-data/meta-data/instance-type
55 """ % (pipes.quote(tags['arv-ping-url']),
62 'ex_customdata': customdata
65 def sync_node(self, cloud_node, arvados_node):
67 self.real.ex_create_tags(cloud_node,
68 {'hostname': arvados_node_fqdn(arvados_node)})
70 except BaseHTTPError as b:
73 def _init_image(self, urn):
74 return "image", self.get_image(urn)
77 # Azure only supports filtering node lists by resource group.
78 # Do our own filtering based on tag.
79 nodes = [node for node in
80 super(ComputeNodeDriver, self).list_nodes(ex_fetch_nic=False)
81 if node.extra["tags"].get("arvados-class") == self.tags["arvados-class"]]
83 # Need to populate Node.size
85 n.size = self.sizes[n.extra["properties"]["hardwareProfile"]["vmSize"]]
88 def broken(self, cloud_node):
89 """Return true if libcloud has indicated the node is in a "broken" state."""
90 # UNKNOWN means the node state is unrecognized, which in practice means some combination
91 # of failure that the Azure libcloud driver doesn't know how to interpret.
92 return (cloud_node.state in (cloud_types.NodeState.ERROR, cloud_types.NodeState.UNKNOWN))
95 def node_fqdn(cls, node):
96 return node.extra["tags"].get("hostname")
99 def node_start_time(cls, node):
100 return arvados_timestamp(node.extra["tags"].get("booted_at"))
103 def node_id(cls, node):