Skip to content

Commit

Permalink
modification of npm methods
Browse files Browse the repository at this point in the history
  • Loading branch information
santipadilla committed Apr 4, 2024
1 parent 769e760 commit 5e8d1ea
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def install_package(host: str, operation_data: Dict[str, Dict], host_manager: Ho
try:
if host_os_arch in install_package_data[host_os_name]:
package_id = install_package_data[host_os_name][host_os_arch]

package_data = load_packages_metadata()[package_id]
package_url = package_data['urls'][host_os_name][host_os_arch]

Expand All @@ -181,7 +181,7 @@ def install_package(host: str, operation_data: Dict[str, Dict], host_manager: Ho
if use_npm:
host_manager.install_npm_package(host, package_url, system)
else:
host_manager.install_package(host, package_url, system, use_npm=use_npm)
host_manager.install_package(host, package_url, system)

logging.info(f"Package {package_url} installed on {host}")

Expand All @@ -199,10 +199,10 @@ def install_package(host: str, operation_data: Dict[str, Dict], host_manager: Ho

check_vulnerability_alerts(results, operation_data['check'], current_datetime, host_manager, host,
package_data, operation='install')

else:
logging.error(f"Error: Package for {host_os_name} and {host_os_arch} not found")

except Exception as e:
logging.critical(f"Error searching package: {e}")

Expand Down Expand Up @@ -266,7 +266,7 @@ def remove_package(host: str, operation_data: Dict[str, Dict], host_manager: Hos
if use_npm:
host_manager.remove_npm_package(host, system, package_uninstall_name=uninstall_name)
else:
host_manager.remove_package(host, system, package_uninstall_name=uninstall_name, use_npm=use_npm)
host_manager.remove_package(host, system, package_uninstall_name=uninstall_name)
elif 'uninstall_custom_playbook' in package_data:
host_manager.remove_package(host, system,
custom_uninstall_playbook=package_data['uninstall_custom_playbook'])
Expand Down Expand Up @@ -343,7 +343,7 @@ def update_package(host: str, operation_data: Dict[str, Dict], host_manager: Hos
try:
if host_os_arch in install_package_data_to[host_os_name]:
package_id_to = install_package_data_to[host_os_name][host_os_arch]

package_data_from = load_packages_metadata()[package_id_from]
package_data_to = load_packages_metadata()[package_id_to]

Expand All @@ -359,7 +359,7 @@ def update_package(host: str, operation_data: Dict[str, Dict], host_manager: Hos
if use_npm:
host_manager.install_npm_package(host, package_url_to, system)
else:
host_manager.install_package(host, package_url_to, system, use_npm=use_npm)
host_manager.install_package(host, package_url_to, system)

logging.info(f"Package {package_url_to} installed on {host}")

Expand All @@ -374,7 +374,7 @@ def update_package(host: str, operation_data: Dict[str, Dict], host_manager: Hos

check_vulnerability_alerts(results, operation_data['check'], current_datetime, host_manager, host,
{'from': package_data_from, 'to': package_data_to}, operation='update')

else:
logging.error(f"Error: Package for {host_os_name} and {host_os_arch} not found")

Expand Down
118 changes: 40 additions & 78 deletions deps/wazuh_testing/wazuh_testing/tools/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def download_file(self, host, url, dest_path, mode='755'):

return result

def install_package(self, host, url, system='ubuntu', use_npm=False):
def install_package(self, host, url, system='ubuntu'):
"""
Installs a package on the specified host.
Expand All @@ -474,54 +474,35 @@ def install_package(self, host, url, system='ubuntu', use_npm=False):
url (str): The URL or name of the package to be installed.
system (str, optional): The operating system type. Defaults to 'ubuntu'.
Supported values: 'windows', 'ubuntu', 'centos'.
use_npm (bool): Determinates whether to use npm for the installation.
Returns:
Dict: Testinfra Ansible Response of the operation
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)
"""
if use_npm:
# Define the npm install command
cmd = f"npm install -g {url}"
result = False
extension = '.msi'

if system == 'macos':
cmd = f"PATH=/usr/local/bin:$PATH {cmd}"
shell_type = "shell"
elif system == 'windows':
shell_type = "win_shell"
if system == 'windows':
if url.lower().endswith(extension):
result = self.get_host(host).ansible("win_package", f"path={url} arguments=/passive", check=False)
else:
shell_type = "shell"

# Execute the command and log the result
result = self.get_host(host).ansible(shell_type, cmd, check=False)
logging.info(f"npm package installed result {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 "
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

Expand Down Expand Up @@ -598,7 +579,7 @@ def get_master(self):

return master_node

def remove_package(self, host, system, package_uninstall_name=None, use_npm=False, custom_uninstall_playbook=None):
def remove_package(self, host, system, package_uninstall_name=None, custom_uninstall_playbook=None):
"""
Removes a package from the specified host.
Expand All @@ -607,15 +588,12 @@ def remove_package(self, host, system, package_uninstall_name=None, use_npm=Fals
package_name (str): The name of the package to be removed.
system (str): The operating system type.
Supported values: 'windows', 'ubuntu', 'centos'.
use_npm (bool): Determinates whether to use npm for the uninstallation.
Returns:
Dict: Testinfra Ansible Response of the operation
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 @@ -627,42 +605,26 @@ def remove_package(self, host, system, package_uninstall_name=None, use_npm=Fals
if custom_uninstall_playbook:
remove_operation_result = self.run_playbook(host, custom_uninstall_playbook)
elif package_uninstall_name:
if use_npm:
# Define the npm uninstall command
cmd = f"npm uninstall -g {package_uninstall_name}"

if system == 'macos':
cmd = f"PATH=/usr/local/bin:$PATH {cmd}"
shell_type = "shell"
elif system == 'windows':
shell_type = "win_shell"
else:
shell_type = "shell"

# Execute the command and log the result
remove_operation_result = self.get_host(host).ansible(shell_type, cmd, check=False)
logging.info(f"npm package removed result {remove_operation_result}")
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}")
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}")

return remove_operation_result

Expand Down

0 comments on commit 5e8d1ea

Please sign in to comment.