Skip to content

Commit

Permalink
add: Add and improve QACTL logging messages #1794
Browse files Browse the repository at this point in the history
  • Loading branch information
jmv74211 committed Aug 27, 2021
1 parent d14177c commit c39e8d1
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ def get_container(self):
return self.docker_client.containers.get(self.name)

def run(self):
DockerWrapper.LOGGER.debug(f"Running {self.name} cointainer...")
DockerWrapper.LOGGER.debug(f"Starting {self.name} cointainer")
container = self.docker_client.containers.run(image=self.image, name=self.name, ports=self.ports,
remove=self.remove, detach=self.detach, stdout=self.stdout,
stderr=self.stderr)
DockerWrapper.LOGGER.debug(f"The container {self.name} is running")
if self.ip and self.network_name:
try:
self.docker_client.networks.get(self.network_name).connect(container, ipv4_address=self.ip)
except docker.errors.APIError: #requests.exceptions.HTTPError:
except docker.errors.APIError:
exception_message = f"Invalid address {self.ip} It does not belong to any of this network's " \
'subnets. Please check if you have already set this docker network ' \
'(run `docker network ls`) and then remove it if it is created with ' \
Expand All @@ -95,8 +96,9 @@ def restart(self):
docker.errors.APIError: If the server returns an error.
"""
try:
DockerWrapper.LOGGER.debug(f"Restarting {self.name} cointainer...")
DockerWrapper.LOGGER.debug(f"Restarting {self.name} cointainer")
self.get_container().restart()
DockerWrapper.LOGGER.debug(f"The {self.name} cointainer has been restarted sucessfully")
except docker.errors.NotFound:
pass

Expand All @@ -107,8 +109,9 @@ def halt(self):
docker.errors.APIError: If the server returns an error.
"""
try:
DockerWrapper.LOGGER.debug(f"Stopping {self.name} cointainer...")
DockerWrapper.LOGGER.debug(f"Stopping {self.name} cointainer")
self.get_container().stop()
DockerWrapper.LOGGER.debug(f"The {self.name} cointainer has been stopped sucessfully")
except docker.errors.NotFound:
pass

Expand All @@ -127,14 +130,16 @@ def destroy(self, remove_image=False):
pass

try:
DockerWrapper.LOGGER.debug(f"Removing {self.name} cointainer...")
DockerWrapper.LOGGER.debug(f"Removing {self.name} cointainer")
self.get_container().remove()
DockerWrapper.LOGGER.debug(f"The {self.name} cointainer has been removed sucessfully")
except docker.errors.NotFound:
pass

if remove_image:
DockerWrapper.LOGGER.debug(f"Removing {self.image.id} docker image...")
DockerWrapper.LOGGER.debug(f"Removing {self.image.id} docker image")
self.docker_client.images.remove(image=self.image.id, force=True)
DockerWrapper.LOGGER.debug(f"The {self.image.id} image has been removed sucessfully")

def get_instance_info(self):
"""Get the parameters information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,24 @@ def __init__(self, instance_list, qa_ctl_configuration):
self.docker_network = None
self.network_address = None

QAInfraestructure.LOGGER.debug('Processing deployment configuration...')
QAInfraestructure.LOGGER.debug('Processing deployment configuration')
for host in instance_list:
for provider in instance_list[host]['provider']:
data = instance_list[host]['provider'][provider]
if not data['enabled']:
continue

if provider == 'vagrant':
QAInfraestructure.LOGGER.debug(f"Setting {data['vm_name']} vagrant instance for deployment...")
QAInfraestructure.LOGGER.debug(f"Setting {data['vm_name']} vagrant instance for deployment")
quiet_out = True if not self.qa_ctl_configuration.vagrant_output else False
vagrant_instance = VagrantWrapper(data['vagrantfile_path'], data['vagrant_box'], data['label'],
data['vm_name'], data['vm_cpu'], data['vm_memory'],
data['vm_system'], data['vm_ip'], quiet_out)
self.instances.append(vagrant_instance)
QAInfraestructure.LOGGER.debug(f"{data['vm_name']} vagrant instance has been set successfully")

elif provider == 'docker':
QAInfraestructure.LOGGER.debug(f"Setting {data['name']} docker instance for deployment...")
QAInfraestructure.LOGGER.debug(f"Setting {data['name']} docker instance for deployment")
if not self.docker_client:
self.docker_client = docker.from_env()

Expand Down Expand Up @@ -85,19 +86,23 @@ def __init__(self, instance_list, qa_ctl_configuration):
self.docker_network = self.docker_client.networks.get(self.DOCKER_NETWORK_NAME)
except docker.errors.NotFound:
QAInfraestructure.LOGGER.debug(f"Docker network {self.network_address} not found."
'Creating it...')
'Creating it')
ipam_pool = docker.types.IPAMPool(subnet=str(self.network_address),
gateway=str(self.network_address[-2]))

ipam_config = docker.types.IPAMConfig(pool_configs=[ipam_pool])
self.docker_network = self.docker_client.networks.create(self.DOCKER_NETWORK_NAME,
driver='bridge',
ipam=ipam_config)

QAInfraestructure.LOGGER.debug(f"Docker network {self.network_address} has been "
'created successfully')
docker_instance = DockerWrapper(self.docker_client, data['dockerfile_path'], data['name'], _remove,
_ports, _detach, _stdout, _stderr, ip=_ip,
network_name=self.DOCKER_NETWORK_NAME)
self.instances.append(docker_instance)
QAInfraestructure.LOGGER.debug(f"{data['vm_name']} docker container has been set successfully")

QAInfraestructure.LOGGER.debug('Deployment configuration processing has been finished successfully')

def __threads_runner(self, threads):
"""Auxiliary function to start and wait for threads
Expand All @@ -113,28 +118,32 @@ def __threads_runner(self, threads):

def run(self):
"""Execute the run method on every configured instance."""
QAInfraestructure.LOGGER.info(f"Running {len(self.instances)} instances deployment...")
QAInfraestructure.LOGGER.info(f"Starting {len(self.instances)} instances deployment")
self.__threads_runner([ThreadExecutor(instance.run) for instance in self.instances])
QAInfraestructure.LOGGER.info('The instances deployment has finished sucessfully')

def halt(self):
"""Execute the 'halt' method on every configured instance."""
QAInfraestructure.LOGGER.info(f"Stopping {len(self.instances)} instances...")
QAInfraestructure.LOGGER.info(f"Stopping {len(self.instances)} instances")
self.__threads_runner([ThreadExecutor(instance.halt) for instance in self.instances])
QAInfraestructure.LOGGER.info('The instances have been stopped sucessfully')

def restart(self):
"""Execute the 'restart' method on every configured instance."""
QAInfraestructure.LOGGER.info(f"Restarting {len(self.instances)} instances...")
QAInfraestructure.LOGGER.info(f"Restarting {len(self.instances)} instances")
self.__threads_runner([ThreadExecutor(instance.restart) for instance in self.instances])

def destroy(self):
"""Execute the 'destroy' method on every configured instance."""
QAInfraestructure.LOGGER.info(f"Destroying {len(self.instances)} instances...")
QAInfraestructure.LOGGER.info(f"Destroying {len(self.instances)} instances")
self.__threads_runner([ThreadExecutor(instance.destroy) for instance in self.instances])
QAInfraestructure.LOGGER.info(f"The instances have been destroyed sucessfully")

if self.docker_network:
QAInfraestructure.LOGGER.debug('Removing docker network...')
QAInfraestructure.LOGGER.info('Removing docker network')
try:
self.docker_network.remove()
QAInfraestructure.LOGGER.info('Docker network has been removed sucessfully')
except docker.errors.NotFound:
QAInfraestructure.LOGGER.error('Could not remove docker network')
pass
Expand All @@ -146,9 +155,10 @@ def status(self):
(dict): Contains the status for each configured instance.
"""
status = {}
QAInfraestructure.LOGGER.debug('Getting instances status...')
QAInfraestructure.LOGGER.debug('Getting instances status')
for instance in self.instances:
status[instance.get_name()] = instance.status()
QAInfraestructure.LOGGER.debug('Instances status info was obtained sucessfully')

return status

Expand All @@ -159,8 +169,9 @@ def get_instances_info(self):
(dict): Dictionary with the information for each configured instance.
"""
info = {}
QAInfraestructure.LOGGER.debug('Getting instances info...')
QAInfraestructure.LOGGER.debug('Getting instances info')
for instance in self.instances:
info[instance.get_name()] = instance.get_instance_info()
QAInfraestructure.LOGGER.debug('Instances info was obtained sucessfully')

return info
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,27 @@ def __init__(self, vagrant_root_folder, vm_box, vm_label, vm_name, vm_cpus, vm_m

def run(self):
"""Write the vagrantfile and starts the VM specified in the vagrantfile."""
VagrantWrapper.LOGGER.debug(f"Running {self.vm_name} vagrant up...")
VagrantWrapper.LOGGER.debug(f"Running {self.vm_name} vagrant up")
self.vagrant.up()
VagrantWrapper.LOGGER.debug(f"Instance {self.vm_name} has been created sucessfully")

def halt(self):
"""Stop the VM specified in the vagrantfile."""
VagrantWrapper.LOGGER.debug(f"Running {self.vm_name} vagrant halt...")
VagrantWrapper.LOGGER.debug(f"Running {self.vm_name} vagrant halt")
self.vagrant.halt()
VagrantWrapper.LOGGER.debug(f"Instance {self.vm_name} has been off sucessfully")

def restart(self):
"""Restart the VM specified in the vagrantfile."""
VagrantWrapper.LOGGER.debug(f"Running {self.vm_name} vagrant restrt...")
VagrantWrapper.LOGGER.debug(f"Running {self.vm_name} vagrant restart")
self.vagrant.restart()
VagrantWrapper.LOGGER.debug(f"Instance {self.vm_name} has been restarted sucessfully")

def destroy(self):
"""Destroy the VM specified in the vagrantfile and remove the vagrantfile."""
VagrantWrapper.LOGGER.debug(f"Running {self.vm_name} vagrant destroy...")
VagrantWrapper.LOGGER.debug(f"Running {self.vm_name} vagrant destroy")
self.vagrant.destroy()
VagrantWrapper.LOGGER.debug(f"{self.vm_name} instance has been destroyed sucessfully")
self.vagrantfile.remove_vagrantfile()
rmtree(self.box_folder)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __get_box_url(self):
try:
return box_mapping[self.box_image]
except KeyError:
Vagrantfile.LOGGER.warning('Using a non default box...')
Vagrantfile.LOGGER.warning('Using a non default box')
return None

def __str__(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ def write_playbook_to_file(self):

def delete_playbook_file(self):
if os.path.exists(self.playbook_file_path):
AnsiblePlaybook.LOGGER.debug(f"Removing {self.playbook_file_path} playbook")
os.remove(self.playbook_file_path)
AnsiblePlaybook.LOGGER.debug(f"{self.playbook_file_path} playbook file was deleted")
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ def install_dependencies(self, inventory_file_path, hosts='all'):
'args': {'chdir': self.workdir}})
ansible_tasks = [dependencies_task]
playbook_parameters = {'hosts': hosts, 'tasks_list': ansible_tasks}
QAFramework.LOGGER.debug(f"Installing python dependencies in {hosts} hosts.")
QAFramework.LOGGER.debug(f"Installing python dependencies in {hosts} hosts")

AnsibleRunner.run_ephemeral_tasks(inventory_file_path, playbook_parameters,
output=self.qa_ctl_configuration.ansible_output)
QAFramework.LOGGER.debug(f"Python dependencies have been installed successfully in {hosts} hosts")


def install_framework(self, inventory_file_path, hosts='all'):
"""Install the wazuh_testing framework to allow the execution of the tests.
Expand All @@ -61,6 +63,7 @@ def install_framework(self, inventory_file_path, hosts='all'):

AnsibleRunner.run_ephemeral_tasks(inventory_file_path, playbook_parameters,
output=self.qa_ctl_configuration.ansible_output)
QAFramework.LOGGER.debug(f"wazuh-qa framework has been installed successfully in {hosts} hosts.")

def download_qa_repository(self, inventory_file_path, hosts='all'):
"""Download the qa-framework in the specified attribute workdir.
Expand All @@ -80,3 +83,5 @@ def download_qa_repository(self, inventory_file_path, hosts='all'):

AnsibleRunner.run_ephemeral_tasks(inventory_file_path, playbook_parameters,
output=self.qa_ctl_configuration.ansible_output)
QAFramework.LOGGER.debug(f"{self.qa_branch} branch of QA repository has been downloaded successfully in "
f"{hosts} hosts")
Loading

0 comments on commit c39e8d1

Please sign in to comment.