nm-quick.sh 27 KB

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