nm-quick.sh 27 KB

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