summaryrefslogtreecommitdiff
path: root/scripts/vyos-show-version
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2016-03-03 10:23:03 -0500
committerDaniil Baturin <daniil@baturin.org>2016-03-03 10:23:03 -0500
commitc18b9618f4577fb80b07a0471f19a11ed2b3dfcb (patch)
tree943883a5b252b631bb34bcaece440f11cc300bfa /scripts/vyos-show-version
parent918b53841364a016eec0e2996640c471f738e93b (diff)
downloadvyatta-op-c18b9618f4577fb80b07a0471f19a11ed2b3dfcb.tar.gz
vyatta-op-c18b9618f4577fb80b07a0471f19a11ed2b3dfcb.zip
New implementation of the version display command (ref T6).
Diffstat (limited to 'scripts/vyos-show-version')
-rw-r--r--scripts/vyos-show-version106
1 files changed, 106 insertions, 0 deletions
diff --git a/scripts/vyos-show-version b/scripts/vyos-show-version
new file mode 100644
index 0000000..a701b84
--- /dev/null
+++ b/scripts/vyos-show-version
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+#
+# 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 <http://www.gnu.org/licenses/>.
+#
+# File: vyos-show-version
+# Purpose:
+# Displays image version and system information.
+# Used by the "run show version" command.
+
+
+import sys
+import subprocess
+import json
+
+import pystache
+
+
+def read_file(name):
+ with open (name, "r") as f:
+ data = f.read()
+ return data.strip()
+
+version_file = '/opt/vyatta/etc/version.json'
+version_data = None
+
+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
+
+"""
+
+# Get and display image version data from the built-in version file
+# This gives us image version, built on, build by, and build UUID
+try:
+ with open(version_file, 'r') as f:
+ version_data = json.load(f)
+except:
+ print("Unable to get VyOS version data")
+ sys.exit(1)
+
+
+# Get system architecture (well, kernel architecture rather)
+version_data['system_arch'] = subprocess.check_output('uname -m', shell=True).strip()
+
+
+# Get hypervisor name, if any
+system_type = "physical"
+try:
+ hypervisor = subprocess.check_output('hvinfo', shell=True).strip()
+ system_type = "{0} guest".format(hypervisor)
+except 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')
+# XXX: serial and uuid files are only readable for root, so we cannot just read them
+# when script is ran by a normal user, hence this ugly fixup
+version_data['hardware_serial'] = subprocess.check_output('sudo cat /sys/class/dmi/id/subsystem/id/product_serial', shell=True).strip()
+version_data['hardware_uuid'] = subprocess.check_output('sudo cat /sys/class/dmi/id/subsystem/id/product_uuid', shell=True).strip()
+
+
+output = pystache.render(version_output_tmpl, version_data).strip()
+print(output)
+