nm-quick.sh 24 KB

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