nm-quick-interactive.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #!/bin/bash
  2. # setup_netclient - installs netclient locally
  3. setup_netclient() {
  4. # DEV_TEMP - Temporary instructions for testing
  5. # wget https://fileserver.netmaker.org/testing/netclient
  6. # chmod +x netclient
  7. # ./netclient install
  8. # RELEASE_REPLACE - Use this once release is ready
  9. # if [ -f /etc/debian_version ]; then
  10. # curl -sL 'https://apt.netmaker.org/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/netclient.asc
  11. # curl -sL 'https://apt.netmaker.org/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/netclient.list
  12. # sudo apt update
  13. # sudo apt install netclient
  14. # elif [ -f /etc/centos-release ]; then
  15. # curl -sL 'https://rpm.netmaker.org/gpg.key' | sudo tee /tmp/gpg.key
  16. # curl -sL 'https://rpm.netmaker.org/netclient-repo' | sudo tee /etc/yum.repos.d/netclient.repo
  17. # sudo rpm --import /tmp/gpg.key
  18. # sudo dnf check-update
  19. # sudo dnf install netclient
  20. # elif [ -f /etc/fedora-release ]; then
  21. # curl -sL 'https://rpm.netmaker.org/gpg.key' | sudo tee /tmp/gpg.key
  22. # curl -sL 'https://rpm.netmaker.org/netclient-repo' | sudo tee /etc/yum.repos.d/netclient.repo
  23. # sudo rpm --import /tmp/gpg.key
  24. # sudo dnf check-update
  25. # sudo dnf install netclient
  26. # elif [ -f /etc/redhat-release ]; then
  27. # curl -sL 'https://rpm.netmaker.org/gpg.key' | sudo tee /tmp/gpg.key
  28. # curl -sL 'https://rpm.netmaker.org/netclient-repo' | sudo tee /etc/yum.repos.d/netclient.repo
  29. # sudo rpm --import /tmp/gpg.key
  30. # sudo dnf check-update(
  31. # sudo dnf install netclient
  32. # elif [ -f /etc/arch-release ]; then
  33. # yay -S netclient
  34. # else
  35. # echo "OS not supported for automatic install"
  36. # exit 1
  37. # fi
  38. # if [ -z "${install_cmd}" ]; then
  39. # echo "OS unsupported for automatic dependency install"
  40. # exit 1
  41. # fi
  42. }
  43. cat << "EOF"
  44. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  45. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  46. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  47. __ __ ______ ______ __ __ ______ __ __ ______ ______
  48. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  49. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  50. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  51. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  52. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  53. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  54. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  55. EOF
  56. if [ $(id -u) -ne 0 ]; then
  57. echo "This script must be run as root"
  58. exit 1
  59. fi
  60. if [ -z "$1" ]; then
  61. echo "-----------------------------------------------------"
  62. echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (EE)?"
  63. echo "EE will require you to create an account at https://dashboard.license.netmaker.io"
  64. echo "-----------------------------------------------------"
  65. select install_option in "Community Edition" "Enterprise Edition"; do
  66. case $REPLY in
  67. 1)
  68. echo "installing Netmaker CE"
  69. INSTALL_TYPE="ce"
  70. break
  71. ;;
  72. 2)
  73. echo "installing Netmaker EE"
  74. INSTALL_TYPE="ee"
  75. break
  76. ;;
  77. *) echo "invalid option $REPLY";;
  78. esac
  79. done
  80. elif [ "$1" = "ce" ]; then
  81. echo "installing Netmaker CE"
  82. INSTALL_TYPE="ce"
  83. elif [ "$1" = "ee" ]; then
  84. echo "installing Netmaker EE"
  85. INSTALL_TYPE="ee"
  86. else
  87. echo "install type invalid (options: 'ce, ee')"
  88. exit 1
  89. fi
  90. wait_seconds() {(
  91. for ((a=1; a <= $1; a++))
  92. do
  93. echo ". . ."
  94. sleep 1
  95. done
  96. )}
  97. confirm() {(
  98. while true; do
  99. read -p 'Does everything look right? [y/n]: ' yn
  100. case $yn in
  101. [Yy]* ) override="true"; break;;
  102. [Nn]* ) echo "exiting..."; exit 1;;
  103. * ) echo "Please answer yes or no.";;
  104. esac
  105. done
  106. )}
  107. echo "checking dependencies..."
  108. OS=$(uname)
  109. if [ -f /etc/debian_version ]; then
  110. dependencies="wireguard wireguard-tools jq docker.io docker-compose"
  111. update_cmd='apt update'
  112. install_cmd='apt-get install -y'
  113. elif [ -f /etc/alpine-release ]; then
  114. dependencies="wireguard jq docker.io docker-compose"
  115. update_cmd='apk update'
  116. install_cmd='apk --update add'
  117. elif [ -f /etc/centos-release ]; then
  118. dependencies="wireguard jq docker.io docker-compose"
  119. update_cmd='yum update'
  120. install_cmd='yum install -y'
  121. elif [ -f /etc/fedora-release ]; then
  122. dependencies="wireguard jq docker.io docker-compose"
  123. update_cmd='dnf update'
  124. install_cmd='dnf install -y'
  125. elif [ -f /etc/redhat-release ]; then
  126. dependencies="wireguard jq docker.io docker-compose"
  127. update_cmd='yum update'
  128. install_cmd='yum install -y'
  129. elif [ -f /etc/arch-release ]; then
  130. dependecies="wireguard-tools jq docker.io docker-compose"
  131. update_cmd='pacman -Sy'
  132. install_cmd='pacman -S --noconfirm'
  133. elif [ "${OS}" = "FreeBSD" ]; then
  134. dependencies="wireguard wget jq docker.io docker-compose"
  135. update_cmd='pkg update'
  136. install_cmd='pkg install -y'
  137. elif [ -f /etc/turris-version ]; then
  138. dependencies="wireguard-tools bash jq docker.io docker-compose"
  139. OS="TurrisOS"
  140. update_cmd='opkg update'
  141. install_cmd='opkg install'
  142. elif [ -f /etc/openwrt_release ]; then
  143. dependencies="wireguard-tools bash jq docker.io docker-compose"
  144. OS="OpenWRT"
  145. update_cmd='opkg update'
  146. install_cmd='opkg install'
  147. else
  148. install_cmd=''
  149. fi
  150. if [ -z "${install_cmd}" ]; then
  151. echo "OS unsupported for automatic dependency install"
  152. exit 1
  153. fi
  154. set -- $dependencies
  155. ${update_cmd}
  156. while [ -n "$1" ]; do
  157. if [ "${OS}" = "FreeBSD" ]; then
  158. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  159. if [ "$is_installed" != "" ]; then
  160. echo " " $1 is installed
  161. else
  162. echo " " $1 is not installed. Attempting install.
  163. ${install_cmd} $1
  164. sleep 5
  165. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  166. if [ "$is_installed" != "" ]; then
  167. echo " " $1 is installed
  168. elif [ -x "$(command -v $1)" ]; then
  169. echo " " $1 is installed
  170. else
  171. echo " " FAILED TO INSTALL $1
  172. echo " " This may break functionality.
  173. fi
  174. fi
  175. else
  176. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  177. is_installed=$(opkg list-installed $1 | grep $1)
  178. else
  179. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  180. fi
  181. if [ "${is_installed}" != "" ]; then
  182. echo " " $1 is installed
  183. else
  184. echo " " $1 is not installed. Attempting install.
  185. ${install_cmd} $1
  186. sleep 5
  187. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  188. is_installed=$(opkg list-installed $1 | grep $1)
  189. else
  190. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  191. fi
  192. if [ "${is_installed}" != "" ]; then
  193. echo " " $1 is installed
  194. elif [ -x "$(command -v $1)" ]; then
  195. echo " " $1 is installed
  196. else
  197. echo " " FAILED TO INSTALL $1
  198. echo " " This may break functionality.
  199. fi
  200. fi
  201. fi
  202. shift
  203. done
  204. echo "-----------------------------------------------------"
  205. echo "dependency check complete"
  206. echo "-----------------------------------------------------"
  207. wait_seconds 3
  208. set -e
  209. NETMAKER_BASE_DOMAIN=nm.$(dig myip.opendns.com @resolver1.opendns.com +short | tr . -).nip.io
  210. COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  211. SERVER_PUBLIC_IP=$(dig myip.opendns.com @resolver1.opendns.com +short)
  212. MASTER_KEY=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  213. DOMAIN_TYPE=""
  214. echo "-----------------------------------------------------"
  215. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  216. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_PUBLIC_IP"
  217. echo "-----------------------------------------------------"
  218. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  219. case $REPLY in
  220. 1)
  221. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  222. DOMAIN_TYPE="auto"
  223. break
  224. ;;
  225. 2)
  226. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_PUBLIC_IP first): " domain
  227. NETMAKER_BASE_DOMAIN=$domain
  228. echo "using $NETMAKER_BASE_DOMAIN"
  229. DOMAIN_TYPE="custom"
  230. break
  231. ;;
  232. *) echo "invalid option $REPLY";;
  233. esac
  234. done
  235. wait_seconds 2
  236. echo "-----------------------------------------------------"
  237. echo "The following subdomains will be used:"
  238. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  239. echo " api.$NETMAKER_BASE_DOMAIN"
  240. echo " broker.$NETMAKER_BASE_DOMAIN"
  241. if [ "$INSTALL_TYPE" = "ee" ]; then
  242. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  243. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  244. echo " grafana.$NETMAKER_BASE_DOMAIN"
  245. fi
  246. echo "-----------------------------------------------------"
  247. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  248. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_PUBLIC_IP"
  249. confirm
  250. fi
  251. wait_seconds 1
  252. if [ "$INSTALL_TYPE" = "ee" ]; then
  253. echo "-----------------------------------------------------"
  254. echo "Provide Details for EE installation:"
  255. echo " 1. Log into https://dashboard.license.netmaker.io"
  256. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  257. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  258. echo " 4. note email address"
  259. echo "-----------------------------------------------------"
  260. unset LICENSE_KEY
  261. while [ -z "$LICENSE_KEY" ]; do
  262. read -p "License Key: " LICENSE_KEY
  263. done
  264. unset ACCOUNT_ID
  265. while [ -z ${ACCOUNT_ID} ]; do
  266. read -p "Account ID: " ACCOUNT_ID
  267. done
  268. fi
  269. unset GET_EMAIL
  270. unset RAND_EMAIL
  271. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  272. read -p "Email Address for Domain Registration (click 'enter' to use $RAND_EMAIL): " GET_EMAIL
  273. if [ -z "$GET_EMAIL" ]; then
  274. echo "using rand email"
  275. EMAIL="$RAND_EMAIL"
  276. else
  277. EMAIL="$GET_EMAIL"
  278. fi
  279. wait_seconds 1
  280. unset GET_MQ_USERNAME
  281. unset GET_MQ_PASSWORD
  282. unset CONFIRM_MQ_PASSWORD
  283. echo "Enter Credentials For MQ..."
  284. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  285. if [ -z "$GET_MQ_USERNAME" ]; then
  286. echo "using default username for mq"
  287. MQ_USERNAME="netmaker"
  288. else
  289. MQ_USERNAME="$GET_MQ_USERNAME"
  290. fi
  291. select domain_option in "Auto Generated Password" "Input Your Own Password"; do
  292. case $REPLY in
  293. 1)
  294. echo "generating random password for mq"
  295. MQ_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  296. break
  297. ;;
  298. 2)
  299. while true
  300. do
  301. echo "Enter your Password For MQ: "
  302. read -s GET_MQ_PASSWORD
  303. echo "Enter your password again to confirm: "
  304. read -s CONFIRM_MQ_PASSWORD
  305. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  306. echo "wrong password entered, try again..."
  307. continue
  308. fi
  309. MQ_PASSWORD="$GET_MQ_PASSWORD"
  310. echo "MQ Password Saved Successfully!!"
  311. break
  312. done
  313. break
  314. ;;
  315. *) echo "invalid option $REPLY";;
  316. esac
  317. done
  318. wait_seconds 2
  319. echo "-----------------------------------------------------------------"
  320. echo " SETUP ARGUMENTS"
  321. echo "-----------------------------------------------------------------"
  322. echo " domain: $NETMAKER_BASE_DOMAIN"
  323. echo " email: $EMAIL"
  324. echo " public ip: $SERVER_PUBLIC_IP"
  325. if [ "$INSTALL_TYPE" = "ee" ]; then
  326. echo " license: $LICENSE_KEY"
  327. echo " account id: $ACCOUNT_ID"
  328. fi
  329. echo "-----------------------------------------------------------------"
  330. echo "Confirm Settings for Installation"
  331. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  332. confirm
  333. echo "-----------------------------------------------------------------"
  334. echo "Beginning installation..."
  335. echo "-----------------------------------------------------------------"
  336. wait_seconds 3
  337. echo "Pulling config files..."
  338. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/master/compose/docker-compose.yml"
  339. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/master/docker/Caddyfile"
  340. if [ "$INSTALL_TYPE" = "ee" ]; then
  341. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/master/compose/docker-compose.ee.yml"
  342. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/master/docker/Caddyfile-EE"
  343. fi
  344. wget -O /root/docker-compose.yml $COMPOSE_URL && wget -O /root/mosquitto.conf https://raw.githubusercontent.com/gravitl/netmaker/master/docker/mosquitto.conf && wget -O /root/Caddyfile $CADDY_URL
  345. wget -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/master/docker/wait.sh
  346. chmod +x /root/wait.sh
  347. mkdir -p /etc/netmaker
  348. echo "Setting docker-compose and Caddyfile..."
  349. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  350. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  351. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  352. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  353. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  354. sed -i "s/REPLACE_MQ_PASSWORD/$MQ_PASSWORD/g" /root/docker-compose.yml
  355. sed -i "s/REPLACE_MQ_USERNAME/$MQ_USERNAME/g" /root/docker-compose.yml
  356. if [ "$INSTALL_TYPE" = "ee" ]; then
  357. sed -i "s~YOUR_LICENSE_KEY~$LICENSE_KEY~g" /root/docker-compose.yml
  358. sed -i "s/YOUR_ACCOUNT_ID/$ACCOUNT_ID/g" /root/docker-compose.yml
  359. fi
  360. echo "Starting containers..."
  361. docker-compose -f /root/docker-compose.yml up -d
  362. sleep 2
  363. test_connection() {
  364. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  365. for i in 1 2 3 4 5 6 7 8
  366. do
  367. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  368. if [[ "$i" == 8 ]]; then
  369. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  370. echo " Exiting..."
  371. exit 1
  372. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  373. echo " Certificates not yet configured, retrying..."
  374. elif [[ "$curlresponse" == *"left intact"* ]]; then
  375. echo " Certificates ok"
  376. break
  377. else
  378. secs=$(($i*5+10))
  379. echo " Issue establishing connection...retrying in $secs seconds..."
  380. fi
  381. sleep $secs
  382. done
  383. }
  384. setup_mesh() {( set -e
  385. wait_seconds 15
  386. echo "Creating netmaker network (10.101.0.0/16)"
  387. curl -s -o /dev/null -d '{"addressrange":"10.101.0.0/16","netid":"netmaker"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/networks
  388. wait_seconds 5
  389. echo "Creating netmaker access key"
  390. curlresponse=$(curl -s -d '{"uses":99999,"name":"netmaker-key"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/networks/netmaker/keys)
  391. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  392. wait_seconds 3
  393. # echo "Installing Netclient"
  394. # setup_netclient
  395. # echo "Adding Netclient to Network"
  396. # netclient join -t $ACCESS_TOKEN
  397. # # TODO - Get Host ID
  398. # echo "Setting Netclient as Default Host"
  399. # HOST_ID=$(grep 'id:' /etc/netclient/netclient.yml | awk '{print $2}')
  400. # echo $HOST_ID
  401. # # TODO - API call to make host default
  402. # echo "Setting Netclient as Ingress Gateway"
  403. # if [[ ! -z "$SERVER_ID" ]]; then
  404. # curl -o /dev/null -s -X POST -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker/$HOST_ID/createingress
  405. # fi
  406. )}
  407. set +e
  408. test_connection
  409. wait_seconds 3
  410. setup_mesh
  411. echo "-----------------------------------------------------------------"
  412. echo "-----------------------------------------------------------------"
  413. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  414. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  415. echo "-----------------------------------------------------------------"
  416. echo "-----------------------------------------------------------------"
  417. # cp -f /etc/skel/.bashrc /root/.bashrc