Passed
Push — master ( 07f8d9...dd24fe )
by Humberto
58s queued 11s
created

kytos.utils.config.create_skel_dir()   A

Complexity

Conditions 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 0
Metric Value
cc 4
eloc 15
nop 0
dl 0
loc 22
ccs 13
cts 15
cp 0.8667
crap 4.0378
rs 9.65
c 0
b 0
f 0
1
"""Kytos utils configuration."""
2
# This file is part of kytos-utils.
3
#
4
# Copyright (c) 2016 Kytos Team
5
#
6
# Authors:
7
#    Beraldo Leal <beraldo AT ncc DOT unesp DOT br>
8
9 1
import json
10 1
import logging
11 1
import os
12 1
import re
13 1
import shutil
14 1
from collections import namedtuple
15 1
from configparser import ConfigParser
16 1
from pathlib import Path
17 1
from urllib.error import URLError
18 1
from urllib.request import urlopen
19
20 1
LOG = logging.getLogger(__name__)
21
22
23 1
def create_skel_dir():
24
    """Install data_files in the /etc directory."""
25 1
    base_env = os.environ.get('VIRTUAL_ENV', '/')
26 1
    etc_kytos = os.path.join(base_env, 'etc/kytos')
27 1
    kytos_skel_path = 'templates/skel'
28 1
    parent_dir = Path(os.path.abspath(os.path.dirname(__file__))).parent
29
30 1
    if not os.path.exists(etc_kytos):
31 1
        os.makedirs(etc_kytos)
32
33 1
    src = os.path.join(parent_dir, kytos_skel_path)
34 1
    skel = kytos_skel_path.replace('templates/', '')
35 1
    dst = os.path.join(etc_kytos, skel)
36
37 1
    if os.path.exists(dst):
38 1
        if not os.listdir(dst):
39
            # Path already exists but it's empty, so we'll populate it
40
            # We remove it first to avoid an exception from copytree
41
            os.rmdir(dst)
42
            shutil.copytree(src, dst)
43
    else:
44 1
        shutil.copytree(src, dst)
45
46
47 1
class KytosConfig():
48
    """Kytos Configs.
49
50
    Read the config file for kytos utils and/or request data for the user in
51
    order to get the correct paths and links.
52
    """
53
54 1
    def __init__(self, config_file='~/.kytosrc'):
55
        """Init method.
56
57
        Receive the config_file as argument.
58
        """
59 1
        create_skel_dir()
60 1
        self.config_file = os.path.expanduser(config_file)
61 1
        self.debug = False
62 1
        if self.debug:
63
            LOG.setLevel(logging.DEBUG)
64
65
        # allow_no_value=True is used to keep the comments on the config file.
66 1
        self.config = ConfigParser(allow_no_value=True)
67
68
        # Parse the config file. If no config file was found, then create some
69
        # default sections on the config variable.
70 1
        self.config.read(self.config_file)
71 1
        self.check_sections(self.config)
72
73 1
        self.set_env_or_defaults()
74
75 1
        if not os.path.exists(self.config_file):
76 1
            LOG.warning("Config file %s not found.", self.config_file)
77 1
            LOG.warning("Creating a new empty config file.")
78 1
            with open(self.config_file, 'w') as output_file:
79 1
                os.chmod(self.config_file, 0o0600)
80 1
                self.config.write(output_file)
81
82 1
    def log_configs(self):
83
        """Log the read configs if debug is enabled."""
84
        for sec in self.config.sections():
85
            LOG.debug('   %s: %s', sec, self.config.options(sec))
86
87 1
    def set_env_or_defaults(self):
88
        """Read some environment variables and set them on the config.
89
90
        If no environment variable is found and the config section/key is
91
        empty, then set some default values.
92
        """
93 1
        option = namedtuple('Option', ['section', 'name', 'env_var',
94
                                       'default_value'])
95
96 1
        options = [option('auth', 'user', 'NAPPS_USER', None),
97
                   option('auth', 'token', 'NAPPS_TOKEN', None),
98
                   option('napps', 'api', 'NAPPS_API_URI',
99
                          'https://napps.kytos.io/api/'),
100
                   option('napps', 'repo', 'NAPPS_REPO_URI',
101
                          'https://napps.kytos.io/repo'),
102
                   option('kytos', 'api', 'KYTOS_API',
103
                          'http://localhost:8181/')]
104
105 1
        for option in options:
106 1
            if not self.config.has_option(option.section, option.name):
107 1
                env_value = os.environ.get(option.env_var,
108
                                           option.default_value)
109 1
                if env_value:
110 1
                    self.config.set(option.section, option.name, env_value)
111
112 1
        self.config.set('global', 'debug', str(self.debug))
113
114 1
    @staticmethod
115
    def check_sections(config):
116
        """Create a empty config file."""
117 1
        default_sections = ['global', 'auth', 'napps', 'kytos']
118 1
        for section in default_sections:
119 1
            if not config.has_section(section):
120 1
                config.add_section(section)
121
122 1
    def save_token(self, user, token):
123
        """Save the token on the config file."""
124
        self.config.set('auth', 'user', user)
125
        self.config.set('auth', 'token', token)
126
        # allow_no_value=True is used to keep the comments on the config file.
127
        new_config = ConfigParser(allow_no_value=True)
128
129
        # Parse the config file. If no config file was found, then create some
130
        # default sections on the config variable.
131
        new_config.read(self.config_file)
132
        self.check_sections(new_config)
133
134
        new_config.set('auth', 'user', user)
135
        new_config.set('auth', 'token', token)
136
        filename = os.path.expanduser(self.config_file)
137
        with open(filename, 'w') as out_file:
138
            os.chmod(filename, 0o0600)
139
            new_config.write(out_file)
140
141 1
    def clear_token(self):
142
        """Clear Token information on config file."""
143
        # allow_no_value=True is used to keep the comments on the config file.
144
        new_config = ConfigParser(allow_no_value=True)
145
146
        # Parse the config file. If no config file was found, then create some
147
        # default sections on the config variable.
148
        new_config.read(self.config_file)
149
        self.check_sections(new_config)
150
151
        new_config.remove_option('auth', 'user')
152
        new_config.remove_option('auth', 'token')
153
        filename = os.path.expanduser(self.config_file)
154
        with open(filename, 'w') as out_file:
155
            os.chmod(filename, 0o0600)
156
            new_config.write(out_file)
157
158 1
    @classmethod
159
    def get_metadata(cls):
160
        """Return kytos-utils metadata."""
161
        meta_path = ("%s/metadata.py" % os.path.dirname(__file__))
162
        meta_file = open(meta_path).read()
163
        metadata = dict(re.findall(r"(__[a-z]+__)\s*=\s*'([^']+)'", meta_file))
164
        return metadata
165
166 1
    @classmethod
167
    def get_remote_metadata(cls):
168
        """Return kytos metadata."""
169
        kytos_api = KytosConfig().config.get('kytos', 'api')
170
        meta_uri = kytos_api + 'api/kytos/core/metadata/'
171
        meta_file = urlopen(meta_uri).read()
172
        metadata = json.loads(meta_file)
173
        return metadata
174
175 1
    @classmethod
176
    def check_versions(cls):
177
        """Check if kytos and kytos-utils metadata are compatible."""
178
        try:
179
            kytos_metadata = cls.get_remote_metadata()
180
            kytos_version = kytos_metadata.get('__version__')
181
        except URLError as exc:
182
            LOG.debug('Couldn\'t connect to kytos server: %s', exc)
183
        else:
184
            kutils_metadata = cls.get_metadata()
185
            kutils_version = kutils_metadata.get('__version__')
186
187
            if kytos_version != kutils_version:
188
                logger = logging.getLogger()
189
                logger.warning('kytos (%s) and kytos utils (%s) versions '
190
                               'are not equal.', kytos_version, kutils_version)
191