code-server.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env bash
  2. IP=$(hostname -I | awk '{print $1}')
  3. YW=`echo "\033[33m"`
  4. BL=`echo "\033[36m"`
  5. RD=`echo "\033[01;31m"`
  6. BGN=`echo "\033[4;92m"`
  7. GN=`echo "\033[1;92m"`
  8. DGN=`echo "\033[32m"`
  9. CL=`echo "\033[m"`
  10. BFR="\\r\\033[K"
  11. HOLD="-"
  12. CM="${GN}✓${CL}"
  13. APP="Code Server"
  14. hostname="$(hostname)"
  15. set -o errexit
  16. set -o errtrace
  17. set -o nounset
  18. set -o pipefail
  19. shopt -s expand_aliases
  20. alias die='EXIT=$? LINE=$LINENO error_exit'
  21. trap die ERR
  22. function error_exit() {
  23. trap - ERR
  24. local reason="Unknown failure occured."
  25. local msg="${1:-$reason}"
  26. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  27. echo -e "$flag $msg" 1>&2
  28. exit $EXIT
  29. }
  30. while true; do
  31. read -p "This will Install ${APP} on $hostname. Proceed(y/n)?" yn
  32. case $yn in
  33. [Yy]* ) break;;
  34. [Nn]* ) exit;;
  35. * ) echo "Please answer yes or no.";;
  36. esac
  37. done
  38. clear
  39. function header_info {
  40. echo -e "${BL}
  41. ______ __ _____
  42. / ____/___ ____/ /__ / ___/___ ______ _____ _____
  43. / / / __ \/ __ / _ \ \__ \/ _ \/ ___/ | / / _ \/ ___/
  44. / /___/ /_/ / /_/ / __/ ___/ / __/ / | |/ / __/ /
  45. \____/\____/\__,_/\___/ /____/\___/_/ |___/\___/_/
  46. ${CL}"
  47. }
  48. header_info
  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"