Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.1 kB)

1 7493ccb6 Stavros Sachtouris
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 7493ccb6 Stavros Sachtouris
#
3 7493ccb6 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 7493ccb6 Stavros Sachtouris
# without modification, are permitted provided that the following
5 7493ccb6 Stavros Sachtouris
# conditions are met:
6 7493ccb6 Stavros Sachtouris
#
7 7493ccb6 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 7493ccb6 Stavros Sachtouris
#      disclaimer.
10 7493ccb6 Stavros Sachtouris
#
11 7493ccb6 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 7493ccb6 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 7493ccb6 Stavros Sachtouris
#      provided with the distribution.
15 7493ccb6 Stavros Sachtouris
#
16 7493ccb6 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 7493ccb6 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 7493ccb6 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 7493ccb6 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 7493ccb6 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 7493ccb6 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 7493ccb6 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 7493ccb6 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 7493ccb6 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 7493ccb6 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 7493ccb6 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 7493ccb6 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 7493ccb6 Stavros Sachtouris
#
29 7493ccb6 Stavros Sachtouris
# The views and conclusions contained in the software and
30 7493ccb6 Stavros Sachtouris
# documentation are those of the authors and should not be
31 7493ccb6 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 7493ccb6 Stavros Sachtouris
# or implied, of GRNET S.A.
33 7493ccb6 Stavros Sachtouris
34 7493ccb6 Stavros Sachtouris
import os
35 7493ccb6 Stavros Sachtouris
36 7493ccb6 Stavros Sachtouris
from collections import defaultdict
37 7493ccb6 Stavros Sachtouris
from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
38 7493ccb6 Stavros Sachtouris
39 7493ccb6 Stavros Sachtouris
try:
40 7493ccb6 Stavros Sachtouris
    from collections import OrderedDict
41 7493ccb6 Stavros Sachtouris
except ImportError:
42 1792ed1d Stavros Sachtouris
    from kamaki.clients.utils.ordereddict import OrderedDict
43 7493ccb6 Stavros Sachtouris
44 7493ccb6 Stavros Sachtouris
45 7493ccb6 Stavros Sachtouris
# Path to the file that stores the configuration
46 7493ccb6 Stavros Sachtouris
CONFIG_PATH = os.path.expanduser('~/.kamakirc')
47 9f1d1fcf Stavros Sachtouris
HISTORY_PATH = os.path.expanduser('~/.kamaki.history')
48 7493ccb6 Stavros Sachtouris
49 7493ccb6 Stavros Sachtouris
# Name of a shell variable to bypass the CONFIG_PATH value
50 7493ccb6 Stavros Sachtouris
CONFIG_ENV = 'KAMAKI_CONFIG'
51 7493ccb6 Stavros Sachtouris
52 7493ccb6 Stavros Sachtouris
HEADER = """
53 05e144e2 Stavros Sachtouris
# Kamaki configuration file v3
54 7493ccb6 Stavros Sachtouris
"""
55 7493ccb6 Stavros Sachtouris
56 7493ccb6 Stavros Sachtouris
DEFAULTS = {
57 7493ccb6 Stavros Sachtouris
    'global': {
58 4f6a21f6 Stavros Sachtouris
        'colors': 'off',
59 c5b9380c Stavros Sachtouris
        'token': '',
60 edf00ab3 Stavros Sachtouris
        'log_file': os.path.expanduser('~/.kamaki.log'),
61 c5b9380c Stavros Sachtouris
        'log_token': 'off',
62 c5b9380c Stavros Sachtouris
        'log_data': 'off',
63 05e144e2 Stavros Sachtouris
        'max_threads': 7,
64 05e144e2 Stavros Sachtouris
        'url': 'https://accounts.okeanos.grnet.gr/astakos/identity/v2.0/'
65 05e144e2 Stavros Sachtouris
    },
66 05e144e2 Stavros Sachtouris
    'cli': {
67 05e144e2 Stavros Sachtouris
        'user': 'astakos',
68 05e144e2 Stavros Sachtouris
        'file': 'pithos',
69 05e144e2 Stavros Sachtouris
        'server': 'cyclades',
70 05e144e2 Stavros Sachtouris
        'flavor': 'cyclades',
71 05e144e2 Stavros Sachtouris
        'network': 'cyclades',
72 05e144e2 Stavros Sachtouris
        'image': 'image',
73 05e144e2 Stavros Sachtouris
        'config': 'config',
74 05e144e2 Stavros Sachtouris
        'history': 'history'
75 7493ccb6 Stavros Sachtouris
    },
76 fd5db045 Stavros Sachtouris
    'history': {
77 fd5db045 Stavros Sachtouris
        'file': HISTORY_PATH
78 554d18b0 Stavros Sachtouris
    },
79 05e144e2 Stavros Sachtouris
    'pithos': {
80 51081e51 Stavros Sachtouris
        'type': 'object-store',
81 8c54338a Stavros Sachtouris
        'version': 'v1'
82 554d18b0 Stavros Sachtouris
    },
83 05e144e2 Stavros Sachtouris
    'cyclades': {
84 05e144e2 Stavros Sachtouris
        'type': 'compute',
85 05e144e2 Stavros Sachtouris
        'version': 'v2.0'
86 05e144e2 Stavros Sachtouris
        },
87 05e144e2 Stavros Sachtouris
    'plankton': {
88 05e144e2 Stavros Sachtouris
        'type': 'image',
89 8c54338a Stavros Sachtouris
        'version': 'v1.0'
90 554d18b0 Stavros Sachtouris
    },
91 05e144e2 Stavros Sachtouris
    'astakos': {
92 05e144e2 Stavros Sachtouris
        'type': 'identity',
93 05e144e2 Stavros Sachtouris
        'version': 'v2.0'
94 7493ccb6 Stavros Sachtouris
    }
95 7493ccb6 Stavros Sachtouris
}
96 7493ccb6 Stavros Sachtouris
97 7493ccb6 Stavros Sachtouris
98 7493ccb6 Stavros Sachtouris
class Config(RawConfigParser):
99 7493ccb6 Stavros Sachtouris
    def __init__(self, path=None):
100 7493ccb6 Stavros Sachtouris
        RawConfigParser.__init__(self, dict_type=OrderedDict)
101 7493ccb6 Stavros Sachtouris
        self.path = path or os.environ.get(CONFIG_ENV, CONFIG_PATH)
102 7493ccb6 Stavros Sachtouris
        self._overrides = defaultdict(dict)
103 7493ccb6 Stavros Sachtouris
        self._load_defaults()
104 7493ccb6 Stavros Sachtouris
        self.read(self.path)
105 7493ccb6 Stavros Sachtouris
106 7493ccb6 Stavros Sachtouris
    def _load_defaults(self):
107 7493ccb6 Stavros Sachtouris
        for section, options in DEFAULTS.items():
108 7493ccb6 Stavros Sachtouris
            for option, val in options.items():
109 7493ccb6 Stavros Sachtouris
                self.set(section, option, val)
110 7493ccb6 Stavros Sachtouris
111 f724cd35 Stavros Sachtouris
    def _get_dict(self, section, include_defaults=True):
112 f724cd35 Stavros Sachtouris
        try:
113 f724cd35 Stavros Sachtouris
            d = dict(DEFAULTS[section]) if include_defaults else {}
114 f724cd35 Stavros Sachtouris
        except KeyError:
115 f724cd35 Stavros Sachtouris
            d = {}
116 f724cd35 Stavros Sachtouris
        try:
117 f724cd35 Stavros Sachtouris
            d.update(RawConfigParser.items(self, section))
118 f724cd35 Stavros Sachtouris
        except NoSectionError:
119 f724cd35 Stavros Sachtouris
            pass
120 f724cd35 Stavros Sachtouris
        return d
121 f724cd35 Stavros Sachtouris
122 8eb8c540 Stavros Sachtouris
    def reload(self):
123 8eb8c540 Stavros Sachtouris
        self = self.__init__(self.path)
124 8eb8c540 Stavros Sachtouris
125 7493ccb6 Stavros Sachtouris
    def get(self, section, option):
126 7493ccb6 Stavros Sachtouris
        value = self._overrides.get(section, {}).get(option)
127 7493ccb6 Stavros Sachtouris
        if value is not None:
128 7493ccb6 Stavros Sachtouris
            return value
129 7493ccb6 Stavros Sachtouris
130 7493ccb6 Stavros Sachtouris
        try:
131 7493ccb6 Stavros Sachtouris
            return RawConfigParser.get(self, section, option)
132 7493ccb6 Stavros Sachtouris
        except (NoSectionError, NoOptionError):
133 7493ccb6 Stavros Sachtouris
            return DEFAULTS.get(section, {}).get(option)
134 7493ccb6 Stavros Sachtouris
135 7493ccb6 Stavros Sachtouris
    def set(self, section, option, value):
136 7493ccb6 Stavros Sachtouris
        if section not in RawConfigParser.sections(self):
137 7493ccb6 Stavros Sachtouris
            self.add_section(section)
138 7493ccb6 Stavros Sachtouris
        RawConfigParser.set(self, section, option, value)
139 7493ccb6 Stavros Sachtouris
140 3fe56be4 Stavros Sachtouris
    def remove_option(self, section, option, also_remove_default=False):
141 7493ccb6 Stavros Sachtouris
        try:
142 3fe56be4 Stavros Sachtouris
            if also_remove_default:
143 3fe56be4 Stavros Sachtouris
                DEFAULTS[section].pop(option)
144 7493ccb6 Stavros Sachtouris
            RawConfigParser.remove_option(self, section, option)
145 7493ccb6 Stavros Sachtouris
        except NoSectionError:
146 7493ccb6 Stavros Sachtouris
            pass
147 7493ccb6 Stavros Sachtouris
148 f724cd35 Stavros Sachtouris
    def keys(self, section, include_defaults=True):
149 f724cd35 Stavros Sachtouris
        d = self._get_dict(section, include_defaults)
150 f724cd35 Stavros Sachtouris
        return d.keys()
151 f724cd35 Stavros Sachtouris
152 57ecec97 Stavros Sachtouris
    def items(self, section, include_defaults=True):
153 f724cd35 Stavros Sachtouris
        d = self._get_dict(section, include_defaults)
154 7493ccb6 Stavros Sachtouris
        return d.items()
155 7493ccb6 Stavros Sachtouris
156 7493ccb6 Stavros Sachtouris
    def override(self, section, option, value):
157 7493ccb6 Stavros Sachtouris
        self._overrides[section][option] = value
158 7493ccb6 Stavros Sachtouris
159 7493ccb6 Stavros Sachtouris
    def write(self):
160 7493ccb6 Stavros Sachtouris
        with open(self.path, 'w') as f:
161 7493ccb6 Stavros Sachtouris
            os.chmod(self.path, 0600)
162 7493ccb6 Stavros Sachtouris
            f.write(HEADER.lstrip())
163 8eb8c540 Stavros Sachtouris
            f.flush()
164 7493ccb6 Stavros Sachtouris
            RawConfigParser.write(self, f)