nm-quick.sh 24 KB

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