Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / config.py @ 8c54338a

History | View | Annotate | Download (5.1 kB)

1
# Copyright 2011-2012 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
import os
35

    
36
from collections import defaultdict
37
from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
38

    
39
try:
40
    from collections import OrderedDict
41
except ImportError:
42
    from kamaki.clients.utils.ordereddict import OrderedDict
43

    
44

    
45
# Path to the file that stores the configuration
46
CONFIG_PATH = os.path.expanduser('~/.kamakirc')
47
HISTORY_PATH = os.path.expanduser('~/.kamaki.history')
48

    
49
# Name of a shell variable to bypass the CONFIG_PATH value
50
CONFIG_ENV = 'KAMAKI_CONFIG'
51

    
52
HEADER = """
53
# Kamaki configuration file v3
54
"""
55

    
56
DEFAULTS = {
57
    'global': {
58
        'colors': 'off',
59
        'token': '',
60
        'log_file': os.path.expanduser('~/.kamaki.log'),
61
        'log_token': 'off',
62
        'log_data': 'off',
63
        'max_threads': 7,
64
        'url': 'https://accounts.okeanos.grnet.gr/astakos/identity/v2.0/'
65
    },
66
    'cli': {
67
        'user': 'astakos',
68
        'file': 'pithos',
69
        'server': 'cyclades',
70
        'flavor': 'cyclades',
71
        'network': 'cyclades',
72
        'image': 'image',
73
        'config': 'config',
74
        'history': 'history'
75
    },
76
    'history': {
77
        'file': HISTORY_PATH
78
    },
79
    'pithos': {
80
        'type': 'object-store',
81
        'version': 'v1'
82
    },
83
    'cyclades': {
84
        'type': 'compute',
85
        'version': 'v2.0'
86
        },
87
    'plankton': {
88
        'type': 'image',
89
        'version': 'v1.0'
90
    },
91
    'astakos': {
92
        'type': 'identity',
93
        'version': 'v2.0'
94
    }
95
}
96

    
97

    
98
class Config(RawConfigParser):
99
    def __init__(self, path=None):
100
        RawConfigParser.__init__(self, dict_type=OrderedDict)
101
        self.path = path or os.environ.get(CONFIG_ENV, CONFIG_PATH)
102
        self._overrides = defaultdict(dict)
103
        self._load_defaults()
104
        self.read(self.path)
105

    
106
    def _load_defaults(self):
107
        for section, options in DEFAULTS.items():
108
            for option, val in options.items():
109
                self.set(section, option, val)
110

    
111
    def _get_dict(self, section, include_defaults=True):
112
        try:
113
            d = dict(DEFAULTS[section]) if include_defaults else {}
114
        except KeyError:
115
            d = {}
116
        try:
117
            d.update(RawConfigParser.items(self, section))
118
        except NoSectionError:
119
            pass
120
        return d
121

    
122
    def reload(self):
123
        self = self.__init__(self.path)
124

    
125
    def get(self, section, option):
126
        value = self._overrides.get(section, {}).get(option)
127
        if value is not None:
128
            return value
129

    
130
        try:
131
            return RawConfigParser.get(self, section, option)
132
        except (NoSectionError, NoOptionError):
133
            return DEFAULTS.get(section, {}).get(option)
134

    
135
    def set(self, section, option, value):
136
        if section not in RawConfigParser.sections(self):
137
            self.add_section(section)
138
        RawConfigParser.set(self, section, option, value)
139

    
140
    def remove_option(self, section, option, also_remove_default=False):
141
        try:
142
            if also_remove_default:
143
                DEFAULTS[section].pop(option)
144
            RawConfigParser.remove_option(self, section, option)
145
        except NoSectionError:
146
            pass
147

    
148
    def keys(self, section, include_defaults=True):
149
        d = self._get_dict(section, include_defaults)
150
        return d.keys()
151

    
152
    def items(self, section, include_defaults=True):
153
        d = self._get_dict(section, include_defaults)
154
        return d.items()
155

    
156
    def override(self, section, option, value):
157
        self._overrides[section][option] = value
158

    
159
    def write(self):
160
        with open(self.path, 'w') as f:
161
            os.chmod(self.path, 0600)
162
            f.write(HEADER.lstrip())
163
            f.flush()
164
            RawConfigParser.write(self, f)