nm-quick.sh 25 KB

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