role_update.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/bash
  2. set -e
  3. #TODO: Support python virtual environments for now global
  4. COLOR_END='\e[0m'
  5. COLOR_RED='\e[0;31m'
  6. # This current directory.
  7. DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  8. ROOT_DIR=$(cd "$DIR/../../" && pwd)
  9. EXTERNAL_ROLE_DIR="$ROOT_DIR/roles/external"
  10. ROLES_REQUIREMNTS_FILE="$ROOT_DIR/roles/thirdparty_roles.yml"
  11. # Exit msg
  12. msg_exit() {
  13. printf "$COLOR_RED$@$COLOR_END"
  14. printf "\n"
  15. printf "Exiting...\n"
  16. exit 1
  17. }
  18. # Trap if ansible-galaxy failed and warn user
  19. cleanup() {
  20. msg_exit "Update failed. Please don't commit or push roles till you fix the issue"
  21. }
  22. trap "cleanup" ERR INT TERM
  23. # Check ansible-galaxy
  24. [[ -z "$(which ansible-galaxy)" ]] && msg_exit "Ansible is not installed or not in your path."
  25. # Check roles req file
  26. [[ ! -f "$ROLES_REQUIREMNTS_FILE" ]] && msg_exit "roles_requirements '$ROLES_REQUIREMNTS_FILE' does not exist or permssion issue.\nPlease check and rerun."
  27. # Remove existing roles
  28. if [ -d "$EXTERNAL_ROLE_DIR" ]; then
  29. cd "$EXTERNAL_ROLE_DIR"
  30. if [ "$(pwd)" == "$EXTERNAL_ROLE_DIR" ];then
  31. echo "Removing current roles in '$EXTERNAL_ROLE_DIR/*'"
  32. rm -rf *
  33. else
  34. msg_exit "Path error could not change dir to $EXTERNAL_ROLE_DIR"
  35. fi
  36. fi
  37. # Install roles
  38. ansible-galaxy install -r "$ROLES_REQUIREMNTS_FILE" --force --no-deps -p "$EXTERNAL_ROLE_DIR"
  39. exit 0