nm-quick.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. if [ "$INSTALL_TYPE" = "ee" ]; then
  389. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  390. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  391. echo " grafana.$NETMAKER_BASE_DOMAIN"
  392. fi
  393. echo "-----------------------------------------------------"
  394. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  395. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_PUBLIC_IP"
  396. confirm
  397. fi
  398. wait_seconds 1
  399. if [ "$INSTALL_TYPE" = "ee" ]; then
  400. echo "-----------------------------------------------------"
  401. echo "Provide Details for EE installation:"
  402. echo " 1. Log into https://dashboard.license.netmaker.io"
  403. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  404. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  405. echo " 4. note email address"
  406. echo "-----------------------------------------------------"
  407. unset LICENSE_KEY
  408. while [ -z "$LICENSE_KEY" ]; do
  409. read -p "License Key: " LICENSE_KEY
  410. done
  411. unset ACCOUNT_ID
  412. while [ -z ${ACCOUNT_ID} ]; do
  413. read -p "Account ID: " ACCOUNT_ID
  414. done
  415. fi
  416. unset GET_EMAIL
  417. unset RAND_EMAIL
  418. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  419. if [ -z $AUTO_BUILD ]; then
  420. read -p "Email Address for Domain Registration (click 'enter' to use $RAND_EMAIL): " GET_EMAIL
  421. fi
  422. if [ -z "$GET_EMAIL" ]; then
  423. echo "using rand email"
  424. EMAIL="$RAND_EMAIL"
  425. else
  426. EMAIL="$GET_EMAIL"
  427. fi
  428. wait_seconds 1
  429. unset GET_MQ_USERNAME
  430. unset GET_MQ_PASSWORD
  431. unset CONFIRM_MQ_PASSWORD
  432. echo "Enter Credentials For MQ..."
  433. if [ -z $AUTO_BUILD ]; then
  434. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  435. fi
  436. if [ -z "$GET_MQ_USERNAME" ]; then
  437. echo "using default username for mq"
  438. MQ_USERNAME="netmaker"
  439. else
  440. MQ_USERNAME="$GET_MQ_USERNAME"
  441. fi
  442. MQ_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  443. if [ -z $AUTO_BUILD ]; then
  444. select domain_option in "Auto Generated Password" "Input Your Own Password"; do
  445. case $REPLY in
  446. 1)
  447. echo "using random password for mq"
  448. break
  449. ;;
  450. 2)
  451. while true
  452. do
  453. echo "Enter your Password For MQ: "
  454. read -s GET_MQ_PASSWORD
  455. echo "Enter your password again to confirm: "
  456. read -s CONFIRM_MQ_PASSWORD
  457. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  458. echo "wrong password entered, try again..."
  459. continue
  460. fi
  461. MQ_PASSWORD="$GET_MQ_PASSWORD"
  462. echo "MQ Password Saved Successfully!!"
  463. break
  464. done
  465. break
  466. ;;
  467. *) echo "invalid option $REPLY";;
  468. esac
  469. done
  470. fi
  471. wait_seconds 2
  472. echo "-----------------------------------------------------------------"
  473. echo " SETUP ARGUMENTS"
  474. echo "-----------------------------------------------------------------"
  475. echo " domain: $NETMAKER_BASE_DOMAIN"
  476. echo " email: $EMAIL"
  477. echo " public ip: $SERVER_PUBLIC_IP"
  478. if [ "$INSTALL_TYPE" = "ee" ]; then
  479. echo " license: $LICENSE_KEY"
  480. echo " account id: $ACCOUNT_ID"
  481. fi
  482. echo "-----------------------------------------------------------------"
  483. echo "Confirm Settings for Installation"
  484. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  485. confirm
  486. }
  487. # install_netmaker - sets the config files and starts docker-compose
  488. install_netmaker() {
  489. echo "-----------------------------------------------------------------"
  490. echo "Beginning installation..."
  491. echo "-----------------------------------------------------------------"
  492. wait_seconds 3
  493. echo "Pulling config files..."
  494. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.yml"
  495. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile"
  496. if [ "$INSTALL_TYPE" = "ee" ]; then
  497. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.ee.yml"
  498. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile-EE"
  499. fi
  500. if [ ! "$BUILD_TYPE" = "local" ]; then
  501. 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
  502. wget -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/wait.sh
  503. fi
  504. chmod +x /root/wait.sh
  505. mkdir -p /etc/netmaker
  506. echo "Setting docker-compose and Caddyfile..."
  507. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  508. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  509. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  510. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  511. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  512. sed -i "s/REPLACE_MQ_PASSWORD/$MQ_PASSWORD/g" /root/docker-compose.yml
  513. sed -i "s/REPLACE_MQ_USERNAME/$MQ_USERNAME/g" /root/docker-compose.yml
  514. if [ "$INSTALL_TYPE" = "ee" ]; then
  515. sed -i "s~YOUR_LICENSE_KEY~$LICENSE_KEY~g" /root/docker-compose.yml
  516. sed -i "s/YOUR_ACCOUNT_ID/$ACCOUNT_ID/g" /root/docker-compose.yml
  517. fi
  518. if [ "$BUILD_TYPE" = "version" ] && [ "$INSTALL_TYPE" = "ee" ]; then
  519. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG-ee/g" /root/docker-compose.yml
  520. else
  521. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  522. fi
  523. if [ "$BUILD_TYPE" = "local" ]; then
  524. sed -i "s/REPLACE_UI_IMAGE_TAG/$LATEST/g" /root/docker-compose.yml
  525. else
  526. sed -i "s/REPLACE_UI_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  527. fi
  528. echo "Starting containers..."
  529. docker-compose -f /root/docker-compose.yml up -d
  530. wait_seconds 2
  531. }
  532. # test_connection - tests to make sure Caddy has proper SSL certs
  533. test_connection() {
  534. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  535. for i in 1 2 3 4 5 6 7 8
  536. do
  537. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  538. if [[ "$i" == 8 ]]; then
  539. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  540. echo " Exiting..."
  541. exit 1
  542. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  543. echo " Certificates not yet configured, retrying..."
  544. elif [[ "$curlresponse" == *"left intact"* ]]; then
  545. echo " Certificates ok"
  546. break
  547. else
  548. secs=$(($i*5+10))
  549. echo " Issue establishing connection...retrying in $secs seconds..."
  550. fi
  551. sleep $secs
  552. done
  553. }
  554. # setup_mesh - sets up a default mesh network on the server
  555. setup_mesh() {
  556. wait_seconds 5
  557. echo "Creating netmaker network (10.101.0.0/16)"
  558. nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16
  559. wait_seconds 5
  560. echo "Creating netmaker enrollment key"
  561. tokenJson=$(nmctl enrollment_key create --unlimited --networks netmaker)
  562. TOKEN=$(jq -r '.token' <<< ${tokenJson})
  563. wait_seconds 3
  564. }
  565. # print_success - prints a success message upon completion
  566. print_success() {
  567. echo "-----------------------------------------------------------------"
  568. echo "-----------------------------------------------------------------"
  569. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  570. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  571. echo "-----------------------------------------------------------------"
  572. echo "-----------------------------------------------------------------"
  573. }
  574. # 1. print netmaker logo
  575. print_logo
  576. # 2. setup the build instructions
  577. set_buildinfo
  578. set +e
  579. # 3. install necessary packages
  580. install_dependencies
  581. # 4. install yq if necessary
  582. install_yq
  583. # 5. if running a local build, clone git and build artifacts
  584. if [ "$BUILD_TYPE" = "local" ]; then
  585. local_install_setup
  586. fi
  587. set -e
  588. # 6. get user input for variables
  589. set_install_vars
  590. # 7. get and set config files, startup docker-compose
  591. install_netmaker
  592. set +e
  593. # 8. make sure Caddy certs are working
  594. test_connection
  595. # 9. install the netmaker CLI
  596. setup_nmctl
  597. # 10. create a default mesh network for netmaker
  598. setup_mesh
  599. set -e
  600. # 11. add netclient to docker-compose and start it up
  601. setup_netclient
  602. # 12. make the netclient a default host and ingress gw
  603. configure_netclient
  604. # 13. print success message
  605. print_success
  606. # cp -f /etc/skel/.bashrc /root/.bashrc