nm-quick.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 pro"
  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="pro"
  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" = "pro" ]; then
  101. INSTALL_TYPE="pro"
  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 (pro)?"
  108. echo "pro 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 pro"
  119. INSTALL_TYPE="pro"
  120. break
  121. ;;
  122. *) echo "invalid option $REPLY" ;;
  123. esac
  124. done
  125. fi
  126. echo "-----------Build Options-----------------------------"
  127. echo " Pro 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. wget -qO netclient https://github.com/gravitl/netclient/releases/download/$LATEST/netclient-linux-$ARCH
  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. local URL="https://github.com/gravitl/netmaker/releases/download/$LATEST/nmctl-linux-$ARCH"
  203. echo "Downloading nmctl..."
  204. wget -qO /usr/bin/nmctl "$URL"
  205. if [ ! -f /usr/bin/nmctl ]; then
  206. echo "Error downloading nmctl from '$URL'"
  207. exit 1
  208. fi
  209. chmod +x /usr/bin/nmctl
  210. echo "using server api.$NETMAKER_BASE_DOMAIN"
  211. echo "using master key $MASTER_KEY"
  212. nmctl context set default --endpoint="https://api.$NETMAKER_BASE_DOMAIN" --master_key="$MASTER_KEY"
  213. nmctl context use default
  214. RESP=$(nmctl network list)
  215. if [[ $RESP == *"unauthorized"* ]]; then
  216. echo "Unable to properly configure NMCTL, exiting..."
  217. exit 1
  218. fi
  219. }
  220. # wait_seconds - wait for the specified period of time
  221. wait_seconds() { (
  222. for ((a = 1; a <= $1; a++)); do
  223. echo ". . ."
  224. sleep 1
  225. done
  226. ); }
  227. # confirm - get user input to confirm that they want to perform the next step
  228. confirm() { (
  229. if [ "$AUTO_BUILD" = "on" ]; then
  230. return 0
  231. fi
  232. while true; do
  233. read -p 'Does everything look right? [y/n]: ' yn
  234. case $yn in
  235. [Yy]*)
  236. override="true"
  237. break
  238. ;;
  239. [Nn]*)
  240. echo "exiting..."
  241. exit 1
  242. # TODO start from the beginning instead
  243. ;;
  244. *) echo "Please answer yes or no." ;;
  245. esac
  246. done
  247. ) }
  248. save_config() { (
  249. echo "Saving the config to $CONFIG_PATH"
  250. touch "$CONFIG_PATH"
  251. save_config_item NM_EMAIL "$EMAIL"
  252. save_config_item NM_DOMAIN "$NETMAKER_BASE_DOMAIN"
  253. save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
  254. if [ "$BUILD_TYPE" = "local" ]; then
  255. save_config_item UI_IMAGE_TAG "$LATEST"
  256. else
  257. save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
  258. fi
  259. # version-specific entries
  260. if [ "$INSTALL_TYPE" = "pro" ]; then
  261. save_config_item NETMAKER_TENANT_ID "$TENANT_ID"
  262. save_config_item LICENSE_KEY "$LICENSE_KEY"
  263. save_config_item METRICS_EXPORTER "on"
  264. save_config_item PROMETHEUS "on"
  265. if [ "$BUILD_TYPE" = "version" ]; then
  266. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG-ee"
  267. else
  268. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG"
  269. fi
  270. else
  271. save_config_item METRICS_EXPORTER "off"
  272. save_config_item PROMETHEUS "off"
  273. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG"
  274. fi
  275. # copy entries from the previous config
  276. local toCopy=("SERVER_HOST" "MASTER_KEY" "MQ_USERNAME" "MQ_PASSWORD"
  277. "INSTALL_TYPE" "NODE_ID" "DNS_MODE" "NETCLIENT_AUTO_UPDATE" "API_PORT"
  278. "CORS_ALLOWED_ORIGIN" "DISPLAY_KEYS" "DATABASE" "SERVER_BROKER_ENDPOINT" "VERBOSITY"
  279. "DEBUG_MODE" "REST_BACKEND" "DISABLE_REMOTE_IP_CHECK" "TELEMETRY" "AUTH_PROVIDER" "CLIENT_ID" "CLIENT_SECRET"
  280. "FRONTEND_URL" "AZURE_TENANT" "OIDC_ISSUER" "EXPORTER_API_PORT" "JWT_VALIDITY_DURATION" "RAC_AUTO_DISABLE")
  281. for name in "${toCopy[@]}"; do
  282. save_config_item $name "${!name}"
  283. done
  284. # preserve debug entries
  285. if test -n "$NM_SKIP_BUILD"; then
  286. save_config_item NM_SKIP_BUILD "$NM_SKIP_BUILD"
  287. fi
  288. if test -n "$NM_SKIP_CLONE"; then
  289. save_config_item NM_SKIP_CLONE "$NM_SKIP_CLONE"
  290. fi
  291. if test -n "$NM_SKIP_DEPS"; then
  292. save_config_item NM_SKIP_DEPS "$NM_SKIP_DEPS"
  293. fi
  294. ); }
  295. save_config_item() { (
  296. local NAME="$1"
  297. local VALUE="$2"
  298. #echo "$NAME=$VALUE"
  299. if test -z "$VALUE"; then
  300. # load the default for empty values
  301. VALUE=$(awk -F'=' "/^$NAME/ { print \$2}" "$SCRIPT_DIR/netmaker.default.env")
  302. # trim quotes for docker
  303. VALUE=$(echo "$VALUE" | sed -E "s|^(['\"])(.*)\1$|\2|g")
  304. #echo "Default for $NAME=$VALUE"
  305. fi
  306. # TODO single quote passwords
  307. if grep -q "^$NAME=" "$CONFIG_PATH"; then
  308. # TODO escape | in the value
  309. sed -i "s|$NAME=.*|$NAME=$VALUE|" "$CONFIG_PATH"
  310. else
  311. echo "$NAME=$VALUE" >>"$CONFIG_PATH"
  312. fi
  313. ); }
  314. # local_install_setup - builds artifacts based on specified branch locally to use in install
  315. local_install_setup() { (
  316. if test -z "$NM_SKIP_CLONE"; then
  317. rm -rf netmaker-tmp
  318. mkdir netmaker-tmp
  319. cd netmaker-tmp
  320. git clone --single-branch --depth=1 --branch=$BUILD_TAG https://www.github.com/gravitl/netmaker
  321. else
  322. cd netmaker-tmp
  323. echo "Skipping git clone on NM_SKIP_CLONE"
  324. fi
  325. cd netmaker
  326. if test -z "$NM_SKIP_BUILD"; then
  327. docker build --no-cache --build-arg version=$IMAGE_TAG -t gravitl/netmaker:$IMAGE_TAG .
  328. else
  329. echo "Skipping build on NM_SKIP_BUILD"
  330. fi
  331. cp compose/docker-compose.yml "$SCRIPT_DIR/docker-compose.yml"
  332. if [ "$INSTALL_TYPE" = "pro" ]; then
  333. cp compose/docker-compose.ee.yml "$SCRIPT_DIR/docker-compose.override.yml"
  334. cp docker/Caddyfile-pro "$SCRIPT_DIR/Caddyfile"
  335. else
  336. cp docker/Caddyfile "$SCRIPT_DIR/Caddyfile"
  337. fi
  338. cp scripts/netmaker.default.env "$SCRIPT_DIR/netmaker.default.env"
  339. cp docker/mosquitto.conf "$SCRIPT_DIR/mosquitto.conf"
  340. cp docker/wait.sh "$SCRIPT_DIR/wait.sh"
  341. cd ../../
  342. if test -z "$NM_SKIP_CLONE"; then
  343. rm -rf netmaker-tmp
  344. fi
  345. ); }
  346. # install_dependencies - install necessary packages to run netmaker
  347. install_dependencies() {
  348. if test -n "$NM_SKIP_DEPS"; then
  349. return
  350. fi
  351. echo "checking dependencies..."
  352. OS=$(uname)
  353. if [ -f /etc/debian_version ]; then
  354. dependencies="git wireguard wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  355. update_cmd='apt update'
  356. install_cmd='apt-get install -y'
  357. elif [ -f /etc/alpine-release ]; then
  358. dependencies="git wireguard jq docker.io docker-compose grep gawk"
  359. update_cmd='apk update'
  360. install_cmd='apk --update add'
  361. elif [ -f /etc/centos-release ]; then
  362. dependencies="git wireguard jq bind-utils docker.io docker-compose grep gawk"
  363. update_cmd='yum update'
  364. install_cmd='yum install -y'
  365. elif [ -f /etc/fedora-release ]; then
  366. dependencies="git wireguard bind-utils jq docker.io docker-compose grep gawk"
  367. update_cmd='dnf update'
  368. install_cmd='dnf install -y'
  369. elif [ -f /etc/redhat-release ]; then
  370. dependencies="git wireguard jq docker.io bind-utils docker-compose grep gawk"
  371. update_cmd='yum update'
  372. install_cmd='yum install -y'
  373. elif [ -f /etc/arch-release ]; then
  374. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  375. update_cmd='pacman -Sy'
  376. install_cmd='pacman -S --noconfirm'
  377. elif [ "${OS}" = "FreeBSD" ]; then
  378. dependencies="git wireguard wget jq docker.io docker-compose grep gawk"
  379. update_cmd='pkg update'
  380. install_cmd='pkg install -y'
  381. else
  382. install_cmd=''
  383. fi
  384. if [ -z "${install_cmd}" ]; then
  385. echo "OS unsupported for automatic dependency install"
  386. # TODO shouldnt exit, check if deps available, if not
  387. # ask the user to install manually and continue when ready
  388. exit 1
  389. fi
  390. # TODO add other supported architectures
  391. ARCH=$(uname -m)
  392. if [ "$ARCH" = "x86_64" ]; then
  393. ARCH=amd64
  394. elif [ "$ARCH" = "aarch64" ]; then
  395. ARCH=arm64
  396. else
  397. echo "Unsupported architechure"
  398. # exit 1
  399. fi
  400. set -- $dependencies
  401. ${update_cmd}
  402. while [ -n "$1" ]; do
  403. if [ "${OS}" = "FreeBSD" ]; then
  404. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  405. if [ "$is_installed" != "" ]; then
  406. echo " " $1 is installed
  407. else
  408. echo " " $1 is not installed. Attempting install.
  409. ${install_cmd} $1
  410. sleep 5
  411. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  412. if [ "$is_installed" != "" ]; then
  413. echo " " $1 is installed
  414. elif [ -x "$(command -v $1)" ]; then
  415. echo " " $1 is installed
  416. else
  417. echo " " FAILED TO INSTALL $1
  418. echo " " This may break functionality.
  419. fi
  420. fi
  421. else
  422. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  423. is_installed=$(opkg list-installed $1 | grep $1)
  424. else
  425. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  426. fi
  427. if [ "${is_installed}" != "" ]; then
  428. echo " " $1 is installed
  429. else
  430. echo " " $1 is not installed. Attempting install.
  431. ${install_cmd} $1
  432. sleep 5
  433. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  434. is_installed=$(opkg list-installed $1 | grep $1)
  435. else
  436. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  437. fi
  438. if [ "${is_installed}" != "" ]; then
  439. echo " " $1 is installed
  440. elif [ -x "$(command -v $1)" ]; then
  441. echo " " $1 is installed
  442. else
  443. echo " " FAILED TO INSTALL $1
  444. echo " " This may break functionality.
  445. fi
  446. fi
  447. fi
  448. shift
  449. done
  450. echo "-----------------------------------------------------"
  451. echo "dependency check complete"
  452. echo "-----------------------------------------------------"
  453. }
  454. set -e
  455. # set_install_vars - sets the variables that will be used throughout installation
  456. set_install_vars() {
  457. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  458. if [ "$IP_ADDR" = "" ]; then
  459. IP_ADDR=$(curl -s ifconfig.me)
  460. fi
  461. if [ "$NETMAKER_BASE_DOMAIN" = "" ]; then
  462. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  463. fi
  464. SERVER_HOST=$IP_ADDR
  465. if test -z "$MASTER_KEY"; then
  466. MASTER_KEY=$(
  467. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  468. echo ''
  469. )
  470. fi
  471. DOMAIN_TYPE=""
  472. echo "-----------------------------------------------------"
  473. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  474. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_HOST"
  475. echo "IMPORTANT: Due to the high volume of requests, the auto-generated domain has been rate-limited by the certificate provider."
  476. 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."
  477. echo "-----------------------------------------------------"
  478. if [ "$AUTO_BUILD" = "on" ]; then
  479. DOMAIN_TYPE="auto"
  480. else
  481. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  482. case $REPLY in
  483. 1)
  484. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  485. DOMAIN_TYPE="auto"
  486. break
  487. ;;
  488. 2)
  489. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_HOST first): " domain
  490. NETMAKER_BASE_DOMAIN=$domain
  491. echo "using $NETMAKER_BASE_DOMAIN"
  492. DOMAIN_TYPE="custom"
  493. break
  494. ;;
  495. *) echo "invalid option $REPLY" ;;
  496. esac
  497. done
  498. fi
  499. wait_seconds 2
  500. echo "-----------------------------------------------------"
  501. echo "The following subdomains will be used:"
  502. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  503. echo " api.$NETMAKER_BASE_DOMAIN"
  504. echo " broker.$NETMAKER_BASE_DOMAIN"
  505. if [ "$INSTALL_TYPE" = "pro" ]; then
  506. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  507. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  508. echo " grafana.$NETMAKER_BASE_DOMAIN"
  509. fi
  510. echo "-----------------------------------------------------"
  511. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  512. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_HOST"
  513. confirm
  514. fi
  515. wait_seconds 1
  516. if [ "$INSTALL_TYPE" = "pro" ]; then
  517. echo "-----------------------------------------------------"
  518. echo "Provide Details for pro installation:"
  519. echo " 1. Log into https://app.netmaker.io"
  520. echo " 2. follow instructions to get a license at: https://docs.netmaker.io/ee/ee-setup.html"
  521. echo " 3. Retrieve License and Tenant ID"
  522. echo " 4. note email address"
  523. echo "-----------------------------------------------------"
  524. unset LICENSE_KEY
  525. while [ -z "$LICENSE_KEY" ]; do
  526. read -p "License Key: " LICENSE_KEY
  527. done
  528. unset TENANT_ID
  529. while [ -z ${TENANT_ID} ]; do
  530. read -p "Tenant ID: " TENANT_ID
  531. done
  532. fi
  533. unset GET_EMAIL
  534. unset RAND_EMAIL
  535. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  536. # suggest the prev email or a random one
  537. EMAIL_SUGGESTED=${NM_EMAIL:-$RAND_EMAIL}
  538. if [ -z $AUTO_BUILD ]; then
  539. read -p "Email Address for Domain Registration (click 'enter' to use $EMAIL_SUGGESTED): " GET_EMAIL
  540. fi
  541. if [ -z "$GET_EMAIL" ]; then
  542. EMAIL="$EMAIL_SUGGESTED"
  543. if [ "$EMAIL" = "$NM_EMAIL" ]; then
  544. echo "using config email"
  545. else
  546. echo "using rand email"
  547. fi
  548. else
  549. EMAIL="$GET_EMAIL"
  550. fi
  551. wait_seconds 1
  552. unset GET_MQ_USERNAME
  553. unset GET_MQ_PASSWORD
  554. unset CONFIRM_MQ_PASSWORD
  555. echo "Enter Credentials For MQ..."
  556. if [ -z $AUTO_BUILD ]; then
  557. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  558. fi
  559. if [ -z "$GET_MQ_USERNAME" ]; then
  560. echo "using default username for mq"
  561. MQ_USERNAME="netmaker"
  562. else
  563. MQ_USERNAME="$GET_MQ_USERNAME"
  564. fi
  565. if test -z "$MQ_PASSWORD"; then
  566. MQ_PASSWORD=$(
  567. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  568. echo ''
  569. )
  570. fi
  571. if [ -z $AUTO_BUILD ]; then
  572. select domain_option in "Auto Generated / Config Password" "Input Your Own Password"; do
  573. case $REPLY in
  574. 1)
  575. echo "using random password for mq"
  576. break
  577. ;;
  578. 2)
  579. while true; do
  580. echo "Enter your Password For MQ: "
  581. read -s GET_MQ_PASSWORD
  582. echo "Enter your password again to confirm: "
  583. read -s CONFIRM_MQ_PASSWORD
  584. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  585. echo "wrong password entered, try again..."
  586. continue
  587. fi
  588. MQ_PASSWORD="$GET_MQ_PASSWORD"
  589. echo "MQ Password Saved Successfully!!"
  590. break
  591. done
  592. break
  593. ;;
  594. *) echo "invalid option $REPLY" ;;
  595. esac
  596. done
  597. fi
  598. wait_seconds 2
  599. echo "-----------------------------------------------------------------"
  600. echo " SETUP ARGUMENTS"
  601. echo "-----------------------------------------------------------------"
  602. echo " domain: $NETMAKER_BASE_DOMAIN"
  603. echo " email: $EMAIL"
  604. echo " public ip: $SERVER_HOST"
  605. if [ "$INSTALL_TYPE" = "pro" ]; then
  606. echo " license: $LICENSE_KEY"
  607. echo " account id: $TENANT_ID"
  608. fi
  609. echo "-----------------------------------------------------------------"
  610. echo "Confirm Settings for Installation"
  611. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  612. if [ ! "$BUILD_TYPE" = "local" ]; then
  613. IMAGE_TAG="$LATEST"
  614. fi
  615. confirm
  616. }
  617. # install_netmaker - sets the config files and starts docker-compose
  618. install_netmaker() {
  619. echo "-----------------------------------------------------------------"
  620. echo "Beginning installation..."
  621. echo "-----------------------------------------------------------------"
  622. wait_seconds 3
  623. echo "Pulling config files..."
  624. if [ "$BUILD_TYPE" = "local" ]; then
  625. local_install_setup
  626. else
  627. local BASE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG"
  628. local COMPOSE_URL="$BASE_URL/compose/docker-compose.yml"
  629. local CADDY_URL="$BASE_URL/docker/Caddyfile"
  630. if [ "$INSTALL_TYPE" = "pro" ]; then
  631. local COMPOSE_OVERRIDE_URL="$BASE_URL/compose/docker-compose.pro.yml"
  632. local CADDY_URL="$BASE_URL/docker/Caddyfile-pro"
  633. fi
  634. wget -qO "$SCRIPT_DIR"/docker-compose.yml $COMPOSE_URL
  635. if test -n "$COMPOSE_OVERRIDE_URL"; then
  636. wget -qO "$SCRIPT_DIR"/docker-compose.override.yml $COMPOSE_OVERRIDE_URL
  637. fi
  638. wget -qO "$SCRIPT_DIR"/Caddyfile "$CADDY_URL"
  639. wget -qO "$SCRIPT_DIR"/netmaker.default.env "$BASE_URL/scripts/netmaker.default.env"
  640. wget -qO "$SCRIPT_DIR"/mosquitto.conf "$BASE_URL/docker/mosquitto.conf"
  641. wget -qO "$SCRIPT_DIR"/wait.sh "$BASE_URL/docker/wait.sh"
  642. fi
  643. chmod +x "$SCRIPT_DIR"/wait.sh
  644. mkdir -p /etc/netmaker
  645. # link .env to the user config
  646. ln -fs "$SCRIPT_DIR/netmaker.env" "$SCRIPT_DIR/.env"
  647. save_config
  648. echo "Starting containers..."
  649. # increase the timeouts
  650. export DOCKER_CLIENT_TIMEOUT=120
  651. export COMPOSE_HTTP_TIMEOUT=120
  652. # start docker and rebuild containers / networks
  653. cd "${SCRIPT_DIR}"
  654. docker-compose up -d --force-recreate
  655. cd -
  656. wait_seconds 2
  657. }
  658. # test_connection - tests to make sure Caddy has proper SSL certs
  659. test_connection() {
  660. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  661. for i in 1 2 3 4 5 6 7 8; do
  662. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  663. if [[ "$i" == 8 ]]; then
  664. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  665. echo " Exiting..."
  666. exit 1
  667. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  668. echo " Certificates not yet configured, retrying..."
  669. elif [[ "$curlresponse" == *"left intact"* ]]; then
  670. echo " Certificates ok"
  671. break
  672. else
  673. secs=$(($i * 5 + 10))
  674. echo " Issue establishing connection...retrying in $secs seconds..."
  675. fi
  676. sleep $secs
  677. done
  678. }
  679. # setup_mesh - sets up a default mesh network on the server
  680. setup_mesh() {
  681. wait_seconds 5
  682. local networkCount=$(nmctl network list -o json | jq '. | length')
  683. # add a network if none present
  684. if [ "$networkCount" -lt 1 ]; then
  685. echo "Creating netmaker network (10.101.0.0/16)"
  686. # TODO causes "Error Status: 400 Response: {"Code":400,"Message":"could not find any records"}"
  687. nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16
  688. wait_seconds 5
  689. fi
  690. echo "Obtaining a netmaker enrollment key..."
  691. local tokenJson=$(nmctl enrollment_key create --unlimited --networks netmaker)
  692. TOKEN=$(jq -r '.token' <<<${tokenJson})
  693. if test -z "$TOKEN"; then
  694. echo "Error creating an enrollment key"
  695. exit 1
  696. else
  697. echo "Enrollment key ready"
  698. fi
  699. wait_seconds 3
  700. }
  701. # print_success - prints a success message upon completion
  702. print_success() {
  703. echo "-----------------------------------------------------------------"
  704. echo "-----------------------------------------------------------------"
  705. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  706. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  707. echo "-----------------------------------------------------------------"
  708. echo "-----------------------------------------------------------------"
  709. }
  710. cleanup() {
  711. # remove the existing netclient's instance from the existing network
  712. if command -v nmctl >/dev/null 2>&1; then
  713. local node_id=$(netclient list | jq '.[0].node_id' 2>/dev/null)
  714. # trim doublequotes
  715. node_id="${node_id//\"/}"
  716. if test -n "$node_id"; then
  717. echo "De-registering the existing netclient..."
  718. nmctl node delete netmaker $node_id >/dev/null 2>&1
  719. fi
  720. fi
  721. echo "Stopping all containers..."
  722. local containers=("mq" "netmaker-ui" "coredns" "turn" "caddy" "netmaker" "netmaker-exporter" "prometheus" "grafana")
  723. for name in "${containers[@]}"; do
  724. local running=$(docker ps | grep -w "$name")
  725. local exists=$(docker ps -a | grep -w "$name")
  726. if test -n "$running"; then
  727. docker stop "$name" 1>/dev/null
  728. fi
  729. if test -n "$exists"; then
  730. docker rm "$name" 1>/dev/null
  731. fi
  732. done
  733. }
  734. # 1. print netmaker logo
  735. print_logo
  736. # read the config
  737. if [ -f "$CONFIG_PATH" ]; then
  738. echo "Using config: $CONFIG_PATH"
  739. source "$CONFIG_PATH"
  740. if [ "$UPGRADE_FLAG" = "yes" ]; then
  741. INSTALL_TYPE="pro"
  742. fi
  743. fi
  744. # 2. setup the build instructions
  745. set_buildinfo
  746. set +e
  747. # 3. install necessary packages
  748. install_dependencies
  749. # 4. install yq if necessary
  750. install_yq
  751. set -e
  752. # 6. get user input for variables
  753. set_install_vars
  754. set +e
  755. cleanup
  756. set -e
  757. # 7. get and set config files, startup docker-compose
  758. install_netmaker
  759. set +e
  760. # 8. make sure Caddy certs are working
  761. test_connection
  762. # 9. install the netmaker CLI
  763. setup_nmctl
  764. # 10. create a default mesh network for netmaker
  765. setup_mesh
  766. set -e
  767. # 11. add netclient to docker-compose and start it up
  768. setup_netclient
  769. # 12. make the netclient a default host and ingress gw
  770. configure_netclient
  771. # 13. print success message
  772. print_success
  773. # cp -f /etc/skel/.bashrc /root/.bashrc