1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
"""
Configuration Base Class
"""
import grp
import json
import logging
import requests
import os
from leap.util.fileutil import mkdir_p
logger = logging.getLogger(name=__name__)
logger.setLevel('DEBUG')
class Configuration(object):
"""
All configurations (providers et al) will be managed in this class.
"""
def __init__(self, provider_url=None):
try:
self.providers = {}
self.error = False
provider_file = self.check_and_get_definition_file(provider_url)
self.providers['default'] = get_config_json(provider_file)
except (requests.HTTPError, requests.RequestException) as e:
self.error = e.message
except requests.ConnectionError as e:
if e.message == "[Errno 113] No route to host":
if not is_internet_up:
self.error = "No valid internet connection found"
else:
self.error = "Provider server appears currently down."
def check_and_get_definition_file(self, provider_url):
"""
checks if provider definition.json file is present.
if not downloads one from the web.
"""
default_provider_path = get_default_provider_path()
if not os.path.isdir(default_provider_path):
mkdir_p(default_provider_path)
definition_file = get_config_file(
'definition.json',
folder=default_provider_path)
if os.path.isfile(definition_file):
return
else:
r = requests.get(provider_url)
r.raise_for_status()
with open(definition_file, 'wb') as f:
f.write(json.dumps(r.json, indent=4))
return definition_file
def get_config_dir():
"""
get the base dir for all leap config
@rparam: config path
@rtype: string
"""
# TODO
# check for $XDG_CONFIG_HOME var?
# get a more sensible path for win/mac
# kclair: opinion? ^^
return os.path.expanduser(
os.path.join('~',
'.config',
'leap'))
def get_config_file(filename, folder=None):
"""
concatenates the given filename
with leap config dir.
@param filename: name of the file
@type filename: string
@rparam: full path to config file
"""
path = []
path.append(get_config_dir())
if folder is not None:
path.append(folder)
path.append(filename)
return os.path.join(*path)
def get_default_provider_path():
default_subpath = os.path.join("providers",
"default")
default_provider_path = get_config_file(
'',
folder=default_subpath)
return default_provider_path
def validate_ip(ip_str):
"""
raises exception if the ip_str is
not a valid representation of an ip
"""
socket.inet_aton(ip_str)
def get_username():
return os.getlogin()
def get_groupname():
gid = os.getgroups()[-1]
return grp.getgrgid(gid).gr_name
def get_config_json(config_file=None):
"""
will replace get_config function be developing them
in parralel for branch purposes.
@param: configuration file
@type: file
@rparam: configuration turples
@rtype: dictionary
"""
if not config_file:
#TODO: NOT SURE WHAT this default should be, if anything
fpath = get_config_file('eip.json')
if not os.path.isfile(fpath):
dpath, cfile = os.path.split(fpath)
if not os.path.isdir(dpath):
mkdir_p(dpath)
with open(fpath, 'wb') as configfile:
configfile.flush()
return json.load(open(fpath))
else:
#TODO: add validity checks of file
return json.load(open(config_file))
def is_internet_up():
"""TODO: Build more robust network diagnosis capabilities
"""
try:
response = requests.get('http://128.30.52.45', timeout=1)
return True
except requests.Timeout as err:
pass
return False
|