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

Implement support for the installation/uninstallation of npm packages #5092

Merged
Merged
Changes from 7 commits
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
115 changes: 74 additions & 41 deletions deps/wazuh_testing/wazuh_testing/tools/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def get_file_content(self, host: str, file_path: str):

return result['stdout']


def apply_config(self, config_yml_path: str, dest_path: str = WAZUH_CONF, clear_files: list = None,
restart_services: list = None):
"""Apply the configuration described in the config_yml_path to the environment.
Expand Down Expand Up @@ -465,7 +464,7 @@ def download_file(self, host, url, dest_path, mode='755'):

return result

def install_package(self, host, url, system='ubuntu'):
def install_package(self, host, url, system='ubuntu', use_npm=False):
"""
Installs a package on the specified host.

Expand All @@ -480,31 +479,49 @@ def install_package(self, host, url, system='ubuntu'):

Example:
host_manager.install_package('my_host', 'http://example.com/package.deb', system='ubuntu')
# To install a package via npm:
host_manager.install_package('my_host', 'package_name', 'system_name', use_npm=True)
"""
result = False
extension = '.msi'

if system == 'windows':
if url.lower().endswith(extension):
result = self.get_host(host).ansible("win_package", f"path={url} arguments=/passive", check=False)
if use_npm:
if system == 'macos':
cmd = f"PATH=/opt/homebrew/bin:$PATH npm install -g {url}"
result = self.get_host(host).ansible("shell", cmd, check=False)
elif system == 'windows':
cmd = f"npm install -g {url}"
result = self.get_host(host).ansible("win_shell", cmd, check=False)
else:
result = self.get_host(host).ansible("win_package", f"path={url} arguments=/S", check=False)
elif system == 'ubuntu':
result = self.get_host(host).ansible("apt", f"deb={url}", check=False)
if result['changed'] and result['stderr'] == '':
result = True
elif system == 'centos':
result = self.get_host(host).ansible("yum", f"name={url} state=present "
cmd = f"npm install -g {url}"
result = self.get_host(host).ansible("shell", cmd, check=False)

logging.info(f"npm package installed result {result}")
return result

else:
result = False
extension = '.msi'

if system == 'windows':
if url.lower().endswith(extension):
result = self.get_host(host).ansible("win_package", f"path={url} arguments=/passive", check=False)
else:
result = self.get_host(host).ansible("win_package", f"path={url} arguments=/S", check=False)
elif system == 'ubuntu':
result = self.get_host(host).ansible("apt", f"deb={url}", check=False)
if result['changed'] and result['stderr'] == '':
result = True
elif system == 'centos':
result = self.get_host(host).ansible("yum", f"name={url} state=present "
'sslverify=false disable_gpg_check=True', check=False)
elif system == 'macos':
package_name = url.split('/')[-1]
result = self.get_host(host).ansible("command", f"curl -LO {url}", check=False)
cmd = f"installer -pkg {package_name} -target /"
result = self.get_host(host).ansible("command", cmd, check=False)
elif system == 'macos':
package_name = url.split('/')[-1]
result = self.get_host(host).ansible("command", f"curl -LO {url}", check=False)
cmd = f"installer -pkg {package_name} -target /"
result = self.get_host(host).ansible("command", cmd, check=False)

logging.info(f"Package installed result {result}")
logging.info(f"Package installed result {result}")

return result
return result
MARCOSD4 marked this conversation as resolved.
Show resolved Hide resolved

def get_master_ip(self):
"""
Expand Down Expand Up @@ -545,7 +562,7 @@ def get_master(self):

return master_node

def remove_package(self, host, system, package_uninstall_name=None, custom_uninstall_playbook=None):
def remove_package(self, host, system, package_uninstall_name=None, use_npm=False, custom_uninstall_playbook=None):
"""
Removes a package from the specified host.

Expand All @@ -560,6 +577,8 @@ def remove_package(self, host, system, package_uninstall_name=None, custom_unins

Example:
host_manager.remove_package('my_host', 'my_package', system='ubuntu')
# To remove a package via npm:
host_manager.remove_package('my_host', 'system_name', 'package_name', use_npm=True)
"""
logging.info(f"Removing package {package_uninstall_name} from host {host}")
logging.info(f"System: {system}")
Expand All @@ -571,24 +590,38 @@ def remove_package(self, host, system, package_uninstall_name=None, custom_unins
if custom_uninstall_playbook:
remove_operation_result = self.run_playbook(host, custom_uninstall_playbook)
elif package_uninstall_name:
if os_name == 'windows':
remove_operation_result = self.get_host(host).ansible("win_command",
f"{package_uninstall_name} /uninstall /quiet /S",
check=False)
elif os_name == 'linux':
os = self.get_host_variables(host)['os'].split('_')[0]
if os == 'centos':
remove_operation_result = self.get_host(host).ansible("yum",
f"name={package_uninstall_name} state=absent",
check=False)
elif os == 'ubuntu':
remove_operation_result = self.get_host(host).ansible("apt",
f"name={package_uninstall_name} state=absent",
check=False)
elif os_name == 'macos':
remove_operation_result = self.get_host(host).ansible("command",
f"brew uninstall {package_uninstall_name}",
check=False)
if use_npm:
if system == 'macos':
cmd = f"PATH=/opt/homebrew/bin:$PATH npm uninstall -g {package_uninstall_name}"
remove_operation_result = self.get_host(host).ansible("shell", cmd, check=False)
elif system == 'windows':
cmd = f"npm uninstall -g {package_uninstall_name}"
remove_operation_result = self.get_host(host).ansible("win_shell", cmd, check=False)
else:
cmd = f"npm uninstall -g {package_uninstall_name}"
remove_operation_result = self.get_host(host).ansible("shell", cmd, check=False)

logging.info(f"npm package removed result {remove_operation_result}")
santipadilla marked this conversation as resolved.
Show resolved Hide resolved
return remove_operation_result
MARCOSD4 marked this conversation as resolved.
Show resolved Hide resolved
else:
if os_name == 'windows':
remove_operation_result = self.get_host(host).ansible("win_command",
f"{package_uninstall_name} /uninstall /quiet /S",
check=False)
elif os_name == 'linux':
os = self.get_host_variables(host)['os'].split('_')[0]
if os == 'centos':
remove_operation_result = self.get_host(host).ansible("yum",
f"name={package_uninstall_name} state=absent",
check=False)
elif os == 'ubuntu':
remove_operation_result = self.get_host(host).ansible("apt",
f"name={package_uninstall_name} state=absent",
check=False)
elif os_name == 'macos':
remove_operation_result = self.get_host(host).ansible("command",
f"brew uninstall {package_uninstall_name}",
check=False)

logging.info(f"Package removed result {remove_operation_result}")

Expand Down Expand Up @@ -670,7 +703,7 @@ def handle_wazuh_services(self, host, operation):
if os == 'linux':
result = binary_path = f"/var/ossec/bin/wazuh-control"
elif os == 'macos':
result= binary_path = f"/Library/Ossec/bin/wazuh-control"
result = binary_path = f"/Library/Ossec/bin/wazuh-control"

result = self.get_host(host).ansible('shell', f"{binary_path} {operation}", check=False)

Expand Down
Loading