nm-quick.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #!/bin/bash
  2. LATEST="v0.18.4"
  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 v0.18.4"
  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. # yq ".services.netclient += {\"container_name\": \"netclient\"}" -i /root/docker-compose.yml
  158. # yq ".services.netclient += {\"image\": \"gravitl/netclient:$IMAGE_TAG\"}" -i /root/docker-compose.yml
  159. # yq ".services.netclient += {\"hostname\": \"netmaker-1\"}" -i /root/docker-compose.yml
  160. # yq ".services.netclient += {\"network_mode\": \"host\"}" -i /root/docker-compose.yml
  161. # yq ".services.netclient.depends_on += [\"netmaker\"]" -i /root/docker-compose.yml
  162. # yq ".services.netclient += {\"restart\": \"always\"}" -i /root/docker-compose.yml
  163. # yq ".services.netclient.environment += {\"TOKEN\": \"$TOKEN\"}" -i /root/docker-compose.yml
  164. # yq ".services.netclient.volumes += [\"/etc/netclient:/etc/netclient\"]" -i /root/docker-compose.yml
  165. # yq ".services.netclient.cap_add += [\"NET_ADMIN\"]" -i /root/docker-compose.yml
  166. # yq ".services.netclient.cap_add += [\"NET_RAW\"]" -i /root/docker-compose.yml
  167. # yq ".services.netclient.cap_add += [\"SYS_MODULE\"]" -i /root/docker-compose.yml
  168. # docker-compose up -d
  169. set +e
  170. netclient uninstall
  171. set -e
  172. wget -O netclient https://github.com/gravitl/netclient/releases/download/$LATEST/netclient_linux_amd64
  173. chmod +x netclient
  174. ./netclient install
  175. netclient join -t $TOKEN
  176. echo "waiting for client to become available"
  177. wait_seconds 10
  178. }
  179. # configure_netclient - configures server's netclient as a default host and an ingress gateway
  180. configure_netclient() {
  181. NODE_ID=$(sudo cat /etc/netclient/nodes.yml | yq -r .netmaker.commonnode.id)
  182. echo "join complete. New node ID: $NODE_ID"
  183. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  184. echo "For first join, making host a default"
  185. echo "Host ID: $HOST_ID"
  186. # set as a default host
  187. set +e
  188. nmctl host update $HOST_ID --default
  189. sleep 5
  190. nmctl node create_ingress netmaker $NODE_ID
  191. set -e
  192. }
  193. # setup_nmctl - pulls nmctl and makes it executable
  194. setup_nmctl() {
  195. # DEV_TEMP - Temporary instructions for testing
  196. wget -O /usr/bin/nmctl https://fileserver.netmaker.org/testing/nmctl
  197. # RELEASE_REPLACE - Use this once release is ready
  198. # wget https://github.com/gravitl/netmaker/releases/download/v0.17.1/nmctl
  199. chmod +x /usr/bin/nmctl
  200. echo "using server api.$NETMAKER_BASE_DOMAIN"
  201. echo "using master key $MASTER_KEY"
  202. nmctl context set default --endpoint="https://api.$NETMAKER_BASE_DOMAIN" --master_key="$MASTER_KEY"
  203. nmctl context use default
  204. RESP=$(nmctl network list)
  205. if [[ $RESP == *"unauthorized"* ]]; then
  206. echo "Unable to properly configure NMCTL, exiting..."
  207. exit 1
  208. fi
  209. }
  210. # wait_seconds - wait for the specified period of time
  211. wait_seconds() {(
  212. for ((a=1; a <= $1; a++))
  213. do
  214. echo ". . ."
  215. sleep 1
  216. done
  217. )}
  218. # confirm - get user input to confirm that they want to perform the next step
  219. confirm() {(
  220. if [ "$AUTO_BUILD" = "on" ]; then
  221. return 0
  222. fi
  223. while true; do
  224. read -p 'Does everything look right? [y/n]: ' yn
  225. case $yn in
  226. [Yy]* ) override="true"; break;;
  227. [Nn]* ) echo "exiting..."; exit 1;;
  228. * ) echo "Please answer yes or no.";;
  229. esac
  230. done
  231. )}
  232. # local_install_setup - builds artifacts based on specified branch locally to use in install
  233. local_install_setup() {(
  234. rm -rf netmaker-tmp
  235. mkdir netmaker-tmp
  236. cd netmaker-tmp
  237. git clone https://www.github.com/gravitl/netmaker
  238. cd netmaker
  239. git checkout $BUILD_TAG
  240. git pull origin $BUILD_TAG
  241. docker build --no-cache --build-arg version=$IMAGE_TAG -t gravitl/netmaker:$IMAGE_TAG .
  242. if [ "$INSTALL_TYPE" = "ee" ]; then
  243. cp compose/docker-compose.ee.yml /root/docker-compose.yml
  244. cp docker/Caddyfile-EE /root/Caddyfile
  245. else
  246. cp compose/docker-compose.yml /root/docker-compose.yml
  247. cp docker/Caddyfile /root/Caddyfile
  248. fi
  249. cp docker/mosquitto.conf /root/mosquitto.conf
  250. cp docker/wait.sh /root/wait.sh
  251. cd ../../
  252. rm -rf netmaker-tmp
  253. )}
  254. # install_dependencies - install necessary packages to run netmaker
  255. install_dependencies() {
  256. echo "checking dependencies..."
  257. OS=$(uname)
  258. if [ -f /etc/debian_version ]; then
  259. dependencies="git wireguard wireguard-tools jq docker.io docker-compose"
  260. update_cmd='apt update'
  261. install_cmd='apt-get install -y'
  262. elif [ -f /etc/alpine-release ]; then
  263. dependencies="git wireguard jq docker.io docker-compose"
  264. update_cmd='apk update'
  265. install_cmd='apk --update add'
  266. elif [ -f /etc/centos-release ]; then
  267. dependencies="git wireguard jq docker.io docker-compose"
  268. update_cmd='yum update'
  269. install_cmd='yum install -y'
  270. elif [ -f /etc/fedora-release ]; then
  271. dependencies="git wireguard jq docker.io docker-compose"
  272. update_cmd='dnf update'
  273. install_cmd='dnf install -y'
  274. elif [ -f /etc/redhat-release ]; then
  275. dependencies="git wireguard jq docker.io docker-compose"
  276. update_cmd='yum update'
  277. install_cmd='yum install -y'
  278. elif [ -f /etc/arch-release ]; then
  279. dependecies="git wireguard-tools jq docker.io docker-compose"
  280. update_cmd='pacman -Sy'
  281. install_cmd='pacman -S --noconfirm'
  282. elif [ "${OS}" = "FreeBSD" ]; then
  283. dependencies="git wireguard wget jq docker.io docker-compose"
  284. update_cmd='pkg update'
  285. install_cmd='pkg install -y'
  286. elif [ -f /etc/turris-version ]; then
  287. dependencies="git wireguard-tools bash jq docker.io docker-compose"
  288. OS="TurrisOS"
  289. update_cmd='opkg update'
  290. install_cmd='opkg install'
  291. elif [ -f /etc/openwrt_release ]; then
  292. dependencies="git wireguard-tools bash jq docker.io docker-compose"
  293. OS="OpenWRT"
  294. update_cmd='opkg update'
  295. install_cmd='opkg install'
  296. else
  297. install_cmd=''
  298. fi
  299. if [ -z "${install_cmd}" ]; then
  300. echo "OS unsupported for automatic dependency install"
  301. exit 1
  302. fi
  303. set -- $dependencies
  304. ${update_cmd}
  305. while [ -n "$1" ]; do
  306. if [ "${OS}" = "FreeBSD" ]; then
  307. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  308. if [ "$is_installed" != "" ]; then
  309. echo " " $1 is installed
  310. else
  311. echo " " $1 is not installed. Attempting install.
  312. ${install_cmd} $1
  313. sleep 5
  314. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  315. if [ "$is_installed" != "" ]; then
  316. echo " " $1 is installed
  317. elif [ -x "$(command -v $1)" ]; then
  318. echo " " $1 is installed
  319. else
  320. echo " " FAILED TO INSTALL $1
  321. echo " " This may break functionality.
  322. fi
  323. fi
  324. else
  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. else
  333. echo " " $1 is not installed. Attempting install.
  334. ${install_cmd} $1
  335. sleep 5
  336. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  337. is_installed=$(opkg list-installed $1 | grep $1)
  338. else
  339. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  340. fi
  341. if [ "${is_installed}" != "" ]; then
  342. echo " " $1 is installed
  343. elif [ -x "$(command -v $1)" ]; then
  344. echo " " $1 is installed
  345. else
  346. echo " " FAILED TO INSTALL $1
  347. echo " " This may break functionality.
  348. fi
  349. fi
  350. fi
  351. shift
  352. done
  353. echo "-----------------------------------------------------"
  354. echo "dependency check complete"
  355. echo "-----------------------------------------------------"
  356. }
  357. set -e
  358. # set_install_vars - sets the variables that will be used throughout installation
  359. set_install_vars() {
  360. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  361. if [ "$IP_ADDR" = "" ]; then
  362. IP_ADDR=$(curl -s ifconfig.me)
  363. fi
  364. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  365. COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  366. SERVER_PUBLIC_IP=$IP_ADDR
  367. MASTER_KEY=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  368. DOMAIN_TYPE=""
  369. echo "-----------------------------------------------------"
  370. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  371. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_PUBLIC_IP"
  372. echo "-----------------------------------------------------"
  373. if [ "$AUTO_BUILD" = "on" ]; then
  374. DOMAIN_TYPE="auto"
  375. else
  376. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  377. case $REPLY in
  378. 1)
  379. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  380. DOMAIN_TYPE="auto"
  381. break
  382. ;;
  383. 2)
  384. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_PUBLIC_IP first): " domain
  385. NETMAKER_BASE_DOMAIN=$domain
  386. echo "using $NETMAKER_BASE_DOMAIN"
  387. DOMAIN_TYPE="custom"
  388. break
  389. ;;
  390. *) echo "invalid option $REPLY";;
  391. esac
  392. done
  393. fi
  394. wait_seconds 2
  395. echo "-----------------------------------------------------"
  396. echo "The following subdomains will be used:"
  397. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  398. echo " api.$NETMAKER_BASE_DOMAIN"
  399. echo " broker.$NETMAKER_BASE_DOMAIN"
  400. if [ "$INSTALL_TYPE" = "ee" ]; then
  401. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  402. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  403. echo " grafana.$NETMAKER_BASE_DOMAIN"
  404. fi
  405. echo "-----------------------------------------------------"
  406. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  407. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_PUBLIC_IP"
  408. confirm
  409. fi
  410. wait_seconds 1
  411. if [ "$INSTALL_TYPE" = "ee" ]; then
  412. echo "-----------------------------------------------------"
  413. echo "Provide Details for EE installation:"
  414. echo " 1. Log into https://dashboard.license.netmaker.io"
  415. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  416. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  417. echo " 4. note email address"
  418. echo "-----------------------------------------------------"
  419. unset LICENSE_KEY
  420. while [ -z "$LICENSE_KEY" ]; do
  421. read -p "License Key: " LICENSE_KEY
  422. done
  423. unset ACCOUNT_ID
  424. while [ -z ${ACCOUNT_ID} ]; do
  425. read -p "Account ID: " ACCOUNT_ID
  426. done
  427. fi
  428. unset GET_EMAIL
  429. unset RAND_EMAIL
  430. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  431. if [ -z $AUTO_BUILD ]; then
  432. read -p "Email Address for Domain Registration (click 'enter' to use $RAND_EMAIL): " GET_EMAIL
  433. fi
  434. if [ -z "$GET_EMAIL" ]; then
  435. echo "using rand email"
  436. EMAIL="$RAND_EMAIL"
  437. else
  438. EMAIL="$GET_EMAIL"
  439. fi
  440. wait_seconds 1
  441. unset GET_MQ_USERNAME
  442. unset GET_MQ_PASSWORD
  443. unset CONFIRM_MQ_PASSWORD
  444. echo "Enter Credentials For MQ..."
  445. if [ -z $AUTO_BUILD ]; then
  446. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  447. fi
  448. if [ -z "$GET_MQ_USERNAME" ]; then
  449. echo "using default username for mq"
  450. MQ_USERNAME="netmaker"
  451. else
  452. MQ_USERNAME="$GET_MQ_USERNAME"
  453. fi
  454. MQ_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  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
  464. do
  465. echo "Enter your Password For MQ: "
  466. read -s GET_MQ_PASSWORD
  467. echo "Enter your password again to confirm: "
  468. read -s CONFIRM_MQ_PASSWORD
  469. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  470. echo "wrong password entered, try again..."
  471. continue
  472. fi
  473. MQ_PASSWORD="$GET_MQ_PASSWORD"
  474. echo "MQ Password Saved Successfully!!"
  475. break
  476. done
  477. break
  478. ;;
  479. *) echo "invalid option $REPLY";;
  480. esac
  481. done
  482. fi
  483. wait_seconds 2
  484. echo "-----------------------------------------------------------------"
  485. echo " SETUP ARGUMENTS"
  486. echo "-----------------------------------------------------------------"
  487. echo " domain: $NETMAKER_BASE_DOMAIN"
  488. echo " email: $EMAIL"
  489. echo " public ip: $SERVER_PUBLIC_IP"
  490. if [ "$INSTALL_TYPE" = "ee" ]; then
  491. echo " license: $LICENSE_KEY"
  492. echo " account id: $ACCOUNT_ID"
  493. fi
  494. echo "-----------------------------------------------------------------"
  495. echo "Confirm Settings for Installation"
  496. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  497. confirm
  498. }
  499. # install_netmaker - sets the config files and starts docker-compose
  500. install_netmaker() {
  501. echo "-----------------------------------------------------------------"
  502. echo "Beginning installation..."
  503. echo "-----------------------------------------------------------------"
  504. wait_seconds 3
  505. echo "Pulling config files..."
  506. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.yml"
  507. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile"
  508. if [ "$INSTALL_TYPE" = "ee" ]; then
  509. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.ee.yml"
  510. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile-EE"
  511. fi
  512. if [ ! "$BUILD_TYPE" = "local" ]; then
  513. 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
  514. wget -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/wait.sh
  515. fi
  516. chmod +x /root/wait.sh
  517. mkdir -p /etc/netmaker
  518. echo "Setting docker-compose and Caddyfile..."
  519. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  520. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  521. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  522. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  523. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  524. sed -i "s/REPLACE_MQ_PASSWORD/$MQ_PASSWORD/g" /root/docker-compose.yml
  525. sed -i "s/REPLACE_MQ_USERNAME/$MQ_USERNAME/g" /root/docker-compose.yml
  526. if [ "$INSTALL_TYPE" = "ee" ]; then
  527. sed -i "s~YOUR_LICENSE_KEY~$LICENSE_KEY~g" /root/docker-compose.yml
  528. sed -i "s/YOUR_ACCOUNT_ID/$ACCOUNT_ID/g" /root/docker-compose.yml
  529. fi
  530. if [ "$BUILD_TYPE" = "version" ] && [ "$INSTALL_TYPE" = "ee" ]; then
  531. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG-ee/g" /root/docker-compose.yml
  532. else
  533. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  534. fi
  535. if [ "$BUILD_TYPE" = "local" ]; then
  536. sed -i "s/REPLACE_UI_IMAGE_TAG/$LATEST/g" /root/docker-compose.yml
  537. else
  538. sed -i "s/REPLACE_UI_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  539. fi
  540. echo "Starting containers..."
  541. docker-compose -f /root/docker-compose.yml up -d
  542. wait_seconds 2
  543. }
  544. # test_connection - tests to make sure Caddy has proper SSL certs
  545. test_connection() {
  546. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  547. for i in 1 2 3 4 5 6 7 8
  548. do
  549. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  550. if [[ "$i" == 8 ]]; then
  551. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  552. echo " Exiting..."
  553. exit 1
  554. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  555. echo " Certificates not yet configured, retrying..."
  556. elif [[ "$curlresponse" == *"left intact"* ]]; then
  557. echo " Certificates ok"
  558. break
  559. else
  560. secs=$(($i*5+10))
  561. echo " Issue establishing connection...retrying in $secs seconds..."
  562. fi
  563. sleep $secs
  564. done
  565. }
  566. # setup_mesh - sets up a default mesh network on the server
  567. setup_mesh() {
  568. wait_seconds 5
  569. echo "Creating netmaker network (10.101.0.0/16)"
  570. nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16
  571. wait_seconds 5
  572. echo "Creating netmaker access key"
  573. nmctl keys create test1 99999 --name netmaker-key
  574. tokenJson=$(nmctl keys create netmaker 2)
  575. TOKEN=$(jq -r '.accessstring' <<< ${tokenJson})
  576. wait_seconds 3
  577. }
  578. # print_success - prints a success message upon completion
  579. print_success() {
  580. echo "-----------------------------------------------------------------"
  581. echo "-----------------------------------------------------------------"
  582. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  583. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  584. echo "-----------------------------------------------------------------"
  585. echo "-----------------------------------------------------------------"
  586. }
  587. # 1. print netmaker logo
  588. print_logo
  589. # 2. setup the build instructions
  590. set_buildinfo
  591. set +e
  592. # 3. install necessary packages
  593. install_dependencies
  594. # 4. install yq if necessary
  595. install_yq
  596. # 5. if running a local build, clone git and build artifacts
  597. if [ "$BUILD_TYPE" = "local" ]; then
  598. local_install_setup
  599. fi
  600. set -e
  601. # 6. get user input for variables
  602. set_install_vars
  603. # 7. get and set config files, startup docker-compose
  604. install_netmaker
  605. set +e
  606. # 8. make sure Caddy certs are working
  607. test_connection
  608. # 9. install the netmaker CLI
  609. setup_nmctl
  610. # 10. create a default mesh network for netmaker
  611. setup_mesh
  612. set -e
  613. # 11. add netclient to docker-compose and start it up
  614. setup_netclient
  615. # 12. make the netclient a default host and ingress gw
  616. configure_netclient
  617. # 13. print success message
  618. print_success
  619. # cp -f /etc/skel/.bashrc /root/.bashrc