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
|
#!/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 os
import re
import sys
from tempfile import NamedTemporaryFile
from argparse import ArgumentParser
from vyos.config import Config
from vyos.remote import urlc
from vyos.component_version import add_system_version_string
from vyos.defaults import directories
from vyos.utils.file import write_file
from vyos.utils.file import write_file_sync
from vyos.utils.file import write_file_atomic
from vyos.utils.file import file_is_persistent
DEFAULT_CONFIG_PATH = os.path.join(directories['config'], 'config.boot')
remote_save = None
parser = ArgumentParser(description='Save configuration')
parser.add_argument('file', type=str, nargs='?', help='Save configuration to file')
parser.add_argument(
'--write-json-file', type=str, help='Save JSON of configuration to file'
)
args = parser.parse_args()
file = args.file
json_file = args.write_json_file
if file is not None:
save_file = file
else:
save_file = DEFAULT_CONFIG_PATH
if re.match(r'\w+:/', save_file):
try:
remote_save = urlc(save_file)
except ValueError as e:
sys.exit(e)
config = Config()
ct = config.get_config_tree(effective=True)
# The effective config is None before boot configuration is complete.
# Nothing to write, nor do we want to invite saving an empty string:
# exit gracefully.
if ct is None:
sys.exit()
config_str = ct.to_string()
versioned_config_str = add_system_version_string(config_str)
# pylint: disable=consider-using-with
file_to_write = (
save_file if remote_save is None else NamedTemporaryFile(delete=False).name
)
if file_is_persistent(file_to_write):
if os.geteuid() == 0:
try:
write_file_atomic(file_to_write, versioned_config_str)
except OSError as e:
print(f'failed to write config file, write_file_atomic: {e}')
sys.exit(1)
else:
try:
write_file_sync(file_to_write, versioned_config_str)
except OSError as e:
print(f'failed to write config file, write_file_sync: {e}')
sys.exit(1)
else:
try:
write_file(file_to_write, versioned_config_str)
except Exception as e: # pylint: disable=broad-exception-caught
print(f'failed to write config file, write_file: {e}')
sys.exit(1)
if json_file is not None and ct is not None:
try:
with open(json_file, 'w') as f:
f.write(ct.to_json())
except OSError as e:
print(f'failed to write JSON file: {e}')
if remote_save is not None:
try:
remote_save.upload(file_to_write)
finally:
os.remove(file_to_write)
|