From 6b0bb7ca41346e0be4c4df70dbb2d33ff0a37533 Mon Sep 17 00:00:00 2001 From: Evgeniia Date: Mon, 8 Jan 2018 20:47:49 +0500 Subject: [PATCH] Check tanner version (#78) * check tanner compatibility * use class for version checker * Message format --- snare.py | 13 ++++++++----- versions_manager.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 versions_manager.py diff --git a/snare.py b/snare.py index ddcee584..fdc5fe5a 100644 --- a/snare.py +++ b/snare.py @@ -13,7 +13,6 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. """ - import argparse import asyncio import configparser @@ -27,7 +26,7 @@ import uuid from concurrent.futures import ProcessPoolExecutor from urllib.parse import urlparse, unquote, parse_qsl - +from versions_manager import VersionManager import aiohttp import git import mimetypes @@ -385,11 +384,15 @@ def parse_timeout(timeout): @asyncio.coroutine -def check_tanner_connection(): +def check_tanner(): + vm = VersionManager() with aiohttp.ClientSession() as client: - req_url = 'http://{}:8090'.format(args.tanner) + req_url = 'http://{}:8090/version'.format(args.tanner) try: resp = yield from client.get(req_url) + result = yield from resp.json() + version = result["version"] + vm.check_compatibility(version) except aiohttp.errors.ClientOSError: print("Can't connect to tanner host {}".format(req_url)) exit(1) @@ -444,7 +447,7 @@ def check_tanner_connection(): else: add_meta_tag(args.page_dir, args.index_page) loop = asyncio.get_event_loop() - loop.run_until_complete(check_tanner_connection()) + loop.run_until_complete(check_tanner()) pool = ProcessPoolExecutor(max_workers=multiprocessing.cpu_count()) compare_version_fut = None diff --git a/versions_manager.py b/versions_manager.py new file mode 100644 index 00000000..e61cc8e5 --- /dev/null +++ b/versions_manager.py @@ -0,0 +1,15 @@ +from distutils.version import StrictVersion + + +class VersionManager: + def __init__(self): + self.version = "0.1.0" + self.version_mapper = { + "0.1.0": "0.4.0" + } + + def check_compatibility(self, tanner_version): + max_version = self.version_mapper[self.version] + if not StrictVersion(tanner_version) <= StrictVersion(max_version): + print("Wrong tanner version: {}. Need version: {} or less".format(tanner_version, max_version)) + exit(1)