Initial commit
[ta/distributed-state-server.git] / src / dss / server / dss_config.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 import ConfigParser
17 from dss.api import dss_error
18
19 SERVER_SECTION = "server"
20 PLUGIN_SECTION = "plugin"
21 DEFAULT_CONFIG_FILE="/etc/dss/dss-server/config.ini"
22
23 class Config(object):
24     """
25     Read the ini file. The structure of the file is as follows:
26     [server]
27     logging_level = info
28     logging_destination = console
29     verbose = true
30     listening_uds = /var/run/.dss-server
31     transport_type = dgram
32     [plugin]
33     plugin = /opt/dss-server/etcd.py
34     config = /etc/dss-server/etcd.ini
35     """
36     
37     def __init__(self, config_file = DEFAULT_CONFIG_FILE):
38         try:
39             # server
40             self.logging_level = None
41             self.logging_destination = None
42             self.verbose = None
43             self.listening_uds = None
44             self.transport_type = None
45
46             # plugin
47             self.plugin = None
48             self.plugin_config = None
49
50             config = ConfigParser.ConfigParser()
51             config.read([config_file])
52
53             self.logging_level = config.get(SERVER_SECTION, "logging_level")
54             self.logging_destination = config.get(SERVER_SECTION, "logging_destination")
55             self.verbose = config.get(SERVER_SECTION, "verbose")
56             self.listening_uds = config.get(SERVER_SECTION, "listening_uds")
57             self.transport_type = config.get(SERVER_SECTION, "transport_type")
58
59             self.plugin = config.get(PLUGIN_SECTION, "plugin")
60             self.plugin_config = config.get(PLUGIN_SECTION, "config")
61
62
63         except Exception as exp:
64             raise dss_error.Error(str(exp))
65
66     def get_logging_level(self):
67         return self.logging_level
68
69     def get_logging_destination(self):
70         return self.logging_destination
71
72     def get_verbose(self):
73         return self.verbose
74
75     def get_listening_uds(self):
76         return self.listening_uds
77
78     def get_plugin(self):
79         return self.plugin
80
81     def get_plugin_config(self):
82         return self.plugin_config
83
84     def get_transport_type(self):
85         return self.transport_type
86
87
88 if __name__ == "__main__":
89     import sys
90     try:
91         config = Config(sys.argv[1])
92     except dss_error.Error as exp:
93         print("Got exception %s" % exp)