nm-quick.sh 24 KB

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