nm-quick-interactive.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #!/bin/bash
  2. cat << "EOF"
  3. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6. __ __ ______ ______ __ __ ______ __ __ ______ ______
  7. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  8. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  9. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  10. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  11. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  12. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14. EOF
  15. if [ $(id -u) -ne 0 ]; then
  16. echo "This script must be run as root"
  17. exit 1
  18. fi
  19. if [ -z "$1" ]; then
  20. echo "-----------------------------------------------------"
  21. echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (EE)?"
  22. echo "EE will require you to create an account at https://dashboard.license.netmaker.io"
  23. echo "-----------------------------------------------------"
  24. select install_option in "Community Edition" "Enterprise Edition"; do
  25. case $REPLY in
  26. 1)
  27. echo "installing Netmaker CE"
  28. INSTALL_TYPE="ce"
  29. break
  30. ;;
  31. 2)
  32. echo "installing Netmaker EE"
  33. INSTALL_TYPE="ee"
  34. break
  35. ;;
  36. *) echo "invalid option $REPLY";;
  37. esac
  38. done
  39. elif [ "$1" = "ce" ]; then
  40. echo "installing Netmaker CE"
  41. INSTALL_TYPE="ce"
  42. elif [ "$1" = "ee" ]; then
  43. echo "installing Netmaker EE"
  44. INSTALL_TYPE="ee"
  45. else
  46. echo "install type invalid (options: 'ce, ee')"
  47. exit 1
  48. fi
  49. wait_seconds() {(
  50. for ((a=1; a <= $1; a++))
  51. do
  52. echo ". . ."
  53. sleep 1
  54. done
  55. )}
  56. confirm() {(
  57. while true; do
  58. read -p 'Does everything look right? [y/n]: ' yn
  59. case $yn in
  60. [Yy]* ) override="true"; break;;
  61. [Nn]* ) echo "exiting..."; exit 1;;
  62. * ) echo "Please answer yes or no.";;
  63. esac
  64. done
  65. )}
  66. echo "checking dependencies..."
  67. OS=$(uname)
  68. if [ -f /etc/debian_version ]; then
  69. dependencies="wireguard wireguard-tools jq docker.io docker-compose"
  70. update_cmd='apt update'
  71. install_cmd='apt-get install -y'
  72. elif [ -f /etc/alpine-release ]; then
  73. dependencies="wireguard jq docker.io docker-compose"
  74. update_cmd='apk update'
  75. install_cmd='apk --update add'
  76. elif [ -f /etc/centos-release ]; then
  77. dependencies="wireguard jq docker.io docker-compose"
  78. update_cmd='yum update'
  79. install_cmd='yum install -y'
  80. elif [ -f /etc/fedora-release ]; then
  81. dependencies="wireguard jq docker.io docker-compose"
  82. update_cmd='dnf update'
  83. install_cmd='dnf install -y'
  84. elif [ -f /etc/redhat-release ]; then
  85. dependencies="wireguard jq docker.io docker-compose"
  86. update_cmd='yum update'
  87. install_cmd='yum install -y'
  88. elif [ -f /etc/arch-release ]; then
  89. dependecies="wireguard-tools jq docker.io docker-compose"
  90. update_cmd='pacman -Sy'
  91. install_cmd='pacman -S --noconfirm'
  92. elif [ "${OS}" = "FreeBSD" ]; then
  93. dependencies="wireguard wget jq docker.io docker-compose"
  94. update_cmd='pkg update'
  95. install_cmd='pkg install -y'
  96. elif [ -f /etc/turris-version ]; then
  97. dependencies="wireguard-tools bash jq docker.io docker-compose"
  98. OS="TurrisOS"
  99. update_cmd='opkg update'
  100. install_cmd='opkg install'
  101. elif [ -f /etc/openwrt_release ]; then
  102. dependencies="wireguard-tools bash jq docker.io docker-compose"
  103. OS="OpenWRT"
  104. update_cmd='opkg update'
  105. install_cmd='opkg install'
  106. else
  107. install_cmd=''
  108. fi
  109. if [ -z "${install_cmd}" ]; then
  110. echo "OS unsupported for automatic dependency install"
  111. exit 1
  112. fi
  113. set -- $dependencies
  114. ${update_cmd}
  115. while [ -n "$1" ]; do
  116. if [ "${OS}" = "FreeBSD" ]; then
  117. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  118. if [ "$is_installed" != "" ]; then
  119. echo " " $1 is installed
  120. else
  121. echo " " $1 is not installed. Attempting install.
  122. ${install_cmd} $1
  123. sleep 5
  124. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  125. if [ "$is_installed" != "" ]; then
  126. echo " " $1 is installed
  127. elif [ -x "$(command -v $1)" ]; then
  128. echo " " $1 is installed
  129. else
  130. echo " " FAILED TO INSTALL $1
  131. echo " " This may break functionality.
  132. fi
  133. fi
  134. else
  135. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  136. is_installed=$(opkg list-installed $1 | grep $1)
  137. else
  138. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  139. fi
  140. if [ "${is_installed}" != "" ]; then
  141. echo " " $1 is installed
  142. else
  143. echo " " $1 is not installed. Attempting install.
  144. ${install_cmd} $1
  145. sleep 5
  146. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  147. is_installed=$(opkg list-installed $1 | grep $1)
  148. else
  149. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  150. fi
  151. if [ "${is_installed}" != "" ]; then
  152. echo " " $1 is installed
  153. elif [ -x "$(command -v $1)" ]; then
  154. echo " " $1 is installed
  155. else
  156. echo " " FAILED TO INSTALL $1
  157. echo " " This may break functionality.
  158. fi
  159. fi
  160. fi
  161. shift
  162. done
  163. echo "-----------------------------------------------------"
  164. echo "dependency check complete"
  165. echo "-----------------------------------------------------"
  166. wait_seconds 3
  167. set -e
  168. NETMAKER_BASE_DOMAIN=nm.$(curl -s ifconfig.me | tr . -).nip.io
  169. COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  170. SERVER_PUBLIC_IP=$(curl -s ifconfig.me)
  171. MASTER_KEY=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  172. MQ_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  173. DOMAIN_TYPE=""
  174. echo "-----------------------------------------------------"
  175. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  176. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_PUBLIC_IP"
  177. echo "-----------------------------------------------------"
  178. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  179. case $REPLY in
  180. 1)
  181. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  182. DOMAIN_TYPE="auto"
  183. break
  184. ;;
  185. 2)
  186. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_PUBLIC_IP first): " domain
  187. NETMAKER_BASE_DOMAIN=$domain
  188. echo "using $NETMAKER_BASE_DOMAIN"
  189. DOMAIN_TYPE="custom"
  190. break
  191. ;;
  192. *) echo "invalid option $REPLY";;
  193. esac
  194. done
  195. wait_seconds 2
  196. echo "-----------------------------------------------------"
  197. echo "The following subdomains will be used:"
  198. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  199. echo " api.$NETMAKER_BASE_DOMAIN"
  200. echo " broker.$NETMAKER_BASE_DOMAIN"
  201. if [ "$INSTALL_TYPE" = "ee" ]; then
  202. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  203. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  204. echo " grafana.$NETMAKER_BASE_DOMAIN"
  205. fi
  206. echo "-----------------------------------------------------"
  207. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  208. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_PUBLIC_IP"
  209. confirm
  210. fi
  211. wait_seconds 1
  212. if [ "$INSTALL_TYPE" = "ee" ]; then
  213. echo "-----------------------------------------------------"
  214. echo "Provide Details for EE installation:"
  215. echo " 1. Log into https://dashboard.license.netmaker.io"
  216. echo " 2. Copy License Key Value: https://dashboard.license.netmaker.io/license-keys"
  217. echo " 3. Retrieve Account ID: https://dashboard.license.netmaker.io/user"
  218. echo " 4. note email address"
  219. echo "-----------------------------------------------------"
  220. unset LICENSE_KEY
  221. while [ -z "$LICENSE_KEY" ]; do
  222. read -p "License Key: " LICENSE_KEY
  223. done
  224. unset ACCOUNT_ID
  225. while [ -z ${ACCOUNT_ID} ]; do
  226. read -p "Account ID: " ACCOUNT_ID
  227. done
  228. fi
  229. unset GET_EMAIL
  230. unset RAND_EMAIL
  231. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  232. read -p "Email Address for Domain Registration (click 'enter' to use $RAND_EMAIL): " GET_EMAIL
  233. if [ -z "$GET_EMAIL" ]; then
  234. echo "using rand email"
  235. EMAIL="$RAND_EMAIL"
  236. else
  237. EMAIL="$GET_EMAIL"
  238. fi
  239. wait_seconds 2
  240. echo "-----------------------------------------------------------------"
  241. echo " SETUP ARGUMENTS"
  242. echo "-----------------------------------------------------------------"
  243. echo " domain: $NETMAKER_BASE_DOMAIN"
  244. echo " email: $EMAIL"
  245. echo " public ip: $SERVER_PUBLIC_IP"
  246. if [ "$INSTALL_TYPE" = "ee" ]; then
  247. echo " license: $LICENSE_KEY"
  248. echo " account id: $ACCOUNT_ID"
  249. fi
  250. echo "-----------------------------------------------------------------"
  251. echo "Confirm Settings for Installation"
  252. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  253. confirm
  254. echo "-----------------------------------------------------------------"
  255. echo "Beginning installation..."
  256. echo "-----------------------------------------------------------------"
  257. wait_seconds 3
  258. echo "Pulling config files..."
  259. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/master/compose/docker-compose.yml"
  260. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/master/docker/Caddyfile"
  261. if [ "$INSTALL_TYPE" = "ee" ]; then
  262. COMPOSE_URL="https://raw.githubusercontent.com/gravitl/netmaker/master/compose/docker-compose.ee.yml"
  263. CADDY_URL="https://raw.githubusercontent.com/gravitl/netmaker/master/docker/Caddyfile-EE"
  264. fi
  265. wget -O /root/docker-compose.yml $COMPOSE_URL && wget -O /root/mosquitto.conf https://raw.githubusercontent.com/gravitl/netmaker/master/docker/mosquitto.conf && wget -O /root/Caddyfile $CADDY_URL && wget -q -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/master/docker/wait.sh && chmod +x /root/wait.sh
  266. mkdir -p /etc/netmaker
  267. echo "Setting docker-compose and Caddyfile..."
  268. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  269. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  270. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  271. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  272. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  273. sed -i "s/REPLACE_MQ_ADMIN_PASSWORD/$MQ_PASSWORD/g" /root/docker-compose.yml
  274. if [ "$INSTALL_TYPE" = "ee" ]; then
  275. sed -i "s~YOUR_LICENSE_KEY~$LICENSE_KEY~g" /root/docker-compose.yml
  276. sed -i "s/YOUR_ACCOUNT_ID/$ACCOUNT_ID/g" /root/docker-compose.yml
  277. fi
  278. echo "Starting containers..."
  279. docker-compose -f /root/docker-compose.yml up -d
  280. sleep 2
  281. test_connection() {
  282. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  283. for i in 1 2 3 4 5 6
  284. do
  285. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  286. if [[ "$i" == 6 ]]; then
  287. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  288. echo " Exiting..."
  289. exit 1
  290. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  291. echo " Certificates not yet configured, retrying..."
  292. elif [[ "$curlresponse" == *"left intact"* ]]; then
  293. echo " Certificates ok"
  294. break
  295. else
  296. secs=$(($i*5+10))
  297. echo " Issue establishing connection...retrying in $secs seconds..."
  298. fi
  299. sleep $secs
  300. done
  301. }
  302. setup_mesh() {( set -e
  303. wait_seconds 5
  304. echo "Creating netmaker network (10.101.0.0/16)"
  305. 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
  306. wait_seconds 5
  307. echo "Creating netmaker access key"
  308. 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)
  309. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  310. wait_seconds 3
  311. echo "Configuring netmaker server as ingress gateway"
  312. for i in 1 2 3 4 5 6
  313. do
  314. echo " waiting for server node to become available"
  315. wait_seconds 5
  316. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker)
  317. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  318. if [[ "$i" == 6 && -z "$SERVER_ID" ]]; then
  319. echo " Netmaker is having issues configuring itself, please investigate (docker logs netmaker)"
  320. echo " Exiting..."
  321. exit 1
  322. elif [ -z "$SERVER_ID" ]; then
  323. echo " server node not yet configured, retrying..."
  324. else
  325. echo " server node is now availble, continuing"
  326. break
  327. fi
  328. done
  329. curl -o /dev/null -s -X POST -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker/$SERVER_ID/createingress
  330. )}
  331. set +e
  332. test_connection
  333. wait_seconds 3
  334. setup_mesh
  335. echo "-----------------------------------------------------------------"
  336. echo "-----------------------------------------------------------------"
  337. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  338. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  339. echo "-----------------------------------------------------------------"
  340. echo "-----------------------------------------------------------------"
  341. # cp -f /etc/skel/.bashrc /root/.bashrc