nm-quick.sh 26 KB

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