nm-quick.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. #!/bin/bash
  2. CONFIG_FILE=netmaker.env
  3. # location of nm-quick.sh (usually `/root`)
  4. SCRIPT_DIR=$(dirname "$(realpath "$0")")
  5. CONFIG_PATH="$SCRIPT_DIR/$CONFIG_FILE"
  6. LATEST=$(curl -s https://api.github.com/repos/gravitl/netmaker/releases/latest | grep "tag_name" | cut -d : -f 2,3 | tr -d [:space:],\")
  7. if [ $(id -u) -ne 0 ]; then
  8. echo "This script must be run as root"
  9. exit 1
  10. fi
  11. unset INSTALL_TYPE
  12. unset BUILD_TYPE
  13. unset BUILD_TAG
  14. unset IMAGE_TAG
  15. unset AUTO_BUILD
  16. # usage - displays usage instructions
  17. usage() {
  18. echo "usage: ./nm-quick.sh [-e] [-b buildtype] [-t tag] [-a auto]"
  19. echo " -e if specified, will install netmaker EE"
  20. echo " -b type of build; options:"
  21. echo " \"version\" - will install a specific version of Netmaker using remote git and dockerhub"
  22. echo " \"local\": - will install by cloning repo and building images from git"
  23. echo " \"branch\": - will install a specific branch using remote git and dockerhub"
  24. echo " -t tag of build; if buildtype=version, tag=version. If builtype=branch or builtype=local, tag=branch"
  25. echo " -a auto-build; skip prompts and use defaults, if none provided"
  26. echo "examples:"
  27. echo " nm-quick.sh -e -b version -t $LATEST"
  28. echo " nm-quick.sh -e -b local -t feature_v0.17.2_newfeature"
  29. echo " nm-quick.sh -e -b branch -t develop"
  30. exit 1
  31. }
  32. while getopts evab:t: flag; do
  33. case "${flag}" in
  34. e)
  35. INSTALL_TYPE="ee"
  36. ;;
  37. v)
  38. usage
  39. exit 0
  40. ;;
  41. a)
  42. AUTO_BUILD="on"
  43. ;;
  44. b)
  45. BUILD_TYPE=${OPTARG}
  46. if [[ ! "$BUILD_TYPE" =~ ^(version|local|branch)$ ]]; then
  47. echo "error: $BUILD_TYPE is invalid"
  48. echo "valid options: version, local, branch"
  49. usage
  50. exit 1
  51. fi
  52. ;;
  53. t)
  54. BUILD_TAG=${OPTARG}
  55. ;;
  56. esac
  57. done
  58. # print_logo - prints the netmaker logo
  59. print_logo() {
  60. cat <<"EOF"
  61. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  62. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  63. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  64. __ __ ______ ______ __ __ ______ __ __ ______ ______
  65. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  66. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  67. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  68. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  69. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  70. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  71. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  72. EOF
  73. }
  74. # set_buildinfo - sets the information based on script input for how the installation should be run
  75. set_buildinfo() {
  76. if [ -z "$BUILD_TYPE" ]; then
  77. BUILD_TYPE="version"
  78. BUILD_TAG=$LATEST
  79. fi
  80. if [ -z "$BUILD_TAG" ] && [ "$BUILD_TYPE" = "version" ]; then
  81. BUILD_TAG=$LATEST
  82. fi
  83. if [ -z "$BUILD_TAG" ] && [ ! -z "$BUILD_TYPE" ]; then
  84. echo "error: must specify build tag when build type \"$BUILD_TYPE\" is specified"
  85. usage
  86. exit 1
  87. fi
  88. IMAGE_TAG=$(sed 's/\//-/g' <<<"$BUILD_TAG")
  89. if [ "$1" = "ce" ]; then
  90. INSTALL_TYPE="ce"
  91. elif [ "$1" = "ee" ]; then
  92. INSTALL_TYPE="ee"
  93. fi
  94. if [ "$AUTO_BUILD" = "on" ] && [ -z "$INSTALL_TYPE" ]; then
  95. INSTALL_TYPE="ce"
  96. elif [ -z "$INSTALL_TYPE" ]; then
  97. echo "-----------------------------------------------------"
  98. echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (EE)?"
  99. echo "EE will require you to create an account at https://dashboard.license.netmaker.io"
  100. echo "-----------------------------------------------------"
  101. select install_option in "Community Edition" "Enterprise Edition"; do
  102. case $REPLY in
  103. 1)
  104. echo "installing Netmaker CE"
  105. INSTALL_TYPE="ce"
  106. break
  107. ;;
  108. 2)
  109. echo "installing Netmaker EE"
  110. INSTALL_TYPE="ee"
  111. break
  112. ;;
  113. *) echo "invalid option $REPLY" ;;
  114. esac
  115. done
  116. fi
  117. echo "-----------Build Options-----------------------------"
  118. echo " EE or CE: $INSTALL_TYPE"
  119. echo " Build Type: $BUILD_TYPE"
  120. echo " Build Tag: $BUILD_TAG"
  121. echo " Image Tag: $IMAGE_TAG"
  122. echo "-----------------------------------------------------"
  123. }
  124. # install_yq - install yq if not present
  125. install_yq() {
  126. if ! command -v yq &>/dev/null; then
  127. wget -O /usr/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_$(dpkg --print-architecture)
  128. chmod +x /usr/bin/yq
  129. fi
  130. set +e
  131. if ! command -v yq &>/dev/null; then
  132. set -e
  133. wget -O /usr/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_amd64
  134. chmod +x /usr/bin/yq
  135. fi
  136. set -e
  137. if ! command -v yq &>/dev/null; then
  138. echo "failed to install yq. Please install yq and try again."
  139. echo "https://github.com/mikefarah/yq/#install"
  140. exit 1
  141. fi
  142. }
  143. # setup_netclient - adds netclient to docker-compose
  144. setup_netclient() {
  145. set +e
  146. netclient uninstall
  147. set -e
  148. wget -O netclient https://github.com/gravitl/netclient/releases/download/$LATEST/netclient-linux-amd64
  149. chmod +x netclient
  150. ./netclient install
  151. netclient register -t $TOKEN
  152. echo "waiting for client to become available"
  153. wait_seconds 10
  154. }
  155. # configure_netclient - configures server's netclient as a default host and an ingress gateway
  156. configure_netclient() {
  157. NODE_ID=$(sudo cat /etc/netclient/nodes.yml | yq -r .netmaker.commonnode.id)
  158. echo "register complete. New node ID: $NODE_ID"
  159. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  160. echo "making host a default"
  161. echo "Host ID: $HOST_ID"
  162. # set as a default host
  163. set +e
  164. nmctl host update $HOST_ID --default
  165. sleep 5
  166. nmctl node create_ingress netmaker $NODE_ID
  167. set -e
  168. }
  169. # setup_nmctl - pulls nmctl and makes it executable
  170. setup_nmctl() {
  171. wget -O /usr/bin/nmctl https://github.com/gravitl/netmaker/releases/download/$LATEST/nmctl-linux-amd64
  172. chmod +x /usr/bin/nmctl
  173. echo "using server api.$NETMAKER_BASE_DOMAIN"
  174. echo "using master key $MASTER_KEY"
  175. nmctl context set default --endpoint="https://api.$NETMAKER_BASE_DOMAIN" --master_key="$MASTER_KEY"
  176. nmctl context use default
  177. RESP=$(nmctl network list)
  178. if [[ $RESP == *"unauthorized"* ]]; then
  179. echo "Unable to properly configure NMCTL, exiting..."
  180. exit 1
  181. fi
  182. }
  183. # wait_seconds - wait for the specified period of time
  184. wait_seconds() { (
  185. for ((a = 1; a <= $1; a++)); do
  186. echo ". . ."
  187. sleep 1
  188. done
  189. ); }
  190. # confirm - get user input to confirm that they want to perform the next step
  191. confirm() { (
  192. if [ "$AUTO_BUILD" = "on" ]; then
  193. return 0
  194. fi
  195. while true; do
  196. read -p 'Does everything look right? [y/n]: ' yn
  197. case $yn in
  198. [Yy]*)
  199. override="true"
  200. break
  201. ;;
  202. [Nn]*)
  203. echo "exiting..."
  204. exit 1
  205. # TODO start from the beginning instead
  206. ;;
  207. *) echo "Please answer yes or no." ;;
  208. esac
  209. done
  210. ) }
  211. save_config() { (
  212. echo "Saving the config to $CONFIG_PATH"
  213. touch "$CONFIG_PATH"
  214. # email
  215. if grep -q "^NM_EMAIL=" "$CONFIG_PATH"; then
  216. sed -i "s/NM_EMAIL=.*/NM_EMAIL=$EMAIL/" "$CONFIG_PATH"
  217. else
  218. echo "NM_EMAIL=$EMAIL" >>"$CONFIG_PATH"
  219. fi
  220. # domain
  221. if grep -q "^NM_DOMAIN=" "$CONFIG_PATH"; then
  222. sed -i "s/NM_DOMAIN=.*/NM_DOMAIN=$NETMAKER_BASE_DOMAIN/" "$CONFIG_PATH"
  223. else
  224. echo "NM_DOMAIN=$NETMAKER_BASE_DOMAIN" >>"$CONFIG_PATH"
  225. fi
  226. )}
  227. # local_install_setup - builds artifacts based on specified branch locally to use in install
  228. local_install_setup() { (
  229. rm -rf netmaker-tmp
  230. mkdir netmaker-tmp
  231. cd netmaker-tmp
  232. git clone --single-branch --depth=1 --branch=$BUILD_TAG https://www.github.com/gravitl/netmaker
  233. cd netmaker
  234. docker build --no-cache --build-arg version=$IMAGE_TAG -t gravitl/netmaker:$IMAGE_TAG .
  235. if [ "$INSTALL_TYPE" = "ee" ]; then
  236. cp compose/docker-compose.ee.yml /root/docker-compose.yml
  237. cp docker/Caddyfile-EE /root/Caddyfile
  238. else
  239. cp compose/docker-compose.yml /root/docker-compose.yml
  240. cp docker/Caddyfile /root/Caddyfile
  241. fi
  242. cp scripts/nm-certs.sh /root/nm-certs.sh
  243. cp docker/mosquitto.conf /root/mosquitto.conf
  244. cp docker/wait.sh /root/wait.sh
  245. cd ../../
  246. rm -rf netmaker-tmp
  247. ); }
  248. # install_dependencies - install necessary packages to run netmaker
  249. install_dependencies() {
  250. echo "checking dependencies..."
  251. OS=$(uname)
  252. if [ -f /etc/debian_version ]; then
  253. dependencies="git wireguard wireguard-tools dnsutils jq docker.io docker-compose"
  254. update_cmd='apt update'
  255. install_cmd='apt-get install -y'
  256. elif [ -f /etc/alpine-release ]; then
  257. dependencies="git wireguard jq docker.io docker-compose"
  258. update_cmd='apk update'
  259. install_cmd='apk --update add'
  260. elif [ -f /etc/centos-release ]; then
  261. dependencies="git wireguard jq bind-utils docker.io docker-compose"
  262. update_cmd='yum update'
  263. install_cmd='yum install -y'
  264. elif [ -f /etc/fedora-release ]; then
  265. dependencies="git wireguard bind-utils jq docker.io docker-compose"
  266. update_cmd='dnf update'
  267. install_cmd='dnf install -y'
  268. elif [ -f /etc/redhat-release ]; then
  269. dependencies="git wireguard jq docker.io bind-utils docker-compose"
  270. update_cmd='yum update'
  271. install_cmd='yum install -y'
  272. elif [ -f /etc/arch-release ]; then
  273. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose"
  274. update_cmd='pacman -Sy'
  275. install_cmd='pacman -S --noconfirm'
  276. elif [ "${OS}" = "FreeBSD" ]; then
  277. dependencies="git wireguard wget jq docker.io docker-compose"
  278. update_cmd='pkg update'
  279. install_cmd='pkg install -y'
  280. else
  281. install_cmd=''
  282. fi
  283. if [ -z "${install_cmd}" ]; then
  284. echo "OS unsupported for automatic dependency install"
  285. # TODO shouldnt exit, check if deps available, if not
  286. # ask the user to install manually and continue when ready
  287. exit 1
  288. fi
  289. set -- $dependencies
  290. ${update_cmd}
  291. while [ -n "$1" ]; do
  292. if [ "${OS}" = "FreeBSD" ]; then
  293. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  294. if [ "$is_installed" != "" ]; then
  295. echo " " $1 is installed
  296. else
  297. echo " " $1 is not installed. Attempting install.
  298. ${install_cmd} $1
  299. sleep 5
  300. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  301. if [ "$is_installed" != "" ]; then
  302. echo " " $1 is installed
  303. elif [ -x "$(command -v $1)" ]; then
  304. echo " " $1 is installed
  305. else
  306. echo " " FAILED TO INSTALL $1
  307. echo " " This may break functionality.
  308. fi
  309. fi
  310. else
  311. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  312. is_installed=$(opkg list-installed $1 | grep $1)
  313. else
  314. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  315. fi
  316. if [ "${is_installed}" != "" ]; then
  317. echo " " $1 is installed
  318. else
  319. echo " " $1 is not installed. Attempting install.
  320. ${install_cmd} $1
  321. sleep 5
  322. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  323. is_installed=$(opkg list-installed $1 | grep $1)
  324. else
  325. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  326. fi
  327. if [ "${is_installed}" != "" ]; then
  328. echo " " $1 is installed
  329. elif [ -x "$(command -v $1)" ]; then
  330. echo " " $1 is installed
  331. else
  332. echo " " FAILED TO INSTALL $1
  333. echo " " This may break functionality.
  334. fi
  335. fi
  336. fi
  337. shift
  338. done
  339. echo "-----------------------------------------------------"
  340. echo "dependency check complete"
  341. echo "-----------------------------------------------------"
  342. }
  343. set -e
  344. # set_install_vars - sets the variables that will be used throughout installation
  345. set_install_vars() {
  346. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  347. if [ "$IP_ADDR" = "" ]; then
  348. IP_ADDR=$(curl -s ifconfig.me)
  349. fi
  350. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  351. # TODO dead code?
  352. # COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  353. SERVER_PUBLIC_IP=$IP_ADDR
  354. MASTER_KEY=$(
  355. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  356. echo ''
  357. )
  358. DOMAIN_TYPE=""
  359. echo "-----------------------------------------------------"
  360. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  361. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_PUBLIC_IP"
  362. echo "IMPORTANT: Due to the high volume of requests, the auto-generated domain has been rate-limited by the certificate provider."
  363. echo "For this reason, we STRONGLY RECOMMEND using your own domain. Using the auto-generated domain may lead to a failed installation due to rate limiting."
  364. echo "-----------------------------------------------------"
  365. if [ "$AUTO_BUILD" = "on" ]; then
  366. DOMAIN_TYPE="auto"
  367. else
  368. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  369. case $REPLY in
  370. 1)
  371. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  372. DOMAIN_TYPE="auto"
  373. break
  374. ;;
  375. 2)
  376. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_PUBLIC_IP first): " domain
  377. NETMAKER_BASE_DOMAIN=$domain
  378. echo "using $NETMAKER_BASE_DOMAIN"
  379. DOMAIN_TYPE="custom"
  380. break
  381. ;;
  382. *) echo "invalid option $REPLY" ;;
  383. esac
  384. done
  385. fi
  386. wait_seconds 2
  387. echo "-----------------------------------------------------"
  388. echo "The following subdomains will be used:"
  389. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  390. echo " api.$NETMAKER_BASE_DOMAIN"
  391. echo " broker.$NETMAKER_BASE_DOMAIN"
  392. echo " stun.$NETMAKER_BASE_DOMAIN"
  393. echo " turn.$NETMAKER_BASE_DOMAIN"
  394. echo " turnapi.$NETMAKER_BASE_DOMAIN"
  395. if [ "$INSTALL_TYPE" = "ee" ]; then
  396. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  397. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  398. echo " grafana.$NETMAKER_BASE_DOMAIN"
  399. fi
  400. echo "-----------------------------------------------------"
  401. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  402. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_PUBLIC_IP"
  403. confirm
  404. fi
  405. wait_seconds 1
  406. if [ "$INSTALL_TYPE" = "ee" ]; then
  407. echo "-----------------------------------------------------"
  408. echo "Provide Details for EE installation:"
  409. echo " 1. Log into https://dashboard.license.netmaker.io"
  410. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  411. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  412. echo " 4. note email address"
  413. echo "-----------------------------------------------------"
  414. unset LICENSE_KEY
  415. while [ -z "$LICENSE_KEY" ]; do
  416. read -p "License Key: " LICENSE_KEY
  417. done
  418. unset ACCOUNT_ID
  419. while [ -z ${ACCOUNT_ID} ]; do
  420. read -p "Account ID: " ACCOUNT_ID
  421. done
  422. fi
  423. # read the config file
  424. if [ -f "$CONFIG_PATH" ]; then
  425. echo "Reading config from $CONFIG_PATH"
  426. source "$CONFIG_PATH"
  427. fi
  428. unset GET_EMAIL
  429. unset RAND_EMAIL
  430. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  431. # suggest the prev email or a random one
  432. EMAIL_SUGGESTED=${NM_EMAIL:-$RAND_EMAIL}
  433. if [ -z $AUTO_BUILD ]; then
  434. read -p "Email Address for Domain Registration (click 'enter' to use $EMAIL_SUGGESTED): " GET_EMAIL
  435. fi
  436. if [ -z "$GET_EMAIL" ]; then
  437. EMAIL="$EMAIL_SUGGESTED"
  438. if [ "$EMAIL" = "$NM_EMAIL" ]; then
  439. echo "using config email"
  440. else
  441. echo "using rand email"
  442. fi
  443. else
  444. EMAIL="$GET_EMAIL"
  445. fi
  446. wait_seconds 1
  447. unset GET_MQ_USERNAME
  448. unset GET_MQ_PASSWORD
  449. unset CONFIRM_MQ_PASSWORD
  450. echo "Enter Credentials For MQ..."
  451. if [ -z $AUTO_BUILD ]; then
  452. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  453. fi
  454. if [ -z "$GET_MQ_USERNAME" ]; then
  455. echo "using default username for mq"
  456. MQ_USERNAME="netmaker"
  457. else
  458. MQ_USERNAME="$GET_MQ_USERNAME"
  459. fi
  460. MQ_PASSWORD=$(
  461. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  462. echo ''
  463. )
  464. if [ -z $AUTO_BUILD ]; then
  465. select domain_option in "Auto Generated Password" "Input Your Own Password"; do
  466. case $REPLY in
  467. 1)
  468. echo "using random password for mq"
  469. break
  470. ;;
  471. 2)
  472. while true; do
  473. echo "Enter your Password For MQ: "
  474. read -s GET_MQ_PASSWORD
  475. echo "Enter your password again to confirm: "
  476. read -s CONFIRM_MQ_PASSWORD
  477. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  478. echo "wrong password entered, try again..."
  479. continue
  480. fi
  481. MQ_PASSWORD="$GET_MQ_PASSWORD"
  482. echo "MQ Password Saved Successfully!!"
  483. break
  484. done
  485. break
  486. ;;
  487. *) echo "invalid option $REPLY" ;;
  488. esac
  489. done
  490. fi
  491. unset GET_TURN_USERNAME
  492. unset GET_TURN_PASSWORD
  493. unset CONFIRM_TURN_PASSWORD
  494. echo "Enter Credentials For TURN..."
  495. if [ -z $AUTO_BUILD ]; then
  496. read -p "TURN Username (click 'enter' to use 'netmaker'): " GET_TURN_USERNAME
  497. fi
  498. if [ -z "$GET_TURN_USERNAME" ]; then
  499. echo "using default username for TURN"
  500. TURN_USERNAME="netmaker"
  501. else
  502. TURN_USERNAME="$GET_TURN_USERNAME"
  503. fi
  504. TURN_PASSWORD=$(
  505. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  506. echo ''
  507. )
  508. if [ -z $AUTO_BUILD ]; then
  509. select domain_option in "Auto Generated Password" "Input Your Own Password"; do
  510. case $REPLY in
  511. 1)
  512. echo "using random password for turn"
  513. break
  514. ;;
  515. 2)
  516. while true; do
  517. echo "Enter your Password For TURN: "
  518. read -s GET_TURN_PASSWORD
  519. echo "Enter your password again to confirm: "
  520. read -s CONFIRM_TURN_PASSWORD
  521. if [ ${GET_TURN_PASSWORD} != ${CONFIRM_TURN_PASSWORD} ]; then
  522. echo "wrong password entered, try again..."
  523. continue
  524. fi
  525. TURN_PASSWORD="$GET_TURN_PASSWORD"
  526. echo "TURN Password Saved Successfully!!"
  527. break
  528. done
  529. break
  530. ;;
  531. *) echo "invalid option $REPLY" ;;
  532. esac
  533. done
  534. fi
  535. wait_seconds 2
  536. echo "-----------------------------------------------------------------"
  537. echo " SETUP ARGUMENTS"
  538. echo "-----------------------------------------------------------------"
  539. echo " domain: $NETMAKER_BASE_DOMAIN"
  540. echo " email: $EMAIL"
  541. echo " public ip: $SERVER_PUBLIC_IP"
  542. if [ "$INSTALL_TYPE" = "ee" ]; then
  543. echo " license: $LICENSE_KEY"
  544. echo " account id: $ACCOUNT_ID"
  545. fi
  546. echo "-----------------------------------------------------------------"
  547. echo "Confirm Settings for Installation"
  548. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  549. confirm
  550. save_config
  551. }
  552. # install_netmaker - sets the config files and starts docker-compose
  553. install_netmaker() {
  554. echo "-----------------------------------------------------------------"
  555. echo "Beginning installation..."
  556. echo "-----------------------------------------------------------------"
  557. wait_seconds 3
  558. # TODO extract wgets to setup(), mirror local_setup()
  559. echo "Pulling config files..."
  560. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.yml"
  561. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile"
  562. if [ "$INSTALL_TYPE" = "ee" ]; then
  563. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.ee.yml"
  564. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile-EE"
  565. fi
  566. if [ ! "$BUILD_TYPE" = "local" ]; then
  567. wget -qO /root/docker-compose.yml $COMPOSE_URL
  568. wget -qO /root/Caddyfile $CADDY_URL
  569. wget -qO /root/mosquitto.conf "https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/mosquitto.conf"
  570. wget -qO /root/nm-certs.sh "https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/scripts/nm-certs.sh"
  571. wget -qO /root/wait.sh "https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/wait.sh"
  572. fi
  573. chmod +x /root/wait.sh
  574. mkdir -p /etc/netmaker
  575. echo "Setting docker-compose and Caddyfile..."
  576. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  577. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  578. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  579. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  580. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  581. sed -i "s/REPLACE_MQ_USERNAME/$MQ_USERNAME/g" /root/docker-compose.yml
  582. sed -i "s/REPLACE_MQ_PASSWORD/$MQ_PASSWORD/g" /root/docker-compose.yml
  583. sed -i "s/REPLACE_TURN_USERNAME/$TURN_USERNAME/g" /root/docker-compose.yml
  584. sed -i "s/REPLACE_TURN_PASSWORD/$TURN_PASSWORD/g" /root/docker-compose.yml
  585. if [ "$INSTALL_TYPE" = "ee" ]; then
  586. sed -i "s~YOUR_LICENSE_KEY~$LICENSE_KEY~g" /root/docker-compose.yml
  587. sed -i "s/YOUR_ACCOUNT_ID/$ACCOUNT_ID/g" /root/docker-compose.yml
  588. fi
  589. if [ "$BUILD_TYPE" = "version" ] && [ "$INSTALL_TYPE" = "ee" ]; then
  590. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG-ee/g" /root/docker-compose.yml
  591. else
  592. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  593. fi
  594. if [ "$BUILD_TYPE" = "local" ]; then
  595. sed -i "s/REPLACE_UI_IMAGE_TAG/$LATEST/g" /root/docker-compose.yml
  596. else
  597. sed -i "s/REPLACE_UI_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  598. fi
  599. echo "Starting containers..."
  600. # increase the timeouts
  601. export DOCKER_CLIENT_TIMEOUT=120
  602. export COMPOSE_HTTP_TIMEOUT=120
  603. # start docker and rebuild containers / networks
  604. docker-compose -f /root/docker-compose.yml up -d --force-recreate
  605. wait_seconds 2
  606. }
  607. # test_connection - tests to make sure Caddy has proper SSL certs
  608. test_connection() {
  609. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  610. for i in 1 2 3 4 5 6 7 8; do
  611. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  612. if [[ "$i" == 8 ]]; then
  613. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  614. echo " Exiting..."
  615. exit 1
  616. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  617. echo " Certificates not yet configured, retrying..."
  618. elif [[ "$curlresponse" == *"left intact"* ]]; then
  619. echo " Certificates ok"
  620. break
  621. else
  622. secs=$(($i * 5 + 10))
  623. echo " Issue establishing connection...retrying in $secs seconds..."
  624. fi
  625. sleep $secs
  626. done
  627. }
  628. # setup_mesh - sets up a default mesh network on the server
  629. setup_mesh() {
  630. wait_seconds 5
  631. echo "Creating netmaker network (10.101.0.0/16)"
  632. nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16
  633. wait_seconds 5
  634. echo "Creating netmaker enrollment key"
  635. tokenJson=$(nmctl enrollment_key create --unlimited --networks netmaker)
  636. TOKEN=$(jq -r '.token' <<<${tokenJson})
  637. wait_seconds 3
  638. }
  639. # print_success - prints a success message upon completion
  640. print_success() {
  641. echo "-----------------------------------------------------------------"
  642. echo "-----------------------------------------------------------------"
  643. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  644. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  645. echo "-----------------------------------------------------------------"
  646. echo "-----------------------------------------------------------------"
  647. }
  648. # 1. print netmaker logo
  649. print_logo
  650. # 2. setup the build instructions
  651. set_buildinfo
  652. set +e
  653. # 3. install necessary packages
  654. install_dependencies
  655. # 4. install yq if necessary
  656. install_yq
  657. # 5. if running a local build, clone git and build artifacts
  658. if [ "$BUILD_TYPE" = "local" ]; then
  659. local_install_setup
  660. fi
  661. set -e
  662. # 6. get user input for variables
  663. set_install_vars
  664. # 7. get and set config files, startup docker-compose
  665. install_netmaker
  666. # Fetch / update certs using certbot
  667. "$SCRIPT_DIR"/nm-certs.sh
  668. set +e
  669. # 8. make sure Caddy certs are working
  670. test_connection
  671. # 9. install the netmaker CLI
  672. setup_nmctl
  673. # 10. create a default mesh network for netmaker
  674. setup_mesh
  675. set -e
  676. # 11. add netclient to docker-compose and start it up
  677. setup_netclient
  678. # 12. make the netclient a default host and ingress gw
  679. configure_netclient
  680. # 13. print success message
  681. print_success
  682. # cp -f /etc/skel/.bashrc /root/.bashrc