Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.04 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. echo "$(whoami)"
  4.  
  5. [ "$UID" -eq 0 ] || sudo "$0" "$@"
  6.  
  7. case $(uname -s) in
  8. Linux) os="linux" ;;
  9. *) os="not_linux" ;;
  10. esac
  11.  
  12. if [ "$os" = "not_linux" ]; then
  13. echo "Unsupported OS $(uname -s). Only Linux binaries are available."
  14. exit
  15. fi
  16.  
  17. case $(uname -m) in
  18. x86_64) arch="x86_64" ;;
  19. *) arch="other" ;;
  20. esac
  21.  
  22. if [ "$arch" = "other" ]; then
  23. echo "Unsupported architecture $(uname -m). Only x64 binaries are available."
  24. exit
  25. fi
  26.  
  27. test_program_installed () {
  28. if command -v $1 > /dev/null; then
  29. echo "Program $1 successfully installed"
  30. else
  31. echo "Program $1 not installed"
  32. exit 1
  33. fi
  34. }
  35.  
  36. install_docker() {
  37. # Uninstall old versions
  38. apt-get remove -qq docker docker-engine docker.io containerd runc
  39. # Update
  40. apt-get update -qq
  41. # Install packages to allow apt to use a repository over HTTPS
  42. apt-get -qq install \
  43. apt-transport-https \
  44. ca-certificates \
  45. curl \
  46. gnupg-agent \
  47. software-properties-common
  48. # Add Docker’s official GPG key
  49. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
  50. # Add repository
  51. add-apt-repository -y \
  52. "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  53. $(lsb_release -cs) \
  54. stable"
  55. # Update
  56. apt-get -qq update
  57. apt-get -qq install docker-ce docker-ce-cli containerd.io
  58. }
  59.  
  60. check_docker_version() {
  61. req_ver='19.03.0'
  62. installed_ver=$(docker --version | cut -f3 -d ' ' | sed 's/,//g')
  63.  
  64. if [[ "$req_ver" < "$installed_ver" || "$req_ver" == $installed_ver ]]; then
  65. echo "Docker version ${installed_ver} is suitable - minimal required version ${req_ver}."
  66. else
  67. echo "Docker version ${installed_ver} is not suitable - minimal required version ${req_ver}."
  68. exit 1
  69. fi
  70. }
  71.  
  72. install_nvidia_docker() {
  73. # Install nvidia-docker2 for nomad
  74. sudo apt-get install nvidia-docker2
  75.  
  76. # Add the package repositories
  77. distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
  78. curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
  79. curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
  80.  
  81. sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
  82. sudo systemctl restart docker
  83. }
  84.  
  85. install_nvidia_drivers() {
  86. #if lspci | grep -i nvidia > /dev/null; then
  87. # echo "NVIDIA grafic card available"
  88. #else
  89. # echo "NVIDIA grafic card is missing"
  90. # exit 1
  91. #fi
  92.  
  93. distro='ubuntu'
  94. arch='x86_64'
  95. version='1804'
  96.  
  97. case $(uname -m && cat /etc/*release) in
  98. *Ubuntu*18.10*|*Ubuntu*18.04*) version='1804';;
  99. *Ubuntu*16.04*) version='1604';;
  100. *Ubuntu*14.04*) version='1404';;
  101. *) echo "Unsupported version of OS"; exit 1;;
  102. esac
  103.  
  104. if command -v gcc --version > /dev/null; then
  105. echo "Program gcc --version successfully installed"
  106. else
  107. echo "Program gcc --version not installed. Installing"
  108. sudo apt -qq -y install gcc
  109. fi
  110.  
  111. sudo apt-get -qq -y install linux-headers-$(uname -r)
  112.  
  113. sudo wget "https://developer.download.nvidia.com/compute/cuda/repos/$distro$version/$arch/cuda-$distro$version.pin"
  114. sudo mv "cuda-$distro$version.pin" /etc/apt/preferences.d/cuda-repository-pin-600
  115. sudo apt-key adv -qq --fetch-keys "https://developer.download.nvidia.com/compute/cuda/repos/$distro$version/$arch/7fa2af80.pub"
  116. sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/$distro$version/$arch/ /"
  117. sudo apt-get update -qq
  118. sudo apt-get -y -qq install cuda-drivers
  119. sudo systemctl restart docker
  120. }
  121.  
  122. make_config () {
  123. token="$1"
  124. ports="$2"
  125.  
  126. file="$fluidstack_home/.fs_config.yaml"
  127.  
  128. echo "################################################################################" > $file
  129. echo "# FluidStack node config file in yaml format #" >> $file
  130. echo "################################################################################" >> $file
  131. echo "" >> $file
  132. echo "################################################################################" >> $file
  133. echo "# clusterToken #" >> $file
  134. echo "# A token unique to a provider that can be found on the provider dashboard #" >> $file
  135. echo "# https://provider.fluidstack.io/dashboard #" >> $file
  136. echo "################################################################################" >> $file
  137. echo "# clusterToken: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" >> $file
  138.  
  139. echo "" >> $file
  140. echo "clusterToken: ${token}" >> $file
  141. echo "" >> $file
  142.  
  143. echo "################################################################################" >> $file
  144. echo "# ports #" >> $file
  145. echo "# A list of port forwards from the router to an internal port in format #" >> $file
  146. echo "# 'local: external' Can be left empty if UPnP is enabled on the router. #" >> $file
  147. echo "################################################################################" >> $file
  148. echo "# ports:" >> $file
  149. echo "# 1000: 2000" >> $file
  150. echo "# 1001: 2001" >> $file
  151.  
  152. echo "" >> $file
  153. echo -n "ports:" >> $file
  154.  
  155. IFS=',' read -ra MAPPINGS <<< "$ports"
  156. for mapping in "${MAPPINGS[@]}"; do
  157. p1=""
  158. p2=""
  159.  
  160. IFS=':' read -ra PORTS <<< "$mapping"
  161. for port in "${PORTS[@]}"; do
  162. if [[ "$p1" -eq "" ]]; then
  163. p1="$port"
  164. else
  165. p2="$port"
  166. fi
  167. done
  168.  
  169. printf "\n %s: %s" "$p1" "$p2" >> "$file"
  170. done
  171.  
  172. echo "" >> $file
  173. }
  174.  
  175. prompt_cluster_token() {
  176. loop="true"
  177.  
  178. while $loop -eq "true"; do
  179. read -p "Enter your cluster token: " -r </dev/tty
  180.  
  181. IFS='-' read -ra TOKEN_PARTS <<< "$REPLY" # split string by dash
  182. c=0 # parts in string, expect 4
  183.  
  184. for _ in "${TOKEN_PARTS[@]}"; do
  185. c=$((c + 1))
  186. done
  187.  
  188. if [[ $c != "5" ]]; then
  189. echo "Bad token, please try again (e.g. '12358f2d9-5d6g-4b3b-a7c3-cc1233dcb123')"
  190. else
  191. loop="false"
  192. fi
  193. done
  194.  
  195. cluster_token="$REPLY"
  196. }
  197.  
  198. prompt_ports() {
  199. loop="true"
  200.  
  201. while $loop -eq "true"; do
  202. read -p "Enter port mappings as local:external, separated by a comma eg: (3000:4000). You can leave empty if UPnP is allowed on router: " -r </dev/tty
  203.  
  204. IFS=',' read -ra PORT_MAPPINGS <<< "$REPLY" # split string by comma
  205. c=0 # parts in string
  206. bad_mapping="false"
  207.  
  208. [[ $REPLY == '' ]] && return
  209.  
  210. for i in "${PORT_MAPPINGS[@]}"; do
  211. c=$((c + 1))
  212.  
  213. # check port mapping contains colon
  214. if [[ ${i} != *":"* ]]; then
  215. bad_mapping="$PORT_PARTS"
  216. fi
  217.  
  218. # check it has two ports either side of colon
  219. IFS=':' read -ra PORT_PARTS <<< "$i" # split string by colon
  220. pc=0 # parts in port mapping, expect 2
  221.  
  222. for p in "${PORT_PARTS[@]}"; do
  223. pc=$((pc + 1))
  224. done
  225.  
  226. if [[ $pc -ne "2" ]]; then
  227. bad_mapping="$PORT_PARTS"
  228. break
  229. else
  230. bad_mapping="false"
  231. fi
  232. done
  233.  
  234. if [[ "$bad_mapping" -ne "false" ]]; then
  235. echo "Bad port mapping: \"$bad_mapping\""
  236. elif [[ $c -lt "5" ]]; then
  237. echo "Expecting at least 5 ports mapping (e.g. \"4000:5000\")"
  238. else
  239. loop="false"
  240. fi
  241. done
  242.  
  243. ports="$REPLY"
  244. }
  245.  
  246. fluidstack_home="/usr/local/fluidstack"
  247. fluidstack_lib="$fluidstack_home/lib"
  248. fluidstack_bin="$fluidstack_home/bin"
  249. fluidstack_nomad_dir="$fluidstack_home/nomad"
  250.  
  251. if [ ! -d "$fluidstack_home" ]; then
  252. echo "Creating new home directory"
  253. mkdir -p $fluidstack_home/{lib,bin,tmp}
  254. fi
  255.  
  256. fluidstack_user="root"
  257. fluidstack_group="root"
  258.  
  259. # Install Docker
  260. install_docker
  261. check_docker_version
  262. # Install nvidia cuda drivers
  263. install_nvidia_drivers
  264. # Install nvidia docker support
  265. install_nvidia_docker
  266.  
  267. # Set variables
  268. node_version="v0.6.5"
  269. archive_folder="$fluidstack_lib/$node_version"
  270. archive_name=$node_version"_linux.tar.gz"
  271. archive="$fluidstack_lib/$archive_name"
  272. exec="fluidnode"
  273. unit_file="fluidstack.service"
  274. release_bucket="fluidstack-releases"
  275. release_uri="https://$release_bucket.s3.eu-west-2.amazonaws.com/$archive_name"
  276.  
  277. curl -fL# -o "$archive" "$release_uri"
  278. [ -d "$archive_folder" ] || mkdir $archive_folder
  279. tar -C $archive_folder -zxf $archive && rm $archive
  280.  
  281. rm "$fluidstack_bin/$exec"
  282. ln -s "$archive_folder/$exec" "$fluidstack_bin/$exec"
  283.  
  284. # Change owner of whole dir
  285. chown $fluidstack_user:$fluidstack_group "$fluidstack_bin/$exec"
  286.  
  287. test_program_installed "$fluidstack_bin/$exec"
  288.  
  289. cp "$archive_folder/$unit_file" "/lib/systemd/system/$unit_file"
  290. chmod 755 "/lib/systemd/system/$unit_file"
  291.  
  292. # Install and test ffmpeg binaries
  293. cp "$archive_folder/ffmpeg" /usr/local/bin
  294. chown $fluidstack_user:$fluidstack_group /usr/local/bin/ffmpeg
  295. chmod +x /usr/local/bin/ffmpeg
  296. test_program_installed "ffmpeg"
  297.  
  298. # Setup syslog
  299. echo "if \$programname == 'fluidstack' then /path/to/log/file.log& stop" > /etc/rsyslog.d/30-fluidstack.conf
  300.  
  301. echo chmod +x "$archive_folder/$exec"
  302. chmod +x "$archive_folder/$exec"
  303. chown -R $fluidstack_user:$fluidstack_group "$fluidstack_home"
  304.  
  305. prompt_cluster_token # sets ret value on $cluster_token
  306. prompt_ports # sets ret value on $ports
  307. make_config "$cluster_token" "$ports"
  308.  
  309. # Install and test nomad binary
  310. cp "$archive_folder/nomad" $fluidstack_bin
  311. chown $fluidstack_user:$fluidstack_group "$fluidstack_bin/nomad"
  312. chmod +x "$fluidstack_bin/nomad"
  313. test_program_installed "$fluidstack_bin/nomad"
  314.  
  315. # Setup nomad dirs
  316. mkdir -p "$fluidstack_nomad_dir/certs"
  317. mkdir -p "$fluidstack_nomad_dir/conf"
  318. mkdir -p "$fluidstack_nomad_dir/data"
  319.  
  320. # Move Nomad config
  321. cp "$archive_folder/client.hcl" "$fluidstack_nomad_dir/conf"
  322.  
  323. systemctl daemon-reload
  324. systemctl restart rsyslog
  325.  
  326. systemctl enable fluidstack.service
  327. systemctl start fluidstack
  328.  
  329. if [ $? -ne 0 ]; then
  330. echo "Installation has failed"
  331. exit 1
  332. fi
  333.  
  334. echo "Installation complete"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement