Files
ansible-common/scripts/setup.sh

75 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# exit immediately if a command exits with a non-zero status
set -e
VENV_DIR=.venv
# define color codes for output
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4; tput bold)
NC=$(tput sgr0) # no color
# function to check for required commands
check_command() {
command -v "$1" >/dev/null 2>&1 || {
echo -e "${RED}error: $1 is not installed. please install it and try again.${NC}" >&2;
exit 1;
}
}
# function to install the latest python
install_python() {
echo -e "${BLUE}installing the latest python...${NC}"
sudo pacman -S python --noconfirm
}
# function to create a virtual environment
create_virtualenv() {
check_command "python"
echo -e "${BLUE}creating virtual environment...${NC}"
if [ ! -d "$VENV_DIR" ]; then
python -m venv $VENV_DIR
echo -e "${GREEN}virtual environment created at $VENV_DIR${NC}"
else
echo -e "${YELLOW}virtual environment already exists at $VENV_DIR${NC}"
fi
}
# function to install python requirements
install_requirements() {
check_command "pip"
echo -e "${BLUE}installing python requirements from requirements.txt...${NC}"
source $VENV_DIR/bin/activate
pip install --upgrade pip
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
else
echo -e "${YELLOW}requirements.txt not found. skipping python requirements installation.${NC}"
fi
deactivate
}
# function to install ansible roles from requirements.yml
install_ansible_requirements() {
check_command "ansible-galaxy"
echo -e "${BLUE}installing ansible roles from requirements.yml...${NC}"
if [ -f "requirements.yml" ]; then
source $VENV_DIR/bin/activate
ansible-galaxy install -r requirements.yml
deactivate
else
echo -e "${YELLOW}requirements.yml not found. skipping ansible requirements installation.${NC}"
fi
}
# main script execution
install_python # install the latest python
create_virtualenv # create a virtual environment
install_requirements # install python requirements
install_ansible_requirements # install ansible requirements
echo -e "${GREEN}setup completed successfully!${NC}"