From d4c10902026b76a6372cf738fc98860b29b39759 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 16 May 2018 16:01:38 +0200 Subject: T644: remove prefixing from all scripts and update environment variables with VyOS paths. --- src/op_mode/dns_forwarding_restart.sh | 8 ++ src/op_mode/dns_forwarding_statistics.py | 24 +++++ src/op_mode/version.py | 123 ++++++++++++++++++++++++++ src/op_mode/vyos_dns_forwarding_statistics.py | 24 ----- src/op_mode/vyos_list_dumpable_interfaces.py | 14 --- src/op_mode/vyos_list_interfaces.py | 8 -- src/op_mode/vyos_restart_dns_forwarding.sh | 8 -- src/op_mode/vyos_show_version.py | 123 -------------------------- 8 files changed, 155 insertions(+), 177 deletions(-) create mode 100755 src/op_mode/dns_forwarding_restart.sh create mode 100755 src/op_mode/dns_forwarding_statistics.py create mode 100755 src/op_mode/version.py delete mode 100755 src/op_mode/vyos_dns_forwarding_statistics.py delete mode 100755 src/op_mode/vyos_list_dumpable_interfaces.py delete mode 100755 src/op_mode/vyos_list_interfaces.py delete mode 100755 src/op_mode/vyos_restart_dns_forwarding.sh delete mode 100755 src/op_mode/vyos_show_version.py (limited to 'src/op_mode') diff --git a/src/op_mode/dns_forwarding_restart.sh b/src/op_mode/dns_forwarding_restart.sh new file mode 100755 index 000000000..12106fcc1 --- /dev/null +++ b/src/op_mode/dns_forwarding_restart.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +if cli-shell-api exists service dns forwarding; then + echo "Restarting the DNS forwarding service" + systemctl restart pdns-recursor +else + echo "DNS forwarding is not configured" +fi diff --git a/src/op_mode/dns_forwarding_statistics.py b/src/op_mode/dns_forwarding_statistics.py new file mode 100755 index 000000000..3d1e30aee --- /dev/null +++ b/src/op_mode/dns_forwarding_statistics.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +import subprocess +import jinja2 + +PDNS_CMD='/usr/bin/rec_control' + +OUT_TMPL_SRC = """ +DNS forwarding statistics: + +Cache entries: {{ cache_entries -}} +Cache size: {{ cache_size }} kbytes + +""" + + +if __name__ == '__main__': + data = {} + + data['cache_entries'] = subprocess.check_output([PDNS_CMD, 'get cache-entries']).decode() + data['cache_size'] = "{0:.2f}".format( int(subprocess.check_output([PDNS_CMD, 'get cache-bytes']).decode()) / 1024 ) + + tmpl = jinja2.Template(OUT_TMPL_SRC) + print(tmpl.render(data)) diff --git a/src/op_mode/version.py b/src/op_mode/version.py new file mode 100755 index 000000000..ce3b3b54f --- /dev/null +++ b/src/op_mode/version.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2016 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 . +# +# File: vyos-show-version +# Purpose: +# Displays image version and system information. +# Used by the "run show version" command. + + +import os +import sys +import subprocess +import argparse +import json + +import pystache + +import vyos.version +import vyos.limericks + + +parser = argparse.ArgumentParser() +parser.add_argument("-a", "--all", action="store_true", help="Include individual package versions") +parser.add_argument("-f", "--funny", action="store_true", help="Add something funny to the output") +parser.add_argument("-j", "--json", action="store_true", help="Produce JSON output") + +def read_file(name): + try: + with open (name, "r") as f: + data = f.read() + return data.strip() + except: + # This works since we only read /sys/class/* stuff + # with this function + return "Unknown" + +version_output_tmpl = """ +Version: VyOS {{version}} +Built by: {{built_by}} +Built on: {{built_on}} +Build ID: {{build_id}} + +Architecture: {{system_arch}} +Boot via: {{boot_via}} +System type: {{system_type}} + +Hardware vendor: {{hardware_vendor}} +Hardware model: {{hardware_model}} +Hardware S/N: {{hardware_serial}} +Hardware UUID: {{hardware_uuid}} + +Copyright: VyOS maintainers and contributors + +""" + +if __name__ == '__main__': + args = parser.parse_args() + + version_data = vyos.version.get_version_data() + + # Get system architecture (well, kernel architecture rather) + version_data['system_arch'] = subprocess.check_output('uname -m', shell=True).decode().strip() + + + # Get hypervisor name, if any + system_type = "bare metal" + try: + hypervisor = subprocess.check_output('hvinfo 2>/dev/null', shell=True).decode().strip() + system_type = "{0} guest".format(hypervisor) + except subprocess.CalledProcessError: + # hvinfo returns 1 if it cannot detect any hypervisor + pass + version_data['system_type'] = system_type + + + # Get boot type, it can be livecd, installed image, or, possible, a system installed + # via legacy "install system" mechanism + # In installed images, the squashfs image file is named after its image version, + # while on livecd it's just "filesystem.squashfs", that's how we tell a livecd boot + # from an installed image + boot_via = "installed image" + if subprocess.call(""" grep -e '^overlay.*/filesystem.squashfs' /proc/mounts >/dev/null""", shell=True) == 0: + boot_via = "livecd" + elif subprocess.call(""" grep '^overlay /' /proc/mounts >/dev/null """, shell=True) != 0: + boot_via = "legacy non-image installation" + version_data['boot_via'] = boot_via + + + # Get hardware details from DMI + version_data['hardware_vendor'] = read_file('/sys/class/dmi/id/sys_vendor') + version_data['hardware_model'] = read_file('/sys/class/dmi/id/product_name') + + # These two assume script is run as root, normal users can't access those files + version_data['hardware_serial'] = read_file('/sys/class/dmi/id/subsystem/id/product_serial') + version_data['hardware_uuid'] = read_file('/sys/class/dmi/id/subsystem/id/product_uuid') + + + if args.json: + print(json.dumps(version_data)) + sys.exit(0) + else: + output = pystache.render(version_output_tmpl, version_data).strip() + print(output) + + if args.all: + print("Package versions:") + os.system("dpkg -l") + + if args.funny: + print(vyos.limericks.get_random()) diff --git a/src/op_mode/vyos_dns_forwarding_statistics.py b/src/op_mode/vyos_dns_forwarding_statistics.py deleted file mode 100755 index 3d1e30aee..000000000 --- a/src/op_mode/vyos_dns_forwarding_statistics.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env python3 - -import subprocess -import jinja2 - -PDNS_CMD='/usr/bin/rec_control' - -OUT_TMPL_SRC = """ -DNS forwarding statistics: - -Cache entries: {{ cache_entries -}} -Cache size: {{ cache_size }} kbytes - -""" - - -if __name__ == '__main__': - data = {} - - data['cache_entries'] = subprocess.check_output([PDNS_CMD, 'get cache-entries']).decode() - data['cache_size'] = "{0:.2f}".format( int(subprocess.check_output([PDNS_CMD, 'get cache-bytes']).decode()) / 1024 ) - - tmpl = jinja2.Template(OUT_TMPL_SRC) - print(tmpl.render(data)) diff --git a/src/op_mode/vyos_list_dumpable_interfaces.py b/src/op_mode/vyos_list_dumpable_interfaces.py deleted file mode 100755 index 53ee89633..000000000 --- a/src/op_mode/vyos_list_dumpable_interfaces.py +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env python3 - -# Extract the list of interfaces available for traffic dumps from tcpdump -D - -import re -import subprocess - -if __name__ == '__main__': - out = subprocess.check_output(['/usr/sbin/tcpdump', '-D']).decode().strip() - out = out.split("\n") - - intfs = " ".join(map(lambda s: re.search(r'\d+\.(\S+)\s', s).group(1), out)) - - print(intfs) diff --git a/src/op_mode/vyos_list_interfaces.py b/src/op_mode/vyos_list_interfaces.py deleted file mode 100755 index 59c9dffad..000000000 --- a/src/op_mode/vyos_list_interfaces.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python3 - -import netifaces - -if __name__ == '__main__': - interfaces = netifaces.interfaces() - - print(" ".join(interfaces)) diff --git a/src/op_mode/vyos_restart_dns_forwarding.sh b/src/op_mode/vyos_restart_dns_forwarding.sh deleted file mode 100755 index 12106fcc1..000000000 --- a/src/op_mode/vyos_restart_dns_forwarding.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -if cli-shell-api exists service dns forwarding; then - echo "Restarting the DNS forwarding service" - systemctl restart pdns-recursor -else - echo "DNS forwarding is not configured" -fi diff --git a/src/op_mode/vyos_show_version.py b/src/op_mode/vyos_show_version.py deleted file mode 100755 index ce3b3b54f..000000000 --- a/src/op_mode/vyos_show_version.py +++ /dev/null @@ -1,123 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (C) 2016 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 . -# -# File: vyos-show-version -# Purpose: -# Displays image version and system information. -# Used by the "run show version" command. - - -import os -import sys -import subprocess -import argparse -import json - -import pystache - -import vyos.version -import vyos.limericks - - -parser = argparse.ArgumentParser() -parser.add_argument("-a", "--all", action="store_true", help="Include individual package versions") -parser.add_argument("-f", "--funny", action="store_true", help="Add something funny to the output") -parser.add_argument("-j", "--json", action="store_true", help="Produce JSON output") - -def read_file(name): - try: - with open (name, "r") as f: - data = f.read() - return data.strip() - except: - # This works since we only read /sys/class/* stuff - # with this function - return "Unknown" - -version_output_tmpl = """ -Version: VyOS {{version}} -Built by: {{built_by}} -Built on: {{built_on}} -Build ID: {{build_id}} - -Architecture: {{system_arch}} -Boot via: {{boot_via}} -System type: {{system_type}} - -Hardware vendor: {{hardware_vendor}} -Hardware model: {{hardware_model}} -Hardware S/N: {{hardware_serial}} -Hardware UUID: {{hardware_uuid}} - -Copyright: VyOS maintainers and contributors - -""" - -if __name__ == '__main__': - args = parser.parse_args() - - version_data = vyos.version.get_version_data() - - # Get system architecture (well, kernel architecture rather) - version_data['system_arch'] = subprocess.check_output('uname -m', shell=True).decode().strip() - - - # Get hypervisor name, if any - system_type = "bare metal" - try: - hypervisor = subprocess.check_output('hvinfo 2>/dev/null', shell=True).decode().strip() - system_type = "{0} guest".format(hypervisor) - except subprocess.CalledProcessError: - # hvinfo returns 1 if it cannot detect any hypervisor - pass - version_data['system_type'] = system_type - - - # Get boot type, it can be livecd, installed image, or, possible, a system installed - # via legacy "install system" mechanism - # In installed images, the squashfs image file is named after its image version, - # while on livecd it's just "filesystem.squashfs", that's how we tell a livecd boot - # from an installed image - boot_via = "installed image" - if subprocess.call(""" grep -e '^overlay.*/filesystem.squashfs' /proc/mounts >/dev/null""", shell=True) == 0: - boot_via = "livecd" - elif subprocess.call(""" grep '^overlay /' /proc/mounts >/dev/null """, shell=True) != 0: - boot_via = "legacy non-image installation" - version_data['boot_via'] = boot_via - - - # Get hardware details from DMI - version_data['hardware_vendor'] = read_file('/sys/class/dmi/id/sys_vendor') - version_data['hardware_model'] = read_file('/sys/class/dmi/id/product_name') - - # These two assume script is run as root, normal users can't access those files - version_data['hardware_serial'] = read_file('/sys/class/dmi/id/subsystem/id/product_serial') - version_data['hardware_uuid'] = read_file('/sys/class/dmi/id/subsystem/id/product_uuid') - - - if args.json: - print(json.dumps(version_data)) - sys.exit(0) - else: - output = pystache.render(version_output_tmpl, version_data).strip() - print(output) - - if args.all: - print("Package versions:") - os.system("dpkg -l") - - if args.funny: - print(vyos.limericks.get_random()) -- cgit v1.2.3