nm-quick.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. set -e
  144. }
  145. # setup_nmctl - pulls nmctl and makes it executable
  146. setup_nmctl() {
  147. local URL="https://github.com/gravitl/netmaker/releases/download/$LATEST/nmctl-linux-$ARCH"
  148. echo "Downloading nmctl..."
  149. wget -qO /usr/bin/nmctl "$URL"
  150. if [ ! -f /usr/bin/nmctl ]; then
  151. echo "Error downloading nmctl from '$URL'"
  152. exit 1
  153. fi
  154. chmod +x /usr/bin/nmctl
  155. echo "using server api.$NETMAKER_BASE_DOMAIN"
  156. echo "using master key $MASTER_KEY"
  157. nmctl context set default --endpoint="https://api.$NETMAKER_BASE_DOMAIN" --master_key="$MASTER_KEY"
  158. nmctl context use default
  159. RESP=$(nmctl network list)
  160. if [[ $RESP == *"unauthorized"* ]]; then
  161. echo "Unable to properly configure NMCTL, exiting..."
  162. exit 1
  163. fi
  164. }
  165. # wait_seconds - wait for the specified period of time
  166. wait_seconds() { (
  167. for ((a = 1; a <= $1; a++)); do
  168. echo ". . ."
  169. sleep 1
  170. done
  171. ); }
  172. # confirm - get user input to confirm that they want to perform the next step
  173. confirm() { (
  174. while true; do
  175. read -p 'Does everything look right? [y/n]: ' yn
  176. case $yn in
  177. [Yy]*)
  178. override="true"
  179. break
  180. ;;
  181. [Nn]*)
  182. echo "exiting..."
  183. exit 1
  184. # TODO start from the beginning instead
  185. ;;
  186. *) echo "Please answer yes or no." ;;
  187. esac
  188. done
  189. ) }
  190. save_config() { (
  191. echo "Saving the config to $CONFIG_PATH"
  192. touch "$CONFIG_PATH"
  193. if [ -n "$EMAIL" ]; then
  194. save_config_item NM_EMAIL "$EMAIL"
  195. fi
  196. if [ -n "$NETMAKER_BASE_DOMAIN" ]; then
  197. save_config_item NM_DOMAIN "$NETMAKER_BASE_DOMAIN"
  198. fi
  199. save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
  200. # version-specific entries
  201. if [ "$INSTALL_TYPE" = "pro" ]; then
  202. save_config_item NETMAKER_TENANT_ID "$TENANT_ID"
  203. save_config_item LICENSE_KEY "$LICENSE_KEY"
  204. if [ "$UPGRADE_FLAG" = "yes" ];then
  205. save_config_item METRICS_EXPORTER "on"
  206. save_config_item PROMETHEUS "on"
  207. fi
  208. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG-ee"
  209. else
  210. save_config_item METRICS_EXPORTER "off"
  211. save_config_item PROMETHEUS "off"
  212. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG"
  213. fi
  214. # copy entries from the previous config
  215. local toCopy=("SERVER_HOST" "MASTER_KEY" "MQ_USERNAME" "MQ_PASSWORD"
  216. "INSTALL_TYPE" "NODE_ID" "DNS_MODE" "NETCLIENT_AUTO_UPDATE" "API_PORT"
  217. "CORS_ALLOWED_ORIGIN" "DISPLAY_KEYS" "DATABASE" "SERVER_BROKER_ENDPOINT" "VERBOSITY"
  218. "DEBUG_MODE" "REST_BACKEND" "DISABLE_REMOTE_IP_CHECK" "TELEMETRY" "AUTH_PROVIDER" "CLIENT_ID" "CLIENT_SECRET"
  219. "FRONTEND_URL" "AZURE_TENANT" "OIDC_ISSUER" "EXPORTER_API_PORT" "JWT_VALIDITY_DURATION" "RAC_AUTO_DISABLE" "CACHING_ENABLED")
  220. for name in "${toCopy[@]}"; do
  221. save_config_item $name "${!name}"
  222. done
  223. # preserve debug entries
  224. if test -n "$NM_SKIP_BUILD"; then
  225. save_config_item NM_SKIP_BUILD "$NM_SKIP_BUILD"
  226. fi
  227. if test -n "$NM_SKIP_CLONE"; then
  228. save_config_item NM_SKIP_CLONE "$NM_SKIP_CLONE"
  229. fi
  230. if test -n "$NM_SKIP_DEPS"; then
  231. save_config_item NM_SKIP_DEPS "$NM_SKIP_DEPS"
  232. fi
  233. ); }
  234. save_config_item() { (
  235. local NAME="$1"
  236. local VALUE="$2"
  237. #echo "$NAME=$VALUE"
  238. if test -z "$VALUE"; then
  239. # load the default for empty values
  240. VALUE=$(awk -F'=' "/^$NAME/ { print \$2}" "$SCRIPT_DIR/netmaker.default.env")
  241. # trim quotes for docker
  242. VALUE=$(echo "$VALUE" | sed -E "s|^(['\"])(.*)\1$|\2|g")
  243. #echo "Default for $NAME=$VALUE"
  244. fi
  245. # TODO single quote passwords
  246. if grep -q "^$NAME=" "$CONFIG_PATH"; then
  247. # TODO escape | in the value
  248. sed -i "s|$NAME=.*|$NAME=$VALUE|" "$CONFIG_PATH"
  249. else
  250. echo "$NAME=$VALUE" >>"$CONFIG_PATH"
  251. fi
  252. ); }
  253. # install_dependencies - install necessary packages to run netmaker
  254. install_dependencies() {
  255. if test -n "$NM_SKIP_DEPS"; then
  256. return
  257. fi
  258. echo "checking dependencies..."
  259. OS=$(uname)
  260. if [ -f /etc/debian_version ]; then
  261. dependencies="git wireguard wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  262. update_cmd='apt update'
  263. install_cmd='apt-get install -y'
  264. elif [ -f /etc/alpine-release ]; then
  265. dependencies="git wireguard jq docker.io docker-compose grep gawk"
  266. update_cmd='apk update'
  267. install_cmd='apk --update add'
  268. elif [ -f /etc/centos-release ]; then
  269. dependencies="git wireguard jq bind-utils docker.io docker-compose grep gawk"
  270. update_cmd='yum update'
  271. install_cmd='yum install -y'
  272. elif [ -f /etc/fedora-release ]; then
  273. dependencies="git wireguard bind-utils jq docker.io docker-compose grep gawk"
  274. update_cmd='dnf update'
  275. install_cmd='dnf install -y'
  276. elif [ -f /etc/redhat-release ]; then
  277. dependencies="git wireguard jq docker.io bind-utils docker-compose grep gawk"
  278. update_cmd='yum update'
  279. install_cmd='yum install -y'
  280. elif [ -f /etc/arch-release ]; then
  281. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  282. update_cmd='pacman -Sy'
  283. install_cmd='pacman -S --noconfirm'
  284. elif [ "${OS}" = "FreeBSD" ]; then
  285. dependencies="git wireguard wget jq docker.io docker-compose grep gawk"
  286. update_cmd='pkg update'
  287. install_cmd='pkg install -y'
  288. else
  289. install_cmd=''
  290. fi
  291. if [ -z "${install_cmd}" ]; then
  292. echo "OS unsupported for automatic dependency install"
  293. # TODO shouldnt exit, check if deps available, if not
  294. # ask the user to install manually and continue when ready
  295. exit 1
  296. fi
  297. # TODO add other supported architectures
  298. ARCH=$(uname -m)
  299. if [ "$ARCH" = "x86_64" ]; then
  300. ARCH=amd64
  301. elif [ "$ARCH" = "aarch64" ]; then
  302. ARCH=arm64
  303. else
  304. echo "Unsupported architechure"
  305. # exit 1
  306. fi
  307. set -- $dependencies
  308. ${update_cmd}
  309. while [ -n "$1" ]; do
  310. if [ "${OS}" = "FreeBSD" ]; then
  311. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  312. if [ "$is_installed" != "" ]; then
  313. echo " " $1 is installed
  314. else
  315. echo " " $1 is not installed. Attempting install.
  316. ${install_cmd} $1
  317. sleep 5
  318. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  319. if [ "$is_installed" != "" ]; then
  320. echo " " $1 is installed
  321. elif [ -x "$(command -v $1)" ]; then
  322. echo " " $1 is installed
  323. else
  324. echo " " FAILED TO INSTALL $1
  325. echo " " This may break functionality.
  326. fi
  327. fi
  328. else
  329. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  330. is_installed=$(opkg list-installed $1 | grep $1)
  331. else
  332. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  333. fi
  334. if [ "${is_installed}" != "" ]; then
  335. echo " " $1 is installed
  336. else
  337. echo " " $1 is not installed. Attempting install.
  338. ${install_cmd} $1
  339. sleep 5
  340. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  341. is_installed=$(opkg list-installed $1 | grep $1)
  342. else
  343. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  344. fi
  345. if [ "${is_installed}" != "" ]; then
  346. echo " " $1 is installed
  347. elif [ -x "$(command -v $1)" ]; then
  348. echo " " $1 is installed
  349. else
  350. echo " " FAILED TO INSTALL $1
  351. echo " " This may break functionality.
  352. fi
  353. fi
  354. fi
  355. shift
  356. done
  357. echo "-----------------------------------------------------"
  358. echo "dependency check complete"
  359. echo "-----------------------------------------------------"
  360. }
  361. set -e
  362. # set_install_vars - sets the variables that will be used throughout installation
  363. set_install_vars() {
  364. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  365. if [ "$IP_ADDR" = "" ]; then
  366. IP_ADDR=$(curl -s ifconfig.me)
  367. fi
  368. if [ "$NETMAKER_BASE_DOMAIN" = "" ]; then
  369. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  370. fi
  371. SERVER_HOST=$IP_ADDR
  372. if test -z "$MASTER_KEY"; then
  373. MASTER_KEY=$(
  374. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  375. echo ''
  376. )
  377. fi
  378. DOMAIN_TYPE=""
  379. echo "-----------------------------------------------------"
  380. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  381. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_HOST"
  382. echo "IMPORTANT: Due to the high volume of requests, the auto-generated domain has been rate-limited by the certificate provider."
  383. 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."
  384. echo "-----------------------------------------------------"
  385. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  386. case $REPLY in
  387. 1)
  388. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  389. DOMAIN_TYPE="auto"
  390. break
  391. ;;
  392. 2)
  393. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_HOST first): " domain
  394. NETMAKER_BASE_DOMAIN=$domain
  395. echo "using $NETMAKER_BASE_DOMAIN"
  396. DOMAIN_TYPE="custom"
  397. break
  398. ;;
  399. *) echo "invalid option $REPLY" ;;
  400. esac
  401. done
  402. wait_seconds 2
  403. echo "-----------------------------------------------------"
  404. echo "The following subdomains will be used:"
  405. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  406. echo " api.$NETMAKER_BASE_DOMAIN"
  407. echo " broker.$NETMAKER_BASE_DOMAIN"
  408. if [ "$UPGRADE_FLAG" = "yes" ]; then
  409. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  410. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  411. echo " grafana.$NETMAKER_BASE_DOMAIN"
  412. fi
  413. echo "-----------------------------------------------------"
  414. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  415. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_HOST"
  416. confirm
  417. fi
  418. wait_seconds 1
  419. unset GET_EMAIL
  420. unset RAND_EMAIL
  421. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  422. # suggest the prev email or a random one
  423. EMAIL_SUGGESTED=${NM_EMAIL:-$RAND_EMAIL}
  424. read -p "Email Address for Domain Registration (click 'enter' to use $EMAIL_SUGGESTED): " GET_EMAIL
  425. if [ -z "$GET_EMAIL" ]; then
  426. EMAIL="$EMAIL_SUGGESTED"
  427. if [ "$EMAIL" = "$NM_EMAIL" ]; then
  428. echo "using config email"
  429. else
  430. echo "using rand email"
  431. fi
  432. else
  433. EMAIL="$GET_EMAIL"
  434. fi
  435. wait_seconds 1
  436. unset GET_MQ_USERNAME
  437. unset GET_MQ_PASSWORD
  438. unset CONFIRM_MQ_PASSWORD
  439. echo "Enter Credentials For MQ..."
  440. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  441. if [ -z "$GET_MQ_USERNAME" ]; then
  442. echo "using default username for mq"
  443. MQ_USERNAME="netmaker"
  444. else
  445. MQ_USERNAME="$GET_MQ_USERNAME"
  446. fi
  447. if test -z "$MQ_PASSWORD"; then
  448. MQ_PASSWORD=$(
  449. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  450. echo ''
  451. )
  452. fi
  453. select domain_option in "Auto Generated / Config Password" "Input Your Own Password"; do
  454. case $REPLY in
  455. 1)
  456. echo "using random password for mq"
  457. break
  458. ;;
  459. 2)
  460. while true; do
  461. echo "Enter your Password For MQ: "
  462. read -s GET_MQ_PASSWORD
  463. echo "Enter your password again to confirm: "
  464. read -s CONFIRM_MQ_PASSWORD
  465. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  466. echo "wrong password entered, try again..."
  467. continue
  468. fi
  469. MQ_PASSWORD="$GET_MQ_PASSWORD"
  470. echo "MQ Password Saved Successfully!!"
  471. break
  472. done
  473. break
  474. ;;
  475. *) echo "invalid option $REPLY" ;;
  476. esac
  477. done
  478. wait_seconds 2
  479. echo "-----------------------------------------------------------------"
  480. echo " SETUP ARGUMENTS"
  481. echo "-----------------------------------------------------------------"
  482. echo " domain: $NETMAKER_BASE_DOMAIN"
  483. echo " email: $EMAIL"
  484. echo " public ip: $SERVER_HOST"
  485. echo "-----------------------------------------------------------------"
  486. echo "Confirm Settings for Installation"
  487. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  488. confirm
  489. }
  490. # install_netmaker - sets the config files and starts docker-compose
  491. install_netmaker() {
  492. echo "-----------------------------------------------------------------"
  493. echo "Beginning installation..."
  494. echo "-----------------------------------------------------------------"
  495. wait_seconds 3
  496. echo "Pulling config files..."
  497. local BASE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG"
  498. local COMPOSE_URL="$BASE_URL/compose/docker-compose.yml"
  499. local CADDY_URL="$BASE_URL/docker/Caddyfile"
  500. if [ "$INSTALL_TYPE" = "pro" ]; then
  501. local COMPOSE_OVERRIDE_URL="$BASE_URL/compose/docker-compose.pro.yml"
  502. local CADDY_URL="$BASE_URL/docker/Caddyfile-pro"
  503. fi
  504. wget -qO "$SCRIPT_DIR"/docker-compose.yml $COMPOSE_URL
  505. if [ "$UPGRADE_FLAG" = "yes" ]; then
  506. wget -qO "$SCRIPT_DIR"/docker-compose.override.yml $COMPOSE_OVERRIDE_URL
  507. elif [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
  508. rm -f "$SCRIPT_DIR"/docker-compose.override.yml
  509. fi
  510. wget -qO "$SCRIPT_DIR"/Caddyfile "$CADDY_URL"
  511. wget -qO "$SCRIPT_DIR"/netmaker.default.env "$BASE_URL/scripts/netmaker.default.env"
  512. wget -qO "$SCRIPT_DIR"/mosquitto.conf "$BASE_URL/docker/mosquitto.conf"
  513. wget -qO "$SCRIPT_DIR"/wait.sh "$BASE_URL/docker/wait.sh"
  514. chmod +x "$SCRIPT_DIR"/wait.sh
  515. mkdir -p /etc/netmaker
  516. # link .env to the user config
  517. ln -fs "$SCRIPT_DIR/netmaker.env" "$SCRIPT_DIR/.env"
  518. save_config
  519. echo "Starting containers..."
  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. unset IMAGE_TAG
  611. unset BUILD_TAG
  612. IMAGE_TAG=$UI_IMAGE_TAG
  613. BUILD_TAG=$UI_IMAGE_TAG
  614. echo "-----------------------------------------------------"
  615. echo "Provide Details for pro installation:"
  616. echo " 1. Log into https://app.netmaker.io"
  617. echo " 2. follow instructions to get a license at: https://docs.netmaker.io/pro/pro-setup.html"
  618. echo " 3. Retrieve License and Tenant ID"
  619. echo "-----------------------------------------------------"
  620. unset LICENSE_KEY
  621. while [ -z "$LICENSE_KEY" ]; do
  622. read -p "License Key: " LICENSE_KEY
  623. done
  624. unset TENANT_ID
  625. while [ -z ${TENANT_ID} ]; do
  626. read -p "Tenant ID: " TENANT_ID
  627. done
  628. save_config
  629. # start docker and rebuild containers / networks
  630. stop_services
  631. install_netmaker
  632. }
  633. downgrade () {
  634. print_logo
  635. unset IMAGE_TAG
  636. unset BUILD_TAG
  637. IMAGE_TAG=$UI_IMAGE_TAG
  638. BUILD_TAG=$UI_IMAGE_TAG
  639. save_config
  640. if [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
  641. rm -f "$SCRIPT_DIR"/docker-compose.override.yml
  642. fi
  643. # start docker and rebuild containers / networks
  644. stop_services
  645. install_netmaker
  646. }
  647. main (){
  648. # read the config
  649. if [ -f "$CONFIG_PATH" ]; then
  650. echo "Using config: $CONFIG_PATH"
  651. source "$CONFIG_PATH"
  652. fi
  653. INSTALL_TYPE="pro"
  654. while getopts :cudv flag; do
  655. case "${flag}" in
  656. c)
  657. INSTALL_TYPE="ce"
  658. ;;
  659. u)
  660. echo "upgrading to pro version..."
  661. INSTALL_TYPE="pro"
  662. UPGRADE_FLAG="yes"
  663. upgrade
  664. exit 0
  665. ;;
  666. d)
  667. echo "downgrading to community version..."
  668. INSTALL_TYPE="ce"
  669. downgrade
  670. exit 0
  671. ;;
  672. v)
  673. usage
  674. exit 0
  675. ;;
  676. esac
  677. done
  678. # 1. print netmaker logo
  679. print_logo
  680. # 2. setup the build instructions
  681. set_buildinfo
  682. set +e
  683. # 3. install necessary packages
  684. install_dependencies
  685. # 4. install yq if necessary
  686. install_yq
  687. set -e
  688. # 6. get user input for variables
  689. set_install_vars
  690. set +e
  691. cleanup
  692. set -e
  693. # 7. get and set config files, startup docker-compose
  694. install_netmaker
  695. set +e
  696. # 8. make sure Caddy certs are working
  697. test_connection
  698. # 9. install the netmaker CLI
  699. setup_nmctl
  700. # 10. create a default mesh network for netmaker
  701. setup_mesh
  702. set -e
  703. # 11. add netclient to docker-compose and start it up
  704. setup_netclient
  705. # 12. make the netclient a default host and ingress gw
  706. configure_netclient
  707. # 13. print success message
  708. print_success
  709. }
  710. main "${@}"