code-server.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env bash
  2. function header_info {
  3. cat <<"EOF"
  4. ______ __ _____
  5. / ____/___ ____/ /__ / ___/___ ______ _____ _____
  6. / / / __ \/ __ / _ \ \__ \/ _ \/ ___/ | / / _ \/ ___/
  7. / /___/ /_/ / /_/ / __/ ___/ / __/ / | |/ / __/ /
  8. \____/\____/\__,_/\___/ /____/\___/_/ |___/\___/_/
  9. EOF
  10. }
  11. IP=$(hostname -I | awk '{print $1}')
  12. YW=$(echo "\033[33m")
  13. BL=$(echo "\033[36m")
  14. RD=$(echo "\033[01;31m")
  15. BGN=$(echo "\033[4;92m")
  16. GN=$(echo "\033[1;92m")
  17. DGN=$(echo "\033[32m")
  18. CL=$(echo "\033[m")
  19. BFR="\\r\\033[K"
  20. HOLD="-"
  21. CM="${GN}✓${CL}"
  22. APP="Code Server"
  23. hostname="$(hostname)"
  24. set -o errexit
  25. set -o errtrace
  26. set -o nounset
  27. set -o pipefail
  28. shopt -s expand_aliases
  29. alias die='EXIT=$? LINE=$LINENO error_exit'
  30. trap die ERR
  31. function error_exit() {
  32. trap - ERR
  33. local reason="Unknown failure occured."
  34. local msg="${1:-$reason}"
  35. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  36. echo -e "$flag $msg" 1>&2
  37. exit $EXIT
  38. }
  39. clear
  40. header_info
  41. while true; do
  42. read -p "This will Install ${APP} on $hostname. Proceed(y/n)?" yn
  43. case $yn in
  44. [Yy]*) break ;;
  45. [Nn]*) exit ;;
  46. *) echo "Please answer yes or no." ;;
  47. esac
  48. done
  49. function msg_info() {
  50. local msg="$1"
  51. echo -ne " ${HOLD} ${YW}${msg}..."
  52. }
  53. function msg_ok() {
  54. local msg="$1"
  55. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  56. }
  57. msg_info "Installing Dependencies"
  58. apt-get install -y curl &>/dev/null
  59. apt-get install -y sudo &>/dev/null
  60. apt-get install -y git &>/dev/null
  61. msg_ok "Installed Dependencies"
  62. VERSION=$(curl -s https://api.github.com/repos/coder/code-server/releases/latest |
  63. grep "tag_name" |
  64. awk '{print substr($2, 3, length($2)-4) }')
  65. msg_info "Installing Code-Server v${VERSION}"
  66. curl -fOL https://github.com/coder/code-server/releases/download/v$VERSION/code-server_${VERSION}_amd64.deb &>/dev/null
  67. sudo dpkg -i code-server_${VERSION}_amd64.deb &>/dev/null
  68. rm -rf code-server_${VERSION}_amd64.deb
  69. mkdir -p ~/.config/code-server/
  70. sudo systemctl enable --now code-server@$USER &>/dev/null
  71. cat <<EOF >~/.config/code-server/config.yaml
  72. bind-addr: 0.0.0.0:8680
  73. auth: none
  74. password:
  75. cert: false
  76. EOF
  77. sudo systemctl restart code-server@$USER
  78. msg_ok "Installed Code-Server v${VERSION} on $hostname"
  79. echo -e "${APP} should be reachable by going to the following URL.
  80. ${BL}http://$IP:8680${CL} \n"