3 from __future__ import absolute_import, print_function
7 import libcloud.compute.base as cloud_base
8 import libcloud.compute.providers as cloud_provider
9 import libcloud.compute.types as cloud_types
10 from libcloud.compute.drivers import ec2 as cloud_ec2
12 from . import BaseComputeNodeDriver
13 from .. import arvados_node_fqdn
15 ### Monkeypatch libcloud to support AWS' new SecurityGroup API.
16 # These classes can be removed when libcloud support specifying
17 # security groups with the SecurityGroupId parameter.
18 class ANMEC2Connection(cloud_ec2.EC2Connection):
19 def request(self, *args, **kwargs):
20 params = kwargs.get('params')
21 if (params is not None) and (params.get('Action') == 'RunInstances'):
22 for key in params.keys():
23 if key.startswith('SecurityGroup.'):
24 new_key = key.replace('Group.', 'GroupId.', 1)
25 params[new_key] = params.pop(key).id
26 kwargs['params'] = params
27 return super(ANMEC2Connection, self).request(*args, **kwargs)
30 class ANMEC2NodeDriver(cloud_ec2.EC2NodeDriver):
31 connectionCls = ANMEC2Connection
34 class ComputeNodeDriver(BaseComputeNodeDriver):
35 """Compute node driver wrapper for EC2.
37 This translates cloud driver requests to EC2's specific parameters.
39 DEFAULT_DRIVER = ANMEC2NodeDriver
43 def __init__(self, auth_kwargs, list_kwargs, create_kwargs,
44 driver_class=DEFAULT_DRIVER):
45 # We need full lists of keys up front because these loops modify
46 # dictionaries in-place.
47 for key in list_kwargs.keys():
48 list_kwargs[key.replace('_', ':')] = list_kwargs.pop(key)
49 self.tags = {key[4:]: value
50 for key, value in list_kwargs.iteritems()
51 if key.startswith('tag:')}
52 super(ComputeNodeDriver, self).__init__(
53 auth_kwargs, {'ex_filters': list_kwargs}, create_kwargs,
55 for key in self.create_kwargs.keys():
56 init_method = getattr(self, '_init_' + key, None)
57 if init_method is not None:
58 new_pair = init_method(self.create_kwargs.pop(key))
59 if new_pair is not None:
60 self.create_kwargs[new_pair[0]] = new_pair[1]
62 def _init_image_id(self, image_id):
63 return 'image', self.search_for(image_id, 'list_images')
65 def _init_ping_host(self, ping_host):
66 self.ping_host = ping_host
68 def _init_security_groups(self, group_names):
69 return 'ex_security_groups', [
70 self.search_for(gname.strip(), 'ex_get_security_groups')
71 for gname in group_names.split(',')]
73 def _init_subnet_id(self, subnet_id):
74 return 'ex_subnet', self.search_for(subnet_id, 'ex_list_subnets')
76 def _init_ssh_key(self, filename):
77 with open(filename) as ssh_file:
78 key = cloud_base.NodeAuthSSHKey(ssh_file.read())
81 def arvados_create_kwargs(self, arvados_node):
82 result = {'name': arvados_node_fqdn(arvados_node)}
83 ping_secret = arvados_node['info'].get('ping_secret')
84 if ping_secret is not None:
85 ping_url = ('https://{}/arvados/v1/nodes/{}/ping?ping_secret={}'.
86 format(self.ping_host, arvados_node['uuid'],
88 result['ex_userdata'] = ping_url
91 def post_create_node(self, cloud_node):
92 self.real.ex_create_tags(cloud_node, self.tags)
94 def sync_node(self, cloud_node, arvados_node):
95 self.real.ex_create_tags(cloud_node,
96 {'Name': arvados_node_fqdn(arvados_node)})
99 def node_start_time(cls, node):
100 time_str = node.extra['launch_time'].split('.', 2)[0] + 'UTC'
101 return time.mktime(time.strptime(
102 time_str,'%Y-%m-%dT%H:%M:%S%Z')) - time.timezone