forked from strizhechenko/netutils-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring: all server-info-* utils have one entry point (strizheche…
…nko#185) * 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
1 parent
d468d32
commit 44617eb
Showing
10 changed files
with
158 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.