Merge branch '4293-node-manager-timed-bootstrap-wip'
[arvados.git] / services / nodemanager / arvnodeman / config.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import ConfigParser
6 import importlib
7 import logging
8 import ssl
9 import sys
10
11 import arvados
12 import httplib2
13 import libcloud.common.types as cloud_types
14 import pykka
15 from apiclient import errors as apierror
16
17 # IOError is the base class for socket.error and friends.
18 # It seems like it hits the sweet spot for operations we want to retry:
19 # it's low-level, but unlikely to catch code bugs.
20 NETWORK_ERRORS = (IOError, ssl.SSLError)
21 ARVADOS_ERRORS = NETWORK_ERRORS + (apierror.Error,)
22 CLOUD_ERRORS = NETWORK_ERRORS + (cloud_types.LibcloudError,)
23
24 actor_class = pykka.ThreadingActor
25
26 class NodeManagerConfig(ConfigParser.SafeConfigParser):
27     """Node Manager Configuration class.
28
29     This a standard Python ConfigParser, with additional helper methods to
30     create objects instantiated with configuration information.
31     """
32
33     LOGGING_NONLEVELS = frozenset(['file'])
34
35     def __init__(self, *args, **kwargs):
36         # Can't use super() because SafeConfigParser is an old-style class.
37         ConfigParser.SafeConfigParser.__init__(self, *args, **kwargs)
38         for sec_name, settings in {
39             'Arvados': {'insecure': 'no',
40                         'timeout': '15'},
41             'Daemon': {'min_nodes': '0',
42                        'max_nodes': '1',
43                        'poll_time': '60',
44                        'max_poll_time': '300',
45                        'poll_stale_after': '600',
46                        'boot_fail_after': str(sys.maxint),
47                        'node_stale_after': str(60 * 60 * 2)},
48             'Logging': {'file': '/dev/stderr',
49                         'level': 'WARNING'},
50         }.iteritems():
51             if not self.has_section(sec_name):
52                 self.add_section(sec_name)
53             for opt_name, value in settings.iteritems():
54                 if not self.has_option(sec_name, opt_name):
55                     self.set(sec_name, opt_name, value)
56
57     def get_section(self, section, transformer=None):
58         result = self._dict()
59         for key, value in self.items(section):
60             if transformer is not None:
61                 try:
62                     value = transformer(value)
63                 except (TypeError, ValueError):
64                     pass
65             result[key] = value
66         return result
67
68     def log_levels(self):
69         return {key: getattr(logging, self.get('Logging', key).upper())
70                 for key in self.options('Logging')
71                 if key not in self.LOGGING_NONLEVELS}
72
73     def dispatch_classes(self):
74         mod_name = 'arvnodeman.computenode.dispatch'
75         if self.has_option('Daemon', 'dispatcher'):
76             mod_name = '{}.{}'.format(mod_name,
77                                       self.get('Daemon', 'dispatcher'))
78         module = importlib.import_module(mod_name)
79         return (module.ComputeNodeSetupActor,
80                 module.ComputeNodeShutdownActor,
81                 module.ComputeNodeUpdateActor,
82                 module.ComputeNodeMonitorActor)
83
84     def new_arvados_client(self):
85         if self.has_option('Daemon', 'certs_file'):
86             certs_file = self.get('Daemon', 'certs_file')
87         else:
88             certs_file = None
89         insecure = self.getboolean('Arvados', 'insecure')
90         http = httplib2.Http(timeout=self.getint('Arvados', 'timeout'),
91                              ca_certs=certs_file,
92                              disable_ssl_certificate_validation=insecure)
93         return arvados.api('v1',
94                            cache=False,  # Don't reuse an existing client.
95                            host=self.get('Arvados', 'host'),
96                            token=self.get('Arvados', 'token'),
97                            insecure=insecure,
98                            http=http)
99
100     def new_cloud_client(self):
101         module = importlib.import_module('arvnodeman.computenode.driver.' +
102                                          self.get('Cloud', 'provider'))
103         auth_kwargs = self.get_section('Cloud Credentials')
104         if 'timeout' in auth_kwargs:
105             auth_kwargs['timeout'] = int(auth_kwargs['timeout'])
106         return module.ComputeNodeDriver(auth_kwargs,
107                                         self.get_section('Cloud List'),
108                                         self.get_section('Cloud Create'))
109
110     def node_sizes(self, all_sizes):
111         size_kwargs = {}
112         for sec_name in self.sections():
113             sec_words = sec_name.split(None, 2)
114             if sec_words[0] != 'Size':
115                 continue
116             size_kwargs[sec_words[1]] = self.get_section(sec_name, int)
117         return [(size, size_kwargs[size.id]) for size in all_sizes
118                 if size.id in size_kwargs]
119
120     def shutdown_windows(self):
121         return [int(n)
122                 for n in self.get('Cloud', 'shutdown_windows').split(',')]