2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 from __future__ import absolute_import, print_function
16 from apiclient import errors as apierror
18 from .baseactor import BaseNodeManagerActor
20 from functools import partial
21 from libcloud.common.types import LibcloudError
22 from libcloud.common.exceptions import BaseHTTPError
24 # IOError is the base class for socket.error, ssl.SSLError, and friends.
25 # It seems like it hits the sweet spot for operations we want to retry:
26 # it's low-level, but unlikely to catch code bugs.
27 NETWORK_ERRORS = (IOError,)
28 ARVADOS_ERRORS = NETWORK_ERRORS + (apierror.Error,)
29 CLOUD_ERRORS = NETWORK_ERRORS + (LibcloudError, BaseHTTPError)
31 actor_class = BaseNodeManagerActor
33 class NodeManagerConfig(ConfigParser.SafeConfigParser):
34 """Node Manager Configuration class.
36 This a standard Python ConfigParser, with additional helper methods to
37 create objects instantiated with configuration information.
40 LOGGING_NONLEVELS = frozenset(['file'])
42 def __init__(self, *args, **kwargs):
43 # Can't use super() because SafeConfigParser is an old-style class.
44 ConfigParser.SafeConfigParser.__init__(self, *args, **kwargs)
45 for sec_name, settings in {
46 'Arvados': {'insecure': 'no',
51 'Daemon': {'min_nodes': '0',
54 'max_poll_time': '300',
55 'poll_stale_after': '600',
56 'max_total_price': '0',
57 'boot_fail_after': str(sys.maxint),
58 'node_stale_after': str(60 * 60 * 2),
60 'node_mem_scaling': '0.95'},
61 'Manage': {'address': '127.0.0.1',
63 'ManagementToken': ''},
64 'Logging': {'file': '/dev/stderr',
67 if not self.has_section(sec_name):
68 self.add_section(sec_name)
69 for opt_name, value in settings.iteritems():
70 if not self.has_option(sec_name, opt_name):
71 self.set(sec_name, opt_name, value)
73 def get_section(self, section, transformers={}, default_transformer=None):
76 bool: self.getboolean,
80 for key, value in self.items(section):
82 if transformers.get(key) in transformer_map:
83 transformer = partial(transformer_map[transformers[key]], section)
84 elif default_transformer in transformer_map:
85 transformer = partial(transformer_map[default_transformer], section)
86 if transformer is not None:
88 value = transformer(key)
89 except (TypeError, ValueError):
95 return {key: getattr(logging, self.get('Logging', key).upper())
96 for key in self.options('Logging')
97 if key not in self.LOGGING_NONLEVELS}
99 def dispatch_classes(self):
100 mod_name = 'arvnodeman.computenode.dispatch'
101 if self.has_option('Daemon', 'dispatcher'):
102 mod_name = '{}.{}'.format(mod_name,
103 self.get('Daemon', 'dispatcher'))
104 module = importlib.import_module(mod_name)
105 return (module.ComputeNodeSetupActor,
106 module.ComputeNodeShutdownActor,
107 module.ComputeNodeUpdateActor,
108 module.ComputeNodeMonitorActor)
110 def new_arvados_client(self):
111 if self.has_option('Daemon', 'certs_file'):
112 certs_file = self.get('Daemon', 'certs_file')
115 insecure = self.getboolean('Arvados', 'insecure')
116 http = httplib2.Http(timeout=self.getint('Arvados', 'timeout'),
118 disable_ssl_certificate_validation=insecure)
119 return arvados.api(version='v1',
120 host=self.get('Arvados', 'host'),
121 token=self.get('Arvados', 'token'),
125 def new_cloud_client(self):
126 module = importlib.import_module('arvnodeman.computenode.driver.' +
127 self.get('Cloud', 'provider'))
128 driver_class = module.ComputeNodeDriver.DEFAULT_DRIVER
129 if self.has_option('Cloud', 'driver_class'):
130 d = self.get('Cloud', 'driver_class').split('.')
131 mod = '.'.join(d[:-1])
133 driver_class = importlib.import_module(mod).__dict__[cls]
134 auth_kwargs = self.get_section('Cloud Credentials')
135 if 'timeout' in auth_kwargs:
136 auth_kwargs['timeout'] = int(auth_kwargs['timeout'])
137 return module.ComputeNodeDriver(auth_kwargs,
138 self.get_section('Cloud List'),
139 self.get_section('Cloud Create'),
140 driver_class=driver_class)
142 def node_sizes(self):
143 """Finds all acceptable NodeSizes for our installation.
145 Returns a list of (NodeSize, kwargs) pairs for each NodeSize object
146 returned by libcloud that matches a size listed in our config file.
148 all_sizes = self.new_cloud_client().list_sizes()
154 for sec_name in self.sections():
155 sec_words = sec_name.split(None, 2)
156 if sec_words[0] != 'Size':
158 size_spec = self.get_section(sec_name, section_types, int)
159 if 'preemptable' not in size_spec:
160 size_spec['preemptable'] = False
161 if 'instance_type' not in size_spec:
162 # Assume instance type is Size name if missing
163 size_spec['instance_type'] = sec_words[1]
164 size_spec['id'] = sec_words[1]
165 size_kwargs[sec_words[1]] = size_spec
166 # EC2 node sizes are identified by id. GCE sizes are identified by name.
168 for size in all_sizes:
170 (size, size_kwargs[s]) for s in size_kwargs
171 if size_kwargs[s]['instance_type'] == size.id
172 or size_kwargs[s]['instance_type'] == size.name
174 return matching_sizes
176 def shutdown_windows(self):
178 for n in self.get('Cloud', 'shutdown_windows').split(',')]