nm-quick.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 " stun.$NETMAKER_BASE_DOMAIN"
  499. echo " turn.$NETMAKER_BASE_DOMAIN"
  500. echo " turnapi.$NETMAKER_BASE_DOMAIN"
  501. if [ "$INSTALL_TYPE" = "ee" ]; then
  502. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  503. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  504. echo " grafana.$NETMAKER_BASE_DOMAIN"
  505. fi
  506. echo "-----------------------------------------------------"
  507. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  508. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_HOST"
  509. confirm
  510. fi
  511. wait_seconds 1
  512. if [ "$INSTALL_TYPE" = "ee" ]; then
  513. echo "-----------------------------------------------------"
  514. echo "Provide Details for EE installation:"
  515. echo " 1. Log into https://dashboard.license.netmaker.io"
  516. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  517. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  518. echo " 4. note email address"
  519. echo "-----------------------------------------------------"
  520. unset LICENSE_KEY
  521. while [ -z "$LICENSE_KEY" ]; do
  522. read -p "License Key: " LICENSE_KEY
  523. done
  524. unset ACCOUNT_ID
  525. while [ -z ${ACCOUNT_ID} ]; do
  526. read -p "Account ID: " ACCOUNT_ID
  527. done
  528. fi
  529. unset GET_EMAIL
  530. unset RAND_EMAIL
  531. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  532. # suggest the prev email or a random one
  533. EMAIL_SUGGESTED=${NM_EMAIL:-$RAND_EMAIL}
  534. if [ -z $AUTO_BUILD ]; then
  535. read -p "Email Address for Domain Registration (click 'enter' to use $EMAIL_SUGGESTED): " GET_EMAIL
  536. fi
  537. if [ -z "$GET_EMAIL" ]; then
  538. EMAIL="$EMAIL_SUGGESTED"
  539. if [ "$EMAIL" = "$NM_EMAIL" ]; then
  540. echo "using config email"
  541. else
  542. echo "using rand email"
  543. fi
  544. else
  545. EMAIL="$GET_EMAIL"
  546. fi
  547. wait_seconds 1
  548. unset GET_MQ_USERNAME
  549. unset GET_MQ_PASSWORD
  550. unset CONFIRM_MQ_PASSWORD
  551. echo "Enter Credentials For MQ..."
  552. if [ -z $AUTO_BUILD ]; then
  553. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  554. fi
  555. if [ -z "$GET_MQ_USERNAME" ]; then
  556. echo "using default username for mq"
  557. MQ_USERNAME="netmaker"
  558. else
  559. MQ_USERNAME="$GET_MQ_USERNAME"
  560. fi
  561. if test -z "$MQ_PASSWORD"; then
  562. MQ_PASSWORD=$(
  563. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  564. echo ''
  565. )
  566. fi
  567. if [ -z $AUTO_BUILD ]; then
  568. select domain_option in "Auto Generated / Config Password" "Input Your Own Password"; do
  569. case $REPLY in
  570. 1)
  571. echo "using random password for mq"
  572. break
  573. ;;
  574. 2)
  575. while true; do
  576. echo "Enter your Password For MQ: "
  577. read -s GET_MQ_PASSWORD
  578. echo "Enter your password again to confirm: "
  579. read -s CONFIRM_MQ_PASSWORD
  580. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  581. echo "wrong password entered, try again..."
  582. continue
  583. fi
  584. MQ_PASSWORD="$GET_MQ_PASSWORD"
  585. echo "MQ Password Saved Successfully!!"
  586. break
  587. done
  588. break
  589. ;;
  590. *) echo "invalid option $REPLY" ;;
  591. esac
  592. done
  593. fi
  594. unset GET_TURN_USERNAME
  595. unset GET_TURN_PASSWORD
  596. unset CONFIRM_TURN_PASSWORD
  597. echo "Enter Credentials For TURN..."
  598. if [ -z $AUTO_BUILD ]; then
  599. read -p "TURN Username (click 'enter' to use 'netmaker'): " GET_TURN_USERNAME
  600. fi
  601. if [ -z "$GET_TURN_USERNAME" ]; then
  602. echo "using default username for TURN"
  603. TURN_USERNAME="netmaker"
  604. else
  605. TURN_USERNAME="$GET_TURN_USERNAME"
  606. fi
  607. if test -z "$TURN_PASSWORD"; then
  608. TURN_PASSWORD=$(
  609. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  610. echo ''
  611. )
  612. fi
  613. if [ -z $AUTO_BUILD ]; then
  614. select domain_option in "Auto Generated / Config Password" "Input Your Own Password"; do
  615. case $REPLY in
  616. 1)
  617. echo "using random password for turn"
  618. break
  619. ;;
  620. 2)
  621. while true; do
  622. echo "Enter your Password For TURN: "
  623. read -s GET_TURN_PASSWORD
  624. echo "Enter your password again to confirm: "
  625. read -s CONFIRM_TURN_PASSWORD
  626. if [ ${GET_TURN_PASSWORD} != ${CONFIRM_TURN_PASSWORD} ]; then
  627. echo "wrong password entered, try again..."
  628. continue
  629. fi
  630. TURN_PASSWORD="$GET_TURN_PASSWORD"
  631. echo "TURN Password Saved Successfully!!"
  632. break
  633. done
  634. break
  635. ;;
  636. *) echo "invalid option $REPLY" ;;
  637. esac
  638. done
  639. fi
  640. wait_seconds 2
  641. echo "-----------------------------------------------------------------"
  642. echo " SETUP ARGUMENTS"
  643. echo "-----------------------------------------------------------------"
  644. echo " domain: $NETMAKER_BASE_DOMAIN"
  645. echo " email: $EMAIL"
  646. echo " public ip: $SERVER_HOST"
  647. if [ "$INSTALL_TYPE" = "ee" ]; then
  648. echo " license: $LICENSE_KEY"
  649. echo " account id: $ACCOUNT_ID"
  650. fi
  651. echo "-----------------------------------------------------------------"
  652. echo "Confirm Settings for Installation"
  653. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  654. if [ ! "$BUILD_TYPE" = "local" ]; then
  655. IMAGE_TAG="$LATEST"
  656. fi
  657. confirm
  658. }
  659. # install_netmaker - sets the config files and starts docker-compose
  660. install_netmaker() {
  661. echo "-----------------------------------------------------------------"
  662. echo "Beginning installation..."
  663. echo "-----------------------------------------------------------------"
  664. wait_seconds 3
  665. echo "Pulling config files..."
  666. if [ "$BUILD_TYPE" = "local" ]; then
  667. local_install_setup
  668. else
  669. local BASE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG"
  670. local COMPOSE_URL="$BASE_URL/compose/docker-compose.yml"
  671. local CADDY_URL="$BASE_URL/docker/Caddyfile"
  672. if [ "$INSTALL_TYPE" = "ee" ]; then
  673. local COMPOSE_OVERRIDE_URL="$BASE_URL/compose/docker-compose.ee.yml"
  674. local CADDY_URL="$BASE_URL/docker/Caddyfile-EE"
  675. fi
  676. wget -qO "$SCRIPT_DIR"/docker-compose.yml $COMPOSE_URL
  677. if test -n "$COMPOSE_OVERRIDE_URL"; then
  678. wget -qO "$SCRIPT_DIR"/docker-compose.override.yml $COMPOSE_OVERRIDE_URL
  679. fi
  680. wget -qO "$SCRIPT_DIR"/Caddyfile "$CADDY_URL"
  681. wget -qO "$SCRIPT_DIR"/netmaker.default.env "$BASE_URL/scripts/netmaker.default.env"
  682. wget -qO "$SCRIPT_DIR"/mosquitto.conf "$BASE_URL/docker/mosquitto.conf"
  683. wget -qO "$SCRIPT_DIR"/nm-certs.sh "$BASE_URL/scripts/nm-certs.sh"
  684. wget -qO "$SCRIPT_DIR"/wait.sh "$BASE_URL/docker/wait.sh"
  685. fi
  686. chmod +x "$SCRIPT_DIR"/wait.sh
  687. mkdir -p /etc/netmaker
  688. # link .env to the user config
  689. ln -fs "$SCRIPT_DIR/netmaker.env" "$SCRIPT_DIR/.env"
  690. save_config
  691. # Fetch / update certs using certbot
  692. chmod +x "$SCRIPT_DIR"/nm-certs.sh
  693. "$SCRIPT_DIR"/nm-certs.sh
  694. echo "Starting containers..."
  695. # increase the timeouts
  696. export DOCKER_CLIENT_TIMEOUT=120
  697. export COMPOSE_HTTP_TIMEOUT=120
  698. # start docker and rebuild containers / networks
  699. docker-compose -f "$SCRIPT_DIR"/docker-compose.yml up -d --force-recreate
  700. wait_seconds 2
  701. }
  702. # test_connection - tests to make sure Caddy has proper SSL certs
  703. test_connection() {
  704. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  705. for i in 1 2 3 4 5 6 7 8; do
  706. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  707. if [[ "$i" == 8 ]]; then
  708. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  709. echo " Exiting..."
  710. exit 1
  711. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  712. echo " Certificates not yet configured, retrying..."
  713. elif [[ "$curlresponse" == *"left intact"* ]]; then
  714. echo " Certificates ok"
  715. break
  716. else
  717. secs=$(($i * 5 + 10))
  718. echo " Issue establishing connection...retrying in $secs seconds..."
  719. fi
  720. sleep $secs
  721. done
  722. }
  723. # setup_mesh - sets up a default mesh network on the server
  724. setup_mesh() {
  725. wait_seconds 5
  726. local networkCount=$(nmctl network list -o json | jq '. | length')
  727. # add a network if none present
  728. if [ "$networkCount" -lt 1 ]; then
  729. echo "Creating netmaker network (10.101.0.0/16)"
  730. # TODO causes "Error Status: 400 Response: {"Code":400,"Message":"could not find any records"}"
  731. nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16
  732. wait_seconds 5
  733. fi
  734. echo "Obtaining a netmaker enrollment key..."
  735. local tokenJson=$(nmctl enrollment_key create --unlimited --networks netmaker)
  736. TOKEN=$(jq -r '.token' <<<${tokenJson})
  737. if test -z "$TOKEN"; then
  738. echo "Error creating an enrollment key"
  739. exit 1
  740. else
  741. echo "Enrollment key ready"
  742. fi
  743. wait_seconds 3
  744. }
  745. # print_success - prints a success message upon completion
  746. print_success() {
  747. echo "-----------------------------------------------------------------"
  748. echo "-----------------------------------------------------------------"
  749. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  750. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  751. echo "-----------------------------------------------------------------"
  752. echo "-----------------------------------------------------------------"
  753. }
  754. cleanup() {
  755. # remove the existing netclient's instance from the existing network
  756. if command -v nmctl >/dev/null 2>&1; then
  757. local node_id=$(netclient list | jq '.[0].node_id' 2>/dev/null)
  758. # trim doublequotes
  759. node_id="${node_id//\"/}"
  760. if test -n "$node_id"; then
  761. echo "De-registering the existing netclient..."
  762. nmctl node delete netmaker $node_id >/dev/null 2>&1
  763. fi
  764. fi
  765. echo "Stopping all containers..."
  766. local containers=("mq" "netmaker-ui" "coredns" "turn" "caddy" "netmaker" "netmaker-exporter" "prometheus" "grafana")
  767. for name in "${containers[@]}"; do
  768. local running=$(docker ps | grep -w "$name")
  769. local exists=$(docker ps -a | grep -w "$name")
  770. if test -n "$running"; then
  771. docker stop "$name" 1>/dev/null
  772. fi
  773. if test -n "$exists"; then
  774. docker rm "$name" 1>/dev/null
  775. fi
  776. done
  777. }
  778. # 1. print netmaker logo
  779. print_logo
  780. # read the config
  781. if [ -f "$CONFIG_PATH" ]; then
  782. echo "Using config: $CONFIG_PATH"
  783. source "$CONFIG_PATH"
  784. fi
  785. # 2. setup the build instructions
  786. set_buildinfo
  787. set +e
  788. # 3. install necessary packages
  789. install_dependencies
  790. # 4. install yq if necessary
  791. install_yq
  792. set -e
  793. # 6. get user input for variables
  794. set_install_vars
  795. set +e
  796. cleanup
  797. set -e
  798. # 7. get and set config files, startup docker-compose
  799. install_netmaker
  800. set +e
  801. # 8. make sure Caddy certs are working
  802. test_connection
  803. # 9. install the netmaker CLI
  804. setup_nmctl
  805. # 10. create a default mesh network for netmaker
  806. setup_mesh
  807. set -e
  808. # 11. add netclient to docker-compose and start it up
  809. setup_netclient
  810. # 12. make the netclient a default host and ingress gw
  811. configure_netclient
  812. # 13. print success message
  813. print_success
  814. # cp -f /etc/skel/.bashrc /root/.bashrc