root / kamaki / cli / config.py @ 14b9a185
History | View | Annotate | Download (6.8 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 |
from logging import getLogger |
36 |
|
37 |
from collections import defaultdict |
38 |
from ConfigParser import RawConfigParser, NoOptionError, NoSectionError |
39 |
from re import match |
40 |
|
41 |
try:
|
42 |
from collections import OrderedDict |
43 |
except ImportError: |
44 |
from kamaki.clients.utils.ordereddict import OrderedDict |
45 |
|
46 |
|
47 |
log = getLogger(__name__) |
48 |
|
49 |
# Path to the file that stores the configuration
|
50 |
CONFIG_PATH = os.path.expanduser('~/.kamakirc')
|
51 |
HISTORY_PATH = os.path.expanduser('~/.kamaki.history')
|
52 |
|
53 |
# Name of a shell variable to bypass the CONFIG_PATH value
|
54 |
CONFIG_ENV = 'KAMAKI_CONFIG'
|
55 |
|
56 |
HEADER = """
|
57 |
# Kamaki configuration file v3 (kamaki >= v0.9)
|
58 |
"""
|
59 |
|
60 |
DEFAULTS = { |
61 |
'global': {
|
62 |
'colors': 'off', |
63 |
'log_file': os.path.expanduser('~/.kamaki.log'), |
64 |
'log_token': 'off', |
65 |
'log_data': 'off', |
66 |
'max_threads': 7, |
67 |
'history_file': HISTORY_PATH,
|
68 |
'user_cli': 'astakos', |
69 |
'file_cli': 'pithos', |
70 |
'server_cli': 'cyclades', |
71 |
'flavor_cli': 'cyclades', |
72 |
'network_cli': 'cyclades', |
73 |
'image_cli': 'image', |
74 |
'config_cli': 'config', |
75 |
'history_cli': 'history' |
76 |
# Optional command specs:
|
77 |
# 'livetest': 'livetest',
|
78 |
# 'astakos': 'snf-astakos'
|
79 |
}, |
80 |
'remotes':
|
81 |
{ |
82 |
'default': {
|
83 |
'url': '', |
84 |
'token': '' |
85 |
#'pithos_type': 'object-store',
|
86 |
#'pithos_version': 'v1',
|
87 |
#'cyclades_type': 'compute',
|
88 |
#'cyclades_version': 'v2.0',
|
89 |
#'plankton_type': 'image',
|
90 |
#'plankton_version': '',
|
91 |
#'astakos_type': 'identity',
|
92 |
#'astakos_version': 'v2.0'
|
93 |
} |
94 |
} |
95 |
} |
96 |
|
97 |
|
98 |
class Config(RawConfigParser): |
99 |
def __init__(self, path=None, with_defaults=True): |
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 |
if with_defaults:
|
104 |
self._load_defaults()
|
105 |
self.read(self.path) |
106 |
|
107 |
assert False, 'Config.__init__: translate remotes to dict first' |
108 |
|
109 |
@staticmethod
|
110 |
def _remote_name(full_section_name): |
111 |
matcher = match('remote "(\w+)"', full_section_name)
|
112 |
return matcher.groups()[0] if matcher else None |
113 |
|
114 |
def rescue_old_file(self): |
115 |
pass
|
116 |
# global.url, global.token --> remote.default.url, remote.default.token
|
117 |
# remove global.url, global.token
|
118 |
# translation for <service> or <command> settings
|
119 |
# <service> or <command group> settings --> translation --> global
|
120 |
|
121 |
def guess_version(self): |
122 |
checker = Config(self.path, with_defaults=False) |
123 |
sections = checker.sections() |
124 |
log.warning('Config file heuristic 1: global section ?')
|
125 |
if 'global' in sections: |
126 |
if checker.get('global', 'url') or checker.get('global', 'token'): |
127 |
log.warning('..... config file has an old global section')
|
128 |
return 2.0 |
129 |
log.warning('........ nope')
|
130 |
log.warning('Config file heuristic 2: at least 1 remote section ?')
|
131 |
for section in sections: |
132 |
if self._remote_name(section): |
133 |
log.warning('... found %s section' % section)
|
134 |
return 3.0 |
135 |
log.warning('........ nope')
|
136 |
log.warning('All heuristics failed, cannot decide')
|
137 |
return 0.0 |
138 |
|
139 |
def _load_defaults(self): |
140 |
for section, options in DEFAULTS.items(): |
141 |
for option, val in options.items(): |
142 |
self.set(section, option, val)
|
143 |
|
144 |
def _get_dict(self, section, include_defaults=True): |
145 |
try:
|
146 |
d = dict(DEFAULTS[section]) if include_defaults else {} |
147 |
except KeyError: |
148 |
d = {} |
149 |
try:
|
150 |
d.update(RawConfigParser.items(self, section))
|
151 |
except NoSectionError:
|
152 |
pass
|
153 |
return d
|
154 |
|
155 |
def reload(self): |
156 |
self = self.__init__(self.path) |
157 |
|
158 |
def get(self, section, option): |
159 |
value = self._overrides.get(section, {}).get(option)
|
160 |
if value is not None: |
161 |
return value
|
162 |
|
163 |
try:
|
164 |
return RawConfigParser.get(self, section, option) |
165 |
except (NoSectionError, NoOptionError):
|
166 |
return DEFAULTS.get(section, {}).get(option)
|
167 |
|
168 |
def set(self, section, option, value): |
169 |
if section not in RawConfigParser.sections(self): |
170 |
self.add_section(section)
|
171 |
RawConfigParser.set(self, section, option, value)
|
172 |
|
173 |
def remove_option(self, section, option, also_remove_default=False): |
174 |
try:
|
175 |
if also_remove_default:
|
176 |
DEFAULTS[section].pop(option) |
177 |
RawConfigParser.remove_option(self, section, option)
|
178 |
except NoSectionError:
|
179 |
pass
|
180 |
|
181 |
def keys(self, section, include_defaults=True): |
182 |
d = self._get_dict(section, include_defaults)
|
183 |
return d.keys()
|
184 |
|
185 |
def items(self, section, include_defaults=True): |
186 |
d = self._get_dict(section, include_defaults)
|
187 |
return d.items()
|
188 |
|
189 |
def override(self, section, option, value): |
190 |
self._overrides[section][option] = value
|
191 |
|
192 |
def write(self): |
193 |
with open(self.path, 'w') as f: |
194 |
os.chmod(self.path, 0600) |
195 |
f.write(HEADER.lstrip()) |
196 |
f.flush() |
197 |
assert False, 'Config.write: Trasnlate remotes to file first' |
198 |
RawConfigParser.write(self, f)
|