Skip to content

Commit

Permalink
Refactoring: all server-info-* utils have one entry point (#185)
Browse files Browse the repository at this point in the history
* Refactoring(server-info): single entry point.
* Refactoring(server-info): deleted old server-info-rate and server-info-show utils.
* Fixed(tests): modern call of server-info --show.
* Added(tests): server-info --rate call should never fail.
* Fixed(tests): server-info --rate is passing now.
* Fixed(README): new examples in README.
  • Loading branch information
strizhechenko committed Jan 2, 2018
1 parent d468d32 commit 44617eb
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Much alike lshw but designed for network processing role of server.

.. code:: yaml
# server-info show
# server-info --show
cpu:
info:
Architecture: x86_64
Expand Down Expand Up @@ -211,7 +211,7 @@ It also can rate hardware and its features in range of 1..10.

.. code:: yaml
# server-info rate
# server-info --rate
cpu:
BogoMIPS: 7
CPU MHz: 7
Expand Down
9 changes: 4 additions & 5 deletions netutils_linux_hardware/assessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Assessor(object):
info = None
avg = None

def __init__(self, data):
def __init__(self, data, args):
self.data = data
self.args = self.parse_args()
self.args = args
if self.data:
self.assess()

Expand Down Expand Up @@ -81,7 +81,7 @@ def assess_memory_size(self, size):
return self.fold({
'MemTotal': Grade.int(size.get('MemTotal'), 2 * (1024 ** 2), 16 * (1024 ** 2)),
'SwapTotal': Grade.int(size.get('SwapTotal'), 512 * 1024, 4 * (1024 ** 2)),
}, FOLDING_DEVICE)
}, FOLDING_DEVICE) if size else 1

def assess_memory(self):
meminfo = self.data.get('memory')
Expand Down Expand Up @@ -133,8 +133,7 @@ def assess_disk(self, disk):

def __assess(self, func, key):
items = self.data.get(key)
if items:
return self.fold(dict((item, func(item)) for item in items), FOLDING_SUBSYSTEM)
return self.fold(dict((item, func(item)) for item in items), FOLDING_SUBSYSTEM) if items else 1

def parse_args(self):
parser = argparse.ArgumentParser()
Expand Down
58 changes: 58 additions & 0 deletions netutils_linux_hardware/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# coding=utf-8

import argparse

from six import print_

from netutils_linux_hardware.assessor import Assessor, FOLDING_NO, FOLDING_DEVICE, FOLDING_SUBSYSTEM, FOLDING_SERVER
from netutils_linux_hardware.collect import ServerInfoCollect
from netutils_linux_hardware.reader import Reader


class ServerInfo(object):
""" Single entry point for --collect, --rate, --show """
args = None
commands = ['--collect', '--rate', '--show', '--help']

def __init__(self):
self.__parse_args()
self.__check_args()
self.main()

def __parse_args(self):
default_directory = '/tmp/netutils_server_info/'
parser = argparse.ArgumentParser()
parser.add_argument('--directory', type=str, help="Specify a data directory or a tarball",
default=default_directory)
parser.add_argument('--collect', action='store_true', help='Collect the data about the server', default=False)
parser.add_argument('--gzip', action='store_true', help="Compress the data", default=False)
parser.add_argument('--show', action='store_true', help='Shows data about the server in YAML', default=False)
parser.add_argument('--rate', action='store_true', help='Rates data about the server', default=False)
parser.add_argument('-f', '--folding', action='count', help='-f - device, -ff - subsystem, -fff - server',
default=FOLDING_NO)
parser.add_argument('--device', action='store_const', const=FOLDING_DEVICE, dest='folding',
help='Folds rates details to entire devices')
parser.add_argument('--subsystem', action='store_const', const=FOLDING_SUBSYSTEM, dest='folding',
help='Folds rates details to entire subsystems')
parser.add_argument('--server', action='store_const', const=FOLDING_SERVER, dest='folding',
help='Folds rates details to entire server')
self.args = parser.parse_args()

def __check_args(self):
""" Maybe they should be positional arguments, not options. But subparsers/groups are stupid """
assert any([self.args.collect, self.args.rate, self.args.show]), "Specify command: {0}".format(self.commands)

def tarball_directory(self):
""" Decision about and smart 'corrections' """
suffix = '.tar.gz'
if self.args.directory.endswith(suffix):
return self.args.directory, self.args.directory[:-7]
return (self.args.directory.rstrip('/') + suffix) if self.args.gzip else None, self.args.directory

def main(self):
""" Main logic """
tarball, directory = self.tarball_directory()
ServerInfoCollect(directory, tarball, self.args.collect)
if self.args.rate or self.args.show:
reader = Reader(directory)
print_(Assessor(reader.info, self.args) if self.args.rate else reader)
29 changes: 29 additions & 0 deletions netutils_linux_hardware/collect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding=utf-8

import os
import shutil


class ServerInfoCollect(object):
""" Temporary wrapper, later collection will be fully rewritten in python """

def __init__(self, directory, tarball, collect):
self.directory = directory
self.tarball = tarball
self.__collect(collect)
self.__archive()

def __collect(self, collect):
already_exists = os.path.exists(self.directory)
if already_exists and not collect:
return
if already_exists:
shutil.rmtree(self.directory)
os.makedirs(self.directory)
os.system('server-info-collect {0}'.format(self.directory))

def __archive(self):
if not self.tarball:
return
os.chdir(os.path.join(self.directory, '..'))
os.system('tar cfz {0} {1} 2>/dev/null'.format(self.tarball, self.directory))
49 changes: 49 additions & 0 deletions tests/server-info-rate
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash

set -euo pipefail

testdata=./tests/server-info-show.tests/
TMPDIR=/tmp/server-info-show/
mkdir -p $TMPDIR

COLOR_SUCCESS="\\033[1;32m"
COLOR_FAILURE="\\033[1;31m"
COLOR_WARNING="\\033[1;33m"
COLOR_NORMAL="\\033[0;39m"

run_test() {
local name="${1##*/}"
local directory="$1"
shift
local rc=0
echo "server-info --rate --directory='$directory' $*" > "$TMPDIR/output"
server-info --rate --directory="$directory" "$@" &>> "$TMPDIR/output" || rc=$?
if [ "$rc" = '0' ]; then
echo -e "$COLOR_SUCCESS# $name $*"
else
echo -e "$COLOR_FAILURE# $name $*:$COLOR_NORMAL"
cat "$TMPDIR/output"
fi
echo -ne "$COLOR_NORMAL"
return "$rc"
}


retval=0
# shellcheck disable=SC2044
for test in $(find "$testdata" -mindepth 1 -maxdepth 1 -type d); do
if [ -f "$test/expected_output" ]; then
if ! run_test "$test"; then
retval=1
break
fi
for param in --device --subsystem --server -f -ff -fff; do
if ! run_test "$test" "$param"; then
retval=1
break 2
fi
done
fi
done
rm -rf $TMPDIR
exit "$retval"
16 changes: 8 additions & 8 deletions tests/server-info-show
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ run_test() {
local name="${1##*/}"
local rc=0
echo -n "# $name "
DATADIR="$1" ./utils/server-info-show > $TMPDIR/output
if [ "${REWRITE:-}" = '1' ]; then
DATADIR="$1" ./utils/server-info-show > $testdata/$name/expected_output
server-info --show --directory="$1" > $TMPDIR/output
if [ "${REWRITE:-0}" = '1' ]; then
server-info --show --directory="$1" > "$testdata/$name/expected_output"
fi
cmp -s $1/expected_output $TMPDIR/output || rc=$?
if [ "$rc" = '0' ]; then
echo OK
else
diff -U 5 $testdata/$name/expected_output $TMPDIR/output
diff -U 5 "$testdata/$name/expected_output" "$TMPDIR/output"
echo FAIL
fi
return "$rc"
Expand All @@ -27,13 +27,13 @@ run_test() {

retval=0
# shellcheck disable=SC2044
for test in $(find $testdata -mindepth 1 -maxdepth 1 -type d); do
if [ -f $test/expected_output ]; then
if ! run_test $test; then
for test in $(find "$testdata" -mindepth 1 -maxdepth 1 -type d); do
if [ -f "$test/expected_output" ]; then
if ! run_test "$test"; then
retval=1
break
fi
fi
done
rm -rf $TMPDIR
exit $retval
exit "$retval"
17 changes: 6 additions & 11 deletions utils/server-info
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#!/bin/bash
#!/usr/bin/env python
# coding=utf-8

set -euo pipefail

if [ "$1" != 'show' ] && [ "$1" != 'rate' ]; then
echo "Usage $0 [show|rate]"
exit 1
fi
server-info-collect server
cd /root/
tar xfz server.tar.gz
cd /root/server/
server-info-"$1"
from netutils_linux_hardware.cli import ServerInfo

if __name__ == '__main__':
ServerInfo()
12 changes: 2 additions & 10 deletions utils/server-info-collect
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -euo pipefail

SYSCONFIG=/etc/sysconfig/network-scripts/
TMPDIR="/tmp/$1"
TMPDIR="$1"

prepare() {
mkdir -p $TMPDIR/ethtool/{g,i,c} $TMPDIR/network-scripts/
Expand Down Expand Up @@ -62,18 +62,10 @@ network_info() {
done
}

make_archive() {
local output="/root/$1.tar.gz"
cd /tmp/
tar cfz "$output" "$1"
rm -rf $TMPDIR
}

main() {
prepare
server_info
network_info
make_archive "$@"
}

main "$@"
main
22 changes: 0 additions & 22 deletions utils/server-info-rate

This file was deleted.

20 changes: 0 additions & 20 deletions utils/server-info-show

This file was deleted.

0 comments on commit 44617eb

Please sign in to comment.