nm-quick.sh 23 KB

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