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
|
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
import argparse
import sys
import os
import grp
from vyos.configsession import ConfigSession
from vyos.config import Config
from vyos.configdiff import get_config_diff
from vyos.xml_ref import is_leaf
CFG_GROUP = 'vyattacfg'
DEBUG = False
def type_str_to_list(value):
if isinstance(value, str):
return value.split()
raise argparse.ArgumentTypeError('path must be a whitespace separated string')
parser = argparse.ArgumentParser()
parser.add_argument('path', type=type_str_to_list, help='section to reload/rollback')
parser.add_argument('--pid', help='pid of config session')
group = parser.add_mutually_exclusive_group()
group.add_argument('--reload', action='store_true', help='retry proposed commit')
group.add_argument(
'--rollback', action='store_true', default=True, help='rollback to stable commit'
)
args = parser.parse_args()
path = args.path
reload = args.reload
rollback = args.rollback
pid = args.pid
try:
if is_leaf(path):
sys.exit('path is leaf node: neither allowed nor useful')
except ValueError:
if DEBUG:
sys.exit('nonexistent path: neither allowed nor useful')
else:
sys.exit()
test = Config()
in_session = test.in_session()
if in_session:
if reload:
sys.exit('reset_section reload not available inside of a config session')
diff = get_config_diff(test)
if not diff.is_node_changed(path):
# No discrepancies at path after commit, hence no error to revert.
sys.exit()
del diff
else:
if not reload:
sys.exit('reset_section rollback not available outside of a config session')
del test
session_id = int(pid) if pid else os.getppid()
if in_session:
# check hint left by vyshim when ConfigError is from apply stage
hint_name = f'/tmp/apply_{session_id}'
if not os.path.exists(hint_name):
# no apply error; exit
sys.exit()
else:
# cleanup hint and continue with reset
os.unlink(hint_name)
cfg_group = grp.getgrnam(CFG_GROUP)
os.setgid(cfg_group.gr_gid)
os.umask(0o002)
shared = not bool(reload)
session = ConfigSession(session_id, shared=shared)
session_env = session.get_session_env()
config = Config(session_env)
d = config.get_config_dict(path, effective=True, get_first_key=True)
if in_session:
session.discard()
session.delete(path)
session.commit()
if not d:
# nothing more to do in either case of reload/rollback
sys.exit()
session.set_section(path, d)
out = session.commit()
print(out)
|