irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

configuration.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import contextlib
3 import filecmp
4 import itertools
5 import json
6 import logging
7 import os
8 import pprint
9 import shutil
10 import sys
11 import tempfile
12 import time
13 
14 from . import six
15 
16 from .exceptions import IrodsError, IrodsWarning
17 from . import lib
18 from . import json_validation
19 from .password_obfuscation import encode, decode
20 from . import paths
21 
22 class IrodsConfig(object):
23  def __init__(self,
24  injected_environment={},
25  insert_behavior=True):
26  self._injected_environment = lib.callback_on_change_dict(self.clear_cache, injected_environment)
27  self._insert_behavior = insert_behavior
28  self.clear_cache()
29 
30  @property
31  def version_tuple(self):
32  if os.path.exists(paths.version_path()):
33  return lib.version_string_to_tuple(self.version['irods_version'])
34 
35  legacy_version_file_path = os.path.join(paths.irods_directory(), 'VERSION')
36  if os.path.exists(legacy_version_file_path):
37  with open(legacy_version_file_path) as f:
38  for line in f:
39  key, _, value = line.strip().partition('=')
40  if key == 'IRODSVERSION':
41  return lib.version_string_to_tuple(value)
42 
43  raise IrodsError('Unable to determine iRODS version')
44 
45  @property
46  def server_config(self):
47  if self._server_config is None:
48  self._server_config = load_json_config(paths.server_config_path(),
49  template_filepath=paths.get_template_filepath(paths.server_config_path()))
50  return self._server_config
51 
52  @property
53  def is_catalog(self):
54  return self.server_config['catalog_service_role'] == 'provider'
55 
56  @property
57  def is_resource(self):
58  return self.server_config['catalog_service_role'] == 'consumer'
59 
60  @property
62  return self.server_config['plugin_configuration']['rule_engines'][0]['instance_name']
63 
64  @property
66  return self.server_config['plugin_configuration']['rule_engines'][0]['plugin_name']
67 
68  @property
70  ret_list = []
71  for re in self.server_config['plugin_configuration']['rule_engines']:
72  ret_list.append(re['plugin_name'])
73  return ret_list
74 
75  @property
76  def database_config(self):
77  try:
78  database_config = [e for e in self.server_config['plugin_configuration']['database'].values()][0]
79  except (KeyError, IndexError):
80  return None
81  if not 'db_odbc_driver' in database_config.keys():
82  l = logging.getLogger(__name__)
83  l.debug('No driver found in the database config, attempting to retrieve the one in the odbc ini file at "%s"...', self.odbc_ini_path)
84  if os.path.exists(self.odbc_ini_path):
85  from . import database_connect
86  with open(self.odbc_ini_path) as f:
87  odbc_ini_contents = database_connect.load_odbc_ini(f)
88  else:
89  l.debug('No odbc.ini file present')
90  odbc_ini_contents = {}
91  if self.catalog_database_type in odbc_ini_contents.keys() and 'Driver' in odbc_ini_contents[self.catalog_database_type].keys():
92  database_config['db_odbc_driver'] = odbc_ini_contents[self.catalog_database_type]['Driver']
93  l.debug('Adding driver "%s" to database_config', database_config['db_odbc_driver'])
94  self.commit(self._server_config, paths.server_config_path(), clear_cache=False)
95  else:
96  l.debug('Unable to retrieve "Driver" field from odbc ini file')
97 
98  return database_config
99 
100  @property
102  try:
103  return [e for e in self.server_config['plugin_configuration']['database'].keys()][0]
104  except (KeyError, IndexError):
105  return None
106 
107  @property
108  def odbc_ini_path(self):
109  if 'ODBCINI' in self.execution_environment:
110  return self.execution_environment['ODBCINI']
111  return os.path.join(paths.home_directory(), '.odbc.ini')
112 
113  @property
114  def version(self):
115  if self._version is None:
116  self._version = load_json_config(paths.version_path())
117  return self._version
118 
119  @property
120  def hosts_config(self):
121  if self._hosts_config is None:
122  self._hosts_config = load_json_config(paths.hosts_config_path(),
123  template_filepath=paths.get_template_filepath(paths.hosts_config_path()))
124  return self._hosts_config
125 
126  @property
128  if self._host_access_control_config is None:
129  self._host_access_control_config = load_json_config(paths.host_access_control_config_path(),
130  template_filepath=paths.get_template_filepath(paths.host_access_control_config_path()))
131  return self._host_access_control_config
132 
133  @property
135  if 'IRODS_ENVIRONMENT_FILE' in self.execution_environment:
136  return self.execution_environment['IRODS_ENVIRONMENT_FILE']
137  else:
138  return paths.default_client_environment_path()
139 
140  @property
142  if self._client_environment is None:
144  return self._client_environment
145 
146  @property
148  return self.server_config.get('environment_variables', {})
149 
151  pprint.pprint(self.execution_environment)
152 
153  @property
155  if self._execution_environment is None:
156  if self.insert_behavior:
158  self._execution_environment.update(os.environ)
159  self._execution_environment['irodsConfigDir'] = paths.config_directory()
160  self._execution_environment['PWD'] = paths.server_bin_directory()
162  else:
164  return self._execution_environment
165 
166  @property
167  def insert_behavior(self):
168  return self._insert_behavior
169 
170  @insert_behavior.setter
171  def insert_behavior(self, value):
172  self._insert_behavior = value
173  self.clear_cache()
174 
175  @property
177  return self._injected_environment
178 
179  @injected_environment.setter
180  def injected_environment(self, value):
181  self._injected_environment = lib.callback_on_change_dict(self.clear_cache, value if value is not None else {})
182  self.clear_cache()
183 
184  @property
185  def schema_uri_prefix(self):
186  if self._schema_uri_prefix is None:
187  l = logging.getLogger(__name__)
188  l.debug('Attempting to construct schema URI...')
189 
190  key = 'schema_validation_base_uri'
191  try:
192  base_uri = self.server_config[key]
193  except KeyError:
194  base_uri = None
195  raise IrodsWarning(
196  '%s did not contain \'%s\'' %
197  (paths.server_config_path(), key))
198 
199  key = 'configuration_schema_version'
200  try:
201  uri_version = self.version[key]
202  except KeyError:
203  uri_version = None
204  raise IrodsWarning(
205  '%s did not contain \'%s\'' %
206  (paths.version_path(), key))
207 
209  base_uri,
210  'v%s' % (uri_version)])
211  l.debug('Successfully constructed schema URI.')
212  return self._schema_uri_prefix
213 
214  @property
215  def admin_password(self):
216  if not os.path.exists(os.path.dirname(paths.password_file_path())):
217  return None
218  with open(paths.password_file_path(), 'rt') as f:
219  return decode(f.read())
220 
221  @admin_password.setter
222  def admin_password(self, value):
223  l = logging.getLogger(__name__)
224  if not os.path.exists(os.path.dirname(paths.password_file_path())):
225  os.makedirs(os.path.dirname(paths.password_file_path()), mode=0o700)
226  mtime = int(time.time())
227  with open(paths.password_file_path(), 'wt') as f:
228  l.debug('Writing password file %s', f.name)
229  print(encode(value, mtime=mtime), end='', file=f)
230  os.utime(paths.password_file_path(), (mtime, mtime))
231 
233  l = logging.getLogger(__name__)
234 
235  configuration_schema_mapping = {
236  'server_config': {
237  'dict': self.server_config,
238  'path': paths.server_config_path()},
239  'VERSION': {
240  'dict': self.version,
241  'path': paths.version_path()},
242  'hosts_config': {
243  'dict': self.hosts_config,
244  'path': paths.hosts_config_path()},
245  'host_access_control_config': {
246  'dict': self.host_access_control_config,
247  'path': paths.host_access_control_config_path()},
248  'service_account_environment': {
249  'dict': self.client_environment,
250  'path': self.client_environment_path}}
251 
252  skipped = []
253 
254  if self.server_config['schema_validation_base_uri'] == 'off':
255  l.warn('Schema validation is disabled; json files will not be validated against schemas. To re-enable schema validation, supply a URL to a set of iRODS schemas in the field "schema_validation_base_uri" and a valid version in the field "schema_version" in the server configuration file (located in %s).', paths.server_config_path())
256  return
257  for schema_uri_suffix, config_file in configuration_schema_mapping.items():
258  try:
259  schema_uri = '%s/%s.json' % (
260  self.schema_uri_prefix,
261  schema_uri_suffix)
262  except IrodsError as e:
263  l.debug('Failed to construct schema URI')
264  six.reraise(IrodsWarning, IrodsWarning('%s\n%s' % (
265  'Preflight Check problem:',
266  lib.indent('JSON Configuration Validation failed.'))),
267  sys.exc_info()[2])
268 
269  l.debug('Attempting to validate %s against %s', config_file['path'], schema_uri)
270  try:
271  json_validation.validate_dict(
272  config_file['dict'],
273  schema_uri,
274  name=config_file['path'])
275  except IrodsWarning as e:
276  l.warning(e)
277  l.warning('Warning encountered in json_validation for %s, skipping validation...',
278  config_file['path'])
279  l.debug('Exception:', exc_info=True)
280  skipped.append(config_file['path'])
281  if skipped:
282  raise IrodsWarning('%s\n%s' % (
283  'Skipped validation for the following files:',
284  lib.indent(*skipped)))
285 
286  def commit(self, config_dict, path, clear_cache=True, make_backup=False):
287  l = logging.getLogger(__name__)
288  l.info('Updating %s...', path)
289  with tempfile.NamedTemporaryFile(mode='wt', delete=False) as f:
290  json.dump(config_dict, f, indent=4, sort_keys=True)
291  if os.path.exists(path):
292  if filecmp.cmp(f.name, path):
293  return
294  if make_backup:
295  shutil.copyfile(path, '.'.join([path, 'prev', str(time.time())]))
296  shutil.move(f.name, path)
297  if clear_cache:
298  self.clear_cache()
299 
300  def clear_cache(self):
301  self._server_config = None
302  self._version = None
303  self._hosts_config = None
304  self._host_access_control_config = None
305  self._client_environment = None
306  self._schema_uri_prefix = None
307  self._execution_environment = None
308 
309  #provide accessors for all the paths
310  @property
311  def root_directory(self):
312  return paths.root_directory()
313 
314  @property
315  def irods_directory(self):
316  return paths.irods_directory()
317 
318  @property
319  def config_directory(self):
320  return paths.config_directory()
321 
322  @property
323  def home_directory(self):
324  return paths.home_directory()
325 
326  @property
327  def core_re_directory(self):
328  return paths.core_re_directory()
329 
330  @property
331  def scripts_directory(self):
332  return paths.scripts_directory()
333 
334  @property
336  return paths.server_config_path()
337 
338  @property
340  return paths.database_config_path()
341 
342  @property
343  def version_path(self):
344  return paths.version_path()
345 
346  @property
347  def hosts_config_path(self):
348  return paths.hosts_config_path()
349 
350  @property
352  return paths.host_access_control_config_path()
353 
354  @property
356  return paths.password_file_path()
357 
358  @property
359  def log_directory(self):
360  return paths.log_directory()
361 
362  @property
363  def control_log_path(self):
364  return paths.control_log_path()
365 
366  @property
367  def setup_log_path(self):
368  return paths.setup_log_path()
369 
370  @property
371  def test_log_path(self):
372  return paths.test_log_path()
373 
374  @property
376  return paths.icommands_test_directory()
377 
378  @property
380  return paths.server_test_directory()
381 
382  @property
384  return paths.server_parent_log_path()
385 
386  @property
387  def server_log_path(self):
388  return paths.server_log_path()
389 
390  @property
391  def re_log_path(self):
392  return paths.re_log_path()
393 
394  @property
396  return paths.server_bin_directory()
397 
398  @property
399  def server_executable(self):
400  return paths.server_executable()
401 
402  @property
404  return paths.rule_engine_executable()
405 
406  @property
408  return paths.xmsg_server_executable()
409 
410  @property
412  return paths.database_schema_update_directory()
413 
414  @property
416  return paths.service_account_file_path()
417 
418  @property
419  def irods_user(self):
420  return paths.irods_user()
421 
422  @property
423  def irods_uid(self):
424  return paths.irods_uid()
425 
426  @property
427  def irods_group(self):
428  return paths.irods_group()
429 
430  @property
431  def irods_gid(self):
432  return paths.irods_gid()
433 
434 def load_json_config(path, template_filepath=None):
435  l = logging.getLogger(__name__)
436  if not os.path.exists(path) and template_filepath is not None:
437  l.debug('%s does not exist, copying from template file %s', path, template_filepath)
438  shutil.copyfile(template_filepath, path)
439  l.debug('Loading %s into dictionary', path)
440  try :
441  return lib.open_and_load_json(path)
442  except ValueError as e:
443  six.reraise(IrodsError,
444  IrodsError('%s\n%s' % (
445  'JSON load failed for [%s]:' % (path),
446  lib.indent('Invalid JSON.',
447  '%s: %s' % (e.__class__.__name__, e)))),
448  sys.exc_info()[2])
irods.configuration.IrodsConfig.server_config
def server_config(self)
Definition: configuration.py:46
irods.configuration.IrodsConfig
Definition: configuration.py:22
irods.configuration.load_json_config
def load_json_config(path, template_filepath=None)
Definition: configuration.py:434
irods.configuration.IrodsConfig.__init__
def __init__(self, injected_environment={}, insert_behavior=True)
Definition: configuration.py:23
irods.configuration.IrodsConfig.rule_engine_executable
def rule_engine_executable(self)
Definition: configuration.py:403
irods.configuration.IrodsConfig.irods_uid
def irods_uid(self)
Definition: configuration.py:423
irods.configuration.IrodsConfig.test_log_path
def test_log_path(self)
Definition: configuration.py:371
irods.configuration.IrodsConfig.admin_password
def admin_password(self)
Definition: configuration.py:215
irods.configuration.IrodsConfig.server_parent_log_path
def server_parent_log_path(self)
Definition: configuration.py:383
irods.configuration.IrodsConfig.setup_log_path
def setup_log_path(self)
Definition: configuration.py:367
irods.configuration.IrodsConfig.client_environment_path
def client_environment_path(self)
Definition: configuration.py:134
irods.configuration.IrodsConfig.injected_environment
def injected_environment(self)
Definition: configuration.py:176
irods.configuration.IrodsConfig.irods_gid
def irods_gid(self)
Definition: configuration.py:431
irods.configuration.IrodsConfig._execution_environment
_execution_environment
Definition: configuration.py:157
irods.configuration.IrodsConfig.execution_environment
def execution_environment(self)
Definition: configuration.py:154
irods.configuration.IrodsConfig.control_log_path
def control_log_path(self)
Definition: configuration.py:363
irods.configuration.IrodsConfig.server_bin_directory
def server_bin_directory(self)
Definition: configuration.py:395
irods.configuration.IrodsConfig._version
_version
Definition: configuration.py:116
irods.configuration.IrodsConfig.catalog_database_type
def catalog_database_type(self)
Definition: configuration.py:101
irods.configuration.IrodsConfig.service_account_file_path
def service_account_file_path(self)
Definition: configuration.py:415
irods.configuration.IrodsConfig.schema_uri_prefix
def schema_uri_prefix(self)
Definition: configuration.py:185
irods.configuration.IrodsConfig.host_access_control_config
def host_access_control_config(self)
Definition: configuration.py:127
irods.configuration.IrodsConfig.odbc_ini_path
def odbc_ini_path(self)
Definition: configuration.py:108
irods.configuration.IrodsConfig.hosts_config_path
def hosts_config_path(self)
Definition: configuration.py:347
irods.configuration.IrodsConfig.insert_behavior
def insert_behavior(self)
Definition: configuration.py:167
irods.configuration.IrodsConfig.server_log_path
def server_log_path(self)
Definition: configuration.py:387
irods.configuration.IrodsConfig.database_config_path
def database_config_path(self)
Definition: configuration.py:339
irods.configuration.IrodsConfig._injected_environment
_injected_environment
Definition: configuration.py:24
irods.lib.callback_on_change_dict
Definition: lib.py:614
irods.configuration.IrodsConfig.database_config
def database_config(self)
Definition: configuration.py:76
irods.configuration.IrodsConfig.default_rule_engine_plugin
def default_rule_engine_plugin(self)
Definition: configuration.py:65
irods.configuration.IrodsConfig.version
def version(self)
Definition: configuration.py:114
irods.configuration.IrodsConfig.client_environment
def client_environment(self)
Definition: configuration.py:141
irods.configuration.IrodsConfig.is_resource
def is_resource(self)
Definition: configuration.py:57
irods.configuration.IrodsConfig.log_directory
def log_directory(self)
Definition: configuration.py:359
irods.configuration.IrodsConfig.irods_user
def irods_user(self)
Definition: configuration.py:419
irods.configuration.IrodsConfig._insert_behavior
_insert_behavior
Definition: configuration.py:25
irods.configuration.IrodsConfig.database_schema_update_directory
def database_schema_update_directory(self)
Definition: configuration.py:411
irods.configuration.IrodsConfig.config_directory
def config_directory(self)
Definition: configuration.py:319
irods.configuration.IrodsConfig.version_tuple
def version_tuple(self)
Definition: configuration.py:31
irods.configuration.IrodsConfig.root_directory
def root_directory(self)
Definition: configuration.py:311
int
typedef int((*funcPtr)())
irods.password_obfuscation.decode
def decode(s, uid=None)
Definition: password_obfuscation.py:43
irods.configuration.IrodsConfig.server_environment
def server_environment(self)
Definition: configuration.py:147
irods.configuration.IrodsConfig.commit
def commit(self, config_dict, path, clear_cache=True, make_backup=False)
Definition: configuration.py:286
irods.configuration.IrodsConfig.version_path
def version_path(self)
Definition: configuration.py:343
irods.configuration.IrodsConfig._server_config
_server_config
Definition: configuration.py:48
irods.configuration.IrodsConfig.scripts_directory
def scripts_directory(self)
Definition: configuration.py:331
irods.configuration.IrodsConfig.irods_group
def irods_group(self)
Definition: configuration.py:427
irods::join
std::string join(std::vector< std::string > &strs, const std::string &separator)
Definition: irods_serialization.cpp:169
irods.configuration.IrodsConfig.server_config_path
def server_config_path(self)
Definition: configuration.py:335
irods.configuration.IrodsConfig.password_file_path
def password_file_path(self)
Definition: configuration.py:355
irods.configuration.IrodsConfig.default_rule_engine_instance
def default_rule_engine_instance(self)
Definition: configuration.py:61
irods.configuration.IrodsConfig.is_catalog
def is_catalog(self)
Definition: configuration.py:53
irods.configuration.IrodsConfig.server_test_directory
def server_test_directory(self)
Definition: configuration.py:379
irods.configuration.IrodsConfig.print_execution_environment
def print_execution_environment(self)
Definition: configuration.py:150
irods.exceptions.IrodsWarning
Definition: exceptions.py:4
irods.configuration.IrodsConfig._host_access_control_config
_host_access_control_config
Definition: configuration.py:129
irods.configuration.IrodsConfig.hosts_config
def hosts_config(self)
Definition: configuration.py:120
irods.configuration.IrodsConfig.host_access_control_config_path
def host_access_control_config_path(self)
Definition: configuration.py:351
irods.configuration.IrodsConfig._client_environment
_client_environment
Definition: configuration.py:143
irods.configuration.IrodsConfig._schema_uri_prefix
_schema_uri_prefix
Definition: configuration.py:208
irods.configuration.IrodsConfig.core_re_directory
def core_re_directory(self)
Definition: configuration.py:327
irods.configuration.IrodsConfig.icommands_test_directory
def icommands_test_directory(self)
Definition: configuration.py:375
irods.configuration.IrodsConfig.configured_rule_engine_plugins
def configured_rule_engine_plugins(self)
Definition: configuration.py:69
irods.exceptions.IrodsError
Definition: exceptions.py:1
irods.configuration.IrodsConfig.re_log_path
def re_log_path(self)
Definition: configuration.py:391
irods.configuration.IrodsConfig.xmsg_server_executable
def xmsg_server_executable(self)
Definition: configuration.py:407
irods.configuration.IrodsConfig.home_directory
def home_directory(self)
Definition: configuration.py:323
irods.password_obfuscation.encode
def encode(s, uid=None, mtime=None)
Definition: password_obfuscation.py:88
irods.configuration.IrodsConfig.irods_directory
def irods_directory(self)
Definition: configuration.py:315
irods.configuration.IrodsConfig.validate_configuration
def validate_configuration(self)
Definition: configuration.py:232
irods.configuration.IrodsConfig._hosts_config
_hosts_config
Definition: configuration.py:122
irods.configuration.IrodsConfig.server_executable
def server_executable(self)
Definition: configuration.py:399
irods.configuration.IrodsConfig.clear_cache
def clear_cache(self)
Definition: configuration.py:300