nm-quick.sh 24 KB

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