Skip to content

Commit

Permalink
homebrew for npm installation
Browse files Browse the repository at this point in the history
  • Loading branch information
santipadilla committed Mar 27, 2024
1 parent a1efcb8 commit 8ec19ef
Showing 1 changed file with 57 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,35 +111,68 @@ def load_vulnerability_detector_configurations(host_manager):

@pytest.fixture(scope='module')
def install_npm(host_manager: HostManager):
"""Ensure npm is installed on macOS agents"""
"""Ensure npm is installed on macOS agents."""
for host in host_manager.get_group_hosts('agent'):
os_type = host_manager.get_host_variables(host).get('os')

if os_type.startswith('macos'):

nvm_check_command = "source ~/.zshrc && command -v nvm"
nvm_check_result = host_manager.get_host(host).ansible("shell", f"sudo -iu vagrant /bin/zsh -c '{nvm_check_command}'", check=False)

if nvm_check_result['stdout'] == '':

nvm_install_and_use_command = (
"sudo -iu vagrant /bin/bash -c '"
"export NVM_DIR=\"$HOME/.nvm\" && "
"curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash && "
"echo export NVM_DIR=\\\"$HOME/.nvm\\\" >> $HOME/.zshrc && "
"echo [ -s \\\"$NVM_DIR/nvm.sh\\\" ] '&&' . \\\"$NVM_DIR/nvm.sh\\\" >> $HOME/.zshrc && "
"echo [ -s \\\"$NVM_DIR/bash_completion\\\" ] '&&' . \\\"$NVM_DIR/bash_completion\\\" >> $HOME/.zshrc && "
". \\\"$NVM_DIR/nvm.sh\\\" && "
"nvm install 21 && "
"nvm use 21'"
)

logger.info(f"Installing nvm and Node.js for vagrant user on {host}")
install_result = host_manager.get_host(host).ansible("shell", nvm_install_and_use_command, check=False)
logger.info(f"nvm and Node.js installation and use result on {host}: {install_result}")

# Check if Homebrew is installed.
logger.info(f"Checking and installing Homebrew on {host}")
brew_check_command = "source /Users/vagrant/.zprofile && command -v brew"
brew_check_result = host_manager.get_host(host).ansible(
"shell",
brew_check_command,
become=True,
become_user='vagrant',
check=False
)
logger.info(f"Brew check result on {host}: {brew_check_result}")
# Install Homebrew if it is not already installed.
if brew_check_result['rc'] != 0:
logger.info("Installing Homebrew")
brew_install_command = 'NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"'
install_result = host_manager.get_host(host).ansible("shell",
brew_install_command,
become=True,
become_user='vagrant',
check=False)
logger.info(f"Homebrew installation result on {host}: {install_result}")

add_brew_to_path = """
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/vagrant/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
"""
path_result = host_manager.get_host(host).ansible("shell",
add_brew_to_path,
become=True,
become_user='vagrant',
check=False)
logger.info(f"Adding Homebrew to PATH result on {host}: {path_result}")
else:
logger.info("Homebrew is already installed.")
# Check if Node and npm is installed
logger.info(f"Checking and installing npm on {host}")
node_check_command = "PATH=/opt/homebrew/bin:$PATH && command -v node"
node_check_result = host_manager.get_host(host).ansible(
"shell",
node_check_command,
become=True,
become_user='vagrant',
check=False
)
logger.info(f"Node check result on {host}: {node_check_result}")
# Install node if it is not already installed.
if node_check_result['rc'] != 0:
logger.info("Installing Node.js and npm")
node_install_command = "PATH=/opt/homebrew/bin:$PATH && brew install node"
node_install_result = host_manager.get_host(host).ansible("shell",
node_install_command,
become=True,
become_user='vagrant',
check=False)
logger.info(f"Node.js and npm installation result on {host}: {node_install_result}")
else:
logger.info(f"nvm is already installed on {host}")
logger.info("Node.js and npm are already installed.")

@pytest.fixture(scope='module')
def setup_vulnerability_tests(host_manager: HostManager) -> Generator:
Expand Down

0 comments on commit 8ec19ef

Please sign in to comment.