Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / settings / setup / services.py @ 7877e6d3

History | View | Annotate | Download (3.9 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from synnefo.util.entry_points import extend_dict_from_entry_point
35
from synnefo.util.keypath import customize_from_items
36
from synnefo.lib.services import fill_endpoints
37
from synnefo.lib import parse_base_url
38

    
39

    
40
def setup_services(settings):
41
    services = {}
42
    extend_dict_from_entry_point(services, 'synnefo', 'services')
43

    
44
    customization = getattr(settings, 'CUSTOMIZE_SERVICES', ())
45
    if isinstance(customization, dict):
46
        customization = customization.items()
47
    customize_from_items(services, customization)
48
    setattr(settings, 'SYNNEFO_SERVICES', services)
49

    
50
    components = {}
51
    for service_name, service in services.items():
52
        component_name = service['component']
53
        if component_name not in components:
54
            components[component_name] = {}
55
        components[component_name][service_name] = service
56
    setattr(settings, 'SYNNEFO_COMPONENTS', components)
57

    
58

    
59
def setup_base_urls(settings):
60
    import os
61
    for component_name in settings.SYNNEFO_COMPONENTS.keys():
62
        name_upper = component_name.upper()
63
        base_url_name = name_upper + '_BASE_URL'
64
        base_host_name = name_upper + '_BASE_HOST'
65
        base_path_name = name_upper + '_BASE_PATH'
66
        base_url = getattr(settings, base_url_name, None)
67
        if base_url is None:
68
            # try environment variable
69
            base_url = os.environ.get(base_url_name)
70
            if base_url is None:
71
                # give up
72
                m = ("No '{setting_name}' setting found even though "
73
                     "component '{component_name}' is installed!\n")
74
                m = m.format(setting_name=base_url_name,
75
                             component_name=component_name)
76
                raise AssertionError(m)
77
            else:
78
                setattr(settings, base_url_name, base_url)
79

    
80
        base_host, base_path = parse_base_url(base_url)
81
        setattr(settings, base_host_name, base_host)
82
        setattr(settings, base_path_name, base_path)
83

    
84

    
85
def setup_endpoints(settings):
86
    for service_name, service in settings.SYNNEFO_SERVICES.iteritems():
87
        component_name = service['component']
88
        base_url_name = component_name.upper() + '_BASE_URL'
89
        base_url = getattr(settings, base_url_name)
90
        fill_endpoints(service, base_url)
91

    
92

    
93
def setup_settings(settings):
94
    setup_services(settings)
95
    setup_base_urls(settings)
96
    setup_endpoints(settings)