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. # read the config file
  12. if [ -f "$CONFIG_PATH" ]; then
  13. echo "Reading config from $CONFIG_PATH"
  14. source "$CONFIG_PATH"
  15. fi
  16. unset INSTALL_TYPE
  17. unset BUILD_TYPE
  18. unset BUILD_TAG
  19. unset IMAGE_TAG
  20. unset AUTO_BUILD
  21. # usage - displays usage instructions
  22. usage() {
  23. echo "usage: ./nm-quick.sh [-e] [-b buildtype] [-t tag] [-a auto]"
  24. echo " -e if specified, will install netmaker EE"
  25. echo " -b type of build; options:"
  26. echo " \"version\" - will install a specific version of Netmaker using remote git and dockerhub"
  27. echo " \"local\": - will install by cloning repo and building images from git"
  28. echo " \"branch\": - will install a specific branch using remote git and dockerhub"
  29. echo " -t tag of build; if buildtype=version, tag=version. If builtype=branch or builtype=local, tag=branch"
  30. echo " -a auto-build; skip prompts and use defaults, if none provided"
  31. echo "examples:"
  32. echo " nm-quick.sh -e -b version -t $LATEST"
  33. echo " nm-quick.sh -e -b local -t feature_v0.17.2_newfeature"
  34. echo " nm-quick.sh -e -b branch -t develop"
  35. exit 1
  36. }
  37. while getopts evab:t: flag; do
  38. case "${flag}" in
  39. e)
  40. INSTALL_TYPE="ee"
  41. ;;
  42. v)
  43. usage
  44. exit 0
  45. ;;
  46. a)
  47. AUTO_BUILD="on"
  48. ;;
  49. b)
  50. BUILD_TYPE=${OPTARG}
  51. if [[ ! "$BUILD_TYPE" =~ ^(version|local|branch)$ ]]; then
  52. echo "error: $BUILD_TYPE is invalid"
  53. echo "valid options: version, local, branch"
  54. usage
  55. exit 1
  56. fi
  57. ;;
  58. t)
  59. BUILD_TAG=${OPTARG}
  60. ;;
  61. esac
  62. done
  63. # print_logo - prints the netmaker logo
  64. print_logo() {
  65. cat <<"EOF"
  66. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  67. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  68. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  69. __ __ ______ ______ __ __ ______ __ __ ______ ______
  70. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  71. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  72. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  73. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  74. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  75. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  76. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  77. EOF
  78. }
  79. # set_buildinfo - sets the information based on script input for how the installation should be run
  80. set_buildinfo() {
  81. if [ -z "$BUILD_TYPE" ]; then
  82. BUILD_TYPE="version"
  83. BUILD_TAG=$LATEST
  84. fi
  85. if [ -z "$BUILD_TAG" ] && [ "$BUILD_TYPE" = "version" ]; then
  86. BUILD_TAG=$LATEST
  87. fi
  88. if [ -z "$BUILD_TAG" ] && [ ! -z "$BUILD_TYPE" ]; then
  89. echo "error: must specify build tag when build type \"$BUILD_TYPE\" is specified"
  90. usage
  91. exit 1
  92. fi
  93. IMAGE_TAG=$(sed 's/\//-/g' <<<"$BUILD_TAG")
  94. if [ "$1" = "ce" ]; then
  95. INSTALL_TYPE="ce"
  96. elif [ "$1" = "ee" ]; then
  97. INSTALL_TYPE="ee"
  98. fi
  99. if [ "$AUTO_BUILD" = "on" ] && [ -z "$INSTALL_TYPE" ]; then
  100. INSTALL_TYPE="ce"
  101. elif [ -z "$INSTALL_TYPE" ]; then
  102. echo "-----------------------------------------------------"
  103. echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (EE)?"
  104. echo "EE will require you to create an account at https://dashboard.license.netmaker.io"
  105. echo "-----------------------------------------------------"
  106. select install_option in "Community Edition" "Enterprise Edition"; do
  107. case $REPLY in
  108. 1)
  109. echo "installing Netmaker CE"
  110. INSTALL_TYPE="ce"
  111. break
  112. ;;
  113. 2)
  114. echo "installing Netmaker EE"
  115. INSTALL_TYPE="ee"
  116. break
  117. ;;
  118. *) echo "invalid option $REPLY" ;;
  119. esac
  120. done
  121. fi
  122. echo "-----------Build Options-----------------------------"
  123. echo " EE or CE: $INSTALL_TYPE"
  124. echo " Build Type: $BUILD_TYPE"
  125. echo " Build Tag: $BUILD_TAG"
  126. echo " Image Tag: $IMAGE_TAG"
  127. echo "-----------------------------------------------------"
  128. }
  129. # install_yq - install yq if not present
  130. install_yq() {
  131. if ! command -v yq &>/dev/null; then
  132. wget -O /usr/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_$(dpkg --print-architecture)
  133. chmod +x /usr/bin/yq
  134. fi
  135. set +e
  136. if ! command -v yq &>/dev/null; then
  137. set -e
  138. wget -O /usr/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_amd64
  139. chmod +x /usr/bin/yq
  140. fi
  141. set -e
  142. if ! command -v yq &>/dev/null; then
  143. echo "failed to install yq. Please install yq and try again."
  144. echo "https://github.com/mikefarah/yq/#install"
  145. exit 1
  146. fi
  147. }
  148. # setup_netclient - adds netclient to docker-compose
  149. setup_netclient() {
  150. set +e
  151. netclient uninstall
  152. set -e
  153. wget -O netclient https://github.com/gravitl/netclient/releases/download/$LATEST/netclient-linux-amd64
  154. chmod +x netclient
  155. ./netclient install
  156. netclient register -t $TOKEN
  157. echo "waiting for client to become available"
  158. wait_seconds 10
  159. }
  160. # configure_netclient - configures server's netclient as a default host and an ingress gateway
  161. configure_netclient() {
  162. NODE_ID=$(sudo cat /etc/netclient/nodes.yml | yq -r .netmaker.commonnode.id)
  163. echo "register complete. New node ID: $NODE_ID"
  164. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  165. echo "making host a default"
  166. echo "Host ID: $HOST_ID"
  167. # set as a default host
  168. set +e
  169. nmctl host update $HOST_ID --default
  170. sleep 5
  171. nmctl node create_ingress netmaker $NODE_ID
  172. set -e
  173. }
  174. # setup_nmctl - pulls nmctl and makes it executable
  175. setup_nmctl() {
  176. wget -O /usr/bin/nmctl https://github.com/gravitl/netmaker/releases/download/$LATEST/nmctl-linux-amd64
  177. chmod +x /usr/bin/nmctl
  178. echo "using server api.$NETMAKER_BASE_DOMAIN"
  179. echo "using master key $MASTER_KEY"
  180. nmctl context set default --endpoint="https://api.$NETMAKER_BASE_DOMAIN" --master_key="$MASTER_KEY"
  181. nmctl context use default
  182. RESP=$(nmctl network list)
  183. if [[ $RESP == *"unauthorized"* ]]; then
  184. echo "Unable to properly configure NMCTL, exiting..."
  185. exit 1
  186. fi
  187. }
  188. # wait_seconds - wait for the specified period of time
  189. wait_seconds() { (
  190. for ((a = 1; a <= $1; a++)); do
  191. echo ". . ."
  192. sleep 1
  193. done
  194. ); }
  195. # confirm - get user input to confirm that they want to perform the next step
  196. confirm() { (
  197. if [ "$AUTO_BUILD" = "on" ]; then
  198. return 0
  199. fi
  200. while true; do
  201. read -p 'Does everything look right? [y/n]: ' yn
  202. case $yn in
  203. [Yy]*)
  204. override="true"
  205. break
  206. ;;
  207. [Nn]*)
  208. echo "exiting..."
  209. exit 1
  210. # TODO start from the beginning instead
  211. ;;
  212. *) echo "Please answer yes or no." ;;
  213. esac
  214. done
  215. ) }
  216. save_config() { (
  217. echo "Saving the config to $CONFIG_PATH"
  218. touch "$CONFIG_PATH"
  219. # email
  220. if grep -q "^NM_EMAIL=" "$CONFIG_PATH"; then
  221. sed -i "s/NM_EMAIL=.*/NM_EMAIL=$EMAIL/" "$CONFIG_PATH"
  222. else
  223. echo "NM_EMAIL=$EMAIL" >>"$CONFIG_PATH"
  224. fi
  225. # domain
  226. if grep -q "^NM_DOMAIN=" "$CONFIG_PATH"; then
  227. sed -i "s/NM_DOMAIN=.*/NM_DOMAIN=$NETMAKER_BASE_DOMAIN/" "$CONFIG_PATH"
  228. else
  229. echo "NM_DOMAIN=$NETMAKER_BASE_DOMAIN" >>"$CONFIG_PATH"
  230. fi
  231. ); }
  232. # local_install_setup - builds artifacts based on specified branch locally to use in install
  233. local_install_setup() { (
  234. rm -rf netmaker-tmp
  235. mkdir netmaker-tmp
  236. cd netmaker-tmp
  237. git clone --single-branch --depth=1 --branch=$BUILD_TAG https://www.github.com/gravitl/netmaker
  238. cd netmaker
  239. if test -z "$NM_SKIP_BUILD"; then
  240. docker build --no-cache --build-arg version=$IMAGE_TAG -t gravitl/netmaker:$IMAGE_TAG .
  241. else
  242. echo "Skipping build on NM_SKIP_BUILD"
  243. fi
  244. if [ "$INSTALL_TYPE" = "ee" ]; then
  245. cp compose/docker-compose.ee.yml /root/docker-compose.yml
  246. cp docker/Caddyfile-EE /root/Caddyfile
  247. else
  248. cp compose/docker-compose.yml /root/docker-compose.yml
  249. cp docker/Caddyfile /root/Caddyfile
  250. fi
  251. cp scripts/nm-certs.sh /root/nm-certs.sh
  252. cp docker/mosquitto.conf /root/mosquitto.conf
  253. cp docker/wait.sh /root/wait.sh
  254. cd ../../
  255. rm -rf netmaker-tmp
  256. ); }
  257. # install_dependencies - install necessary packages to run netmaker
  258. install_dependencies() {
  259. echo "checking dependencies..."
  260. OS=$(uname)
  261. if [ -f /etc/debian_version ]; then
  262. dependencies="git wireguard wireguard-tools dnsutils jq docker.io docker-compose"
  263. update_cmd='apt update'
  264. install_cmd='apt-get install -y'
  265. elif [ -f /etc/alpine-release ]; then
  266. dependencies="git wireguard jq docker.io docker-compose"
  267. update_cmd='apk update'
  268. install_cmd='apk --update add'
  269. elif [ -f /etc/centos-release ]; then
  270. dependencies="git wireguard jq bind-utils docker.io docker-compose"
  271. update_cmd='yum update'
  272. install_cmd='yum install -y'
  273. elif [ -f /etc/fedora-release ]; then
  274. dependencies="git wireguard bind-utils jq docker.io docker-compose"
  275. update_cmd='dnf update'
  276. install_cmd='dnf install -y'
  277. elif [ -f /etc/redhat-release ]; then
  278. dependencies="git wireguard jq docker.io bind-utils docker-compose"
  279. update_cmd='yum update'
  280. install_cmd='yum install -y'
  281. elif [ -f /etc/arch-release ]; then
  282. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose"
  283. update_cmd='pacman -Sy'
  284. install_cmd='pacman -S --noconfirm'
  285. elif [ "${OS}" = "FreeBSD" ]; then
  286. dependencies="git wireguard wget jq docker.io docker-compose"
  287. update_cmd='pkg update'
  288. install_cmd='pkg install -y'
  289. else
  290. install_cmd=''
  291. fi
  292. if [ -z "${install_cmd}" ]; then
  293. echo "OS unsupported for automatic dependency install"
  294. # TODO shouldnt exit, check if deps available, if not
  295. # ask the user to install manually and continue when ready
  296. exit 1
  297. fi
  298. set -- $dependencies
  299. ${update_cmd}
  300. while [ -n "$1" ]; do
  301. if [ "${OS}" = "FreeBSD" ]; then
  302. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  303. if [ "$is_installed" != "" ]; then
  304. echo " " $1 is installed
  305. else
  306. echo " " $1 is not installed. Attempting install.
  307. ${install_cmd} $1
  308. sleep 5
  309. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  310. if [ "$is_installed" != "" ]; then
  311. echo " " $1 is installed
  312. elif [ -x "$(command -v $1)" ]; then
  313. echo " " $1 is installed
  314. else
  315. echo " " FAILED TO INSTALL $1
  316. echo " " This may break functionality.
  317. fi
  318. fi
  319. else
  320. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  321. is_installed=$(opkg list-installed $1 | grep $1)
  322. else
  323. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  324. fi
  325. if [ "${is_installed}" != "" ]; then
  326. echo " " $1 is installed
  327. else
  328. echo " " $1 is not installed. Attempting install.
  329. ${install_cmd} $1
  330. sleep 5
  331. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  332. is_installed=$(opkg list-installed $1 | grep $1)
  333. else
  334. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  335. fi
  336. if [ "${is_installed}" != "" ]; then
  337. echo " " $1 is installed
  338. elif [ -x "$(command -v $1)" ]; then
  339. echo " " $1 is installed
  340. else
  341. echo " " FAILED TO INSTALL $1
  342. echo " " This may break functionality.
  343. fi
  344. fi
  345. fi
  346. shift
  347. done
  348. echo "-----------------------------------------------------"
  349. echo "dependency check complete"
  350. echo "-----------------------------------------------------"
  351. }
  352. set -e
  353. # set_install_vars - sets the variables that will be used throughout installation
  354. set_install_vars() {
  355. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  356. if [ "$IP_ADDR" = "" ]; then
  357. IP_ADDR=$(curl -s ifconfig.me)
  358. fi
  359. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  360. # TODO dead code?
  361. # COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  362. SERVER_PUBLIC_IP=$IP_ADDR
  363. MASTER_KEY=$(
  364. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  365. echo ''
  366. )
  367. DOMAIN_TYPE=""
  368. echo "-----------------------------------------------------"
  369. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  370. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_PUBLIC_IP"
  371. echo "IMPORTANT: Due to the high volume of requests, the auto-generated domain has been rate-limited by the certificate provider."
  372. 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."
  373. echo "-----------------------------------------------------"
  374. if [ "$AUTO_BUILD" = "on" ]; then
  375. DOMAIN_TYPE="auto"
  376. else
  377. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  378. case $REPLY in
  379. 1)
  380. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  381. DOMAIN_TYPE="auto"
  382. break
  383. ;;
  384. 2)
  385. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_PUBLIC_IP first): " domain
  386. NETMAKER_BASE_DOMAIN=$domain
  387. echo "using $NETMAKER_BASE_DOMAIN"
  388. DOMAIN_TYPE="custom"
  389. break
  390. ;;
  391. *) echo "invalid option $REPLY" ;;
  392. esac
  393. done
  394. fi
  395. wait_seconds 2
  396. echo "-----------------------------------------------------"
  397. echo "The following subdomains will be used:"
  398. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  399. echo " api.$NETMAKER_BASE_DOMAIN"
  400. echo " broker.$NETMAKER_BASE_DOMAIN"
  401. echo " stun.$NETMAKER_BASE_DOMAIN"
  402. echo " turn.$NETMAKER_BASE_DOMAIN"
  403. echo " turnapi.$NETMAKER_BASE_DOMAIN"
  404. if [ "$INSTALL_TYPE" = "ee" ]; then
  405. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  406. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  407. echo " grafana.$NETMAKER_BASE_DOMAIN"
  408. fi
  409. echo "-----------------------------------------------------"
  410. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  411. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_PUBLIC_IP"
  412. confirm
  413. fi
  414. wait_seconds 1
  415. if [ "$INSTALL_TYPE" = "ee" ]; then
  416. echo "-----------------------------------------------------"
  417. echo "Provide Details for EE installation:"
  418. echo " 1. Log into https://dashboard.license.netmaker.io"
  419. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  420. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  421. echo " 4. note email address"
  422. echo "-----------------------------------------------------"
  423. unset LICENSE_KEY
  424. while [ -z "$LICENSE_KEY" ]; do
  425. read -p "License Key: " LICENSE_KEY
  426. done
  427. unset ACCOUNT_ID
  428. while [ -z ${ACCOUNT_ID} ]; do
  429. read -p "Account ID: " ACCOUNT_ID
  430. done
  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