2
0

nm-quick.sh 27 KB

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