c9b8d88697
Moving some methods into Tools to cleanup browbeat.py + Adding Ansible task to test regather Change-Id: I4a562ae77a90528df2e7f8779895b1627c9dff11
129 lines
4.7 KiB
Python
129 lines
4.7 KiB
Python
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import PerfKit
|
|
import Rally
|
|
import Shaker
|
|
import logging
|
|
import os
|
|
import subprocess
|
|
import yaml
|
|
from pykwalify import core as pykwalify_core
|
|
from pykwalify import errors as pykwalify_errors
|
|
|
|
class Tools(object):
|
|
|
|
def __init__(self, config=None):
|
|
self.logger = logging.getLogger('browbeat.Tools')
|
|
self.config = config
|
|
return None
|
|
|
|
# Run command, return stdout as result
|
|
def run_cmd(self, cmd):
|
|
self.logger.debug("Running command : %s" % cmd)
|
|
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE)
|
|
stdout, stderr = process.communicate()
|
|
if len(stderr) > 0:
|
|
return None
|
|
else:
|
|
return stdout.strip()
|
|
|
|
# Find Command on host
|
|
def find_cmd(self, cmd):
|
|
_cmd = "which %s" % cmd
|
|
self.logger.debug('Find Command : Command : %s' % _cmd)
|
|
command = self.run_cmd(_cmd)
|
|
if command is None:
|
|
self.logger.error("Unable to find %s" % cmd)
|
|
raise Exception("Unable to find command : '%s'" % cmd)
|
|
return False
|
|
else:
|
|
return command.strip()
|
|
|
|
def create_run_dir(self, results_dir, run):
|
|
try:
|
|
os.makedirs("%s/run-%s" % (results_dir, run))
|
|
return "%s/run-%s" % (results_dir, run)
|
|
except OSError:
|
|
return False
|
|
|
|
# Create directory for results
|
|
def create_results_dir(self, results_dir, timestamp, service, scenario):
|
|
the_directory = "{}/{}/{}/{}".format(results_dir, timestamp, service, scenario)
|
|
if not os.path.isdir(the_directory):
|
|
try:
|
|
os.makedirs(the_directory)
|
|
except OSError as err:
|
|
self.logger.error("Error creating the results directory: {}".format(err))
|
|
return False
|
|
return the_directory
|
|
|
|
def _load_config(self, path):
|
|
try:
|
|
stream = open(path, 'r')
|
|
except IOError:
|
|
self.logger.error("Configuration file {} passed is missing".format(path))
|
|
exit(1)
|
|
config = yaml.load(stream)
|
|
stream.close()
|
|
self.config = config
|
|
self.validate_yaml()
|
|
return config
|
|
|
|
def validate_yaml(self):
|
|
self.logger.info("Validating the configuration file passed by the user")
|
|
stream = open("lib/validate.yaml", 'r')
|
|
schema = yaml.load(stream)
|
|
check = pykwalify_core.Core(source_data=self.config, schema_data=schema)
|
|
try:
|
|
check.validate(raise_exception=True)
|
|
self.logger.info("Validation successful")
|
|
except pykwalify_errors.SchemaError as e:
|
|
self.logger.error("Schema Validation failed")
|
|
raise Exception('File does not conform to schema: {}'.format(e))
|
|
|
|
def _run_workload_provider(self, provider):
|
|
self.logger = logging.getLogger('browbeat')
|
|
if provider == "perfkit":
|
|
perfkit = PerfKit.PerfKit(self.config)
|
|
perfkit.start_workloads()
|
|
elif provider == "rally":
|
|
rally = Rally.Rally(self.config)
|
|
rally.start_workloads()
|
|
elif provider == "shaker":
|
|
shaker = Shaker.Shaker(self.config)
|
|
shaker.run_shaker()
|
|
else:
|
|
self.logger.error("Unknown workload provider: {}".format(provider))
|
|
|
|
def check_metadata(self):
|
|
meta = self.config['elasticsearch']['metadata_files']
|
|
for _meta in meta:
|
|
if not os.path.isfile(_meta['file']):
|
|
self.logger.error("Metadata file {} is not present".format(_meta['file']))
|
|
return False
|
|
return True
|
|
|
|
def gather_metadata(self):
|
|
os.putenv("ANSIBLE_SSH_ARGS"," -F {}".format(self.config['ansible']['ssh_config']))
|
|
ansible_cmd = \
|
|
'ansible-playbook -i {} {}' \
|
|
.format(self.config['ansible']['hosts'], self.config['ansible']['metadata'])
|
|
self.run_cmd(ansible_cmd)
|
|
if not self.check_metadata():
|
|
self.logger.warning("Metadata could not be gathered")
|
|
return False
|
|
else:
|
|
self.logger.info("Metadata about cloud has been gathered")
|
|
return True
|