diff --git a/quick_install.sh b/quick_install.sh new file mode 100755 index 00000000..7ab8693e --- /dev/null +++ b/quick_install.sh @@ -0,0 +1,256 @@ +#!/bin/bash + +function quick_install() +{ +local force +force=$1 +if [[ ( -n $force ) && ( ( $force == "--force" ) || ( $force == "--F" ) ) ]]; then + rm -rf "$HOME/.besman" +elif [[ ( -n $force ) && ( ( $force != "--force" ) || ( $force != "--F" ) ) ]]; then + echo "Usage: ./quick_install [--force|-F]" + echo "--force | -F : Removes the existing installation of BeSman" + return +fi +BESMAN_PLATFORM=$(uname) +export BESMAN_SERVICE="https://raw.githubusercontent.com" + + +# BESMAN_DIST_BRANCH=${BESMAN_DIST_BRANCH:-REL-${BESMAN_VERSION}} + +BESMAN_NAMESPACE="Be-Secure" +BESMAN_VERSION="current_branch_$(git branch --show-current)" +BESMAN_ENV_REPOS="$BESMAN_NAMESPACE/besecure-ce-env-repo" + +if [ -z "$BESMAN_DIR" ]; then + export BESMAN_DIR="$HOME/.besman" +fi + +# variables +besman_bin_folder="${BESMAN_DIR}/bin" +besman_src_folder="${BESMAN_DIR}/src" +besman_tmp_folder="${BESMAN_DIR}/tmp" +besman_stage_folder="${besman_tmp_folder}/stage" +besman_zip_file="${besman_tmp_folder}/besman-${BESMAN_VERSION}.zip" +besman_env_folder="${BESMAN_DIR}/envs" +besman_stage_folder="${besman_tmp_folder}/stage" +besman_etc_folder="${BESMAN_DIR}/etc" +besman_var_folder="${BESMAN_DIR}/var" +besman_config_file="${besman_etc_folder}/config" +besman_user_config_file="${besman_etc_folder}/user-config.cfg" +besman_bash_profile="${HOME}/.bash_profile" +besman_profile="${HOME}/.profile" +besman_bashrc="${HOME}/.bashrc" +besman_zshrc="${HOME}/.zshrc" + +besman_init_snippet=$( cat << EOF +#THIS MUST BE AT THE END OF THE FILE FOR BESMAN TO WORK!!! +export BESMAN_DIR="$BESMAN_DIR" +[[ -s "${BESMAN_DIR}/bin/besman-init.sh" ]] && source "${BESMAN_DIR}/bin/besman-init.sh" +EOF +) + +# OS specific support (must be 'true' or 'false'). +cygwin=false; +darwin=false; +solaris=false; +freebsd=false; +case "$(uname)" in + CYGWIN*) + cygwin=true + ;; + Darwin*) + darwin=true + ;; + SunOS*) + solaris=true + ;; + FreeBSD*) + freebsd=true +esac + + +# Sanity checks + +echo "Looking for a previous installation of BeSman..." +if [ -d "$BESMAN_DIR/bin" ]; then + echo "BeSman found." + echo "" + echo "======================================================================================================" + echo " You already have BeSman installed." + echo " BeSman was found at:" + echo "" + echo " ${BESMAN_DIR}" + echo "" + echo " Please consider running the following if you need to upgrade." + echo "" + echo " $ bes selfupdate force" + echo "" + echo "======================================================================================================" + echo "" + exit 0 +fi +echo ' BBBBBBBBBBBBBBBBB SSSSSSSSSSSSSSS ' +echo ' B::::::::::::::::B SS:::::::::::::::S ' +echo ' B::::::BBBBBB:::::B S:::::SSSSSS::::::S ' +echo ' BB:::::B B:::::B S:::::S SSSSSSS ' +echo ' B::::B B:::::B eeeeeeeeeeee S:::::S mmmmmmm mmmmmmm aaaaaaaaaaaaa nnnn nnnnnnnn ' +echo ' B::::B B:::::B ee::::::::::::ee S:::::S mm:::::::m m:::::::mm a::::::::::::a n:::nn::::::::nn ' +echo ' B::::BBBBBB:::::B e::::::eeeee:::::eeS::::SSSS m::::::::::mm::::::::::m aaaaaaaaa:::::an::::::::::::::nn ' +echo ' B:::::::::::::BB e::::::e e:::::e SS::::::SSSSS m::::::::::::::::::::::m a::::ann:::::::::::::::n ' +echo ' B::::BBBBBB:::::B e:::::::eeeee::::::e SSS::::::::SS m:::::mmm::::::mmm:::::m aaaaaaa:::::a n:::::nnnn:::::n ' +echo ' B::::B B:::::Be:::::::::::::::::e SSSSSS::::S m::::m m::::m m::::m aa::::::::::::a n::::n n::::n ' +echo ' B::::B B:::::Be::::::eeeeeeeeeee S:::::Sm::::m m::::m m::::m a::::aaaa::::::a n::::n n::::n ' +echo ' B::::B B:::::Be:::::::e S:::::Sm::::m m::::m m::::ma::::a a:::::a n::::n n::::n ' +echo ' BB:::::BBBBBB::::::Be::::::::e SSSSSSS S:::::Sm::::m m::::m m::::ma::::a a:::::a n::::n n::::n ' +echo ' B:::::::::::::::::B e::::::::eeeeeeee S::::::SSSSSS:::::Sm::::m m::::m m::::ma:::::aaaa::::::a n::::n n::::n ' +echo ' B::::::::::::::::B ee:::::::::::::e S:::::::::::::::SS m::::m m::::m m::::m a::::::::::aa:::a n::::n n::::n ' +echo ' BBBBBBBBBBBBBBBBB eeeeeeeeeeeeee SSSSSSSSSSSSSSS mmmmmm mmmmmm mmmmmm aaaaaaaaaa aaaa nnnnnn nnnnnn ' + +if [[ "$solaris" == true ]]; then + echo "Looking for gsed..." + if [ -z "$(which gsed)" ]; then + echo "Not found." + echo "" + echo "======================================================================================================" + echo " Please install gsed on your solaris system." + echo "" + echo " BeSman uses gsed extensively." + echo "" + echo " Restart after installing gsed." + echo "======================================================================================================" + echo "" + exit 1 + fi +else + echo "Looking for sed..." + if [ -z "$(which sed)" ]; then + echo "Not found." + echo "" + echo "======================================================================================================" + echo " Please install sed on your system using your favourite package manager." + echo "" + echo " Restart after installing sed." + echo "======================================================================================================" + echo "" + exit 1 + fi +fi + +if [[ -z $(which ansible) ]]; then + echo "Installing ansible" + sudo apt-add-repository -y ppa:ansible/ansible + sudo apt update + sudo apt install ansible -y +fi + +if [[ -z $(which gh) ]]; then + echo "Installing GitHub Cli" + type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y) + curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg + sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg + echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null + sudo apt update + sudo apt install gh -y + +fi + + +echo "Installing BeSMAN scripts..." + + +# Create directory structure + +echo "Create distribution directories..." +mkdir -p "$besman_bin_folder" +mkdir -p "$besman_src_folder" +mkdir -p "$besman_tmp_folder" +mkdir -p "$besman_stage_folder" +mkdir -p "$besman_env_folder" +mkdir -p "$besman_etc_folder" +mkdir -p "$besman_var_folder" + + + +echo "Prime the config file..." +echo "config selfupdate/debug_mode = true" + +touch "$besman_config_file" +{ + echo "besman_auto_answer=false" + echo "besman_auto_selfupdate=false" + echo "besman_insecure_ssl=false" + echo "besman_curl_connect_timeout=7" + echo "besman_curl_max_time=10" + echo "besman_beta_channel=false" + echo "besman_debug_mode=true" + echo "besman_colour_enable=true" +} >> "$besman_config_file" + +echo "Setting up user configs" +touch "$besman_user_config_file" +{ + echo "BESMAN_VERSION=$BESMAN_VERSION" + echo "BESMAN_USER_NAMESPACE=" + echo "BESMAN_ENV_ROOT=$HOME/BeSman_env" + echo "BESMAN_NAMESPACE=$BESMAN_NAMESPACE" + echo "BESMAN_INTERACTIVE_USER_MODE=true" + echo "BESMAN_DIR=$HOME/.besman" + echo "BESMAN_ENV_REPOS=$BESMAN_ENV_REPOS" + echo "BESMAN_PLAYBOOK_REPO=besecure-ce-playbook-repo" + echo "BESMAN_GH_TOKEN=" + echo "BESMAN_OFFLINE_MODE=true" + echo "BESMAN_LOCAL_ENV=False" + echo "BESMAN_LIGHT_MODE=False" +} >> "$besman_user_config_file" + +cp ./src/main/bash/besman-* "$besman_src_folder" +cp ./src/main/bash/commands/besman-* "$besman_src_folder" +mv "$besman_src_folder/besman-init.sh" "$besman_bin_folder" + +touch "$besman_var_folder/list.txt" + +echo "Set version to $BESMAN_VERSION ..." +echo "$BESMAN_VERSION" > "${BESMAN_DIR}/var/version.txt" + +if [[ $darwin == true ]]; then + touch "$besman_bash_profile" + echo "Attempt update of login bash profile on OSX..." + if [[ -z $(grep 'besman-init.sh' "$besman_bash_profile") ]]; then + echo -e "\n$besman_init_snippet" >> "$besman_bash_profile" + echo "Added besman init snippet to $besman_bash_profile" + fi +else + echo "Attempt update of interactive bash profile on regular UNIX..." + touch "${besman_bashrc}" + if [[ -z $(grep 'besman-init.sh' "$besman_bashrc") ]]; then + echo -e "\n$besman_init_snippet" >> "$besman_bashrc" + echo "Added besman init snippet to $besman_bashrc" + fi +if [ -f "${besman_profile}" ]; then + if [[ -z $(grep 'oah-init.sh' "${besman_profile}") ]]; then + echo -e "\n${besman_init_snippet}" >> "${besman_profile}" + echo "Updated existing ${besman_profile}" + fi + fi +fi +echo "Attempt update of zsh profile..." +touch "$besman_zshrc" +if [[ -z $(grep 'besman-init.sh' "$besman_zshrc") ]]; then + echo -e "\n$besman_init_snippet" >> "$besman_zshrc" + echo "Updated existing ${besman_zshrc}" +fi +source "${BESMAN_DIR}/bin/besman-init.sh" + +echo -e "\n\n\nAll done!\n\n" + +echo "Please open a new terminal, or run the following in the existing one:" +echo "" +echo " source \"${BESMAN_DIR}/bin/besman-init.sh\"" + +echo " " +echo "Then issue the following command:" +echo "" +echo " bes help" +echo "" +} +quick_install "$1" \ No newline at end of file diff --git a/scripts/tmpl/get.besman.io.tmpl b/scripts/tmpl/get.besman.io.tmpl index b18c1f2b..dfcefa15 100755 --- a/scripts/tmpl/get.besman.io.tmpl +++ b/scripts/tmpl/get.besman.io.tmpl @@ -67,22 +67,21 @@ case "$(uname)" in esac -echo "Looking for figlet..." -if [ -z $(which figlet) ]; then - echo "Not found." - echo "======================================================================================================" - echo " so installing figlet on your system " - sudo apt install -y figlet - #echo "" - #echo " Execute after installing figlet." - #echo "======================================================================================================" - #echo "" - #exit 1 -fi +# echo "Looking for figlet..." +# if [ -z $(which figlet) ]; then +# echo "Not found." +# echo "======================================================================================================" +# echo " so installing figlet on your system " +# sudo apt install -y figlet +# #echo "" +# #echo " Execute after installing figlet." +# #echo "======================================================================================================" +# #echo "" +# #exit 1 +# fi + + -figlet Setting up BeSman >> besman.txt -cat besman.txt -rm besman.txt # Sanity checks echo "Looking for a previous installation of BeSman..." @@ -103,7 +102,22 @@ if [ -d $BESMAN_DIR/bin ]; then echo "" exit 0 fi - +echo ' BBBBBBBBBBBBBBBBB SSSSSSSSSSSSSSS ' +echo ' B::::::::::::::::B SS:::::::::::::::S ' +echo ' B::::::BBBBBB:::::B S:::::SSSSSS::::::S ' +echo ' BB:::::B B:::::B S:::::S SSSSSSS ' +echo ' B::::B B:::::B eeeeeeeeeeee S:::::S mmmmmmm mmmmmmm aaaaaaaaaaaaa nnnn nnnnnnnn ' +echo ' B::::B B:::::B ee::::::::::::ee S:::::S mm:::::::m m:::::::mm a::::::::::::a n:::nn::::::::nn ' +echo ' B::::BBBBBB:::::B e::::::eeeee:::::eeS::::SSSS m::::::::::mm::::::::::m aaaaaaaaa:::::an::::::::::::::nn ' +echo ' B:::::::::::::BB e::::::e e:::::e SS::::::SSSSS m::::::::::::::::::::::m a::::ann:::::::::::::::n ' +echo ' B::::BBBBBB:::::B e:::::::eeeee::::::e SSS::::::::SS m:::::mmm::::::mmm:::::m aaaaaaa:::::a n:::::nnnn:::::n ' +echo ' B::::B B:::::Be:::::::::::::::::e SSSSSS::::S m::::m m::::m m::::m aa::::::::::::a n::::n n::::n ' +echo ' B::::B B:::::Be::::::eeeeeeeeeee S:::::Sm::::m m::::m m::::m a::::aaaa::::::a n::::n n::::n ' +echo ' B::::B B:::::Be:::::::e S:::::Sm::::m m::::m m::::ma::::a a:::::a n::::n n::::n ' +echo ' BB:::::BBBBBB::::::Be::::::::e SSSSSSS S:::::Sm::::m m::::m m::::ma::::a a:::::a n::::n n::::n ' +echo ' B:::::::::::::::::B e::::::::eeeeeeee S::::::SSSSSS:::::Sm::::m m::::m m::::ma:::::aaaa::::::a n::::n n::::n ' +echo ' B::::::::::::::::B ee:::::::::::::e S:::::::::::::::SS m::::m m::::m m::::m a::::::::::aa:::a n::::n n::::n ' +echo ' BBBBBBBBBBBBBBBBB eeeeeeeeeeeeee SSSSSSSSSSSSSSS mmmmmm mmmmmm mmmmmm aaaaaaaaaa aaaa nnnnnn nnnnnn ' echo "Looking for unzip..." if [ -z $(which unzip) ]; then echo "Not found." @@ -147,6 +161,24 @@ if [ -z $(which curl) ]; then #exit 1 fi +if [[ -z $(which ansible) ]]; then + echo "Installing ansible" + sudo apt-add-repository -y ppa:ansible/ansible + sudo apt update + sudo apt install ansible -y +fi + +if [[ -z $(which gh) ]]; then + echo "Installing GitHub Cli" + type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y) + curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg + sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg + echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null + sudo apt update + sudo apt install gh -y + +fi + if [[ "$solaris" == true ]]; then echo "Looking for gsed..." if [ -z $(which gsed) ]; then @@ -198,27 +230,33 @@ echo "Prime the config file..." echo "config selfupdate/debug_mode = true" touch "$besman_config_file" -echo "besman_auto_answer=false" >> "$besman_config_file" -echo "besman_auto_selfupdate=false" >> "$besman_config_file" -echo "besman_insecure_ssl=false" >> "$besman_config_file" -echo "besman_curl_connect_timeout=7" >> "$besman_config_file" -echo "besman_curl_max_time=10" >> "$besman_config_file" -echo "besman_beta_channel=false" >> "$besman_config_file" -echo "besman_debug_mode=true" >> "$besman_config_file" -echo "besman_colour_enable=true" >> "$besman_config_file" +{ + echo "besman_auto_answer=false" + echo "besman_auto_selfupdate=false" + echo "besman_insecure_ssl=false" + echo "besman_curl_connect_timeout=7" + echo "besman_curl_max_time=10" + echo "besman_beta_channel=false" + echo "besman_debug_mode=true" + echo "besman_colour_enable=true" +} >> "$besman_config_file" echo "Setting up user configs" touch "$besman_user_config_file" -echo "BESMAN_VERSION=$BESMAN_VERSION" >> "$besman_user_config_file" -echo "BESMAN_USER_NAMESPACE=" >> "$besman_user_config_file" -echo "BESMAN_ENV_ROOT=$HOME/BeSman_env" >> "$besman_user_config_file" -echo "BESMAN_NAMESPACE=$BESMAN_NAMESPACE" >> "$besman_user_config_file" -echo "BESMAN_INTERACTIVE_USER_MODE=true" >> "$besman_user_config_file" -echo "BESMAN_DIR=$HOME/.besman" >> "$besman_user_config_file" -echo "BESMAN_ENV_REPOS=$BESMAN_ENV_REPOS" >> "$besman_user_config_file" -echo "BESMAN_PLAYBOOK_REPO=besecure-ce-playbook-repo" >> "$besman_user_config_file" -echo "BESMAN_GH_TOKEN=" >> "$besman_user_config_file" -echo "BESMAN_PLAYBOOK_DIR=$BESMAN_DIR/playbook" >> "$besman_user_config_file" +{ + echo "BESMAN_VERSION=$BESMAN_VERSION" + echo "BESMAN_USER_NAMESPACE=" + echo "BESMAN_ENV_ROOT=$HOME/BeSman_env" + echo "BESMAN_NAMESPACE=$BESMAN_NAMESPACE" + echo "BESMAN_INTERACTIVE_USER_MODE=true" + echo "BESMAN_DIR=$HOME/.besman" + echo "BESMAN_ENV_REPOS=$BESMAN_ENV_REPOS" + echo "BESMAN_PLAYBOOK_REPO=besecure-ce-playbook-repo" + echo "BESMAN_GH_TOKEN=" + echo "BESMAN_OFFLINE_MODE=true" + echo "BESMAN_LOCAL_ENV=False" + echo "BESMAN_LIGHT_MODE=False" +} >> "$besman_user_config_file" echo "Download script archive..." # once move to besman namespace needs to update besman-latest.zip @@ -250,15 +288,7 @@ unzip -qo "$besman_zip_file" -d "$besman_stage_folder" echo "Install scripts..." -curl -sL "https://raw.githubusercontent.com/${BESMAN_NAMESPACE}/BeSman/master/dist/environments" > tmp.txt -#echo "BESman" > tmp.txt -sed -i 's/,/ /g' tmp.txt -environments=$( + $ export BESMAN_ANSIBLE_ROLES_PATH=$HOME/ EOF return 1 @@ -42,7 +42,7 @@ function __besman_check_for_trigger_playbook { local playbook=$1 if [[ ! -f $playbook ]]; then - echo $playbook + echo "$playbook" return 1 else return 0 @@ -52,74 +52,104 @@ function __besman_check_for_trigger_playbook function __besman_update_requirements_file { - local roles namespace repo_name github_url + local roles namespace repo_name github_url role github_url=https://github.com - roles=$(echo $BESMAN_ANSIBLE_ROLES | sed 's/:/ /g') - [[ ! -f $BESMAN_ANSIBLE_ROLE_PATH/requirements.yml ]] && touch $BESMAN_ANSIBLE_ROLE_PATH/requirements.yml && echo "---" >> $BESMAN_ANSIBLE_ROLE_PATH/requirements.yml - for i in ${roles[@]}; do - namespace=$(echo $i | cut -d "/" -f 1) - repo_name=$(echo $i | cut -d "/" -f 2) - if ! cat $BESMAN_ANSIBLE_ROLE_PATH/requirements.yml | grep -wq "$github_url/$namespace/$repo_name" + readarray -d ',' -t roles <<< "$BESMAN_ANSIBLE_ROLES" + [[ ! -d $BESMAN_ANSIBLE_ROLES_PATH ]] && mkdir -p "$BESMAN_ANSIBLE_ROLES_PATH" + [[ ! -f $BESMAN_DIR/tmp/$BESMAN_OSSP/requirements.yaml ]] && touch "$BESMAN_DIR/tmp/$BESMAN_OSSP/requirements.yaml" && echo "---" >> "$BESMAN_DIR/tmp/$BESMAN_OSSP/requirements.yaml" + for role in "${roles[@]}"; do + namespace=$(echo "$role" | cut -d "/" -f 1) + repo_name=$(echo "$role" | cut -d "/" -f 2) + if ! grep -wq "$github_url/$namespace/$repo_name" "$BESMAN_DIR/tmp/$BESMAN_OSSP/requirements.yaml" then - echo "- src: $github_url/$namespace/$repo_name" >> $BESMAN_ANSIBLE_ROLE_PATH/requirements.yml + echo "- src: $github_url/$namespace/$repo_name" >> "$BESMAN_DIR/tmp/$BESMAN_OSSP/requirements.yaml" continue else __besman_echo_no_colour "Ignoring role $github_url/$namespace/$repo_name as it is already present in requirements.yml" fi done - unset roles namespace repo_name github_url + unset roles namespace repo_name github_url role } function __besman_run_ansible_playbook_with_become { local playbook=$1 __besman_echo_white "Running $playbook" - ansible-playbook $playbook --ask-become-pass + ansible-playbook "$playbook" --ask-become-pass unset playbook } function __besman_run_ansible_playbook_extra_vars { local playbook vars + if [[ ( -n $BESMAN_DISPLAY_SKIPPED_ANSIBLE_HOSTS ) && ($BESMAN_DISPLAY_SKIPPED_ANSIBLE_HOSTS == false) ]]; then + export ANSIBLE_DISPLAY_SKIPPED_HOSTS=false + __besman_echo_yellow "Not displaying skipped hosts" + __besman_echo_yellow "If you wish to display them run the below command" + __besman_echo_white "export BESMAN_DISPLAY_SKIPPED_ANSIBLE_HOSTS=true" + elif [[ ( -n $BESMAN_DISPLAY_SKIPPED_ANSIBLE_HOSTS ) && ($BESMAN_DISPLAY_SKIPPED_ANSIBLE_HOSTS == true) ]]; then + export BESMAN_DISPLAY_SKIPPED_ANSIBLE_HOSTS=true + __besman_echo_yellow "Displaying skipped hosts" + __besman_echo_yellow "If you wish to not display them run the below command" + __besman_echo_white "export BESMAN_DISPLAY_SKIPPED_ANSIBLE_HOSTS=false" + fi playbook=$1 [[ ! -f $playbook ]] && __besman_echo_red "$playbook not found" && unset playbook vars && return 1 vars=$2 __besman_echo_white "Running $playbook with --extra-vars $vars" - ansible-playbook $playbook --ask-become-pass --extra-vars "$vars" + ansible-playbook "$playbook" --ask-become-pass --extra-vars "$vars" [[ "$?" -ne 0 ]] && return 1 unset playbook vars } +function __besman_create_roles_config_file() +{ + local env_config_file roles_config_file + [[ ! -d $BESMAN_OSS_TRIGGER_PLAYBOOK_PATH ]] && mkdir -p "$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH" + env_config_file=$BESMAN_ENV_CONFIG_FILE_PATH # BESMAN_ENV_CONFIG_FILE_PATH is set from env-helpers:__besman_source_env_params() + roles_config_file=$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/$BESMAN_OSSP-roles-config.yml + touch "$roles_config_file" + echo "---" > "$roles_config_file" + while read -r line; do + [[ "$line" == "---" ]] && continue + if echo "$line" | grep -qe "^BESMAN_" # For only exporting env variables starting with BESMAN_ + then + continue + else + echo "$line" >> "$roles_config_file" + fi + done < "$env_config_file" +} function __besman_ansible_galaxy_install_roles_from_requirements { - __besman_echo_white "Installing ansible roles from $BESMAN_ANSIBLE_ROLE_PATH/requirements.yml under $BESMAN_ANSIBLE_ROLE_PATH" - ansible-galaxy install -r $BESMAN_ANSIBLE_ROLE_PATH/requirements.yml -p $BESMAN_ANSIBLE_ROLE_PATH + __besman_echo_white "Installing ansible roles from $BESMAN_DIR/tmp/$BESMAN_OSSP/requirements.yaml under $BESMAN_ANSIBLE_ROLES_PATH" + ansible-galaxy install -r "$BESMAN_DIR/tmp/$BESMAN_OSSP/requirements.yaml" -p "$BESMAN_ANSIBLE_ROLES_PATH" } function __besman_create_ansible_playbook { - local playbook=$1 - touch $playbook - cat <> $playbook + + cat <> "$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/$BESMAN_OSS_TRIGGER_PLAYBOOK" --- - name: Triggering roles hosts: localhost || all + vars_files: + - ./$BESMAN_OSSP-roles-config.yml vars: - home_dir: lookup('env','HOME') - oah_command: '{{ bes_command }}' - - role_path: '{{ role_path }}' roles: EOF - roles=$(echo $BESMAN_ANSIBLE_ROLES | sed 's/:/ /g') - for i in ${roles[@]}; do - repo_name=$(echo $i | cut -d "/" -f 2) - [[ -z $BESMAN_ANSIBLE_ROLE_PATH/$repo_name ]] && __besman_echo_white "$repo_name not found" && continue - echo " - '{{ role_path }}/$repo_name'" >> $playbook + readarray -d ',' -t roles <<< "$BESMAN_ANSIBLE_ROLES" + for i in "${roles[@]}"; do + repo_name=$(echo "$i" | cut -d "/" -f 2) + [[ ! -d $BESMAN_ANSIBLE_ROLES_PATH/$repo_name ]] && __besman_echo_white "$repo_name not found" && continue + echo " - $repo_name" >> "$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/$BESMAN_OSS_TRIGGER_PLAYBOOK" done } diff --git a/src/main/bash/besman-env-helpers.sh b/src/main/bash/besman-env-helpers.sh index e65ba949..e9f09647 100644 --- a/src/main/bash/besman-env-helpers.sh +++ b/src/main/bash/besman-env-helpers.sh @@ -1,81 +1,87 @@ #!/usr/bin/env bash -function __besman_source_env_parameters +function __besman_source_env_params { - - local environment=$1 - - local env_config="$HOME/besman-$environment.yml" - - local local_vars_flag env_vars_flag key value line tmp_var_file - - tmp_var_file=$HOME/tmp_var_file.sh - - sed -i '/^[[:space:]]*$/d' $env_config - - local_vars_flag=false - env_vars_flag=false - [[ -f $tmp_var_file ]] && rm $tmp_var_file - while read line; - do - key="" - value="" - if echo $line | grep -qw "local_vars:" - then - - local_vars_flag=true - continue + local key value line tmp_var_file environment env_config + environment=$1 + env_config="besman-$environment-config.yaml" - elif echo $line | grep -qw "env_vars:" - then - local_vars_flag=false - env_vars_flag=true - continue - - elif echo $line | grep -qw "\-\-\-" - then - continue + # checks whether user configuration exists + if [[ -f $HOME/$env_config ]]; then + + export BESMAN_ENV_CONFIG_FILE_PATH=$HOME/$env_config + __besman_echo_yellow "Sourcing user config parameters from $BESMAN_ENV_CONFIG_FILE_PATH" - fi + elif [[ -f $BESMAN_DIR/tmp/$env_config ]]; then - key=$(echo $line | sed "s/ //g" | cut -d ":" -f 1 | cut -d "-" -f 2) - value=$(echo $line | sed "s/ //g" | cut -d ":" -f 2) + export BESMAN_ENV_CONFIG_FILE_PATH=$BESMAN_DIR/tmp/$env_config + __besman_echo_yellow "Sourcing default config parameters from $BESMAN_ENV_CONFIG_FILE_PATH" + fi - if [[ $local_vars_flag == true ]]; then - echo "$key=$value" >> $tmp_var_file + # creating a temporary shell script file for exporting variables from config file. + # Otherwise the '$' variables inside the config file wont be replaced with the actual value. + tmp_var_file="$BESMAN_DIR/tmp/$environment-config.sh" + touch "$tmp_var_file" + echo "#!/bin/bash" >> "$tmp_var_file" + while read -r line; + do + + [[ $line == "---" ]] && continue # To skip the --- from starting of yaml file + if echo "$line" | grep -qe "^BESMAN_"; then # Check to export only environment variables starting with BESMAN_ + + key=$(echo "$line" | cut -d ":" -f 1) # For getting the var name + value=$(echo "$line" | cut -d ":" -f 2 | cut -d " " -f 2) # For getting the value. + unset "$key" + echo "export $key=$value" >> "$tmp_var_file" + else + continue + fi + + done < "$BESMAN_ENV_CONFIG_FILE_PATH" - elif [[ $env_vars_flag == true ]]; then - - echo "export $key=$value" >> $tmp_var_file - - elif [[ (( $local_vars_flag == false )) && (( $env_vars_flag == false )) ]]; then - - echo "error" - return 1 - - fi + source "$tmp_var_file" + [[ -f $tmp_var_file ]] && rm "$tmp_var_file" - done < $env_config - - source $tmp_var_file - - } -function __besman_unset_env_parameters +function __besman_unset_env_parameters_and_cleanup() { - - local tmp_var_file=$HOME/tmp_var_file.sh - - sed -i "s/export//g" $tmp_var_file - - unset $(awk -F'=' '{print $1}' $tmp_var_file) - - [[ -f $tmp_var_file ]] && rm $tmp_var_file + local environment ossp + enviroment=$1 + ossp=$(echo "$enviroment" | cut -d "-" -f 1) + while read -r line; + do + # To skip comments + if echo "$line" | grep -qe "^#" ; then + + continue + fi + + [[ $line == "---" ]] && continue # To skip the --- from starting of yaml file + if echo "$line" | grep -qe "^BESMAN_"; then # Check to export only environment variables starting with BESMAN_ + + key=$(echo "$line" | cut -d ":" -f 1) # For getting the var name + unset "$key" + else + continue + fi + + done < "$BESMAN_ENV_CONFIG_FILE_PATH" + + [[ -f $BESMAN_DIR/tmp/besman-$enviroment-config.yaml ]] && rm "$BESMAN_ENV_CONFIG_FILE_PATH" + [[ -d $BESMAN_DIR/tmp/$ossp ]] && rm -rf "$BESMAN_DIR/tmp/$ossp" +} +function __besman_check_environment_exists() +{ + local input_environment environment_name + input_environment=$1 + environment_name=$(echo "$input_environment" | cut -d "/" -f 3) + [[ ! -f $BESMAN_DIR/envs/besman-$environment_name.sh ]] && __besman_echo_red "Environment $environment_name does not exist" && return 1 } + function __besman_check_input_env_format { local environment=$1 @@ -181,10 +187,11 @@ function __besman_open_file function __besman_validate_environment { - local environment_name=$1 - echo ${environment_name} > $BESMAN_DIR/var/current - cat $BESMAN_DIR/var/list.txt | grep -w "$environment_name" > /dev/null - if [ "$?" != "0" ]; then + local environment_name input_environment_name namespace repo + environment_name=$1 + + if ! grep -q "${environment_name}" "$BESMAN_DIR/var/list.txt" + then __besman_echo_debug "Environment $environment_name does not exist" return 1 diff --git a/src/main/bash/besman-main.sh b/src/main/bash/besman-main.sh index eb8934ce..45a0cc63 100644 --- a/src/main/bash/besman-main.sh +++ b/src/main/bash/besman-main.sh @@ -45,7 +45,7 @@ function bes { [[ -z $environment ]] && environment="${args[1]}" [[ -z $version ]] && version="${args[2]}" fi - __besman_check_for_command_file $command || return 1 + __besman_check_for_command_file "$command" || return 1 case $command in install) @@ -53,6 +53,7 @@ function bes { [[ ${#opts[@]} -ne 2 ]] && __besman_echo_red "Incorrect syntax" && __bes_help && return 1 [[ ${#args[@]} -ne 3 ]] && __besman_echo_red "Incorrect syntax" && __bes_help && return 1 __besman_validate_environment $environment || return 1 + # __besman_check_environment_exists "$environment" || return 1 __besman_check_if_version_exists $environment $version || return 1 __besman_validate_version_format $version || return 1 __bes_$command $environment $version @@ -77,6 +78,21 @@ function bes { [[ "${#opts[@]}" -ne 0 ]] && __besman_echo_red "Incorrect syntax" && return 1 __bes_$command ;; + add) + [[ "${#args[@]}" -ne 2 ]] && __besman_echo_red "Incorrect syntax" && return 1 + [[ "${#opts[@]}" -ne 1 ]] && __besman_echo_red "Incorrect syntax" && return 1 + [[ "${opts[0]}" != "-env" ]] && __besman_echo_red "Expected option -env" && return 1 + environment="${args[1]}" + __bes_$command "$environment" + ;; + set) + # [[ "${#args[@]}" -ne 3 ]] && __besman_echo_red "Incorrect syntax" && return 1 + # [[ "${#opts[@]}" -ne 0 ]] && __besman_echo_red "Incorrect syntax" && return 1 + local variable value + variable=${args[1]} + value=${args[2]} + __bes_"$command" "$variable" "$value" + ;; run) if [[ -z "${#opts[1]}" ]]; then @@ -173,17 +189,16 @@ function bes { ext=${args[i]} fi done - # cve=${args[1]} - # vuln=${args[2]} - # env=${args[3]} - # ext=${args[4]} - if [[ $assess_flag -eq 1 ]]; then __bes_$command "$type" "$assess_flag" "$purpose" "$vuln" "$env" "$ext" else __bes_$command "$type" "$assess_flag" "$purpose" "$vuln" "$env" "$ext" fi - + + elif [[ ( -n $type ) && ( $type == --environment || $type == -env ) ]]; then + env_name=${args[1]} + local template_type=${args[2]} + __bes_"$command" "$type" "$env_name" "$template_type" fi unset type purpose vuln env ext ;; @@ -203,4 +218,4 @@ function bes { esac unset environment version command args opts assess_flag purpose -} +} \ No newline at end of file diff --git a/src/main/bash/commands/besman-create.sh b/src/main/bash/commands/besman-create.sh index e878dd5a..dcfb21c3 100755 --- a/src/main/bash/commands/besman-create.sh +++ b/src/main/bash/commands/besman-create.sh @@ -13,7 +13,7 @@ function __bes_create # checks whether the user github id has been populated or not under $BESMAN_USER_NAMESPACE __besman_check_github_id || return 1 # checks whether the user has already logged in or not to gh tool - __besman_gh_auth_status $BESMAN_USER_NAMESPACE + __besman_gh_auth_status "$BESMAN_USER_NAMESPACE" return_val=$? # if return_val == 0 then the user is already logged in if [[ $return_val == "0" ]]; then @@ -29,13 +29,13 @@ function __bes_create fi __besman_echo_white "forking" - __besman_gh_fork $BESMAN_NAMESPACE $BESMAN_PLAYBOOK_REPO + __besman_gh_fork "$BESMAN_NAMESPACE" "$BESMAN_PLAYBOOK_REPO" [[ "$?" != "0" ]] && return 1 if [[ ! -d $HOME/$BESMAN_PLAYBOOK_REPO ]]; then __besman_echo_white "cloning" - __besman_gh_clone $BESMAN_USER_NAMESPACE $BESMAN_PLAYBOOK_REPO $HOME/$BESMAN_PLAYBOOK_REPO + __besman_gh_clone "$BESMAN_USER_NAMESPACE" "$BESMAN_PLAYBOOK_REPO" "$HOME/$BESMAN_PLAYBOOK_REPO" [[ "$?" != "0" ]] && return 1 fi @@ -54,12 +54,206 @@ function __bes_create unset vuln env ext target_path return_val purpose else - # TODO - create_env + # bes create -env fastjson-RT-env + # $1 would be the type - env/playbook + local environment_name overwrite template_type env_file version ossp env_file_name + environment_name=$2 + template_type=$3 + version=$4 + [[ -z $version ]] && version="0.0.1" + ossp=$(echo "$environment_name" | cut -d "-" -f 1) + env_file_name="besman-$environment_name.sh" + env_file_path=$BESMAN_ENV_REPOS/$ossp/$version/$env_file_name + __besman_set_variables + mkdir -p "$BESMAN_ENV_REPOS/$ossp/$version" + if [[ -f "$env_file_path" ]]; then + __besman_echo_yellow "File exists with the same name under $env_file_path" + read -rp "Do you wish to overwrite (y/n)?: " overwrite + if [[ ( "$overwrite" == "" ) || ( "$overwrite" == "y" ) || ( "$overwrite" == "Y" ) ]]; then + rm "$env_file_path" + else + __besman_echo_yellow "Exiting..." + return 1 + fi + fi + + if [[ ( -n "$template_type" ) && ( "$template_type" == "basic" ) ]]; then + + __besman_create_env_basic "$env_file_path" || return 1 + elif [[ -z "$template_type" ]]; then + __besman_create_env_with_config "$env_file_path" + __besman_create_env_config "$environment_name" "$version" + + fi + + fi + __besman_update_env_dir_list "$environment_name" "$version" + code "$env_file_path" +} + +function __besman_set_variables() +{ + local path + __bes_set "BESMAN_LOCAL_ENV" "True" + while [[ ( -z $path ) || ( ! -d $path ) ]] + do + read -rp "Enter the complete path to your local environment directory: " path + done + __bes_set "BESMAN_ENV_REPOS" "$path" + +} + +function __besman_create_env_config() +{ + local environment_name config_file ossp_name env_type config_file_path version + environment_name=$1 + version=$2 + ossp_name=$(echo "$environment_name" | cut -d "-" -f 1) + env_type=$(echo "$environment_name" | cut -d "-" -f 2) + config_file="besman-$ossp_name-$env_type-env-config.yaml" + config_file_path=$BESMAN_ENV_REPOS/$ossp/$version/$config_file + [[ ! -f $config_file_path ]] && touch "$config_file_path" && __besman_echo_yellow "Creating new config file $config_file_path" + cat < "$config_file_path" +--- +BESMAN_ORG: Be-Secure +BESMAN_OSSP: $ossp_name +BESMAN_OSSP_CLONE_PATH: \$HOME/\$BESMAN_OSSP +BESMAN_ANSIBLE_ROLES_PATH: \$BESMAN_DIR/tmp/\$BESMAN_OSSP/roles +BESMAN_ANSIBLE_ROLES: +BESMAN_OSS_TRIGGER_PLAYBOOK_PATH: \$BESMAN_DIR/tmp/\$BESMAN_OSSP +BESMAN_OSS_TRIGGER_PLAYBOOK: besman-\$BESMAN_OSSP-$env_type-trigger-playbook.yaml +BESMAN_DISPLAY_SKIPPED_ANSIBLE_HOSTS: false +# Please add other variables as well as ansible variables here +EOF + code "$config_file_path" +} + +function __besman_create_env_with_config() +{ + local env_file_path + env_file_path=$1 + + cat < "$env_file_path" +#!/bin/bash + +function __besman_install_$environment_name +{ + __besman_check_for_gh || return 1 + __besman_check_github_id || return 1 + __besman_check_for_ansible || return 1 + __besman_update_requirements_file + __besman_ansible_galaxy_install_roles_from_requirements + __besman_check_for_trigger_playbook "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" + [[ "\$?" -eq 1 ]] && __besman_create_ansible_playbook + __besman_run_ansible_playbook_extra_vars "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" "bes_command=install role_path=\$BESMAN_ANSIBLE_ROLES_PATH" || return 1 + if [[ -d \$BESMAN_OSSP_CLONE_PATH ]]; then + __besman_echo_white "The clone path already contains dir names \$BESMAN_OSSP" + else + __besman_gh_clone "\$BESMAN_ORG" "\$BESMAN_OSSP" "\$BESMAN_OSSP_CLONE_PATH" + fi + # Please add the rest of the code here for installation +} + +function __besman_uninstall_$environment_name +{ + __besman_check_for_trigger_playbook "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" + [[ "\$?" -eq 1 ]] && __besman_create_ansible_playbook + __besman_run_ansible_playbook_extra_vars "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" "bes_command=remove role_path=\$BESMAN_ANSIBLE_ROLES_PATH" || return 1 + if [[ -d \$BESMAN_OSSP_CLONE_PATH ]]; then + __besman_echo_white "Removing \$BESMAN_OSSP_CLONE_PATH..." + rm -rf "\$BESMAN_OSSP_CLONE_PATH" + else + __besman_echo_yellow "Could not find dir \$BESMAN_OSSP_CLONE_PATH" fi + # Please add the rest of the code here for uninstallation + } +function __besman_update_$environment_name +{ + __besman_check_for_trigger_playbook "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" + [[ "\$?" -eq 1 ]] && __besman_create_ansible_playbook + __besman_run_ansible_playbook_extra_vars "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" "bes_command=update role_path=\$BESMAN_ANSIBLE_ROLES_PATH" || return 1 + # Please add the rest of the code here for update + +} + +function __besman_validate_$environment_name +{ + __besman_check_for_trigger_playbook "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" + [[ "\$?" -eq 1 ]] && __besman_create_ansible_playbook + __besman_run_ansible_playbook_extra_vars "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" "bes_command=validate role_path=\$BESMAN_ANSIBLE_ROLES_PATH" || return 1 + # Please add the rest of the code here for validate + +} + +function __besman_reset_$environment_name +{ + __besman_check_for_trigger_playbook "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" + [[ "\$?" -eq 1 ]] && __besman_create_ansible_playbook + __besman_run_ansible_playbook_extra_vars "\$BESMAN_OSS_TRIGGER_PLAYBOOK_PATH/\$BESMAN_OSS_TRIGGER_PLAYBOOK" "bes_command=reset role_path=\$BESMAN_ANSIBLE_ROLES_PATH" || return 1 + # Please add the rest of the code here for reset + +} +EOF + __besman_echo_white "Created env file $environment_name under $BESMAN_DIR/envs" + +} + +function __besman_create_env_basic +{ + local env_file_path + env_file_path=$1 + [[ -f $env_file ]] && __besman_echo_red "Environment file exists" && return 1 + cat <> "$env_file" +#!/bin/bash + +function __besman_install_$environment_name +{ + +} + +function __besman_uninstall_$environment_name +{ + +} + +function __besman_update_$environment_name +{ + +} + +function __besman_validate_$environment_name +{ + +} + +function __besman_reset_$environment_name +{ + +} +EOF +__besman_echo_white "Creating env file.." +} + +function __besman_update_env_dir_list() +{ + local environment_name version + environment_name=$1 + version=$2 + + if grep -qw "Be-Secure/besecure-ce-env-repo/$environment_name,$version" "$BESMAN_ENV_REPOS/list.txt" + then + return 1 + else + __besman_echo_white "Updating local list" + echo "Be-Secure/besecure-ce-env-repo/$environment_name,$version" >> "$BESMAN_ENV_REPOS/list.txt" + fi + +} + + function __besman_create_playbook { local args=("${@}") @@ -98,8 +292,3 @@ function __besman_create_playbook unset args vuln env ext purpose } -# function create_env -# { -# # TODO -# } - diff --git a/src/main/bash/commands/besman-help.sh b/src/main/bash/commands/besman-help.sh index 8346653e..56621e2c 100755 --- a/src/main/bash/commands/besman-help.sh +++ b/src/main/bash/commands/besman-help.sh @@ -1,111 +1,159 @@ #!/usr/bin/env bash function __bes_help { -cat << EOF -BeSman - Help ---------------------------------------------------------- -$ bes +__besman_echo_yellow ' BeSman - Help ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' Usage: $ bes ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' : environment, version, namespace ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' install - To install a RT/BT environment ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes install -env -V ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' Eg: bes install -env tomcat-BT-env -V 0.0.1 ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' uninstall - To uninstall the environment ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes uninstall -env ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' or ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes uninstall -env -V ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' list - To list the available environments and playbooks ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes list ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes list --playbook ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' status - To show the installed environments ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes status ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' ~ - Shows the recently installed environment ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' set - To set the BeSman environment variables ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes set ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' To show the available list of variables run ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes set ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' create - To create an environment or playbook ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' To create a environment ' +__besman_echo_no_colour ' ----------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' For environments which relies on ansible role ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes create -env ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' For environments with only the skeletal code ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes create -env basic ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' To create a playbook ' +__besman_echo_no_colour ' ----------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes create --playbook ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' : ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -cve : To pass CVE details. ' +__besman_echo_no_colour ' $ bes create --playbook -cve CVE-2018-2019 ' +__besman_echo_no_colour ' -vuln : To pass the vulnerability category. ' +__besman_echo_no_colour ' $ bes create --playbook -vuln xss ' +__besman_echo_no_colour ' -env : To pass the environment name. ' +__besman_echo_no_colour ' $ bes create --playbook -env drupal ' +__besman_echo_no_colour ' -ext : To pass the extension of the playbook. ' +__besman_echo_no_colour ' $ bes create --playbook -ext py ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' NOTE: If any of the above values are not passed, default value "untitled" will be assigned in its place except for -ext. ' +__besman_echo_no_colour ' If -ext is not passed, the default value will be "md". ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' Eg: $ bes create --playbook -cve CVE-2018-2019 -vuln rce -env drupal -ext php ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' upgrade - Upgrades BeSman to the latest version ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes upgrade ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' update - Runs the update function of the environment script ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes update -env ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' help - Displays the BeSman commands ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes help ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' version - Displays the version of BeSmand or an environment script ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' Version of BeSman Utility ' +__besman_echo_no_colour ' ------------------------------ ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes -V ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes --version ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' Version of an Environment ' +__besman_echo_no_colour ' ------------------------------ ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes -V -env [env_name] ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' remove - To uninstall the BeSman utility ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes rm ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' pull - To pull the playbooks from repo ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes pull --playbook ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_yellow ' run - To execute a playbook ' +__besman_echo_no_colour ' -------------------------------------------------------------------------------------- ' +__besman_echo_no_colour ' ' +__besman_echo_no_colour ' $ bes run --playbook [playbook name] --input [input arguments] ' - : environment, version, namespace - - - ---------------------------------------------------------- -install : $ bes install –env [namespace]/[repo name]/[environment name] -V [version] - The command is used to install the specified - environment. - - Eg: $ bes install -env Be-Secure/besecure-ce-env-repo/BeSman-dev-env –V 0.0.1 - -uninstall : $ bes uninstall --environment [env_name] - - OR - - $ bes uninstall –env [env_name] -V [version] - - The command is used to uninstall the specified - environment. - - Eg: $ bes uninstall –env BeSman - -status : $ bes status - - The command displays the installed environments. - -list : $ bes list - - The command lists the various environment that - can be installed. - - OR - - : $ bes list --playbook - - Used to list the playbooks - -upgrade : $ bes upgrade - - Upgrades BeSman to next version available. - -update : $ bes update - - Updates the list file with lastest changes. - - OR - - : $ bes update -env [environment name] - - Update the environment. - -version : Version of BeSman Utility - ------------------------- - $ bes --version or bes –V - This command displays the version of BeSman - installed on the host. - Version of an Environment - ------------------------- - $ bes -V -env [env_name] - The command displays the version of the specified - environment. - Eg: $ bes -V -env greenlight - -help : $ bes help - - Displays the BeSman manual - -remove : $ bes rm - - Removes BeSman utility and installed environments - from the local system - -create : To create a playbook. The playbook will be named - based on the options passed within the command. - - $ bes create --playbook - - Options: - - -cve : To pass CVE details. - $ bes create --playbook -cve CVE-2018-2019 - -vuln : To pass the vulnerability category. - $ bes create --playbook -vuln xss - -env : To pass the environment name. - $ bes create --playbook -env drupal - -ext : To pass the extension of the playbook. - $ bes create --playbook -ext py - - NOTE: If any of the above values are not passed, default value "untitled" will be assigned in its place except for -ext. - If -ext is not passed, the default value will be "md". - - Eg: $ bes create --playbook -cve CVE-2018-2019 -vuln rce -env drupal -ext php - - -pull : $ bes pull --playbook - - To pull down playbooks from Be-Secure playbook repository. - -run : $ bes run --playbook [playbook name] --input [input arguments] - - To run the playbook - -EOF } diff --git a/src/main/bash/commands/besman-install.sh b/src/main/bash/commands/besman-install.sh index 7d6fbdfb..d55da9c5 100755 --- a/src/main/bash/commands/besman-install.sh +++ b/src/main/bash/commands/besman-install.sh @@ -1,53 +1,42 @@ #!/usr/bin/env bash - function __bes_install { - local input_environment_name=$1 # The format of the input environment name: // - - # Syntax check for var input_environment_name. - # Format of input environment name = // - # Word count of var input_environment_name, after replace '/' with ' ', will be 3 - [[ $(echo $input_environment_name | sed "s/\// /g" | wc -w) -ne 3 ]] && __besman_echo_red "Please provide the environment name as //" && return 1 - - local version_id=$2 - local return_val ossp env_repo_namespace env_repo environment_name - - # To get the actual environment name, after removing and - environment_name=$(echo "$input_environment_name" | cut -d "/" -f 3) - - # If environmnet not installed. - if [[ ! -d "${BESMAN_DIR}/envs/besman-${environment_name}/$version_id" ]]; - then + local environment_name env_repo environment_name version_id + environment_name=$1 - __besman_get_remote_env "$input_environment_name" "$environment_name" || return 1 + version_id=$2 + if [[ ( -n $BESMAN_LOCAL_ENV ) && ( $BESMAN_LOCAL_ENV == "True" ) ]]; then + __besman_get_local_env "$environment_name" "$version_id" || return 1 + fi + # If environmnet not installed. + if [[ ! -d "${BESMAN_DIR}/envs/besman-${environment_name}/$version_id" ]]; then - mkdir -p ${BESMAN_DIR}/envs/besman-"${environment_name}" - touch ${BESMAN_DIR}/envs/besman-${environment_name}/current + if [[ ( -n $BESMAN_LOCAL_ENV ) && ( $BESMAN_LOCAL_ENV == "False" ) ]]; then + __besman_get_remote_env "$environment_name" || return 1 + fi + mkdir -p "${BESMAN_DIR}/envs/besman-${environment_name}" + touch "${BESMAN_DIR}/envs/besman-${environment_name}/current" current="${BESMAN_DIR}/envs/besman-${environment_name}/current" - - - mkdir -p ${BESMAN_DIR}/envs/besman-${environment_name}/$version_id - - __besman_echo_no_colour "$version_id" > "$current" + echo "${environment_name}" > "$BESMAN_DIR/var/current" - if [[ -f $HOME/besman-$environment_name.yml ]]; then - __besman_echo_white "Sourcing env parameters" - __besman_source_env_parameters "$environment_name" - fi + mkdir -p "${BESMAN_DIR}/envs/besman-${environment_name}/$version_id" - cp "${BESMAN_DIR}/envs/besman-${environment_name}.sh" ${BESMAN_DIR}/envs/besman-${environment_name}/$version_id/ + __besman_echo_no_colour "$version_id" >"$current" + + cp "${BESMAN_DIR}/envs/besman-${environment_name}.sh" "${BESMAN_DIR}/envs/besman-${environment_name}/$version_id/" + [[ ( -n $BESMAN_LIGHT_MODE ) && ( $BESMAN_LIGHT_MODE == "False" ) ]] && __besman_source_env_params "$environment_name" + [[ ( -n $BESMAN_LIGHT_MODE ) && ( $BESMAN_LIGHT_MODE == "False" ) ]] && __besman_create_roles_config_file source "${BESMAN_DIR}/envs/besman-${environment_name}/${version_id}/besman-${environment_name}.sh" __besman_install_"${environment_name}" "${environment_name}" "${version_id}" - __besman_unset_env_parameters + return_val="$?" __besman_manage_install_out "$return_val" "$environment_name" # if environmnet installed, but user wants to install a different version of the same environment. - elif [[ -d ${BESMAN_DIR}/envs/besman-${environment_name}/$version_id && $(cat ${BESMAN_DIR}/envs/besman-${environment_name}/current) != "$version_id" ]]; - then + elif [[ -d ${BESMAN_DIR}/envs/besman-${environment_name}/$version_id && $(cat "${BESMAN_DIR}/envs/besman-${environment_name}/current") != "$version_id" ]]; then __besman_echo_white "Please remove the existing installation for $environment_name with version $version_id and try again." return 1 @@ -55,70 +44,91 @@ function __bes_install { else # If user tries to install the already installed version of the environment __besman_echo_white "${environment_name} $version_id is currently installed in your system " - + fi + unset return_val env_repo environment_name namespace version_id } -function __besman_manage_install_out +function __besman_get_local_env() { - local return_val environment + local environment version default_config_path + + environment=$1 + version=$2 + ossp=$(echo "$environment" | cut -d "-" -f 1) + default_config_path=$BESMAN_DIR/tmp/besman-$environment_name-config.yaml + [[ ! -d $BESMAN_ENV_REPOS ]] && __besman_echo_red "Could not find dir $BESMAN_ENV_REPOS" && return 1 + cp "$BESMAN_ENV_REPOS/$ossp/$version/besman-$environment.sh" "$BESMAN_DIR/envs/" + if [[ ( -n $BESMAN_LIGHT_MODE ) && ( $BESMAN_LIGHT_MODE == "False" ) && ( ! -f "$HOME/besman-$environment-config.yaml" ) ]]; then + + if [[ -f $default_config_path ]]; then + __besman_echo_yellow "A config file already exists" + read -rp "Do you wish to replace it(y/n)?: " replace + fi + if [[ ( -z $replace ) || ( $replace == 'Y' ) || ( $replace == 'y' ) ]]; then + rm "$default_config_path" + touch "$default_config_path" + [[ ! -f "$BESMAN_ENV_REPOS/$ossp/$version/besman-$environment-config.yaml" ]] && __besman_echo_red "Could not find config file in the path $BESMAN_ENV_REPOS/$ossp/$version/" && return 1 + cp "$BESMAN_ENV_REPOS/$ossp/$version/besman-$environment-config.yaml" "$default_config_path" + fi + fi +} + +function __besman_manage_install_out { + local return_val environment + return_val=$1 environment=$2 if [[ $return_val == "0" ]]; then __besman_echo_green "Successfully installed $environment" - + else - + __besman_echo_red "Installation failed" __besman_error_rollback "$environment" - + fi } -function __besman_get_remote_env -{ - - local input_environment_name environment_name env_repo_namespace env_repo ossp_dir env_url - - input_environment_name=$1 - environment_name=$2 - env_repo_namespace=$(echo "$input_environment_name" | cut -d "/" -f 1) - env_repo=$(echo "$input_environment_name" | cut -d "/" -f 2) - ossp_dir=$(echo "$input_environment_name" | cut -d "/" -f 3 | cut -d "-" -f 1) - env_url="https://raw.githubusercontent.com/${env_repo_namespace}/${env_repo}/master/${ossp_dir}/${version_id}/besman-${environment_name}.sh" - - __besman_secure_curl "$env_url" >> ${BESMAN_DIR}/envs/besman-${environment_name}.sh +function __besman_get_remote_env { + + # This code fetches the environment and its config file from env repo + local environment_name env_repo_namespace env_repo ossp env_url default_config_path replace curl_flag + env_repo_namespace=$(echo "$BESMAN_ENV_REPOS" | cut -d "/" -f 1) + env_repo=$(echo "$BESMAN_ENV_REPOS" | cut -d "/" -f 2) + environment_name=$1 + env_type=$(echo "$environment_name" | cut -d "-" -f 2) + ossp=$(echo "$environment_name" | cut -d "-" -f 1) + env_url="https://raw.githubusercontent.com/${env_repo_namespace}/${env_repo}/master/${ossp}/${version_id}/besman-${environment_name}.sh" + default_config_path=$BESMAN_DIR/tmp/besman-$environment_name-config.yaml + curl_flag=true + __besman_secure_curl "$env_url" >>"${BESMAN_DIR}/envs/besman-${environment_name}.sh" [[ "$?" -ne 0 ]] && __besman_echo_red "Failed while trying to get the besman-${environment_name}.sh" && return 1 - - # Checks for user level config file for the env. If not found, download the default config file from remote repo. - if [[ ! -f $HOME/besman-$environment_name.yml ]]; then - - env_config_url="https://raw.githubusercontent.com/${env_repo_namespace}/${env_repo}/master/${ossp_dir}/${version_id}/besman-${environment_name}.yml" - - if ( curl -o/dev/null -sfI "$env_config_url" ); then - - __besman_echo_white "Using environment level configuration." - __besman_secure_curl "$env_config_url" >> $HOME/besman-${environment_name}.yml - - else - - __besman_echo_yellow "Could not get the env level configuration" - + if [[ ( -n $BESMAN_LIGHT_MODE ) && ( $BESMAN_LIGHT_MODE == "False" ) && ( ! -f "$HOME/besman-${ossp}-${env_type}-env-config.yaml" ) ]]; then + config_url="https://raw.githubusercontent.com/${env_repo_namespace}/${env_repo}/master/${ossp}/${version_id}/besman-${ossp}-${env_type}-env-config.yaml" + if [[ -f $default_config_path ]]; then + __besman_echo_yellow "A config file already exists" + read -rp "Do you wish to replace it(y/n)?: " replace + if [[ ( -z $replace ) || ( $replace == 'Y' ) || ( $replace == 'y' ) ]]; then + rm "$default_config_path" + else + curl_flag=false + fi + fi + if [[ $curl_flag == true ]]; then + touch "$default_config_path" + if ! __besman_secure_curl "$config_url" >> "$default_config_path"; + then + __besman_echo_red "Failed while trying to get the besman-${ossp}-${env_type}-env-config.yaml" + return 1 + fi fi - - else - - __besman_echo_white "User level configuration found" - fi - - unset input_environment_name environment_name env_repo_namespace env_repo ossp_dir env_url - - + } \ No newline at end of file diff --git a/src/main/bash/commands/besman-list.sh b/src/main/bash/commands/besman-list.sh index cd9c2101..522d6d36 100755 --- a/src/main/bash/commands/besman-list.sh +++ b/src/main/bash/commands/besman-list.sh @@ -2,74 +2,108 @@ function __bes_list { -local playbook_flag=$1 +local flag=$1 local env # For listing playbooks -if [[ ( -n $playbook_flag ) && ( -d $BESMAN_DIR/playbook ) ]]; then - [[ -z $(ls $BESMAN_DIR/playbook | grep -v "README.md") ]] && __besman_echo_white "No playbook available" && return 1 - ls $BESMAN_DIR/playbook >> $HOME/temp.txt - __besman_echo_no_colour "Available playbooks" - __besman_echo_no_colour "-------------------" - cat $HOME/temp.txt | grep -v "README.md" - [[ -f $HOME/temp.txt ]] && rm $HOME/temp.txt -elif [[ ( -n $playbook_flag ) && ( ! -d $BESMAN_DIR/playbook ) ]]; then +if [[ ( -n $flag ) && ( $flag == "--playbook" ) ]]; then + if [[ -d $BESMAN_DIR/playbook ]]; then + [[ -z $(ls $BESMAN_DIR/playbook | grep -v "README.md") ]] && __besman_echo_white "No playbook available" && return 1 + ls $BESMAN_DIR/playbook >> $HOME/temp.txt + __besman_echo_no_colour "Available playbooks" + __besman_echo_no_colour "-------------------" + cat $HOME/temp.txt | grep -v "README.md" + [[ -f $HOME/temp.txt ]] && rm $HOME/temp.txt + else __besman_echo_white "No playbook available" + fi else + __besman_check_repo_exist || return 1 + __besman_update_list + # __besman_echo_no_colour "Github Org Repo Environment Version" + # __besman_echo_no_colour "-----------------------------------------------------------------------------------" + + # For listing environments - __besman_echo_no_colour "Available environments and their respective version numbers (* - local; ^ - remote)" + printf "%-14s %-32s %-25s %-8s\n" "Github Org" "Repo" "Environment" "Version" __besman_echo_no_colour "-----------------------------------------------------------------------------------" + - sed -i '/^$/d' $BESMAN_DIR/var/list.txt + sed -i '/^$/d' "$BESMAN_DIR/var/list.txt" + OLD_IFS=$IFS + IFS="/" - while read line; + while read -r line; do - local arr=() - arr=$(echo $line | sed 's/,/ /g') - - local list="" - - for i in ${arr[@]}; - do - - if echo $i | grep -q "env" # If i is env name. - then - - list=$i - - local env=$(echo $i | cut -d "/" -f 3) # To get the name of the env. Removes namespace and repo name - - elif [[ -d $BESMAN_DIR/envs/besman-$env ]]; then - if ls $BESMAN_DIR/envs/besman-$env | grep -q "$i" # Checks if version is listed under installed dir of env - then - - list="$list $i*" # * depicts local - - else - - list="$list $i^" # ^ depicts remote - - fi - - else - - list="$list $i^" # Remote and env not installed - - fi + converted_line=$(echo "$line" | sed 's|,|/|g') + read -r org repo env version <<< "$converted_line" + printf "%-14s %-32s %-25s %-8s\n" "$org" "$repo" "$env" "$version" - done - - echo $list - - done < $BESMAN_DIR/var/list.txt + + + done < "$BESMAN_DIR/var/list.txt" + IFS=$OLD_IFS __besman_echo_no_colour "" - unset playbook_flag arr env list + unset flag arr env list + if [[ $BESMAN_LOCAL_ENV == "True" ]]; then + + __besman_echo_yellow "Pointing to local dir $BESMAN_ENV_REPOS" + __besman_echo_no_colour "" + __besman_echo_white "If you wish to change, run the below command" + __besman_echo_yellow "$ bes set BESMAN_LOCAL_ENV False" + __besman_echo_yellow "$ bes set BESMAN_ENV_REPOS " + else + __besman_echo_yellow "Pointing to $BESMAN_ENV_REPOS" + __besman_echo_yellow "If you wish to change the repo run the below command" + __besman_echo_yellow "$ bes set BESMAN_ENV_REPOS " + fi fi +} +function __besman_check_repo_exist() +{ + local namespace repo response repo_url + namespace=$(echo "$BESMAN_ENV_REPOS" | cut -d "/" -f 1) + repo=$(echo "$BESMAN_ENV_REPOS" | cut -d "/" -f 2) + repo_url="https://api.github.com/repos/$namespace/$repo" + + response=$(curl --head --silent "$repo_url" | head -n 1 | awk '{print $2}') + + if [ "$response" -ne 200 ]; then + __besman_echo_red "Repository $repo does not exist under $namespace" + return 1 + fi + +} + +function __besman_update_list() +{ + local bes_list + if [[ ( -n $BESMAN_LOCAL_ENV ) && ( $BESMAN_LOCAL_ENV == "True" )]]; then + local env_dir_list bes_list + [[ -z $BESMAN_ENV_REPOS ]] && __besman_echo_red "Please set the local env dir first" && return 1 + [[ ! -d $BESMAN_ENV_REPOS ]] && __besman_echo_red "Could not find dir $BESMAN_ENV_REPOS" && return 1 + + env_dir_list=$(< "$BESMAN_ENV_REPOS/list.txt") + bes_list=$BESMAN_DIR/var/list.txt + echo "$env_dir_list" > "$bes_list" + else + + local org repo path + org=$(echo "$BESMAN_ENV_REPOS" | cut -d "/" -f 1) + repo=$(echo "$BESMAN_ENV_REPOS" | cut -d "/" -f 2) + bes_list="$BESMAN_DIR/var/list.txt" + path="https://raw.githubusercontent.com/$org/$repo/master/list.txt" + __besman_secure_curl "$path" > "$bes_list" + fi + + + + } \ No newline at end of file diff --git a/src/main/bash/commands/besman-set.sh b/src/main/bash/commands/besman-set.sh new file mode 100644 index 00000000..6c653cd1 --- /dev/null +++ b/src/main/bash/commands/besman-set.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +function __bes_set() +{ + local variable_name file_path new_value + variable_name=$1 + new_value=$2 + file_path="$BESMAN_DIR/etc/user-config.cfg" + + if [[ ( -z $variable_name ) || (-z $new_value) ]]; then + __besman_display_set_usage "$file_path" + return 1 + fi + + if [[ ! -f "$file_path" ]]; then + __besman_echo_red "File not found: $file_path" + return 1 + fi + __besman_check_if_variable_exist "$variable_name" "$file_path" || return 1 + __besman_update_env_value "$variable_name" "$new_value" + +} + +function __besman_display_set_usage() +{ + local file_path + file_path=$1 + __besman_echo_white "Usage:" + __besman_echo_no_colour "" + __besman_echo_yellow "$ bes set " + __besman_echo_no_colour "" + __besman_echo_white ":" + while read -r line; + do + + echo "$line" | cut -d "=" -f 1 + + done < "$file_path" + __besman_echo_no_colour "" + + return 1 +} + +function __besman_check_if_variable_exist() +{ + local variable_name file_path + variable_name=$1 + file_path=$2 + if ! grep -q "$variable_name" "$file_path" + then + __besman_echo_red "Not a valid variable" + return 1 + fi +} +function __besman_update_env_value() +{ + local variable_name new_value + variable_name=$1 + new_value=$2 + if [[ $variable_name == "BESMAN_ENV_REPOS" && $(echo "$new_value" | sed 's|/| |g' | wc -w) -eq 2 ]]; then + sed -i "s|\($variable_name *= *\).*|\1$new_value|" "$file_path" + elif [[ $variable_name == "BESMAN_ENV_REPOS" && $(echo "$new_value" | sed 's|/| |g' | wc -w) -eq 1 ]]; then + sed -i "s|\($variable_name *= *\).*|\1$new_value/besecure-ce-env-repo|" "$file_path" + else + sed -i "s|\($variable_name *= *\).*|\1$new_value|" "$file_path" + fi + source "$BESMAN_DIR/bin/besman-init.sh" + __besman_echo_yellow "Variable '$variable_name' value updated to '$new_value'" + +} diff --git a/src/main/bash/commands/besman-uninstall.sh b/src/main/bash/commands/besman-uninstall.sh index b60e5eb6..5ec606fc 100755 --- a/src/main/bash/commands/besman-uninstall.sh +++ b/src/main/bash/commands/besman-uninstall.sh @@ -2,40 +2,33 @@ function __bes_uninstall { -local environment version +local environment version installed_envs environment=$1 version=$2 # Condition where all the envs and its available versions will be removed if [[ $environment == "all" && -z $version ]]; then - __besman_echo_white "This operation would remove all the environments and its files" - __besman_interactive_uninstall || return 1 - __besman_echo_white "Removing files..." - __besman_secure_curl "https://raw.githubusercontent.com/$BESMAN_NAMESPACE/BeSman/master/dist/environments" >> $HOME/env_tmp.txt - - sed -i 's/,/ /g' $HOME/env_tmp.txt - - local curled_environment=$(cat $HOME/env_tmp.txt) - - for i in ${curled_environment[@]}; - do - - if [[ -d $BESMAN_DIR/envs/besman-$i ]]; then - - __besman_uninstall_$i "$i" - - fi - - done - - [ -f $HOME/env_tmp.txt ] && rm $HOME/env_tmp.txt + __besman_echo_white "This operation would remove all the environments and its files" + __besman_interactive_uninstall || return 1 + __besman_echo_white "Removing files..." + + installed_envs=$(basename "$(find "$BESMAN_DIR/envs/" -mindepth 1 -maxdepth 1 -type d)") + + for i in ${installed_envs[@]}; do + + if [[ -d $BESMAN_DIR/envs/besman-$i ]]; then + + __besman_uninstall_"$i" "$i" + fi + + done + __besman_unset_env_parameters_and_cleanup "$environment" + find $BESMAN_DIR/envs -maxdepth 1 -mindepth 1 -type d -exec rm -rf '{}' \; + + unset curled_environment - find $BESMAN_DIR/envs -maxdepth 1 -mindepth 1 -type d -exec rm -rf '{}' \; - - unset curled_environment - - __besman_echo_green "Files removed successfully." + __besman_echo_green "Files removed successfully." # Condition where no current file is present and the user executes uninstall without version parameter elif [[ ! -f $BESMAN_DIR/envs/besman-$environment/current && -z $version ]]; then @@ -52,8 +45,7 @@ elif [[ -f $BESMAN_DIR/envs/besman-$environment/current && $version == $(cat $BE __besman_interactive_uninstall || return 1 __besman_echo_no_colour "Uninstalling version $version of $environment" __besman_uninstall_$environment "$environment" - [[ -f $HOME/besman-$environment.yml && -f $HOME/tmp_var_file.sh ]] && __besman_unset_env_parameters - + rm $BESMAN_DIR/envs/besman-$environment/current rm -rf $BESMAN_DIR/envs/besman-$environment/$version @@ -65,6 +57,7 @@ elif [[ -f $BESMAN_DIR/envs/besman-$environment/current && $version == $(cat $BE rm -rf $BESMAN_DIR/envs/besman-$environment fi + __besman_unset_env_parameters_and_cleanup "$environment" [[ -f $BESMAN_DIR/envs/besman-$environment.sh ]] && rm $BESMAN_DIR/envs/besman-$environment.sh @@ -73,29 +66,28 @@ elif [[ -f $BESMAN_DIR/envs/besman-$environment/current && $version == $(cat $BE # 1) Current file is present and the user prompts to remove a previous version and 2) Current file is not present and the user prompts to reove a previous version elif [[ -f $BESMAN_DIR/envs/besman-$environment/current && $version != $(cat $BESMAN_DIR/envs/besman-$environment/current) || ! -f $BESMAN_DIR/envs/besman-$environment/current && -d $BESMAN_DIR/envs/besman-$environment/$version ]]; then - __besman_echo_white "$version for $environment is not the current version" - __besman_echo_white "The operation will still remove the files for the version." - __besman_interactive_uninstall || return 1 - __besman_echo_no_colour "Removing files..." - - rm -rf $BESMAN_DIR/envs/besman-$environment/$version - - # To remove the folder besman-$environment if its empty - l=$(ls $BESMAN_DIR/envs/besman-$environment) - if [[ -z $l ]]; then - - rm -rf $BESMAN_DIR/envs/besman-$environment - - fi - - [[ -f $BESMAN_DIR/envs/besman-$environment.sh ]] && rm $BESMAN_DIR/envs/besman-$environment.sh - - __besman_echo_green "Files removed successfully." + __besman_echo_white "$version for $environment is not the current version" + __besman_echo_white "The operation will still remove the files for the version." + __besman_interactive_uninstall || return 1 + __besman_echo_no_colour "Removing files..." + + rm -rf $BESMAN_DIR/envs/besman-$environment/$version + + # To remove the folder besman-$environment if its empty + l=$(ls $BESMAN_DIR/envs/besman-$environment) + if [[ -z $l ]]; then + + rm -rf $BESMAN_DIR/envs/besman-$environment + + fi + __besman_unset_env_parameters_and_cleanup "$environment" + + [[ -f $BESMAN_DIR/envs/besman-$environment.sh ]] && rm $BESMAN_DIR/envs/besman-$environment.sh + + __besman_echo_green "Files removed successfully." fi } - -