Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: trying to merge 3 entry points into one. #185

Merged
merged 6 commits into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactoring(server-info): single entry point.
  • Loading branch information
strizhechenko committed Jan 2, 2018
commit 83ed7c7c83171c9e5e8c2875594d542b88b4175f
62 changes: 23 additions & 39 deletions netutils_linux_hardware/cli.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,30 @@
# coding=utf-8

import os
import argparse

from six import print_

from netutils_linux_hardware.collect import ServerInfoCollect
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


# 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"

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.__parse_args()
self.__check_args()
self.main()

def parse_args(self):
""" Разбор аргументов для выбора следующей утилиты """
def __parse_args(self):
default_directory = '/tmp/netutils_server_info/'
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, help="Specify a data directory or a tarball")
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('--output', type=str, help="Store the data in directory")
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)
Expand All @@ -49,26 +37,22 @@ def parse_args(self):
parser.add_argument('--server', action='store_const', const=FOLDING_SERVER, dest='folding',
help='Folds rates details to entire server')
self.args = parser.parse_args()
# print(self.args)

def check_args(self):
""" Проверка аргументов """
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)
if self.args.collect:
assert self.args.output, 'Specify output directory'
if not self.args.input:
cwd = os.getcwd()
if os.path.isfile(os.path.join(cwd, 'lspci')):
self.args.input = cwd
elif os.getenv('DATADIR'):
self.args.input = os.getenv('DATADIR')
assert self.args.input, 'Specify --input/DATADIR=<path> or cd into data directory'

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):
""" Вывод необходимых данных и их сбор при необходимости """
if not self.args.input or self.args.collect:
ServerInfoCollect()
if not self.args.rate and not self.args.show:
return
reader = Reader(self.args.input)
print_(Assessor(reader.info, self.args) if self.args.rate else reader)
""" 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)
30 changes: 21 additions & 9 deletions netutils_linux_hardware/collect.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
# coding=utf-8

import argparse
import os
import shutil


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

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

def check_args(self):
pass
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 main(self):
pass
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))
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