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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# Copyright 2021-2022 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
import os
import json
from ariadne import convert_camel_case_to_snake
from vyos.config import Config
from vyos.configtree import ConfigTree
from vyos.defaults import directories
from vyos.template import render
from vyos.opmode import Error as OpModeError
from api.graphql.libs.op_mode import load_op_mode_as_module, split_compound_op_mode_name
from api.graphql.libs.op_mode import normalize_output
op_mode_include_file = os.path.join(directories['data'], 'op-mode-standardized.json')
def get_config_dict(path=[], effective=False, key_mangling=None,
get_first_key=False, no_multi_convert=False,
no_tag_node_value_mangle=False):
config = Config()
return config.get_config_dict(path=path, effective=effective,
key_mangling=key_mangling,
get_first_key=get_first_key,
no_multi_convert=no_multi_convert,
no_tag_node_value_mangle=no_tag_node_value_mangle)
def get_user_info(user):
user_info = {}
info = get_config_dict(['system', 'login', 'user', user],
get_first_key=True)
if not info:
raise ValueError("No such user")
user_info['user'] = user
user_info['full_name'] = info.get('full-name', '')
return user_info
class Session:
"""
Wrapper for calling configsession functions based on GraphQL requests.
Non-nullable fields in the respective schema allow avoiding a key check
in 'data'.
"""
def __init__(self, session, data):
self._session = session
self._data = data
self._name = convert_camel_case_to_snake(type(self).__name__)
try:
with open(op_mode_include_file) as f:
self._op_mode_list = json.loads(f.read())
except Exception:
self._op_mode_list = None
def show_config(self):
session = self._session
data = self._data
out = ''
try:
out = session.show_config(data['path'])
if data.get('config_format', '') == 'json':
config_tree = ConfigTree(out)
out = json.loads(config_tree.to_json())
except Exception as error:
raise error
return out
def save_config_file(self):
session = self._session
data = self._data
if 'file_name' not in data or not data['file_name']:
data['file_name'] = '/config/config.boot'
try:
session.save_config(data['file_name'])
except Exception as error:
raise error
def load_config_file(self):
session = self._session
data = self._data
try:
session.load_config(data['file_name'])
session.commit()
except Exception as error:
raise error
def show(self):
session = self._session
data = self._data
out = ''
try:
out = session.show(data['path'])
except Exception as error:
raise error
return out
def add_system_image(self):
session = self._session
data = self._data
try:
res = session.install_image(data['location'])
except Exception as error:
raise error
return res
def delete_system_image(self):
session = self._session
data = self._data
try:
res = session.remove_image(data['name'])
except Exception as error:
raise error
return res
def show_user_info(self):
session = self._session
data = self._data
user_info = {}
user = data['user']
try:
user_info = get_user_info(user)
except Exception as error:
raise error
return user_info
def system_status(self):
import api.graphql.session.composite.system_status as system_status
session = self._session
data = self._data
status = {}
status['host_name'] = session.show(['host', 'name']).strip()
status['version'] = system_status.get_system_version()
status['uptime'] = system_status.get_system_uptime()
status['ram'] = system_status.get_system_ram_usage()
return status
def gen_op_query(self):
session = self._session
data = self._data
name = self._name
op_mode_list = self._op_mode_list
# handle the case that the op-mode file contains underscores:
if op_mode_list is None:
raise FileNotFoundError(f"No op-mode file list at '{op_mode_include_file}'")
(func_name, scriptname) = split_compound_op_mode_name(name, op_mode_list)
if scriptname == '':
raise FileNotFoundError(f"No op-mode file named in string '{name}'")
mod = load_op_mode_as_module(f'{scriptname}')
func = getattr(mod, func_name)
try:
res = func(True, **data)
except OpModeError as e:
raise e
res = normalize_output(res)
return res
def gen_op_mutation(self):
session = self._session
data = self._data
name = self._name
op_mode_list = self._op_mode_list
# handle the case that the op-mode file name contains underscores:
if op_mode_list is None:
raise FileNotFoundError(f"No op-mode file list at '{op_mode_include_file}'")
(func_name, scriptname) = split_compound_op_mode_name(name, op_mode_list)
if scriptname == '':
raise FileNotFoundError(f"No op-mode file named in string '{name}'")
mod = load_op_mode_as_module(f'{scriptname}')
func = getattr(mod, func_name)
try:
res = func(**data)
except OpModeError as e:
raise e
return res
|