nm-quick.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. #!/bin/bash
  2. LATEST="v0.18.0"
  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. usage () {(
  27. echo "usage: ./nm-quick.sh [-e] [-b buildtype] [-t tag]"
  28. echo " -e if specified, will install netmaker EE"
  29. echo " -b type of build; options:"
  30. echo " \"version\" - will install a specific version of Netmaker using remote git and dockerhub"
  31. echo " \"local\": - will install by cloning repo and and building images from git"
  32. echo " \"branch\": - will install a specific branch using remote git and dockerhub "
  33. echo " -t tag of build; if buildtype=version, tag=version. If builtype=branch or builtype=local, tag=branch"
  34. echo "examples:"
  35. echo " nm-quick.sh -e -b version -t v0.18.0"
  36. echo " nm-quick.sh -e -b local -t feature_v0.17.2_newfeature"
  37. echo " nm-quick.sh -e -b branch -t develop"
  38. exit 1
  39. )}
  40. while getopts evb:t: flag
  41. do
  42. case "${flag}" in
  43. e)
  44. INSTALL_TYPE="ee"
  45. ;;
  46. v)
  47. usage
  48. exit 0
  49. ;;
  50. b)
  51. BUILD_TYPE=${OPTARG}
  52. if [[ ! "$BUILD_TYPE" =~ ^(version|local|branch)$ ]]; then
  53. echo "error: $BUILD_TYPE is invalid"
  54. echo "valid options: version, local, branch"
  55. usage
  56. exit 1
  57. fi
  58. ;;
  59. t)
  60. BUILD_TAG=${OPTARG}
  61. ;;
  62. esac
  63. done
  64. if [ -z "$BUILD_TYPE" ]; then
  65. BUILD_TYPE="version"
  66. BUILD_TAG=$LATEST
  67. fi
  68. if [ -z "$BUILD_TAG" ] && [ "$BUILD_TYPE" = "version" ]; then
  69. BUILD_TAG=$LATEST
  70. fi
  71. if [ -z "$BUILD_TAG" ] && [ ! -z "$BUILD_TYPE" ]; then
  72. echo "error: must specify build tag when build type \"$BUILD_TYPE\" is specified"
  73. usage
  74. exit 1
  75. fi
  76. IMAGE_TAG=$(sed 's/\//-/g' <<< "$BUILD_TAG")
  77. if [ "$1" = "ce" ]; then
  78. INSTALL_TYPE="ce"
  79. elif [ "$1" = "ee" ]; then
  80. INSTALL_TYPE="ee"
  81. fi
  82. if [ -z "$INSTALL_TYPE" ]; then
  83. echo "-----------------------------------------------------"
  84. echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (EE)?"
  85. echo "EE will require you to create an account at https://dashboard.license.netmaker.io"
  86. echo "-----------------------------------------------------"
  87. select install_option in "Community Edition" "Enterprise Edition"; do
  88. case $REPLY in
  89. 1)
  90. echo "installing Netmaker CE"
  91. INSTALL_TYPE="ce"
  92. break
  93. ;;
  94. 2)
  95. echo "installing Netmaker EE"
  96. INSTALL_TYPE="ee"
  97. break
  98. ;;
  99. *) echo "invalid option $REPLY";;
  100. esac
  101. done
  102. fi
  103. echo "-----------Build Options-----------------------------"
  104. echo " EE or CE: $INSTALL_TYPE";
  105. echo " Build Type: $BUILD_TYPE";
  106. echo " Build Tag: $BUILD_TAG";
  107. echo " Image Tag: $IMAGE_TAG";
  108. echo "-----------------------------------------------------"
  109. print_logo
  110. wait_seconds() {(
  111. for ((a=1; a <= $1; a++))
  112. do
  113. echo ". . ."
  114. sleep 1
  115. done
  116. )}
  117. confirm() {(
  118. while true; do
  119. read -p 'Does everything look right? [y/n]: ' yn
  120. case $yn in
  121. [Yy]* ) override="true"; break;;
  122. [Nn]* ) echo "exiting..."; exit 1;;
  123. * ) echo "Please answer yes or no.";;
  124. esac
  125. done
  126. )}
  127. local_install_setup() {(
  128. rm -rf netmaker-tmp
  129. mkdir netmaker-tmp
  130. cd netmaker-tmp
  131. git clone https://www.github.com/gravitl/netmaker
  132. cd netmaker
  133. git checkout $BUILD_TAG
  134. git pull origin $BUILD_TAG
  135. docker build --no-cache --build-arg version=$IMAGE_TAG -t gravitl/netmaker:$IMAGE_TAG .
  136. if [ "$INSTALL_TYPE" = "ee" ]; then
  137. cp compose/docker-compose.ee.yml /root/docker-compose.yml
  138. cp docker/Caddyfile-EE /root/Caddyfile
  139. else
  140. cp compose/docker-compose.yml /root/docker-compose.yml
  141. cp docker/Caddyfile /root/Caddyfile
  142. fi
  143. cp docker/mosquitto.conf /root/mosquitto.conf
  144. cp docker/wait.sh /root/wait.sh
  145. cd ../../
  146. rm -rf netmaker-tmp
  147. )}
  148. echo "checking dependencies..."
  149. OS=$(uname)
  150. if [ -f /etc/debian_version ]; then
  151. dependencies="git wireguard wireguard-tools jq docker.io docker-compose"
  152. update_cmd='apt update'
  153. install_cmd='apt-get install -y'
  154. elif [ -f /etc/alpine-release ]; then
  155. dependencies="git wireguard jq docker.io docker-compose"
  156. update_cmd='apk update'
  157. install_cmd='apk --update add'
  158. elif [ -f /etc/centos-release ]; then
  159. dependencies="git wireguard jq docker.io docker-compose"
  160. update_cmd='yum update'
  161. install_cmd='yum install -y'
  162. elif [ -f /etc/fedora-release ]; then
  163. dependencies="git wireguard jq docker.io docker-compose"
  164. update_cmd='dnf update'
  165. install_cmd='dnf install -y'
  166. elif [ -f /etc/redhat-release ]; then
  167. dependencies="git wireguard jq docker.io docker-compose"
  168. update_cmd='yum update'
  169. install_cmd='yum install -y'
  170. elif [ -f /etc/arch-release ]; then
  171. dependecies="git wireguard-tools jq docker.io docker-compose"
  172. update_cmd='pacman -Sy'
  173. install_cmd='pacman -S --noconfirm'
  174. elif [ "${OS}" = "FreeBSD" ]; then
  175. dependencies="git wireguard wget jq docker.io docker-compose"
  176. update_cmd='pkg update'
  177. install_cmd='pkg install -y'
  178. elif [ -f /etc/turris-version ]; then
  179. dependencies="git wireguard-tools bash jq docker.io docker-compose"
  180. OS="TurrisOS"
  181. update_cmd='opkg update'
  182. install_cmd='opkg install'
  183. elif [ -f /etc/openwrt_release ]; then
  184. dependencies="git wireguard-tools bash jq docker.io docker-compose"
  185. OS="OpenWRT"
  186. update_cmd='opkg update'
  187. install_cmd='opkg install'
  188. else
  189. install_cmd=''
  190. fi
  191. if [ -z "${install_cmd}" ]; then
  192. echo "OS unsupported for automatic dependency install"
  193. exit 1
  194. fi
  195. set -- $dependencies
  196. ${update_cmd}
  197. while [ -n "$1" ]; do
  198. if [ "${OS}" = "FreeBSD" ]; then
  199. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  200. if [ "$is_installed" != "" ]; then
  201. echo " " $1 is installed
  202. else
  203. echo " " $1 is not installed. Attempting install.
  204. ${install_cmd} $1
  205. sleep 5
  206. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  207. if [ "$is_installed" != "" ]; then
  208. echo " " $1 is installed
  209. elif [ -x "$(command -v $1)" ]; then
  210. echo " " $1 is installed
  211. else
  212. echo " " FAILED TO INSTALL $1
  213. echo " " This may break functionality.
  214. fi
  215. fi
  216. else
  217. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  218. is_installed=$(opkg list-installed $1 | grep $1)
  219. else
  220. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  221. fi
  222. if [ "${is_installed}" != "" ]; then
  223. echo " " $1 is installed
  224. else
  225. echo " " $1 is not installed. Attempting install.
  226. ${install_cmd} $1
  227. sleep 5
  228. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  229. is_installed=$(opkg list-installed $1 | grep $1)
  230. else
  231. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  232. fi
  233. if [ "${is_installed}" != "" ]; then
  234. echo " " $1 is installed
  235. elif [ -x "$(command -v $1)" ]; then
  236. echo " " $1 is installed
  237. else
  238. echo " " FAILED TO INSTALL $1
  239. echo " " This may break functionality.
  240. fi
  241. fi
  242. fi
  243. shift
  244. done
  245. echo "-----------------------------------------------------"
  246. echo "dependency check complete"
  247. echo "-----------------------------------------------------"
  248. wait_seconds 3
  249. if [ "$BUILD_TYPE" = "local" ]; then
  250. local_install_setup
  251. fi
  252. set -e
  253. NETMAKER_BASE_DOMAIN=nm.$(curl -s ifconfig.me | tr . -).nip.io
  254. COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  255. SERVER_PUBLIC_IP=$(curl -s ifconfig.me)
  256. MASTER_KEY=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  257. DOMAIN_TYPE=""
  258. echo "-----------------------------------------------------"
  259. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  260. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_PUBLIC_IP"
  261. echo "-----------------------------------------------------"
  262. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  263. case $REPLY in
  264. 1)
  265. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  266. DOMAIN_TYPE="auto"
  267. break
  268. ;;
  269. 2)
  270. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_PUBLIC_IP first): " domain
  271. NETMAKER_BASE_DOMAIN=$domain
  272. echo "using $NETMAKER_BASE_DOMAIN"
  273. DOMAIN_TYPE="custom"
  274. break
  275. ;;
  276. *) echo "invalid option $REPLY";;
  277. esac
  278. done
  279. wait_seconds 2
  280. echo "-----------------------------------------------------"
  281. echo "The following subdomains will be used:"
  282. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  283. echo " api.$NETMAKER_BASE_DOMAIN"
  284. echo " broker.$NETMAKER_BASE_DOMAIN"
  285. if [ "$INSTALL_TYPE" = "ee" ]; then
  286. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  287. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  288. echo " grafana.$NETMAKER_BASE_DOMAIN"
  289. fi
  290. echo "-----------------------------------------------------"
  291. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  292. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_PUBLIC_IP"
  293. confirm
  294. fi
  295. wait_seconds 1
  296. if [ "$INSTALL_TYPE" = "ee" ]; then
  297. echo "-----------------------------------------------------"
  298. echo "Provide Details for EE installation:"
  299. echo " 1. Log into https://dashboard.license.netmaker.io"
  300. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  301. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  302. echo " 4. note email address"
  303. echo "-----------------------------------------------------"
  304. unset LICENSE_KEY
  305. while [ -z "$LICENSE_KEY" ]; do
  306. read -p "License Key: " LICENSE_KEY
  307. done
  308. unset ACCOUNT_ID
  309. while [ -z ${ACCOUNT_ID} ]; do
  310. read -p "Account ID: " ACCOUNT_ID
  311. done
  312. fi
  313. unset GET_EMAIL
  314. unset RAND_EMAIL
  315. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  316. read -p "Email Address for Domain Registration (click 'enter' to use $RAND_EMAIL): " GET_EMAIL
  317. if [ -z "$GET_EMAIL" ]; then
  318. echo "using rand email"
  319. EMAIL="$RAND_EMAIL"
  320. else
  321. EMAIL="$GET_EMAIL"
  322. fi
  323. wait_seconds 1
  324. unset GET_MQ_USERNAME
  325. unset GET_MQ_PASSWORD
  326. unset CONFIRM_MQ_PASSWORD
  327. echo "Enter Credentials For MQ..."
  328. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  329. if [ -z "$GET_MQ_USERNAME" ]; then
  330. echo "using default username for mq"
  331. MQ_USERNAME="netmaker"
  332. else
  333. MQ_USERNAME="$GET_MQ_USERNAME"
  334. fi
  335. select domain_option in "Auto Generated Password" "Input Your Own Password"; do
  336. case $REPLY in
  337. 1)
  338. echo "generating random password for mq"
  339. MQ_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  340. break
  341. ;;
  342. 2)
  343. while true
  344. do
  345. echo "Enter your Password For MQ: "
  346. read -s GET_MQ_PASSWORD
  347. echo "Enter your password again to confirm: "
  348. read -s CONFIRM_MQ_PASSWORD
  349. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  350. echo "wrong password entered, try again..."
  351. continue
  352. fi
  353. MQ_PASSWORD="$GET_MQ_PASSWORD"
  354. echo "MQ Password Saved Successfully!!"
  355. break
  356. done
  357. break
  358. ;;
  359. *) echo "invalid option $REPLY";;
  360. esac
  361. done
  362. wait_seconds 2
  363. echo "-----------------------------------------------------------------"
  364. echo " SETUP ARGUMENTS"
  365. echo "-----------------------------------------------------------------"
  366. echo " domain: $NETMAKER_BASE_DOMAIN"
  367. echo " email: $EMAIL"
  368. echo " public ip: $SERVER_PUBLIC_IP"
  369. if [ "$INSTALL_TYPE" = "ee" ]; then
  370. echo " license: $LICENSE_KEY"
  371. echo " account id: $ACCOUNT_ID"
  372. fi
  373. echo "-----------------------------------------------------------------"
  374. echo "Confirm Settings for Installation"
  375. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  376. confirm
  377. echo "-----------------------------------------------------------------"
  378. echo "Beginning installation..."
  379. echo "-----------------------------------------------------------------"
  380. wait_seconds 3
  381. echo "Pulling config files..."
  382. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.yml"
  383. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile"
  384. if [ "$INSTALL_TYPE" = "ee" ]; then
  385. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/compose/docker-compose.ee.yml"
  386. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/Caddyfile-EE"
  387. fi
  388. if [ ! "$BUILD_TYPE" = "local" ]; then
  389. 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
  390. wget -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG/docker/wait.sh
  391. fi
  392. chmod +x /root/wait.sh
  393. mkdir -p /etc/netmaker
  394. echo "Setting docker-compose and Caddyfile..."
  395. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  396. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  397. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  398. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  399. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  400. sed -i "s/REPLACE_MQ_PASSWORD/$MQ_PASSWORD/g" /root/docker-compose.yml
  401. sed -i "s/REPLACE_MQ_USERNAME/$MQ_USERNAME/g" /root/docker-compose.yml
  402. if [ "$INSTALL_TYPE" = "ee" ]; then
  403. sed -i "s~YOUR_LICENSE_KEY~$LICENSE_KEY~g" /root/docker-compose.yml
  404. sed -i "s/YOUR_ACCOUNT_ID/$ACCOUNT_ID/g" /root/docker-compose.yml
  405. fi
  406. if [ "$BUILD_TYPE" = "version" ] && [ "$INSTALL_TYPE" = "ee" ]; then
  407. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG-ee/g" /root/docker-compose.yml
  408. else
  409. sed -i "s/REPLACE_SERVER_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  410. fi
  411. if [ "$BUILD_TYPE" = "local" ]; then
  412. sed -i "s/REPLACE_UI_IMAGE_TAG/$LATEST/g" /root/docker-compose.yml
  413. else
  414. sed -i "s/REPLACE_UI_IMAGE_TAG/$IMAGE_TAG/g" /root/docker-compose.yml
  415. fi
  416. echo "Starting containers..."
  417. docker-compose -f /root/docker-compose.yml up -d
  418. sleep 2
  419. test_connection() {
  420. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  421. for i in 1 2 3 4 5 6 7 8
  422. do
  423. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  424. if [[ "$i" == 8 ]]; then
  425. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  426. echo " Exiting..."
  427. exit 1
  428. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  429. echo " Certificates not yet configured, retrying..."
  430. elif [[ "$curlresponse" == *"left intact"* ]]; then
  431. echo " Certificates ok"
  432. break
  433. else
  434. secs=$(($i*5+10))
  435. echo " Issue establishing connection...retrying in $secs seconds..."
  436. fi
  437. sleep $secs
  438. done
  439. }
  440. setup_mesh() {( set -e
  441. wait_seconds 15
  442. echo "Creating netmaker network (10.101.0.0/16)"
  443. curl -s -o /dev/null -d '{"addressrange":"10.101.0.0/16","netid":"netmaker"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/networks
  444. wait_seconds 5
  445. echo "Creating netmaker access key"
  446. curlresponse=$(curl -s -d '{"uses":99999,"name":"netmaker-key"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/networks/netmaker/keys)
  447. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  448. wait_seconds 3
  449. )}
  450. set +e
  451. test_connection
  452. wait_seconds 3
  453. setup_mesh
  454. echo "-----------------------------------------------------------------"
  455. echo "-----------------------------------------------------------------"
  456. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  457. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  458. echo "-----------------------------------------------------------------"
  459. echo "-----------------------------------------------------------------"
  460. # cp -f /etc/skel/.bashrc /root/.bashrc