nm-quick.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #!/bin/bash
  2. echo "checking for root permissions..."
  3. echo "setting flags..."
  4. while getopts d:e:m:v:c: flag
  5. do
  6. case "${flag}" in
  7. d) domain=${OPTARG};;
  8. e) email=${OPTARG};;
  9. m) addmesh=${OPTARG};;
  10. v) addvpn=${OPTARG};;
  11. c) num_clients=${OPTARG};;
  12. esac
  13. done
  14. echo "checking for root permissions..."
  15. if [ $EUID -ne 0 ]; then
  16. echo "This script must be run as root"
  17. exit 1
  18. fi
  19. echo "checking dependencies..."
  20. declare -A osInfo;
  21. osInfo[/etc/debian_version]="apt-get install -y"u
  22. osInfo[/etc/alpine-release]="apk --update add"
  23. osInfo[/etc/centos-release]="yum install -y"
  24. osInfo[/etc/fedora-release]="dnf install -y"
  25. for f in ${!osInfo[@]}
  26. do
  27. if [[ -f $f ]];then
  28. install_cmd=${osInfo[$f]}
  29. fi
  30. done
  31. if [ -f /etc/debian_version ]; then
  32. apt update
  33. elif [ -f /etc/alpine-release ]; then
  34. apk update
  35. elif [ -f /etc/centos-release ]; then
  36. yum update
  37. elif [ -f /etc/fedora-release ]; then
  38. dnf update
  39. fi
  40. dependencies=( "docker.io" "docker-compose" "wireguard" "jq" )
  41. for dependency in ${dependencies[@]}; do
  42. is_installed=$(dpkg-query -W --showformat='${Status}\n' ${dependency} | grep "install ok installed")
  43. if [ "${is_installed}" == "install ok installed" ]; then
  44. echo " " ${dependency} is installed
  45. else
  46. echo " " ${dependency} is not installed. Attempting install.
  47. ${install_cmd} ${dependency}
  48. sleep 5
  49. is_installed=$(dpkg-query -W --showformat='${Status}\n' ${dependency} | grep "install ok installed")
  50. if [ "${is_installed}" == "install ok installed" ]; then
  51. echo " " ${dependency} is installed
  52. elif [ -x "$(command -v ${dependency})" ]; then
  53. echo " " ${dependency} is installed
  54. else
  55. echo " " failed to install ${dependency}. Exiting.
  56. exit 1
  57. fi
  58. fi
  59. done
  60. set -e
  61. NETMAKER_BASE_DOMAIN=nm.$(curl -s ifconfig.me | tr . -).nip.io
  62. COREDNS_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')
  63. SERVER_PUBLIC_IP=$(curl -s ifconfig.me)
  64. MASTER_KEY=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  65. EMAIL="$(echo $RANDOM | md5sum | head -c 32)@email.com"
  66. MQ_ADMIN_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 64 ; echo '')
  67. if [ -n "$domain" ]; then
  68. NETMAKER_BASE_DOMAIN=$domain
  69. fi
  70. if [ -n "$email" ]; then
  71. EMAIL=$email
  72. fi
  73. if [ -n "$addmesh" ]; then
  74. MESH_SETUP=$addmesh
  75. else
  76. MESH_SETUP="true"
  77. fi
  78. if [ -n "$addvpn" ]; then
  79. VPN_SETUP=$addvpn
  80. else
  81. VPN_SETUP="false"
  82. fi
  83. if [ -n "$num_clients" ]; then
  84. NUM_CLIENTS=$num_clients
  85. else
  86. NUM_CLIENTS=5
  87. fi
  88. echo " ----------------------------"
  89. echo " SETUP ARGUMENTS"
  90. echo " ----------------------------"
  91. echo " domain: $NETMAKER_BASE_DOMAIN"
  92. echo " email: $EMAIL"
  93. echo " coredns ip: $COREDNS_IP"
  94. echo " public ip: $SERVER_PUBLIC_IP"
  95. echo " master key: $MASTER_KEY"
  96. echo " setup mesh?: $MESH_SETUP"
  97. echo " setup vpn?: $VPN_SETUP"
  98. if [ "${VPN_SETUP}" == "true" ]; then
  99. echo " # clients: $NUM_CLIENTS"
  100. fi
  101. echo " ----------------------------"
  102. sleep 5
  103. echo "setting mosquitto.conf..."
  104. wget -q -O /root/mosquitto.conf https://raw.githubusercontent.com/gravitl/netmaker/master/docker/mosquitto.conf
  105. wget -q -O /root/Caddyfile https://raw.githubusercontent.com/gravitl/netmaker/master/docker/Caddyfile
  106. wget -q -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/master/docker/wait.sh
  107. chmod +x /root/wait.sh
  108. echo "setting docker-compose..."
  109. mkdir -p /etc/netmaker
  110. wget -q -O /root/docker-compose.yml https://raw.githubusercontent.com/gravitl/netmaker/master/compose/docker-compose.yml
  111. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/docker-compose.yml
  112. sed -i "s/NETMAKER_BASE_DOMAIN/$NETMAKER_BASE_DOMAIN/g" /root/Caddyfile
  113. sed -i "s/SERVER_PUBLIC_IP/$SERVER_PUBLIC_IP/g" /root/docker-compose.yml
  114. sed -i "s/COREDNS_IP/$COREDNS_IP/g" /root/docker-compose.yml
  115. sed -i "s/REPLACE_MASTER_KEY/$MASTER_KEY/g" /root/docker-compose.yml
  116. sed -i "s/YOUR_EMAIL/$EMAIL/g" /root/Caddyfile
  117. sed -i "s/REPLACE_MQ_ADMIN_PASSWORD/$MQ_ADMIN_PASSWORD/g" /root/docker-compose.yml
  118. echo "starting containers..."
  119. docker-compose -f /root/docker-compose.yml up -d
  120. test_connection() {
  121. echo "testing Traefik setup (please be patient, this may take 1-2 minutes)"
  122. for i in 1 2 3 4 5 6
  123. do
  124. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  125. if [[ "$i" == 6 ]]; then
  126. echo " Traefik is having an issue setting up certificates, please investigate (docker logs traefik)"
  127. echo " exiting..."
  128. exit 1
  129. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  130. echo " certificates not yet configured, retrying..."
  131. elif [[ "$curlresponse" == *"left intact"* ]]; then
  132. echo " certificates ok"
  133. break
  134. else
  135. secs=$(($i*5+10))
  136. echo " issue establishing connection...retrying in $secs seconds..."
  137. fi
  138. sleep $secs
  139. done
  140. }
  141. set +e
  142. test_connection
  143. cat << "EOF"
  144. __ __ ______ ______ __ __ ______ __ __ ______ ______
  145. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  146. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  147. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  148. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  149. EOF
  150. echo "visit https://dashboard.$NETMAKER_BASE_DOMAIN to log in"
  151. echo "visit https://grafana.$NETMAKER_BASE_DOMAIN to view metrics on grafana dashboard"
  152. echo "visit https://prometheus.$NETMAKER_BASE_DOMAIN to view metrics on prometheus"
  153. sleep 7
  154. setup_mesh() {( set -e
  155. echo "creating netmaker network (10.101.0.0/16)"
  156. 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
  157. sleep 5
  158. echo "creating netmaker access key"
  159. 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)
  160. ACCESS_TOKEN=$(jq -r '.accessstring' <<< ${curlresponse})
  161. sleep 5
  162. echo "configuring netmaker server as ingress gateway"
  163. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/netmaker)
  164. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  165. 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
  166. sleep 5
  167. echo "finished configuring server and network. You can now add clients."
  168. echo ""
  169. echo "For Linux, Mac, Windows, and FreeBSD:"
  170. echo " 1. Install the netclient: https://docs.netmaker.org/netclient.html#installation"
  171. echo " 2. Join the network: netclient join -t $ACCESS_TOKEN"
  172. echo ""
  173. echo "For Android and iOS clients, perform the following steps:"
  174. echo " 1. Log into UI at dashboard.$NETMAKER_BASE_DOMAIN"
  175. echo " 2. Navigate to \"EXTERNAL CLIENTS\" tab"
  176. echo " 3. Select the gateway and create clients"
  177. echo " 4. Scan the QR Code from WireGuard app in iOS or Android"
  178. echo ""
  179. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  180. )}
  181. setup_vpn() {( set -e
  182. echo "creating vpn network (10.201.0.0/16)"
  183. curl -s -o /dev/null -d '{"addressrange":"10.201.0.0/16","netid":"vpn","defaultextclientdns":"10.201.255.254"}' -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/networks
  184. sleep 5
  185. echo "configuring netmaker server as vpn inlet..."
  186. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  187. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  188. curl -s -o /dev/null -X POST -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn/$SERVER_ID/createingress
  189. echo "waiting 5 seconds for server to apply configuration..."
  190. sleep 5
  191. echo "configuring netmaker server vpn gateway..."
  192. [ -z "$GATEWAY_IFACE" ] && GATEWAY_IFACE=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)')
  193. echo "gateway iface: $GATEWAY_IFACE"
  194. curlresponse=$(curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn)
  195. SERVER_ID=$(jq -r '.[0].id' <<< ${curlresponse})
  196. EGRESS_JSON=$( jq -n \
  197. --arg gw "$GATEWAY_IFACE" \
  198. '{ranges: ["0.0.0.0/0"], interface: $gw}' )
  199. echo "egress json: $EGRESS_JSON"
  200. curl -s -o /dev/null -X POST -d "$EGRESS_JSON" -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/nodes/vpn/$SERVER_ID/creategateway
  201. sleep 3
  202. echo "creating client configs..."
  203. for ((a=1; a <= $NUM_CLIENTS; a++))
  204. do
  205. CLIENT_JSON=$( jq -n \
  206. --arg clientid "vpnclient-$a" \
  207. '{clientid: $clientid}' )
  208. curl -s -o /dev/null -d "$CLIENT_JSON" -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://api.${NETMAKER_BASE_DOMAIN}/api/extclients/vpn/$SERVER_ID
  209. sleep 2
  210. done
  211. echo "finished configuring vpn server."
  212. echo ""
  213. echo "To configure clients, perform the following steps:"
  214. echo " 1. log into dashboard.$NETMAKER_BASE_DOMAIN"
  215. echo " 2. Navigate to \"EXTERNAL CLIENTS\" tab"
  216. echo " 3. Download or scan a client config (vpnclient-x) to the appropriate device"
  217. echo " 4. Follow the steps for your system to configure WireGuard on the appropriate device"
  218. echo " 5. Create and delete clients as necessary. Changes to netmaker server settings require regenerating ext clients."
  219. )}
  220. if [ "${MESH_SETUP}" != "false" ]; then
  221. setup_mesh
  222. fi
  223. if [ "${VPN_SETUP}" == "true" ]; then
  224. setup_vpn
  225. fi
  226. echo ""
  227. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."