nm-quick.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. if ! $NM_SKIP_BUILD; then
  235. docker build --no-cache --build-arg version=$IMAGE_TAG -t gravitl/netmaker:$IMAGE_TAG .
  236. else
  237. echo "Skipping build on NM_SKIP_BUILD"
  238. fi
  239. if [ "$INSTALL_TYPE" = "ee" ]; then
  240. cp compose/docker-compose.ee.yml /root/docker-compose.yml
  241. cp docker/Caddyfile-EE /root/Caddyfile
  242. else
  243. cp compose/docker-compose.yml /root/docker-compose.yml
  244. cp docker/Caddyfile /root/Caddyfile
  245. fi
  246. cp scripts/nm-certs.sh /root/nm-certs.sh
  247. cp docker/mosquitto.conf /root/mosquitto.conf
  248. cp docker/wait.sh /root/wait.sh
  249. cd ../../
  250. rm -rf netmaker-tmp
  251. ); }
  252. # install_dependencies - install necessary packages to run netmaker
  253. install_dependencies() {
  254. echo "checking dependencies..."
  255. OS=$(uname)
  256. if [ -f /etc/debian_version ]; then
  257. dependencies="git wireguard wireguard-tools dnsutils jq docker.io docker-compose"
  258. update_cmd='apt update'
  259. install_cmd='apt-get install -y'
  260. elif [ -f /etc/alpine-release ]; then
  261. dependencies="git wireguard jq docker.io docker-compose"
  262. update_cmd='apk update'
  263. install_cmd='apk --update add'
  264. elif [ -f /etc/centos-release ]; then
  265. dependencies="git wireguard jq bind-utils docker.io docker-compose"
  266. update_cmd='yum update'
  267. install_cmd='yum install -y'
  268. elif [ -f /etc/fedora-release ]; then
  269. dependencies="git wireguard bind-utils jq docker.io docker-compose"
  270. update_cmd='dnf update'
  271. install_cmd='dnf install -y'
  272. elif [ -f /etc/redhat-release ]; then
  273. dependencies="git wireguard jq docker.io bind-utils docker-compose"
  274. update_cmd='yum update'
  275. install_cmd='yum install -y'
  276. elif [ -f /etc/arch-release ]; then
  277. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose"
  278. update_cmd='pacman -Sy'
  279. install_cmd='pacman -S --noconfirm'
  280. elif [ "${OS}" = "FreeBSD" ]; then
  281. dependencies="git wireguard wget jq docker.io docker-compose"
  282. update_cmd='pkg update'
  283. install_cmd='pkg install -y'
  284. else
  285. install_cmd=''
  286. fi
  287. if [ -z "${install_cmd}" ]; then
  288. echo "OS unsupported for automatic dependency install"
  289. # TODO shouldnt exit, check if deps available, if not
  290. # ask the user to install manually and continue when ready
  291. exit 1
  292. fi
  293. set -- $dependencies
  294. ${update_cmd}
  295. while [ -n "$1" ]; do
  296. if [ "${OS}" = "FreeBSD" ]; then
  297. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  298. if [ "$is_installed" != "" ]; then
  299. echo " " $1 is installed
  300. else
  301. echo " " $1 is not installed. Attempting install.
  302. ${install_cmd} $1
  303. sleep 5
  304. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  305. if [ "$is_installed" != "" ]; then
  306. echo " " $1 is installed
  307. elif [ -x "$(command -v $1)" ]; then
  308. echo " " $1 is installed
  309. else
  310. echo " " FAILED TO INSTALL $1
  311. echo " " This may break functionality.
  312. fi
  313. fi
  314. else
  315. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  316. is_installed=$(opkg list-installed $1 | grep $1)
  317. else
  318. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  319. fi
  320. if [ "${is_installed}" != "" ]; then
  321. echo " " $1 is installed
  322. else
  323. echo " " $1 is not installed. Attempting install.
  324. ${install_cmd} $1
  325. sleep 5
  326. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  327. is_installed=$(opkg list-installed $1 | grep $1)
  328. else
  329. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  330. fi
  331. if [ "${is_installed}" != "" ]; then
  332. echo " " $1 is installed
  333. elif [ -x "$(command -v $1)" ]; then
  334. echo " " $1 is installed
  335. else
  336. echo " " FAILED TO INSTALL $1
  337. echo " " This may break functionality.
  338. fi
  339. fi
  340. fi
  341. shift
  342. done
  343. echo "-----------------------------------------------------"
  344. echo "dependency check complete"
  345. echo "-----------------------------------------------------"
  346. }
  347. set -e
  348. # set_install_vars - sets the variables that will be used throughout installation
  349. set_install_vars() {
  350. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  351. if [ "$IP_ADDR" = "" ]; then
  352. IP_ADDR=$(curl -s ifconfig.me)
  353. fi
  354. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  355. # TODO dead code?
  356. # COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  357. SERVER_PUBLIC_IP=$IP_ADDR
  358. MASTER_KEY=$(
  359. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  360. echo ''
  361. )
  362. DOMAIN_TYPE=""
  363. echo "-----------------------------------------------------"
  364. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  365. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_PUBLIC_IP"
  366. echo "IMPORTANT: Due to the high volume of requests, the auto-generated domain has been rate-limited by the certificate provider."
  367. 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."
  368. echo "-----------------------------------------------------"
  369. if [ "$AUTO_BUILD" = "on" ]; then
  370. DOMAIN_TYPE="auto"
  371. else
  372. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  373. case $REPLY in
  374. 1)
  375. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  376. DOMAIN_TYPE="auto"
  377. break
  378. ;;
  379. 2)
  380. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_PUBLIC_IP first): " domain
  381. NETMAKER_BASE_DOMAIN=$domain
  382. echo "using $NETMAKER_BASE_DOMAIN"
  383. DOMAIN_TYPE="custom"
  384. break
  385. ;;
  386. *) echo "invalid option $REPLY" ;;
  387. esac
  388. done
  389. fi
  390. wait_seconds 2
  391. echo "-----------------------------------------------------"
  392. echo "The following subdomains will be used:"
  393. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  394. echo " api.$NETMAKER_BASE_DOMAIN"
  395. echo " broker.$NETMAKER_BASE_DOMAIN"
  396. echo " stun.$NETMAKER_BASE_DOMAIN"
  397. echo " turn.$NETMAKER_BASE_DOMAIN"
  398. echo " turnapi.$NETMAKER_BASE_DOMAIN"
  399. if [ "$INSTALL_TYPE" = "ee" ]; then
  400. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  401. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  402. echo " grafana.$NETMAKER_BASE_DOMAIN"
  403. fi
  404. echo "-----------------------------------------------------"
  405. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  406. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_PUBLIC_IP"
  407. confirm
  408. fi
  409. wait_seconds 1
  410. if [ "$INSTALL_TYPE" = "ee" ]; then
  411. echo "-----------------------------------------------------"
  412. echo "Provide Details for EE installation:"
  413. echo " 1. Log into https://dashboard.license.netmaker.io"
  414. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  415. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  416. echo " 4. note email address"
  417. echo "-----------------------------------------------------"
  418. unset LICENSE_KEY
  419. while [ -z "$LICENSE_KEY" ]; do
  420. read -p "License Key: " LICENSE_KEY
  421. done
  422. unset ACCOUNT_ID
  423. while [ -z ${ACCOUNT_ID} ]; do
  424. read -p "Account ID: " ACCOUNT_ID
  425. done
  426. fi
  427. # read the config file
  428. if [ -f "$CONFIG_PATH" ]; then
  429. echo "Reading config from $CONFIG_PATH"
  430. source "$CONFIG_PATH"
  431. fi
  432. unset GET_EMAIL
  433. unset RAND_EMAIL
  434. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  435. # suggest the prev email or a random one
  436. EMAIL_SUGGESTED=${NM_EMAIL:-$RAND_EMAIL}
  437. if [ -z $AUTO_BUILD ]; then
  438. read -p "Email Address for Domain Registration (click 'enter' to use $EMAIL_SUGGESTED): " GET_EMAIL
  439. fi
  440. if [ -z "$GET_EMAIL" ]; then
  441. EMAIL="$EMAIL_SUGGESTED"
  442. if [ "$EMAIL" = "$NM_EMAIL" ]; then
  443. echo "using config email"
  444. else
  445. echo "using rand email"
  446. fi
  447. else
  448. EMAIL="$GET_EMAIL"
  449. fi
  450. wait_seconds 1
  451. unset GET_MQ_USERNAME
  452. unset GET_MQ_PASSWORD
  453. unset CONFIRM_MQ_PASSWORD
  454. echo "Enter Credentials For MQ..."
  455. if [ -z $AUTO_BUILD ]; then
  456. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  457. fi
  458. if [ -z "$GET_MQ_USERNAME" ]; then
  459. echo "using default username for mq"
  460. MQ_USERNAME="netmaker"
  461. else
  462. MQ_USERNAME="$GET_MQ_USERNAME"
  463. fi
  464. MQ_PASSWORD=$(
  465. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  466. echo ''
  467. )
  468. if [ -z $AUTO_BUILD ]; then
  469. select domain_option in "Auto Generated Password" "Input Your Own Password"; do
  470. case $REPLY in
  471. 1)
  472. echo "using random password for mq"
  473. break
  474. ;;
  475. 2)
  476. while true; do
  477. echo "Enter your Password For MQ: "
  478. read -s GET_MQ_PASSWORD
  479. echo "Enter your password again to confirm: "
  480. read -s CONFIRM_MQ_PASSWORD
  481. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  482. echo "wrong password entered, try again..."
  483. continue
  484. fi
  485. MQ_PASSWORD="$GET_MQ_PASSWORD"
  486. echo "MQ Password Saved Successfully!!"
  487. break
  488. done
  489. break
  490. ;;
  491. *) echo "invalid option $REPLY" ;;
  492. esac
  493. done
  494. fi
  495. unset GET_TURN_USERNAME
  496. unset GET_TURN_PASSWORD
  497. unset CONFIRM_TURN_PASSWORD
  498. echo "Enter Credentials For TURN..."
  499. if [ -z $AUTO_BUILD ]; then
  500. read -p "TURN Username (click 'enter' to use 'netmaker'): " GET_TURN_USERNAME
  501. fi
  502. if [ -z "$GET_TURN_USERNAME" ]; then
  503. echo "using default username for TURN"
  504. TURN_USERNAME="netmaker"
  505. else
  506. TURN_USERNAME="$GET_TURN_USERNAME"
  507. fi
  508. TURN_PASSWORD=$(
  509. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  510. echo ''
  511. )
  512. if [ -z $AUTO_BUILD ]; then
  513. select domain_option in "Auto Generated Password" "Input Your Own Password"; do
  514. case $REPLY in
  515. 1)
  516. echo "using random password for turn"
  517. break
  518. ;;
  519. 2)
  520. while true; do
  521. echo "Enter your Password For TURN: "
  522. read -s GET_TURN_PASSWORD
  523. echo "Enter your password again to confirm: "
  524. read -s CONFIRM_TURN_PASSWORD
  525. if [ ${GET_TURN_PASSWORD} != ${CONFIRM_TURN_PASSWORD} ]; then
  526. echo "wrong password entered, try again..."
  527. continue
  528. fi
  529. TURN_PASSWORD="$GET_TURN_PASSWORD"
  530. echo "TURN Password Saved Successfully!!"
  531. break
  532. done
  533. break
  534. ;;
  535. *) echo "invalid option $REPLY" ;;
  536. esac
  537. done
  538. fi
  539. wait_seconds 2
  540. echo "-----------------------------------------------------------------"
  541. echo " SETUP ARGUMENTS"
  542. echo "-----------------------------------------------------------------"
  543. echo " domain: $NETMAKER_BASE_DOMAIN"
  544. echo " email: $EMAIL"
  545. echo " public ip: $SERVER_PUBLIC_IP"
  546. if [ "$INSTALL_TYPE" = "ee" ]; then
  547. echo " license: $LICENSE_KEY"
  548. echo " account id: $ACCOUNT_ID"
  549. fi
  550. echo "-----------------------------------------------------------------"
  551. echo "Confirm Settings for Installation"
  552. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  553. confirm
  554. save_config
  555. }
  556. # install_netmaker - sets the config files and starts docker-compose
  557. install_netmaker() {
  558. echo "-----------------------------------------------------------------"
  559. echo "Beginning installation..."
  560. echo "-----------------------------------------------------------------"
  561. wait_seconds 3
  562. # TODO extract wgets to setup(), mirror local_setup()
  563. echo "Pulling config files..."
  564. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.yml"
  565. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile"
  566. if [ "$INSTALL_TYPE" = "ee" ]; then
  567. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.ee.yml"
  568. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile-EE"
  569. fi
  570. if [ ! "$BUILD_TYPE" = "local" ]; then
  571. wget -qO /root/docker-compose.yml $COMPOSE_URL
  572. wget -qO /root/Caddyfile $CADDY_URL
  573. wget -qO /root/mosquitto.conf "https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/mosquitto.conf"
  574. wget -qO /root/nm-certs.sh "https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/scripts/nm-certs.sh"
  575. wget -qO /root/wait.sh "https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/wait.sh"
  576. fi
  577. chmod +x /root/wait.sh
  578. mkdir -p /etc/netmaker
  579. echo "Setting docker-compose and Caddyfile..."
  580. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  581. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  582. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  583. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  584. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  585. sed -i "s/REPLACE_MQ_USERNAME/$MQ_USERNAME/g" /root/docker-compose.yml
  586. sed -i "s/REPLACE_MQ_PASSWORD/$MQ_PASSWORD/g" /root/docker-compose.yml
  587. sed -i "s/REPLACE_TURN_USERNAME/$TURN_USERNAME/g" /root/docker-compose.yml
  588. sed -i "s/REPLACE_TURN_PASSWORD/$TURN_PASSWORD/g" /root/docker-compose.yml
  589. if [ "$INSTALL_TYPE" = "ee" ]; then
  590. sed -i "s~YOUR_LICENSE_KEY~$LICENSE_KEY~g" /root/docker-compose.yml
  591. sed -i "s/YOUR_ACCOUNT_ID/$ACCOUNT_ID/g" /root/docker-compose.yml
  592. fi
  593. if [ "$BUILD_TYPE" = "version" ] && [ "$INSTALL_TYPE" = "ee" ]; then
  594. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG-ee/g" /root/docker-compose.yml
  595. else
  596. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  597. fi
  598. if [ "$BUILD_TYPE" = "local" ]; then
  599. sed -i "s/REPLACE_UI_IMAGE_TAG/$LATEST/g" /root/docker-compose.yml
  600. else
  601. sed -i "s/REPLACE_UI_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  602. fi
  603. echo "Starting containers..."
  604. # increase the timeouts
  605. export DOCKER_CLIENT_TIMEOUT=120
  606. export COMPOSE_HTTP_TIMEOUT=120
  607. # start docker and rebuild containers / networks
  608. docker-compose -f /root/docker-compose.yml up -d --force-recreate
  609. wait_seconds 2
  610. }
  611. # test_connection - tests to make sure Caddy has proper SSL certs
  612. test_connection() {
  613. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  614. for i in 1 2 3 4 5 6 7 8; do
  615. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  616. if [[ "$i" == 8 ]]; then
  617. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  618. echo " Exiting..."
  619. exit 1
  620. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  621. echo " Certificates not yet configured, retrying..."
  622. elif [[ "$curlresponse" == *"left intact"* ]]; then
  623. echo " Certificates ok"
  624. break
  625. else
  626. secs=$(($i * 5 + 10))
  627. echo " Issue establishing connection...retrying in $secs seconds..."
  628. fi
  629. sleep $secs
  630. done
  631. }
  632. # setup_mesh - sets up a default mesh network on the server
  633. setup_mesh() {
  634. wait_seconds 5
  635. echo "Creating netmaker network (10.101.0.0/16)"
  636. nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16
  637. wait_seconds 5
  638. echo "Creating netmaker enrollment key"
  639. tokenJson=$(nmctl enrollment_key create --unlimited --networks netmaker)
  640. TOKEN=$(jq -r '.token' <<<${tokenJson})
  641. wait_seconds 3
  642. }
  643. # print_success - prints a success message upon completion
  644. print_success() {
  645. echo "-----------------------------------------------------------------"
  646. echo "-----------------------------------------------------------------"
  647. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  648. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  649. echo "-----------------------------------------------------------------"
  650. echo "-----------------------------------------------------------------"
  651. }
  652. # 1. print netmaker logo
  653. print_logo
  654. # 2. setup the build instructions
  655. set_buildinfo
  656. set +e
  657. # 3. install necessary packages
  658. install_dependencies
  659. # 4. install yq if necessary
  660. install_yq
  661. # 5. if running a local build, clone git and build artifacts
  662. if [ "$BUILD_TYPE" = "local" ]; then
  663. local_install_setup
  664. fi
  665. set -e
  666. # 6. get user input for variables
  667. set_install_vars
  668. # stop
  669. for name in "mq" "netmaker-ui" "coredns" "turn" "caddy" "netmaker"; do
  670. if test -n "$(docker ps | grep name)"; then
  671. docker stop $name
  672. fi
  673. done
  674. # Fetch / update certs using certbot
  675. "$SCRIPT_DIR"/nm-certs.sh
  676. # 7. get and set config files, startup docker-compose
  677. install_netmaker
  678. set +e
  679. # 8. make sure Caddy certs are working
  680. test_connection
  681. # 9. install the netmaker CLI
  682. setup_nmctl
  683. # 10. create a default mesh network for netmaker
  684. setup_mesh
  685. set -e
  686. # 11. add netclient to docker-compose and start it up
  687. setup_netclient
  688. # 12. make the netclient a default host and ingress gw
  689. configure_netclient
  690. # 13. print success message
  691. print_success
  692. # cp -f /etc/skel/.bashrc /root/.bashrc