Skip to content

Commit

Permalink
feat: add dependencies check to build.sh (#924)
Browse files Browse the repository at this point in the history
Added dependencies check to `build.sh` to validate before running.

Signed-off-by: Jared Burns <[email protected]>
  • Loading branch information
burnsjared0415 committed May 16, 2024
1 parent b24cd74 commit c66201f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
63 changes: 62 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,62 @@ json_path="project.json"
os_names=$(jq -r '.os[] | .name' $json_path)
os_array=($os_names)

check_command() {
cmd=$1
capitalized_cmd=$(echo $cmd | awk '{print toupper(substr($0,1,1))substr($0,2)}')
version_requirement=$(jq -r --arg cmd "$cmd" '.dependencies[] | select(.name == $cmd) | .version_requirement' $json_path)
operator=$(echo $version_requirement | cut -d " " -f 1)
required_version=$(echo $version_requirement | cut -d " " -f 2)

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

CHECKMARK="${GREEN}[✔]${NC}"
CROSSMARK="${RED}[✘]${NC}"

if command -v $cmd &>/dev/null; then
installed_version=$($cmd --version | head -n 1 | awk '{print $NF}' | sed 's/[^0-9.]*//g')
case $operator in
">=")
if echo -e "$required_version\n$installed_version" | sort -V -C; then
echo -e "$CHECKMARK $capitalized_cmd: $installed_version is installed. $required_version or later is required."
else
echo -e "$CROSSMARK $capitalized_cmd: $installed_version is installed. $required_version or later is required"
fi
;;
"==")
if [ "$installed_version" == "$required_version" ]; then
echo -e "$CHECKMARK $capitalized_cmd: $installed_version is installed. $required_version or later is required"
else
echo -e "$CROSSMARK $capitalized_cmd: $installed_version is installed. $required_version or later is required"
fi
;;
*)
echo -e "$CROSSMARK Unknown operator: $operator"
;;
esac
else
echo -e "$CROSSMARK $capitalized_cmd is not installed. $required_version or later required"
fi
}

# This function checks if the required dependencies are installed.
check_dependencies() {
check_command packer
check_command ansible
check_command terraform
check_command git
check_command gomplate
printf "\nPress \033[32mEnter\033[0m to continue."
read -r input
if [[ -z "$input" ]]; then
return
else
printf "\nPress \033[32mEnter\033[0m to continue."
fi
}

# Get the project information from the JSON file.
get_project_info() {
local field=$1
Expand Down Expand Up @@ -114,6 +170,7 @@ show_help() {
script_name=$(basename $0)
printf "Usage: $script_name [options]\n\n"
printf "Options:\n"
printf " --deps, -d, -D Check the dependencies.\n"
printf " --json, -j, -J Specify the JSON file path.\n"
printf " --show, -s, -S Display the build command used to build the image.\n"
printf " --help, -h, -H Display this help message.\n\n"
Expand Down Expand Up @@ -181,7 +238,7 @@ print_title() {
}

# This function selects the guest operating system family.
# Only `Linux`` and `Windows`` are supported presently in the JSON file.
# Only `Linux` and `Windows` are supported presently in the JSON file.
select_os() {
clear
print_title
Expand Down Expand Up @@ -664,6 +721,10 @@ while (("$#")); do
json_path="$2"
shift 2
;;
--deps | -d | -D)
check_dependencies
shift
;;
--show | -s | -S)
show_command=1
shift
Expand Down
1 change: 1 addition & 0 deletions docs/getting-started/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ You can use the following options with the script.
| Option | Short Form | Description |
| -------- | ---------- | --------------------------------------------------------------- |
| `--help` | `-h`, `-H` | Display the help. |
| `--deps` | `-d`, `-D` | Run a dependency check before running a build. |
| `--json` | `-j`, `-J` | Override the default JSON configuration file. |
| `--show` | `-s`, `-S` | Display the command that the script uses to initialize a build. |

Expand Down
13 changes: 11 additions & 2 deletions project.json
Original file line number Diff line number Diff line change
Expand Up @@ -972,13 +972,22 @@
"version_requirement": ">= 3.11.7"
},
{
"name": "ansible-core",
"name": "ansible",
"description": "Ansible Core includes the Ansible language and runtime, a set of built-in modules and command-line tools, and a framework for extending automation with collections.",
"publisher": {
"name": "Red Hat",
"url": "https://redhat.com"
},
"version_requirement": ">= 2.15.0"
"version_requirement": ">= 2.16.0"
},
{
"name": "git",
"description": "Version control system for tracking changes.",
"publisher": {
"name": "git",
"url": "https://git-scm.com/"
},
"version_requirement": ">= 2.45.0"
}
]
}

0 comments on commit c66201f

Please sign in to comment.