nm-quick.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. UPGRADE_FLAG="yes"
  42. ;;
  43. v)
  44. usage
  45. exit 0
  46. ;;
  47. a)
  48. AUTO_BUILD="on"
  49. ;;
  50. b)
  51. BUILD_TYPE=${OPTARG}
  52. if [[ ! "$BUILD_TYPE" =~ ^(version|local|branch)$ ]]; then
  53. echo "error: $BUILD_TYPE is invalid"
  54. echo "valid options: version, local, branch"
  55. usage
  56. exit 1
  57. fi
  58. ;;
  59. t)
  60. BUILD_TAG=${OPTARG}
  61. ;;
  62. d)
  63. NETMAKER_BASE_DOMAIN=${OPTARG}
  64. ;;
  65. esac
  66. done
  67. # print_logo - prints the netmaker logo
  68. print_logo() {
  69. cat <<"EOF"
  70. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  71. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  72. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  73. __ __ ______ ______ __ __ ______ __ __ ______ ______
  74. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  75. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  76. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  77. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  78. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  79. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  80. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  81. EOF
  82. }
  83. # set_buildinfo - sets the information based on script input for how the installation should be run
  84. set_buildinfo() {
  85. if [ -z "$BUILD_TYPE" ]; then
  86. BUILD_TYPE="version"
  87. BUILD_TAG=$LATEST
  88. fi
  89. if [ -z "$BUILD_TAG" ] && [ "$BUILD_TYPE" = "version" ]; then
  90. BUILD_TAG=$LATEST
  91. fi
  92. if [ -z "$BUILD_TAG" ] && [ ! -z "$BUILD_TYPE" ]; then
  93. echo "error: must specify build tag when build type \"$BUILD_TYPE\" is specified"
  94. usage
  95. exit 1
  96. fi
  97. IMAGE_TAG=$(sed 's/\//-/g' <<<"$BUILD_TAG")
  98. if [ "$1" = "ce" ]; then
  99. INSTALL_TYPE="ce"
  100. elif [ "$1" = "ee" ]; then
  101. INSTALL_TYPE="ee"
  102. fi
  103. if [ "$AUTO_BUILD" = "on" ] && [ -z "$INSTALL_TYPE" ]; then
  104. INSTALL_TYPE="ce"
  105. elif [ -z "$INSTALL_TYPE" ]; then
  106. echo "-----------------------------------------------------"
  107. echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (EE)?"
  108. echo "EE will require you to create an account at https://app.netmaker.io"
  109. echo "-----------------------------------------------------"
  110. select install_option in "Community Edition" "Enterprise Edition"; do
  111. case $REPLY in
  112. 1)
  113. echo "installing Netmaker CE"
  114. INSTALL_TYPE="ce"
  115. break
  116. ;;
  117. 2)
  118. echo "installing Netmaker EE"
  119. INSTALL_TYPE="ee"
  120. break
  121. ;;
  122. *) echo "invalid option $REPLY" ;;
  123. esac
  124. done
  125. fi
  126. echo "-----------Build Options-----------------------------"
  127. echo " EE or CE: $INSTALL_TYPE"
  128. echo " Build Type: $BUILD_TYPE"
  129. echo " Build Tag: $BUILD_TAG"
  130. echo " Image Tag: $IMAGE_TAG"
  131. echo " Installer: v$NM_QUICK_VERSION"
  132. echo "-----------------------------------------------------"
  133. }
  134. # install_yq - install yq if not present
  135. install_yq() {
  136. if ! command -v yq &>/dev/null; then
  137. wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_$(dpkg --print-architecture)
  138. chmod +x /usr/bin/yq
  139. fi
  140. set +e
  141. if ! command -v yq &>/dev/null; then
  142. set -e
  143. wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_amd64
  144. chmod +x /usr/bin/yq
  145. fi
  146. set -e
  147. if ! command -v yq &>/dev/null; then
  148. echo "failed to install yq. Please install yq and try again."
  149. echo "https://github.com/mikefarah/yq/#install"
  150. exit 1
  151. fi
  152. }
  153. # setup_netclient - adds netclient to docker-compose
  154. setup_netclient() {
  155. set +e
  156. netclient uninstall
  157. set -e
  158. # TODO arm support
  159. wget -qO netclient https://github.com/gravitl/netclient/releases/download/$LATEST/netclient-linux-amd64
  160. chmod +x netclient
  161. ./netclient install
  162. echo "Register token: $TOKEN"
  163. netclient register -t $TOKEN
  164. echo "waiting for netclient to become available"
  165. local found=false
  166. local file=/etc/netclient/nodes.yml
  167. for ((a = 1; a <= 90; a++)); do
  168. if [ -f "$file" ]; then
  169. found=true
  170. break
  171. fi
  172. sleep 1
  173. done
  174. if [ "$found" = false ]; then
  175. echo "Error - $file not present"
  176. exit 1
  177. fi
  178. }
  179. # configure_netclient - configures server's netclient as a default host and an ingress gateway
  180. configure_netclient() {
  181. NODE_ID=$(sudo cat /etc/netclient/nodes.yml | yq -r .netmaker.commonnode.id)
  182. if [ "$NODE_ID" = "" ] || [ "$NODE_ID" = "null" ]; then
  183. echo "Error obtaining NODE_ID for the new network"
  184. exit 1
  185. fi
  186. echo "register complete. New node ID: $NODE_ID"
  187. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  188. if [ "$HOST_ID" = "" ] || [ "$HOST_ID" = "null" ]; then
  189. echo "Error obtaining HOST_ID for the new network"
  190. exit 1
  191. fi
  192. echo "making host a default"
  193. echo "Host ID: $HOST_ID"
  194. # set as a default host
  195. set +e
  196. nmctl host update $HOST_ID --default
  197. sleep 5
  198. nmctl node create_ingress netmaker $NODE_ID
  199. set -e
  200. }
  201. # setup_nmctl - pulls nmctl and makes it executable
  202. setup_nmctl() {
  203. # TODO arm support
  204. local URL="https://github.com/gravitl/netmaker/releases/download/$LATEST/nmctl-linux-amd64"
  205. echo "Downloading nmctl..."
  206. wget -qO /usr/bin/nmctl "$URL"
  207. if [ ! -f /usr/bin/nmctl ]; then
  208. echo "Error downloading nmctl from '$URL'"
  209. exit 1
  210. fi
  211. chmod +x /usr/bin/nmctl
  212. echo "using server api.$NETMAKER_BASE_DOMAIN"
  213. echo "using master key $MASTER_KEY"
  214. nmctl context set default --endpoint="https://api.$NETMAKER_BASE_DOMAIN" --master_key="$MASTER_KEY"
  215. nmctl context use default
  216. RESP=$(nmctl network list)
  217. if [[ $RESP == *"unauthorized"* ]]; then
  218. echo "Unable to properly configure NMCTL, exiting..."
  219. exit 1
  220. fi
  221. }
  222. # wait_seconds - wait for the specified period of time
  223. wait_seconds() { (
  224. for ((a = 1; a <= $1; a++)); do
  225. echo ". . ."
  226. sleep 1
  227. done
  228. ); }
  229. # confirm - get user input to confirm that they want to perform the next step
  230. confirm() { (
  231. if [ "$AUTO_BUILD" = "on" ]; then
  232. return 0
  233. fi
  234. while true; do
  235. read -p 'Does everything look right? [y/n]: ' yn
  236. case $yn in
  237. [Yy]*)
  238. override="true"
  239. break
  240. ;;
  241. [Nn]*)
  242. echo "exiting..."
  243. exit 1
  244. # TODO start from the beginning instead
  245. ;;
  246. *) echo "Please answer yes or no." ;;
  247. esac
  248. done
  249. ) }
  250. save_config() { (
  251. echo "Saving the config to $CONFIG_PATH"
  252. touch "$CONFIG_PATH"
  253. save_config_item NM_EMAIL "$EMAIL"
  254. save_config_item NM_DOMAIN "$NETMAKER_BASE_DOMAIN"
  255. save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
  256. if [ "$BUILD_TYPE" = "local" ]; then
  257. save_config_item UI_IMAGE_TAG "$LATEST"
  258. else
  259. save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
  260. fi
  261. # version-specific entries
  262. if [ "$INSTALL_TYPE" = "ee" ]; then
  263. save_config_item NETMAKER_TENANT_ID "$TENANT_ID"
  264. save_config_item LICENSE_KEY "$LICENSE_KEY"
  265. save_config_item METRICS_EXPORTER "on"
  266. save_config_item PROMETHEUS "on"
  267. if [ "$BUILD_TYPE" = "version" ]; then
  268. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG-ee"
  269. else
  270. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG"
  271. fi
  272. else
  273. save_config_item METRICS_EXPORTER "off"
  274. save_config_item PROMETHEUS "off"
  275. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG"
  276. fi
  277. # copy entries from the previous config
  278. local toCopy=("SERVER_HOST" "MASTER_KEY" "TURN_USERNAME" "TURN_PASSWORD" "MQ_USERNAME" "MQ_PASSWORD"
  279. "INSTALL_TYPE" "NODE_ID" "DNS_MODE" "NETCLIENT_AUTO_UPDATE" "API_PORT"
  280. "CORS_ALLOWED_ORIGIN" "DISPLAY_KEYS" "DATABASE" "SERVER_BROKER_ENDPOINT" "STUN_PORT" "VERBOSITY"
  281. "DEFAULT_PROXY_MODE" "TURN_PORT" "USE_TURN" "DEBUG_MODE" "TURN_API_PORT" "REST_BACKEND"
  282. "DISABLE_REMOTE_IP_CHECK" "NETCLIENT_ENDPOINT_DETECTION" "TELEMETRY" "AUTH_PROVIDER" "CLIENT_ID" "CLIENT_SECRET"
  283. "FRONTEND_URL" "AZURE_TENANT" "OIDC_ISSUER" "EXPORTER_API_PORT")
  284. for name in "${toCopy[@]}"; do
  285. save_config_item $name "${!name}"
  286. done
  287. # preserve debug entries
  288. if test -n "$NM_SKIP_BUILD"; then
  289. save_config_item NM_SKIP_BUILD "$NM_SKIP_BUILD"
  290. fi
  291. if test -n "$NM_SKIP_CLONE"; then
  292. save_config_item NM_SKIP_CLONE "$NM_SKIP_CLONE"
  293. fi
  294. if test -n "$NM_SKIP_DEPS"; then
  295. save_config_item NM_SKIP_DEPS "$NM_SKIP_DEPS"
  296. fi
  297. ); }
  298. save_config_item() { (
  299. local NAME="$1"
  300. local VALUE="$2"
  301. #echo "$NAME=$VALUE"
  302. if test -z "$VALUE"; then
  303. # load the default for empty values
  304. VALUE=$(awk -F'=' "/^$NAME/ { print \$2}" "$SCRIPT_DIR/netmaker.default.env")
  305. # trim quotes for docker
  306. VALUE=$(echo "$VALUE" | sed -E "s|^(['\"])(.*)\1$|\2|g")
  307. #echo "Default for $NAME=$VALUE"
  308. fi
  309. # TODO single quote passwords
  310. if grep -q "^$NAME=" "$CONFIG_PATH"; then
  311. # TODO escape | in the value
  312. sed -i "s|$NAME=.*|$NAME=$VALUE|" "$CONFIG_PATH"
  313. else
  314. echo "$NAME=$VALUE" >>"$CONFIG_PATH"
  315. fi
  316. ); }
  317. # local_install_setup - builds artifacts based on specified branch locally to use in install
  318. local_install_setup() { (
  319. if test -z "$NM_SKIP_CLONE"; then
  320. rm -rf netmaker-tmp
  321. mkdir netmaker-tmp
  322. cd netmaker-tmp
  323. git clone --single-branch --depth=1 --branch=$BUILD_TAG https://www.github.com/gravitl/netmaker
  324. else
  325. cd netmaker-tmp
  326. echo "Skipping git clone on NM_SKIP_CLONE"
  327. fi
  328. cd netmaker
  329. if test -z "$NM_SKIP_BUILD"; then
  330. docker build --no-cache --build-arg version=$IMAGE_TAG -t gravitl/netmaker:$IMAGE_TAG .
  331. else
  332. echo "Skipping build on NM_SKIP_BUILD"
  333. fi
  334. cp compose/docker-compose.yml "$SCRIPT_DIR/docker-compose.yml"
  335. if [ "$INSTALL_TYPE" = "ee" ]; then
  336. cp compose/docker-compose.ee.yml "$SCRIPT_DIR/docker-compose.override.yml"
  337. cp docker/Caddyfile-EE "$SCRIPT_DIR/Caddyfile"
  338. else
  339. cp docker/Caddyfile "$SCRIPT_DIR/Caddyfile"
  340. fi
  341. cp scripts/nm-certs.sh "$SCRIPT_DIR/nm-certs.sh"
  342. cp scripts/netmaker.default.env "$SCRIPT_DIR/netmaker.default.env"
  343. cp docker/mosquitto.conf "$SCRIPT_DIR/mosquitto.conf"
  344. cp docker/wait.sh "$SCRIPT_DIR/wait.sh"
  345. cd ../../
  346. if test -z "$NM_SKIP_CLONE"; then
  347. rm -rf netmaker-tmp
  348. fi
  349. ); }
  350. # install_dependencies - install necessary packages to run netmaker
  351. install_dependencies() {
  352. if test -n "$NM_SKIP_DEPS"; then
  353. return
  354. fi
  355. echo "checking dependencies..."
  356. OS=$(uname)
  357. if [ -f /etc/debian_version ]; then
  358. dependencies="git wireguard wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  359. update_cmd='apt update'
  360. install_cmd='apt-get install -y'
  361. elif [ -f /etc/alpine-release ]; then
  362. dependencies="git wireguard jq docker.io docker-compose grep gawk"
  363. update_cmd='apk update'
  364. install_cmd='apk --update add'
  365. elif [ -f /etc/centos-release ]; then
  366. dependencies="git wireguard jq bind-utils docker.io docker-compose grep gawk"
  367. update_cmd='yum update'
  368. install_cmd='yum install -y'
  369. elif [ -f /etc/fedora-release ]; then
  370. dependencies="git wireguard bind-utils jq docker.io docker-compose grep gawk"
  371. update_cmd='dnf update'
  372. install_cmd='dnf install -y'
  373. elif [ -f /etc/redhat-release ]; then
  374. dependencies="git wireguard jq docker.io bind-utils docker-compose grep gawk"
  375. update_cmd='yum update'
  376. install_cmd='yum install -y'
  377. elif [ -f /etc/arch-release ]; then
  378. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  379. update_cmd='pacman -Sy'
  380. install_cmd='pacman -S --noconfirm'
  381. elif [ "${OS}" = "FreeBSD" ]; then
  382. dependencies="git wireguard wget jq docker.io docker-compose grep gawk"
  383. update_cmd='pkg update'
  384. install_cmd='pkg install -y'
  385. else
  386. install_cmd=''
  387. fi
  388. if [ -z "${install_cmd}" ]; then
  389. echo "OS unsupported for automatic dependency install"
  390. # TODO shouldnt exit, check if deps available, if not
  391. # ask the user to install manually and continue when ready
  392. exit 1
  393. fi
  394. set -- $dependencies
  395. ${update_cmd}
  396. while [ -n "$1" ]; do
  397. if [ "${OS}" = "FreeBSD" ]; then
  398. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  399. if [ "$is_installed" != "" ]; then
  400. echo " " $1 is installed
  401. else
  402. echo " " $1 is not installed. Attempting install.
  403. ${install_cmd} $1
  404. sleep 5
  405. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  406. if [ "$is_installed" != "" ]; then
  407. echo " " $1 is installed
  408. elif [ -x "$(command -v $1)" ]; then
  409. echo " " $1 is installed
  410. else
  411. echo " " FAILED TO INSTALL $1
  412. echo " " This may break functionality.
  413. fi
  414. fi
  415. else
  416. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  417. is_installed=$(opkg list-installed $1 | grep $1)
  418. else
  419. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  420. fi
  421. if [ "${is_installed}" != "" ]; then
  422. echo " " $1 is installed
  423. else
  424. echo " " $1 is not installed. Attempting install.
  425. ${install_cmd} $1
  426. sleep 5
  427. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  428. is_installed=$(opkg list-installed $1 | grep $1)
  429. else
  430. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  431. fi
  432. if [ "${is_installed}" != "" ]; then
  433. echo " " $1 is installed
  434. elif [ -x "$(command -v $1)" ]; then
  435. echo " " $1 is installed
  436. else
  437. echo " " FAILED TO INSTALL $1
  438. echo " " This may break functionality.
  439. fi
  440. fi
  441. fi
  442. shift
  443. done
  444. echo "-----------------------------------------------------"
  445. echo "dependency check complete"
  446. echo "-----------------------------------------------------"
  447. }
  448. set -e
  449. # set_install_vars - sets the variables that will be used throughout installation
  450. set_install_vars() {
  451. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  452. if [ "$IP_ADDR" = "" ]; then
  453. IP_ADDR=$(curl -s ifconfig.me)
  454. fi
  455. if [ "$NETMAKER_BASE_DOMAIN" = "" ]; then
  456. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  457. fi
  458. SERVER_HOST=$IP_ADDR
  459. if test -z "$MASTER_KEY"; then
  460. MASTER_KEY=$(
  461. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  462. echo ''
  463. )
  464. fi
  465. DOMAIN_TYPE=""
  466. echo "-----------------------------------------------------"
  467. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  468. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_HOST"
  469. echo "IMPORTANT: Due to the high volume of requests, the auto-generated domain has been rate-limited by the certificate provider."
  470. 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."
  471. echo "-----------------------------------------------------"
  472. if [ "$AUTO_BUILD" = "on" ]; then
  473. DOMAIN_TYPE="auto"
  474. else
  475. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  476. case $REPLY in
  477. 1)
  478. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  479. DOMAIN_TYPE="auto"
  480. break
  481. ;;
  482. 2)
  483. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_HOST first): " domain
  484. NETMAKER_BASE_DOMAIN=$domain
  485. echo "using $NETMAKER_BASE_DOMAIN"
  486. DOMAIN_TYPE="custom"
  487. break
  488. ;;
  489. *) echo "invalid option $REPLY" ;;
  490. esac
  491. done
  492. fi
  493. wait_seconds 2
  494. echo "-----------------------------------------------------"
  495. echo "The following subdomains will be used:"
  496. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  497. echo " api.$NETMAKER_BASE_DOMAIN"
  498. echo " broker.$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://app.netmaker.io"
  516. echo " 2. follow instructions to get a license at: https://docs.netmaker.io/ee/ee-setup.html"
  517. echo " 3. Retrieve License and Tenant ID"
  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 TENANT_ID
  525. while [ -z ${TENANT_ID} ]; do
  526. read -p "Tenant ID: " TENANT_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: $TENANT_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. if [ "$UPGRADE_FLAG" = "yes" ]; then
  785. INSTALL_TYPE="ee"
  786. fi
  787. fi
  788. # 2. setup the build instructions
  789. set_buildinfo
  790. set +e
  791. # 3. install necessary packages
  792. install_dependencies
  793. # 4. install yq if necessary
  794. install_yq
  795. set -e
  796. # 6. get user input for variables
  797. set_install_vars
  798. set +e
  799. cleanup
  800. set -e
  801. # 7. get and set config files, startup docker-compose
  802. install_netmaker
  803. set +e
  804. # 8. make sure Caddy certs are working
  805. test_connection
  806. # 9. install the netmaker CLI
  807. setup_nmctl
  808. # 10. create a default mesh network for netmaker
  809. setup_mesh
  810. set -e
  811. # 11. add netclient to docker-compose and start it up
  812. setup_netclient
  813. # 12. make the netclient a default host and ingress gw
  814. configure_netclient
  815. # 13. print success message
  816. print_success
  817. # cp -f /etc/skel/.bashrc /root/.bashrc