common_utils.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. # Copyright Epic Games, Inc. All Rights Reserved.
  3. function log_msg() { #message
  4. if [ ! -z $VERBOSE ]; then
  5. echo $1
  6. fi
  7. }
  8. function print_usage() {
  9. echo "
  10. Usage:
  11. ${0} [--help] [--publicip <IP Address>] [--turn <turn server>] [--stun <stun server>] [cirrus options...]
  12. Where:
  13. --help will print this message and stop this script.
  14. --debug will run all scripts with --inspect
  15. --nosudo will run all scripts without \`sudo\` command useful for when run in containers.
  16. --verbose will enable additional logging
  17. --package-manager <package manager name> specify an alternative package manager to apt-get
  18. --publicip is used to define public ip address (using default port) for turn server, syntax: --publicip ; it is used for
  19. default value: Retrieved from 'curl https://api.ipify.org' or if unsuccessful then set to 127.0.0.1. It is the IP address of the Cirrus server and the default IP address of the TURN server
  20. --turn defines what TURN server to be used, syntax: --turn 127.0.0.1:19303
  21. default value: as above, IP address downloaded from https://api.ipify.org; in case if download failure it is set to 127.0.0.1
  22. --stun defined what STUN server to be used, syntax: --stun stun.l.google.com:19302
  23. default value as above
  24. Other options: stored and passed to the Cirrus server. All parameters printed once the script values are set.
  25. Command line options might be omitted to run with defaults and it is a good practice to omit specific ones when just starting the TURN or the STUN server alone, not the whole set of scripts.
  26. "
  27. exit 1
  28. }
  29. function print_parameters() {
  30. echo ""
  31. echo "${0} is running with the following parameters:"
  32. echo "--------------------------------------"
  33. if [[ -n "${stunserver}" ]]; then echo "STUN server : ${stunserver}" ; fi
  34. if [[ -n "${turnserver}" ]]; then echo "TURN server : ${turnserver}" ; fi
  35. echo "Public IP address : ${publicip}"
  36. echo "Cirrus server command line arguments: ${cirruscmd}"
  37. echo ""
  38. }
  39. function set_start_default_values() {
  40. # publicip and cirruscmd are always needed
  41. publicip=$(curl -s https://api.ipify.org)
  42. if [[ -z $publicip ]]; then
  43. publicip="127.0.0.1"
  44. fi
  45. cirruscmd=""
  46. if [ "$1" = "y" ]; then
  47. turnserver="${publicip}:19303"
  48. fi
  49. if [ "$2" = "y" ]; then
  50. stunserver="stun.l.google.com:19302"
  51. fi
  52. }
  53. function use_args() {
  54. while(($#)) ; do
  55. case "$1" in
  56. --debug ) IS_DEBUG=1; shift;;
  57. --nosudo ) NO_SUDO=1; shift;;
  58. --verbose ) VERBOSE=1; shift;;
  59. --stun ) stunserver="$2"; shift 2;;
  60. --turn ) turnserver="$2"; shift 2;;
  61. --publicip ) publicip="$2"; turnserver="${publicip}:19303"; shift 2;;
  62. --help ) print_usage;;
  63. * ) echo "Unknown command, adding to cirrus command line: $1"; cirruscmd+=" $1"; shift;;
  64. esac
  65. done
  66. }
  67. function call_setup_sh() {
  68. bash "setup.sh"
  69. }
  70. function start_process() {
  71. if [ ! -z $NO_SUDO ]; then
  72. log_msg "running with sudo removed"
  73. eval $(echo "$@" | sed 's/sudo//g')
  74. else
  75. eval $@
  76. fi
  77. }
  78. function get_version() {
  79. local version=$1
  80. if command -v $version; then
  81. version=$($@)
  82. fi
  83. echo $version | sed -E 's/[^0-9.]//g'
  84. }