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
|
#!/usr/bin/env python3
#
# Copyright (C) 2022 VyOS maintainers and contributors
#
# 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 jmespath
import json
import sys
import requests
import typing
from sys import exit
from vyos.configquery import ConfigTreeQuery
import vyos.opmode
import vyos.version
config = ConfigTreeQuery()
base = ['system', 'update-check']
def _compare_version_raw():
url = config.value(base + ['url'])
local_data = vyos.version.get_full_version_data()
remote_data = vyos.version.get_remote_version(url)
if not remote_data:
return {"error": True,
"reason": "Unable to get remote version"}
if local_data.get('version') and remote_data:
local_version = local_data.get('version')
remote_version = jmespath.search('[0].version', remote_data)
image_url = jmespath.search('[0].url', remote_data)
if local_data.get('version') != remote_version:
return {"error": False,
"update_available": True,
"local_version": local_version,
"remote_version": remote_version,
"url": image_url}
return {"update_available": False,
"local_version": local_version,
"remote_version": remote_version}
def _formatted_compare_version(data):
local_version = data.get('local_version')
remote_version = data.get('remote_version')
url = data.get('url')
if {'update_available','local_version', 'remote_version', 'url'} <= set(data):
return f'Current version: {local_version}\n\nUpdate available: {remote_version}\nUpdate URL: {url}'
elif local_version == remote_version and remote_version is not None:
return f'No available updates for your system \n' \
f'current version: {local_version}\nremote version: {remote_version}'
else:
return 'Update not found'
def _verify():
if not config.exists(base):
return False
return True
def show_update(raw: bool):
if not _verify():
raise vyos.opmode.UnconfiguredSubsystem("system update-check not configured")
data = _compare_version_raw()
if raw:
return data
else:
return _formatted_compare_version(data)
if __name__ == '__main__':
try:
res = vyos.opmode.run(sys.modules[__name__])
if res:
print(res)
except (ValueError, vyos.opmode.Error) as e:
print(e)
sys.exit(1)
|