Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / config.py @ e8bd81eb

History | View | Annotate | Download (4.6 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 7493ccb6 Stavros Sachtouris
    from 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 7493ccb6 Stavros Sachtouris
48 7493ccb6 Stavros Sachtouris
# Name of a shell variable to bypass the CONFIG_PATH value
49 7493ccb6 Stavros Sachtouris
CONFIG_ENV = 'KAMAKI_CONFIG'
50 7493ccb6 Stavros Sachtouris
51 7493ccb6 Stavros Sachtouris
HEADER = """
52 7493ccb6 Stavros Sachtouris
# Kamaki configuration file
53 7493ccb6 Stavros Sachtouris
"""
54 7493ccb6 Stavros Sachtouris
55 7493ccb6 Stavros Sachtouris
DEFAULTS = {
56 7493ccb6 Stavros Sachtouris
    'global': {
57 7493ccb6 Stavros Sachtouris
        'colors': 'on',
58 7493ccb6 Stavros Sachtouris
        'token': ''
59 7493ccb6 Stavros Sachtouris
    },
60 7493ccb6 Stavros Sachtouris
    'config': {
61 7493ccb6 Stavros Sachtouris
        'cli': 'config_cli',
62 7493ccb6 Stavros Sachtouris
        'description': 'Configuration commands'
63 b46307af Stavros Sachtouris
    },
64 b46307af Stavros Sachtouris
    'history':{
65 554d18b0 Stavros Sachtouris
        'cli': 'history_cli',
66 b46307af Stavros Sachtouris
        'file':'.kamaki.history'
67 554d18b0 Stavros Sachtouris
    },
68 554d18b0 Stavros Sachtouris
    'store': {
69 554d18b0 Stavros Sachtouris
        'cli': 'pithos_cli',
70 e8bd81eb Stavros Sachtouris
        'url': 'https://pithos.okeanos.grnet.gr/v1'
71 554d18b0 Stavros Sachtouris
    },
72 554d18b0 Stavros Sachtouris
    'compute': {
73 554d18b0 Stavros Sachtouris
        'url': 'https://cyclades.okeanos.grnet.gr/api/v1.1'
74 554d18b0 Stavros Sachtouris
    },
75 554d18b0 Stavros Sachtouris
    'server': {
76 554d18b0 Stavros Sachtouris
        'cli':'cyclades_cli'
77 554d18b0 Stavros Sachtouris
    },
78 554d18b0 Stavros Sachtouris
    'flavor': {
79 554d18b0 Stavros Sachtouris
        'cli':'cyclades_cli'
80 554d18b0 Stavros Sachtouris
    },
81 e8bd81eb Stavros Sachtouris
    'network': {
82 e8bd81eb Stavros Sachtouris
        'cli':'cyclades_cli'
83 e8bd81eb Stavros Sachtouris
    },
84 554d18b0 Stavros Sachtouris
    'image': {
85 554d18b0 Stavros Sachtouris
        'cli':'image_cli',
86 554d18b0 Stavros Sachtouris
        'url': 'https://plankton.okeanos.grnet.gr'
87 554d18b0 Stavros Sachtouris
    },
88 554d18b0 Stavros Sachtouris
    'astakos': {
89 554d18b0 Stavros Sachtouris
        'cli': 'astakos_cli',
90 554d18b0 Stavros Sachtouris
        'url': 'url = https://accounts.okeanos.grnet.gr'
91 7493ccb6 Stavros Sachtouris
    }
92 7493ccb6 Stavros Sachtouris
}
93 7493ccb6 Stavros Sachtouris
94 7493ccb6 Stavros Sachtouris
95 7493ccb6 Stavros Sachtouris
class Config(RawConfigParser):
96 7493ccb6 Stavros Sachtouris
    def __init__(self, path=None):
97 7493ccb6 Stavros Sachtouris
        RawConfigParser.__init__(self, dict_type=OrderedDict)
98 7493ccb6 Stavros Sachtouris
        self.path = path or os.environ.get(CONFIG_ENV, CONFIG_PATH)
99 7493ccb6 Stavros Sachtouris
        self._overrides = defaultdict(dict)
100 7493ccb6 Stavros Sachtouris
        self._load_defaults()
101 7493ccb6 Stavros Sachtouris
        self.read(self.path)
102 7493ccb6 Stavros Sachtouris
103 7493ccb6 Stavros Sachtouris
    def _load_defaults(self):
104 7493ccb6 Stavros Sachtouris
        for section, options in DEFAULTS.items():
105 7493ccb6 Stavros Sachtouris
            for option, val in options.items():
106 7493ccb6 Stavros Sachtouris
                self.set(section, option, val)
107 7493ccb6 Stavros Sachtouris
108 7493ccb6 Stavros Sachtouris
    def apis(self):
109 7493ccb6 Stavros Sachtouris
        return [api for api in self.sections() if api != 'global']
110 7493ccb6 Stavros Sachtouris
111 7493ccb6 Stavros Sachtouris
    def get(self, section, option):
112 7493ccb6 Stavros Sachtouris
        value = self._overrides.get(section, {}).get(option)
113 7493ccb6 Stavros Sachtouris
        if value is not None:
114 7493ccb6 Stavros Sachtouris
            return value
115 7493ccb6 Stavros Sachtouris
116 7493ccb6 Stavros Sachtouris
        try:
117 7493ccb6 Stavros Sachtouris
            return RawConfigParser.get(self, section, option)
118 7493ccb6 Stavros Sachtouris
        except (NoSectionError, NoOptionError):
119 7493ccb6 Stavros Sachtouris
            return DEFAULTS.get(section, {}).get(option)
120 7493ccb6 Stavros Sachtouris
121 7493ccb6 Stavros Sachtouris
    def set(self, section, option, value):
122 7493ccb6 Stavros Sachtouris
        if section not in RawConfigParser.sections(self):
123 7493ccb6 Stavros Sachtouris
            self.add_section(section)
124 7493ccb6 Stavros Sachtouris
        RawConfigParser.set(self, section, option, value)
125 7493ccb6 Stavros Sachtouris
126 7493ccb6 Stavros Sachtouris
    def remove_option(self, section, option):
127 7493ccb6 Stavros Sachtouris
        try:
128 7493ccb6 Stavros Sachtouris
            RawConfigParser.remove_option(self, section, option)
129 7493ccb6 Stavros Sachtouris
        except NoSectionError:
130 7493ccb6 Stavros Sachtouris
            pass
131 7493ccb6 Stavros Sachtouris
132 7493ccb6 Stavros Sachtouris
    def items(self, section, include_defaults=False):
133 26cd03ca Stavros Sachtouris
        try:
134 26cd03ca Stavros Sachtouris
            d = dict(DEFAULTS[section]) if include_defaults else {}
135 26cd03ca Stavros Sachtouris
        except KeyError:
136 26cd03ca Stavros Sachtouris
            d = {}
137 7493ccb6 Stavros Sachtouris
        try:
138 7493ccb6 Stavros Sachtouris
            d.update(RawConfigParser.items(self, section))
139 7493ccb6 Stavros Sachtouris
        except NoSectionError:
140 7493ccb6 Stavros Sachtouris
            pass
141 7493ccb6 Stavros Sachtouris
        return d.items()
142 7493ccb6 Stavros Sachtouris
143 7493ccb6 Stavros Sachtouris
    def override(self, section, option, value):
144 7493ccb6 Stavros Sachtouris
        self._overrides[section][option] = value
145 7493ccb6 Stavros Sachtouris
146 7493ccb6 Stavros Sachtouris
    def write(self):
147 7493ccb6 Stavros Sachtouris
        with open(self.path, 'w') as f:
148 7493ccb6 Stavros Sachtouris
            os.chmod(self.path, 0600)
149 7493ccb6 Stavros Sachtouris
            f.write(HEADER.lstrip())
150 7493ccb6 Stavros Sachtouris
            RawConfigParser.write(self, f)