setup.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env bash
  2. set -e
  3. RED='\033[0;31m'
  4. GREEN='\033[0;32m'
  5. BOLD='\033[1m'
  6. NC='\033[0m'
  7. if [ -z "$OPENPILOT_ROOT" ]; then
  8. # default to current directory for installation
  9. OPENPILOT_ROOT="$(pwd)/openpilot"
  10. fi
  11. function show_motd() {
  12. cat << 'EOF'
  13. .~ssos+.
  14. +8888888888i,
  15. {888888888888o.
  16. h8888888888888k
  17. t888888888s888k
  18. `t88888d/ h88k
  19. ``` h88l
  20. ,88k`
  21. .d8h`
  22. +d8h
  23. _+d8h`
  24. ;y8h+`
  25. |-`
  26. openpilot installer
  27. EOF
  28. }
  29. function sentry_send_event() {
  30. SENTRY_KEY=dd0cba62ba0ac07ff9f388f8f1e6a7f4
  31. SENTRY_URL=https://sentry.io/api/4507726145781760/store/
  32. EVENT=$1
  33. EVENT_TYPE=${2:-$EVENT}
  34. EVENT_LOG=${3:-"NA"}
  35. PLATFORM=$(uname -s)
  36. ARCH=$(uname -m)
  37. SYSTEM=$(uname -a)
  38. if [[ $PLATFORM == "Darwin" ]]; then
  39. OS="macos"
  40. elif [[ $PLATFORM == "Linux" ]]; then
  41. OS="linux"
  42. fi
  43. if [[ $ARCH == armv8* ]] || [[ $ARCH == arm64* ]] || [[ $ARCH == aarch64* ]]; then
  44. ARCH="aarch64"
  45. elif [[ $ARCH == "x86_64" ]] || [[ $ARCH == i686* ]]; then
  46. ARCH="x86"
  47. fi
  48. PYTHON_VERSION=$(echo $(python3 --version 2> /dev/null || echo "NA"))
  49. BRANCH=$(echo $(git -C $OPENPILOT_ROOT rev-parse --abbrev-ref HEAD 2> /dev/null || echo "NA"))
  50. COMMIT=$(echo $(git -C $OPENPILOT_ROOT rev-parse HEAD 2> /dev/null || echo "NA"))
  51. curl -s -o /dev/null -X POST -g --data "{ \"exception\": { \"values\": [{ \"type\": \"$EVENT\" }] }, \"tags\" : { \"event_type\" : \"$EVENT_TYPE\", \"event_log\" : \"$EVENT_LOG\", \"os\" : \"$OS\", \"arch\" : \"$ARCH\", \"python_version\" : \"$PYTHON_VERSION\" , \"git_branch\" : \"$BRANCH\", \"git_commit\" : \"$COMMIT\", \"system\" : \"$SYSTEM\" } }" \
  52. -H 'Content-Type: application/json' \
  53. -H "X-Sentry-Auth: Sentry sentry_version=7, sentry_key=$SENTRY_KEY, sentry_client=op_setup/0.1" \
  54. $SENTRY_URL 2> /dev/null
  55. }
  56. function check_stdin() {
  57. if [ -t 0 ]; then
  58. INTERACTIVE=1
  59. else
  60. echo "Checking for valid invocation..."
  61. echo -e " ↳ [${RED}✗${NC}] stdin not found! Running in non-interactive mode."
  62. echo -e " Run ${BOLD}'bash <(curl -fsSL openpilot.comma.ai)'${NC} to run in interactive mode.\n"
  63. fi
  64. }
  65. function ask_dir() {
  66. echo -n "Enter directory in which to install openpilot (default $OPENPILOT_ROOT): "
  67. if [[ -z $INTERACTIVE ]]; then
  68. echo -e "\nBecause your are running in non-interactive mode, the installation"
  69. echo -e "will default to $OPENPILOT_ROOT\n"
  70. return 0
  71. fi
  72. read
  73. if [[ ! -z "$REPLY" ]]; then
  74. mkdir -p $REPLY
  75. OPENPILOT_ROOT="$(realpath $REPLY)/openpilot"
  76. fi
  77. }
  78. function check_dir() {
  79. echo "Checking for installation directory..."
  80. if [ -d "$OPENPILOT_ROOT" ]; then
  81. echo -e " ↳ [${RED}✗${NC}] Installation destination $OPENPILOT_ROOT already exists!"
  82. # not a valid clone, can't continue
  83. if [[ ! -z "$(ls -A $OPENPILOT_ROOT)" && ! -f "$OPENPILOT_ROOT/launch_openpilot.sh" ]]; then
  84. echo -e " $OPENPILOT_ROOT already contains files but does not seems"
  85. echo -e " to be a valid openpilot git clone. Choose another location for"
  86. echo -e " installing openpilot!\n"
  87. return 1
  88. fi
  89. # already a "valid" openpilot clone, skip cloning again
  90. if [[ ! -z "$(ls -A $OPENPILOT_ROOT)" ]]; then
  91. SKIP_GIT_CLONE=1
  92. fi
  93. # by default, don't try installing in already existing directory
  94. if [[ -z $INTERACTIVE ]]; then
  95. return 0
  96. fi
  97. read -p " Would you like to attempt installation anyway? [Y/n] " -n 1 -r
  98. echo -e "\n"
  99. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  100. return 1
  101. fi
  102. return 0
  103. fi
  104. echo -e " ↳ [${GREEN}✔${NC}] Successfully chosen $OPENPILOT_ROOT as installation directory\n"
  105. }
  106. function check_git() {
  107. echo "Checking for git..."
  108. if ! command -v "git" > /dev/null 2>&1; then
  109. echo -e " ↳ [${RED}✗${NC}] git not found on your system, can't continue!"
  110. sentry_send_event "SETUP_FAILURE" "ERROR_GIT_NOT_FOUND"
  111. return 1
  112. else
  113. echo -e " ↳ [${GREEN}✔${NC}] git found.\n"
  114. fi
  115. }
  116. function git_clone() {
  117. st="$(date +%s)"
  118. echo "Cloning openpilot..."
  119. if $(git clone --filter=blob:none https://github.com/commaai/openpilot.git "$OPENPILOT_ROOT"); then
  120. if [[ -f $OPENPILOT_ROOT/launch_openpilot.sh ]]; then
  121. et="$(date +%s)"
  122. echo -e " ↳ [${GREEN}✔${NC}] Successfully cloned openpilot in $((et - st)) seconds.\n"
  123. return 0
  124. fi
  125. fi
  126. echo -e " ↳ [${RED}✗${NC}] failed to clone openpilot!"
  127. sentry_send_event "SETUP_FAILURE" "ERROR_GIT_CLONE"
  128. return 1
  129. }
  130. function install_with_op() {
  131. cd $OPENPILOT_ROOT
  132. $OPENPILOT_ROOT/tools/op.sh install
  133. $OPENPILOT_ROOT/tools/op.sh post-commit
  134. LOG_FILE=$(mktemp)
  135. if ! $OPENPILOT_ROOT/tools/op.sh --log $LOG_FILE setup; then
  136. echo -e "\n[${RED}✗${NC}] failed to install openpilot!"
  137. ERROR_TYPE="$(cat "$LOG_FILE" | sed '1p;d')"
  138. ERROR_LOG="$(cat "$LOG_FILE" | sed '2p;d')"
  139. sentry_send_event "SETUP_FAILURE" "$ERROR_TYPE" "$ERROR_LOG" || true
  140. return 1
  141. else
  142. sentry_send_event "SETUP_SUCCESS" || true
  143. fi
  144. echo -e "\n----------------------------------------------------------------------"
  145. echo -e "[${GREEN}✔${NC}] openpilot was successfully installed into ${BOLD}$OPENPILOT_ROOT${NC}"
  146. echo -e "Checkout the docs at https://docs.comma.ai"
  147. echo -e "Checkout how to contribute at https://github.com/commaai/openpilot/blob/master/docs/CONTRIBUTING.md"
  148. }
  149. show_motd
  150. check_stdin
  151. ask_dir
  152. check_dir
  153. check_git
  154. [ -z $SKIP_GIT_CLONE ] && git_clone
  155. install_with_op