nm-quick.sh 23 KB

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