Statistics
| Branch: | Tag: | Revision:

root / kamaki / config.py @ c4ab2af9

History | View | Annotate | Download (4.1 kB)

1 eb3ca8ca Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 eb3ca8ca Giorgos Verigakis
#
3 eb3ca8ca Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 eb3ca8ca Giorgos Verigakis
# without modification, are permitted provided that the following
5 eb3ca8ca Giorgos Verigakis
# conditions are met:
6 eb3ca8ca Giorgos Verigakis
#
7 eb3ca8ca Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 eb3ca8ca Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 eb3ca8ca Giorgos Verigakis
#      disclaimer.
10 eb3ca8ca Giorgos Verigakis
#
11 eb3ca8ca Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 eb3ca8ca Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 eb3ca8ca Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 eb3ca8ca Giorgos Verigakis
#      provided with the distribution.
15 eb3ca8ca Giorgos Verigakis
#
16 eb3ca8ca Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 eb3ca8ca Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 eb3ca8ca Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 eb3ca8ca Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 eb3ca8ca Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 eb3ca8ca Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 eb3ca8ca Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 eb3ca8ca Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 eb3ca8ca Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 eb3ca8ca Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 eb3ca8ca Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 eb3ca8ca Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 eb3ca8ca Giorgos Verigakis
#
29 eb3ca8ca Giorgos Verigakis
# The views and conclusions contained in the software and
30 eb3ca8ca Giorgos Verigakis
# documentation are those of the authors and should not be
31 eb3ca8ca Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 eb3ca8ca Giorgos Verigakis
# or implied, of GRNET S.A.
33 eb3ca8ca Giorgos Verigakis
34 eb3ca8ca Giorgos Verigakis
import json
35 eb3ca8ca Giorgos Verigakis
import logging
36 eb3ca8ca Giorgos Verigakis
import os
37 eb3ca8ca Giorgos Verigakis
38 a1c50326 Giorgos Verigakis
from os.path import exists, expanduser
39 a1c50326 Giorgos Verigakis
40 a1c50326 Giorgos Verigakis
41 a1c50326 Giorgos Verigakis
# Path to the file that stores the configuration
42 a1c50326 Giorgos Verigakis
CONFIG_PATH = expanduser('~/.kamakirc')
43 a1c50326 Giorgos Verigakis
44 a1c50326 Giorgos Verigakis
# Name of a shell variable to bypass the CONFIG_PATH value
45 a1c50326 Giorgos Verigakis
CONFIG_ENV = 'KAMAKI_CONFIG'
46 a1c50326 Giorgos Verigakis
47 a1c50326 Giorgos Verigakis
# The defaults also determine the allowed keys
48 a1c50326 Giorgos Verigakis
CONFIG_DEFAULTS = {
49 d2cea1e2 Giorgos Verigakis
    'apis': 'compute image storage cyclades pithos',
50 a1c50326 Giorgos Verigakis
    'token': '',
51 d2cea1e2 Giorgos Verigakis
    'url': '',
52 d2cea1e2 Giorgos Verigakis
    'compute_token': '',
53 a1c50326 Giorgos Verigakis
    'compute_url': 'https://okeanos.grnet.gr/api/v1',
54 d2cea1e2 Giorgos Verigakis
    'image_token': '',
55 a1c50326 Giorgos Verigakis
    'image_url': 'https://okeanos.grnet.gr/plankton',
56 a1c50326 Giorgos Verigakis
    'storage_account': '',
57 a1c50326 Giorgos Verigakis
    'storage_container': '',
58 d2cea1e2 Giorgos Verigakis
    'storage_token': '',
59 d2cea1e2 Giorgos Verigakis
    'storage_url': 'https://plus.pithos.grnet.gr/v1'
60 a1c50326 Giorgos Verigakis
}
61 a1c50326 Giorgos Verigakis
62 eb3ca8ca Giorgos Verigakis
63 eb3ca8ca Giorgos Verigakis
log = logging.getLogger('kamaki.config')
64 eb3ca8ca Giorgos Verigakis
65 eb3ca8ca Giorgos Verigakis
66 eb3ca8ca Giorgos Verigakis
class ConfigError(Exception):
67 eb3ca8ca Giorgos Verigakis
    pass
68 eb3ca8ca Giorgos Verigakis
69 eb3ca8ca Giorgos Verigakis
70 eb3ca8ca Giorgos Verigakis
class Config(object):
71 a1c50326 Giorgos Verigakis
    def __init__(self):
72 a1c50326 Giorgos Verigakis
        self.path = os.environ.get(CONFIG_ENV, CONFIG_PATH)
73 a1c50326 Giorgos Verigakis
        self.defaults = CONFIG_DEFAULTS
74 eb3ca8ca Giorgos Verigakis
        
75 eb3ca8ca Giorgos Verigakis
        d = self.read()
76 eb3ca8ca Giorgos Verigakis
        for key, val in d.items():
77 a1c50326 Giorgos Verigakis
            if key not in self.defaults:
78 eb3ca8ca Giorgos Verigakis
                log.warning('Ignoring unknown config key "%s".', key)
79 eb3ca8ca Giorgos Verigakis
        
80 eb3ca8ca Giorgos Verigakis
        self.d = d
81 eb3ca8ca Giorgos Verigakis
        self.overrides = {}
82 eb3ca8ca Giorgos Verigakis
    
83 eb3ca8ca Giorgos Verigakis
    def read(self):
84 a1c50326 Giorgos Verigakis
        if not exists(self.path):
85 eb3ca8ca Giorgos Verigakis
            return {}
86 eb3ca8ca Giorgos Verigakis
        
87 eb3ca8ca Giorgos Verigakis
        with open(self.path) as f:
88 eb3ca8ca Giorgos Verigakis
            data = f.read()
89 eb3ca8ca Giorgos Verigakis
        
90 eb3ca8ca Giorgos Verigakis
        try:
91 eb3ca8ca Giorgos Verigakis
            d = json.loads(data)
92 eb3ca8ca Giorgos Verigakis
            assert isinstance(d, dict)
93 eb3ca8ca Giorgos Verigakis
            return d
94 eb3ca8ca Giorgos Verigakis
        except (ValueError, AssertionError):
95 eb3ca8ca Giorgos Verigakis
            msg = '"%s" does not look like a kamaki config file.' % self.path
96 eb3ca8ca Giorgos Verigakis
            raise ConfigError(msg)
97 eb3ca8ca Giorgos Verigakis
    
98 eb3ca8ca Giorgos Verigakis
    def write(self):
99 eb3ca8ca Giorgos Verigakis
        self.read()     # Make sure we don't overwrite anything wrong
100 eb3ca8ca Giorgos Verigakis
        with open(self.path, 'w') as f:
101 eb3ca8ca Giorgos Verigakis
            data = json.dumps(self.d, indent=True)
102 eb3ca8ca Giorgos Verigakis
            f.write(data)
103 eb3ca8ca Giorgos Verigakis
    
104 eb3ca8ca Giorgos Verigakis
    def items(self):
105 eb3ca8ca Giorgos Verigakis
        for key, val in self.defaults.items():
106 eb3ca8ca Giorgos Verigakis
            yield key, self.get(key)
107 eb3ca8ca Giorgos Verigakis
    
108 eb3ca8ca Giorgos Verigakis
    def get(self, key):
109 eb3ca8ca Giorgos Verigakis
        if key in self.overrides:
110 eb3ca8ca Giorgos Verigakis
            return self.overrides[key]
111 eb3ca8ca Giorgos Verigakis
        if key in self.d:
112 eb3ca8ca Giorgos Verigakis
            return self.d[key]
113 a1c50326 Giorgos Verigakis
        return self.defaults.get(key, '')
114 eb3ca8ca Giorgos Verigakis
    
115 eb3ca8ca Giorgos Verigakis
    def set(self, key, val):
116 eb3ca8ca Giorgos Verigakis
        if key not in self.defaults:
117 eb3ca8ca Giorgos Verigakis
            log.warning('Ignoring unknown config key "%s".', key)
118 eb3ca8ca Giorgos Verigakis
            return
119 eb3ca8ca Giorgos Verigakis
        self.d[key] = val
120 eb3ca8ca Giorgos Verigakis
        self.write()
121 eb3ca8ca Giorgos Verigakis
    
122 eb3ca8ca Giorgos Verigakis
    def delete(self, key):
123 eb3ca8ca Giorgos Verigakis
        if key not in self.defaults:
124 eb3ca8ca Giorgos Verigakis
            log.warning('Ignoring unknown config key "%s".', key)
125 eb3ca8ca Giorgos Verigakis
            return
126 eb3ca8ca Giorgos Verigakis
        self.d.pop(key, None)
127 eb3ca8ca Giorgos Verigakis
        self.write()
128 eb3ca8ca Giorgos Verigakis
    
129 eb3ca8ca Giorgos Verigakis
    def override(self, key, val):
130 eb3ca8ca Giorgos Verigakis
        assert key in self.defaults
131 eb3ca8ca Giorgos Verigakis
        if val is not None:
132 eb3ca8ca Giorgos Verigakis
            self.overrides[key] = val