dev 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/bin/bash
  2. # devctl is an utility script for automating some development tasks and actions.
  3. # To find out what options are available, run it without any arguments.
  4. # Text styles
  5. RED='\033[0;31m'
  6. BOLD=$(tput bold)
  7. NORMAL=$(tput sgr0)
  8. # Docker aliases
  9. misago_run="docker-compose run --rm misago"
  10. # Define dev paths
  11. # Those are paths to dirs and files created for dev project
  12. dev_paths=(
  13. "./avatargallery"
  14. "./devproject"
  15. "./media"
  16. "./static"
  17. "./userdata"
  18. "./manage.py"
  19. )
  20. print_dev_paths() {
  21. for x in "${dev_paths[@]}"; do
  22. echo "next element is '$x'"
  23. done
  24. }
  25. # Some commond shorthands
  26. docker_stop() {
  27. docker-compose stop
  28. }
  29. # Test required ports
  30. # We test if those ports are free before running docker
  31. port_django=8000
  32. port_postgresql=5432
  33. required_ports=($port_postgresql)
  34. # Commands
  35. intro() {
  36. echo "Development project:"
  37. echo
  38. echo " ${BOLD}init${NORMAL} initialize new dev project for development, do nothing if project already exists."
  39. echo " ${BOLD}clear${NORMAL} if dev project exists, delete it's files and destroy docker containers."
  40. echo " ${BOLD}rebuild${NORMAL} rebuild docker containers (uses --no-cache)."
  41. echo
  42. echo "Testing:"
  43. echo
  44. echo " ${BOLD}test${NORMAL} run tests suite."
  45. echo " ${BOLD}test module${NORMAL} run tests suite in specified python module, eg. misago.users."
  46. }
  47. invalid_option() {
  48. echo -e "Invalid option: ${RED}$1${NORMAL}"
  49. echo "Please run this script without any arguments to see the list of available options."
  50. exit 1
  51. }
  52. # Initialize new dev project
  53. init() {
  54. for dev_path in "${dev_paths[@]}"; do
  55. if [ -e $dev_path ]; then
  56. echo -e "${RED}Error:${NORMAL} Dev project already exists, or was not deleted completely."
  57. echo
  58. echo "Please use \"clear\" option to clear any pissible remaining files and try again."
  59. exit 1
  60. fi
  61. done
  62. for port in "${required_ports[@]}"; do
  63. nc "127.0.0.1" "$port" < /dev/null
  64. if [[ $? = "0" ]]; then
  65. echo -e "${RED}Error:${NORMAL} Port ${BOLD}$port${NORMAL} is not available."
  66. echo
  67. if [[ $port = $port_django ]]; then
  68. echo "Django application appears to already be running on http://127.0.0.1:8000"
  69. elif [[ $port = $port_postgresql ]]; then
  70. echo "PostgreSQL appears to already be running on port $port."
  71. fi
  72. exit 1
  73. fi
  74. done
  75. }
  76. # Clear existing dev project
  77. clear() {
  78. echo -e "${RED}Warning:${NORMAL} You are going clear current development project."
  79. echo
  80. will_delete_files=false
  81. for dev_path in "${dev_paths[@]}"; do
  82. if [ -e $dev_path ]; then
  83. will_delete_files=true
  84. fi
  85. done
  86. if [[ $will_delete_files = true ]]; then
  87. echo "Following files and directories will be deleted:"
  88. for dev_path in "${dev_paths[@]}"; do
  89. if [ -e $dev_path ]; then
  90. echo " $dev_path"
  91. fi
  92. done
  93. echo
  94. fi
  95. echo "This will also stop and remove docker containers used by this project."
  96. echo
  97. echo "Enter \"y\" to confirm:"
  98. read confirmation
  99. if [[ $confirmation = "y" ]]; then
  100. for dev_path in "${dev_paths[@]}"; do
  101. if [ -e $dev_path ]; then
  102. echo "Removing $dev_path"
  103. rm -rf $dev_path
  104. fi
  105. done
  106. docker-compose stop
  107. docker-compose down
  108. exit 0
  109. else
  110. echo "Operation canceled."
  111. exit 0
  112. fi
  113. }
  114. # Rebuild docker containers
  115. rebuild() {
  116. docker_stop
  117. docker-compose build --no-cache
  118. exit 0
  119. }
  120. # Run tests suite
  121. test() {
  122. $misago_run runtests.py $1
  123. docker_stop
  124. exit 0
  125. }
  126. # Command dispatcher
  127. if [[ $1 ]]; then
  128. if [[ $1 = "init" ]]; then
  129. init
  130. elif [[ $1 = "clear" ]]; then
  131. clear
  132. elif [[ $1 = "rebuild" ]]; then
  133. rebuild
  134. elif [[ $1 = "test" ]]; then
  135. test $2
  136. else
  137. invalid_option $1
  138. fi
  139. else
  140. intro
  141. fi