nm-quick.sh 23 KB

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