nm-upgrade.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. #!/bin/bash
  2. LATEST="testing"
  3. # check_version - make sure current version is 0.17.1 before continuing
  4. check_version() {
  5. IMG_TAG=$(yq -r '.services.netmaker.image' docker-compose.yml)
  6. if [[ "$IMG_TAG" == *"v0.17.1"* ]]; then
  7. echo "version is $IMG_TAG"
  8. else
  9. echo "error, current version is $IMG_TAG"
  10. echo "please upgrade to v0.17.1 in order to use the upgrade script"
  11. exit 1
  12. fi
  13. }
  14. # wait_seconds - wait a number of seconds, print a log
  15. wait_seconds() {
  16. for ((a=1; a <= $1; a++))
  17. do
  18. echo ". . ."
  19. sleep 1
  20. done
  21. }
  22. # confirm - confirm a choice, or exit script
  23. confirm() {
  24. while true; do
  25. read -p 'Does everything look right? [y/n]: ' yn
  26. case $yn in
  27. [Yy]* ) override="true"; break;;
  28. [Nn]* ) echo "exiting..."; exit 1;;
  29. * ) echo "Please answer yes or no.";;
  30. esac
  31. done
  32. }
  33. # install_dependencies - install system dependencies necessary for script to run
  34. install_dependencies() {
  35. OS=$(uname)
  36. is_ubuntu=$(sudo cat /etc/lsb-release | grep "Ubuntu")
  37. if [ "${is_ubuntu}" != "" ]; then
  38. dependencies="yq jq wireguard jq docker.io docker-compose"
  39. update_cmd='apt update'
  40. install_cmd='snap install'
  41. elif [ -f /etc/debian_version ]; then
  42. dependencies="yq jq wireguard jq docker.io docker-compose"
  43. update_cmd='apt update'
  44. install_cmd='apt install -y'
  45. elif [ -f /etc/centos-release ]; then
  46. dependencies="wireguard jq docker.io docker-compose"
  47. update_cmd='yum update'
  48. install_cmd='yum install -y'
  49. elif [ -f /etc/fedora-release ]; then
  50. dependencies="wireguard jq docker.io docker-compose"
  51. update_cmd='dnf update'
  52. install_cmd='dnf install -y'
  53. elif [ -f /etc/redhat-release ]; then
  54. dependencies="wireguard jq docker.io docker-compose"
  55. update_cmd='yum update'
  56. install_cmd='yum install -y'
  57. elif [ -f /etc/arch-release ]; then
  58. dependecies="wireguard-tools jq docker.io docker-compose netclient"
  59. update_cmd='pacman -Sy'
  60. install_cmd='pacman -S --noconfirm'
  61. else
  62. echo "OS not supported for automatic install"
  63. exit 1
  64. fi
  65. set -- $dependencies
  66. ${update_cmd}
  67. set +e
  68. while [ -n "$1" ]; do
  69. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  70. if [ "${is_installed}" != "" ]; then
  71. echo " " $1 is installed
  72. else
  73. echo " " $1 is not installed. Attempting install.
  74. ${install_cmd} $1
  75. sleep 5
  76. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  77. is_installed=$(opkg list-installed $1 | grep $1)
  78. else
  79. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  80. fi
  81. if [ "${is_installed}" != "" ]; then
  82. echo " " $1 is installed
  83. elif [ -x "$(command -v $1)" ]; then
  84. echo " " $1 is installed
  85. else
  86. echo " " FAILED TO INSTALL $1
  87. echo " " This may break functionality.
  88. fi
  89. fi
  90. shift
  91. done
  92. set -e
  93. echo "-----------------------------------------------------"
  94. echo "dependency install complete"
  95. echo "-----------------------------------------------------"
  96. }
  97. # get_email- gets upgrader's email address
  98. get_email() {
  99. unset GET_EMAIL
  100. unset RAND_EMAIL
  101. RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
  102. read -p "Email Address for Domain Registration (click 'enter' to use $RAND_EMAIL): " GET_EMAIL
  103. if [ -z "$GET_EMAIL" ]; then
  104. echo "using rand email"
  105. EMAIL="$RAND_EMAIL"
  106. else
  107. EMAIL="$GET_EMAIL"
  108. fi
  109. }
  110. # collect_server_settings - retrieve server settings from existing compose file
  111. collect_server_settings() {
  112. MASTER_KEY=$(yq -r .services.netmaker.environment.MASTER_KEY docker-compose.yml)
  113. echo "-----------------------------------------------------"
  114. echo "Is $MASTER_KEY the correct master key for your Netmaker installation?"
  115. echo "-----------------------------------------------------"
  116. select mkey_option in "yes" "no (enter manually)"; do
  117. case $REPLY in
  118. 1)
  119. echo "using $MASTER_KEY for master key"
  120. break
  121. ;;
  122. 2)
  123. read -p "Enter Master Key: " mkey
  124. MASTER_KEY=$mkey
  125. echo "using $MASTER_KEY"
  126. break
  127. ;;
  128. *) echo "invalid option $REPLY, choose 1 or 2";;
  129. esac
  130. done
  131. SERVER_HTTP_HOST=$(yq -r .services.netmaker.environment.SERVER_HTTP_HOST docker-compose.yml)
  132. echo "-----------------------------------------------------"
  133. echo "Is $SERVER_HTTP_HOST the correct api endpoint for your Netmaker installation?"
  134. echo "-----------------------------------------------------"
  135. select endpoint_option in "yes" "no (enter manually)"; do
  136. case $REPLY in
  137. 1)
  138. echo "using $SERVER_HTTP_HOST for api endpoint"
  139. break
  140. ;;
  141. 2)
  142. read -p "Enter API Endpoint: " endpoint
  143. SERVER_HTTP_HOST=$endpoint
  144. echo "using $SERVER_HTTP_HOST"
  145. break
  146. ;;
  147. *) echo "invalid option $REPLY";;
  148. esac
  149. done
  150. BROKER_NAME=$(yq -r .services.netmaker.environment.SERVER_NAME docker-compose.yml)
  151. echo "-----------------------------------------------------"
  152. echo "Is $BROKER_NAME the correct domain for your MQ broker?"
  153. echo "-----------------------------------------------------"
  154. select broker_option in "yes" "no (enter manually)"; do
  155. case $REPLY in
  156. 1)
  157. echo "using $BROKER_NAME for endpoint"
  158. break
  159. ;;
  160. 2)
  161. read -p "Enter Broker Domain: " broker
  162. BROKER_NAME=$broker
  163. echo "using $BROKER_NAME"
  164. break
  165. ;;
  166. *) echo "invalid option $REPLY";;
  167. esac
  168. done
  169. SERVER_NAME=${BROKER_NAME#"broker."}
  170. echo "-----------------------------------------------------"
  171. echo "Is $SERVER_NAME the correct base domain for your installation?"
  172. echo "-----------------------------------------------------"
  173. select domain_option in "yes" "no (enter manually)"; do
  174. case $REPLY in
  175. 1)
  176. echo "using $SERVER_NAME for domain"
  177. break
  178. ;;
  179. 2)
  180. read -p "Enter Server Domain: " broker
  181. SERVER_NAME=$server
  182. echo "using $SERVER_NAME"
  183. break
  184. ;;
  185. *) echo "invalid option $REPLY";;
  186. esac
  187. done
  188. STUN_DOMAIN="stun.$SERVER_NAME"
  189. echo "-----------------------------------------------------"
  190. echo "Netmaker v0.18 requires a new DNS entry for $STUN_DOMAIN."
  191. echo "Please confirm this is added to your DNS provider before continuing"
  192. echo "(note: this is not required if using an nip.io address)"
  193. echo "-----------------------------------------------------"
  194. confirm
  195. }
  196. # collect_node_settings - get existing server node configuration
  197. collect_node_settings() {
  198. curl -s -H "Authorization: Bearer $MASTER_KEY" -H 'Content-Type: application/json' https://$SERVER_HTTP_HOST/api/nodes | jq -c '[ .[] | select(.isserver=="yes") ]' > nodejson.tmp
  199. NODE_LEN=$(jq length nodejson.tmp)
  200. HAS_INGRESS="no"
  201. HAS_RELAY="no"
  202. if [ "$NODE_LEN" -gt 0 ]; then
  203. echo "===SERVER NODES==="
  204. for i in $(seq 1 $NODE_LEN); do
  205. NUM=$(($i-1))
  206. echo " SERVER NODE $NUM:"
  207. echo " network: $(jq -r ".[$NUM].network" ./nodejson.tmp)"
  208. echo " name: $(jq -r ".[$NUM].name" ./nodejson.tmp)"
  209. echo " private ipv4: $(jq -r ".[$NUM].address" ./nodejson.tmp)"
  210. echo " private ipv6: $(jq -r ".[$NUM].address6" ./nodejson.tmp)"
  211. echo " is egress: $(jq -r ".[$NUM].isegressgateway" ./nodejson.tmp)"
  212. if [[ $(jq -r ".[$NUM].isegressgateway" ./nodejson.tmp) == "yes" ]]; then
  213. echo " egress range: $(jq -r ".[$NUM].egressgatewayranges" ./nodejson.tmp)"
  214. fi
  215. echo " is ingress: $(jq -r ".[$NUM].isingressgateway" ./nodejson.tmp)"
  216. if [[ $(jq -r ".[$NUM].isingressgateway" ./nodejson.tmp) == "yes" ]]; then
  217. HAS_INGRESS="yes"
  218. fi
  219. echo " is relay: $(jq -r ".[$NUM].isrelay" ./nodejson.tmp)"
  220. if [[ $(jq -r ".[$NUM].isrelay" ./nodejson.tmp) == "yes" ]]; then
  221. HAS_RELAY="yes"
  222. echo " relay addrs: $(jq -r ".[$NUM].relayaddrs" ./nodejson.tmp | tr -d '[]\n"[:space:]')"
  223. fi
  224. echo " is failover: $(jq -r ".[$NUM].failover" ./nodejson.tmp)"
  225. echo " ------------"
  226. done
  227. echo "=================="
  228. else
  229. echo "no nodes to parse"
  230. fi
  231. echo "Please confirm that the above output matches the server nodes in your Netmaker server."
  232. confirm
  233. if [[ $HAS_INGRESS == "yes" ]]; then
  234. echo "WARNING: Your server contains an Ingress Gateway. After upgrading, existing Ext Clients will be lost and must be recreated. Please confirm that you would like to continue."
  235. confirm
  236. fi
  237. if [[ $HAS_RELAY == "yes" ]]; then
  238. echo "WARNING: Your server contains a Relay. After upgrading, relay will be unset. Relay functionality has been moved to the 'host' level, and must be reconfigured once all machines are upgraded."
  239. confirm
  240. fi
  241. }
  242. # setup_caddy - updates Caddy with new info
  243. setup_caddy() {
  244. echo "backing up Caddyfile to /root/Caddyfile.backup"
  245. cp /root/Caddyfile /root/Caddyfile.backup
  246. if grep -wq "acme.zerossl.com/v2/DV90" Caddyfile; then
  247. echo "zerossl already set, continuing"
  248. else
  249. echo "editing Caddyfile"
  250. sed -i '0,/email/{s~email~acme_ca https://acme.zerossl.com/v2/DV90\n\t&~}' /root/Caddyfile
  251. fi
  252. cat <<EOT >> /root/Caddyfile
  253. # STUN
  254. https://$STUN_DOMAIN {
  255. reverse_proxy netmaker:3478
  256. }
  257. EOT
  258. }
  259. # set_mq_credentials - sets mq credentials
  260. set_mq_credentials() {
  261. unset GET_MQ_USERNAME
  262. unset GET_MQ_PASSWORD
  263. unset CONFIRM_MQ_PASSWORD
  264. echo "Enter Credentials For MQ..."
  265. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  266. if [ -z "$GET_MQ_USERNAME" ]; then
  267. echo "using default username for mq"
  268. MQ_USERNAME="netmaker"
  269. else
  270. MQ_USERNAME="$GET_MQ_USERNAME"
  271. fi
  272. select domain_option in "Auto Generated Password" "Input Your Own Password"; do
  273. case $REPLY in
  274. 1)
  275. echo "generating random password for mq"
  276. MQ_PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 30 ; echo '')
  277. break
  278. ;;
  279. 2)
  280. while true
  281. do
  282. echo "Enter your Password For MQ: "
  283. read -s GET_MQ_PASSWORD
  284. echo "Enter your password again to confirm: "
  285. read -s CONFIRM_MQ_PASSWORD
  286. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  287. echo "wrong password entered, try again..."
  288. continue
  289. fi
  290. MQ_PASSWORD="$GET_MQ_PASSWORD"
  291. echo "MQ Password Saved Successfully!!"
  292. break
  293. done
  294. break
  295. ;;
  296. *) echo "invalid option $REPLY";;
  297. esac
  298. done
  299. }
  300. # set_compose - set compose file with proper values
  301. set_compose() {
  302. set_mq_credentials
  303. echo "retrieving updated wait script and mosquitto conf"
  304. rm /root/wait.sh
  305. rm /root/mosquitto.conf
  306. # DEV_TEMP
  307. wget -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/develop/docker/wait.sh
  308. # RELEASE_REPLACE - Use this once release is ready
  309. # wget -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/master/docker/wait.sh
  310. chmod +x /root/wait.sh
  311. # DEV_TEMP
  312. wget -O /root/mosquitto.conf https://raw.githubusercontent.com/gravitl/netmaker/develop/docker/mosquitto.conf
  313. # RELEASE_REPLACE - Use this once release is ready
  314. # wget -O /root/wait.sh https://raw.githubusercontent.com/gravitl/netmaker/master/docker/wait.sh
  315. chmod +x /root/mosquitto.conf
  316. # DEV_TEMP
  317. sed -i "s/v0.17.1/$LATEST/g" /root/docker-compose.yml
  318. STUN_PORT=3478
  319. # RELEASE_REPLACE - Use this once release is ready
  320. #sed -i "s/v0.17.1/v0.18.3/g" /root/docker-compose.yml
  321. yq ".services.netmaker.environment.SERVER_NAME = \"$SERVER_NAME\"" -i /root/docker-compose.yml
  322. yq ".services.netmaker.environment += {\"BROKER_ENDPOINT\": \"wss://$BROKER_NAME\"}" -i /root/docker-compose.yml
  323. yq ".services.netmaker.environment += {\"SERVER_BROKER_ENDPOINT\": \"wss://mq:1883\"}" -i /root/docker-compose.yml
  324. yq ".services.netmaker.environment += {\"STUN_LIST\": \"$STUN_DOMAIN:$STUN_PORT,stun1.netmaker.io:3478,stun2.netmaker.io:3478,stun1.l.google.com:19302,stun2.l.google.com:19302\"}" -i /root/docker-compose.yml
  325. yq ".services.netmaker.environment += {\"MQ_PASSWORD\": \"$MQ_PASSWORD\"}" -i /root/docker-compose.yml
  326. yq ".services.netmaker.environment += {\"MQ_USERNAME\": \"$MQ_USERNAME\"}" -i /root/docker-compose.yml
  327. yq ".services.netmaker.environment += {\"STUN_PORT\": \"$STUN_PORT\"}" -i /root/docker-compose.yml
  328. yq ".services.netmaker.ports += \"3478:3478/udp\"" -i /root/docker-compose.yml
  329. yq ".services.mq.environment += {\"MQ_PASSWORD\": \"$MQ_PASSWORD\"}" -i /root/docker-compose.yml
  330. yq ".services.mq.environment += {\"MQ_USERNAME\": \"$MQ_USERNAME\"}" -i /root/docker-compose.yml
  331. #remove unnecessary ports
  332. yq eval 'del( .services.netmaker.ports[] | select(. == "51821*") )' -i /root/docker-compose.yml
  333. yq eval 'del( .services.mq.ports[] | select(. == "8883*") )' -i /root/docker-compose.yml
  334. yq eval 'del( .services.mq.ports[] | select(. == "1883*") )' -i /root/docker-compose.yml
  335. yq eval 'del( .services.mq.expose[] | select(. == "8883*") )' -i /root/docker-compose.yml
  336. yq eval 'del( .services.mq.expose[] | select(. == "1883*") )' -i /root/docker-compose.yml
  337. # delete unnecessary compose sections
  338. yq eval 'del(.services.netmaker.cap_add)' -i /root/docker-compose.yml
  339. yq eval 'del(.services.netmaker.sysctls)' -i /root/docker-compose.yml
  340. yq eval 'del(.services.netmaker.environment.MQ_ADMIN_PASSWORD)' -i /root/docker-compose.yml
  341. yq eval 'del(.services.netmaker.environment.MQ_HOST)' -i /root/docker-compose.yml
  342. yq eval 'del(.services.netmaker.environment.MQ_PORT)' -i /root/docker-compose.yml
  343. yq eval 'del(.services.netmaker.environment.MQ_SERVER_PORT)' -i /root/docker-compose.yml
  344. yq eval 'del(.services.netmaker.environment.PORT_FORWARD_SERVICES)' -i /root/docker-compose.yml
  345. yq eval 'del(.services.netmaker.environment.CLIENT_MODE)' -i /root/docker-compose.yml
  346. yq eval 'del(.services.netmaker.environment.HOST_NETWORK)' -i /root/docker-compose.yml
  347. yq eval 'del(.services.mq.environment.NETMAKER_SERVER_HOST)' -i /root/docker-compose.yml
  348. yq eval 'del( .services.netmaker.volumes[] | select(. == "mosquitto_data*") )' -i /root/docker-compose.yml
  349. yq eval 'del( .services.mq.volumes[] | select(. == "mosquitto_data*") )' -i /root/docker-compose.yml
  350. yq eval 'del( .volumes.mosquitto_data )' -i /root/docker-compose.yml
  351. }
  352. # start_containers - run docker-compose up -d
  353. start_containers() {
  354. docker-compose -f /root/docker-compose.yml up -d
  355. }
  356. # test_caddy - make sure caddy is working
  357. test_caddy() {
  358. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  359. for i in 1 2 3 4 5 6 7 8
  360. do
  361. curlresponse=$(curl -vIs https://${SERVER_HTTP_HOST} 2>&1)
  362. if [[ "$i" == 8 ]]; then
  363. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  364. echo " Exiting..."
  365. exit 1
  366. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  367. echo " Certificates not yet configured, retrying..."
  368. elif [[ "$curlresponse" == *"left intact"* ]]; then
  369. echo " Certificates ok"
  370. break
  371. else
  372. secs=$(($i*5+10))
  373. echo " Issue establishing connection...retrying in $secs seconds..."
  374. fi
  375. sleep $secs
  376. done
  377. }
  378. # setup_netclient - adds netclient to docker-compose
  379. setup_netclient() {
  380. # yq ".services.netclient += {\"container_name\": \"netclient\"}" -i /root/docker-compose.yml
  381. # yq ".services.netclient += {\"image\": \"gravitl/netclient:testing\"}" -i /root/docker-compose.yml
  382. # yq ".services.netclient += {\"hostname\": \"netmaker-1\"}" -i /root/docker-compose.yml
  383. # yq ".services.netclient += {\"network_mode\": \"host\"}" -i /root/docker-compose.yml
  384. # yq ".services.netclient.depends_on += [\"netmaker\"]" -i /root/docker-compose.yml
  385. # yq ".services.netclient += {\"restart\": \"always\"}" -i /root/docker-compose.yml
  386. # yq ".services.netclient.environment += {\"TOKEN\": \"$KEY\"}" -i /root/docker-compose.yml
  387. # yq ".services.netclient.volumes += [\"/etc/netclient:/etc/netclient\"]" -i /root/docker-compose.yml
  388. # yq ".services.netclient.cap_add += [\"NET_ADMIN\"]" -i /root/docker-compose.yml
  389. # yq ".services.netclient.cap_add += [\"NET_RAW\"]" -i /root/docker-compose.yml
  390. # yq ".services.netclient.cap_add += [\"SYS_MODULE\"]" -i /root/docker-compose.yml
  391. # docker-compose up -d
  392. set +e
  393. netclient uninstall
  394. set -e
  395. wget -O /tmp/netclient https://fileserver.netmaker.org/$LATEST/netclient
  396. chmod +x /tmp/netclient
  397. /tmp/netclient install
  398. netclient join -t $KEY
  399. echo "waiting for client to become available"
  400. wait_seconds 10
  401. }
  402. # setup_nmctl - pulls nmctl and makes it executable
  403. setup_nmctl() {
  404. # DEV_TEMP - Temporary instructions for testing
  405. wget https://fileserver.netmaker.org/testing/nmctl
  406. # RELEASE_REPLACE - Use this once release is ready
  407. # wget https://github.com/gravitl/netmaker/releases/download/v0.17.1/nmctl
  408. chmod +x nmctl
  409. echo "using server $SERVER_HTTP_HOST"
  410. echo "using master key $MASTER_KEY"
  411. ./nmctl context set default --endpoint="https://$SERVER_HTTP_HOST" --master_key="$MASTER_KEY"
  412. ./nmctl context use default
  413. RESP=$(./nmctl network list)
  414. if [[ $RESP == *"unauthorized"* ]]; then
  415. echo "Unable to properly configure NMCTL, exiting..."
  416. exit 1
  417. fi
  418. }
  419. # join_networks - joins netclient into the networks using old settings
  420. join_networks() {
  421. NODE_LEN=$(jq length nodejson.tmp)
  422. if [ "$NODE_LEN" -gt 0 ]; then
  423. for i in $(seq 1 $NODE_LEN); do
  424. HAS_INGRESS="no"
  425. HAS_EGRESS="no"
  426. EGRESS_RANGES=""
  427. HAS_RELAY="no"
  428. RELAY_ADDRS=""
  429. HAS_FAILOVER="no"
  430. NUM=$(($i-1))
  431. NETWORK=$(jq -r ".[$NUM].network" ./nodejson.tmp)
  432. echo " joining network $NETWORK with following settings. Please confirm:"
  433. echo " network: $(jq -r ".[$NUM].network" ./nodejson.tmp)"
  434. echo " name: $(jq -r ".[$NUM].name" ./nodejson.tmp)"
  435. echo " private ipv4: $(jq -r ".[$NUM].address" ./nodejson.tmp)"
  436. echo " private ipv6: $(jq -r ".[$NUM].address6" ./nodejson.tmp)"
  437. echo " is egress: $(jq -r ".[$NUM].isegressgateway" ./nodejson.tmp)"
  438. if [[ $(jq -r ".[$NUM].isegressgateway" ./nodejson.tmp) == "yes" ]]; then
  439. HAS_EGRESS="yes"
  440. echo " egress ranges: $(jq -r ".[$NUM].egressgatewayranges" ./nodejson.tmp | tr -d '[]\n"[:space:]')"
  441. EGRESS_RANGES=$(jq -r ".[$NUM].egressgatewayranges" ./nodejson.tmp | tr -d '[]\n"[:space:]')
  442. fi
  443. echo " is ingress: $(jq -r ".[$NUM].isingressgateway" ./nodejson.tmp)"
  444. if [[ $(jq -r ".[$NUM].isingressgateway" ./nodejson.tmp) == "yes" ]]; then
  445. HAS_INGRESS="yes"
  446. fi
  447. echo " is relay: $(jq -r ".[$NUM].isrelay" ./nodejson.tmp)"
  448. if [[ $(jq -r ".[$NUM].isrelay" ./nodejson.tmp) == "yes" ]]; then
  449. HAS_RELAY="yes"
  450. RELAY_ADDRS=$(jq -r ".[$NUM].relayaddrs" ./nodejson.tmp | tr -d '[]\n"[:space:]')
  451. fi
  452. echo " is failover: $(jq -r ".[$NUM].failover" ./nodejson.tmp)"
  453. if [[ $(jq -r ".[$NUM].failover" ./nodejson.tmp) == "yes" ]]; then
  454. HAS_FAILOVER="yes"
  455. fi
  456. echo " ------------"
  457. confirm
  458. if [[ $NUM -eq 0 ]]; then
  459. echo "running command: ./nmctl keys create $NETWORK 1"
  460. KEY_JSON=$(./nmctl keys create $NETWORK 1)
  461. KEY=$(echo $KEY_JSON | jq -r .accessstring)
  462. echo "join key created: $KEY"
  463. setup_netclient
  464. else
  465. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  466. ./nmctl host add_network $HOST_ID $NETWORK
  467. fi
  468. NAME=$(jq -r ".[$NUM].name" ./nodejson.tmp)
  469. ADDRESS=$(jq -r ".[$NUM].address" ./nodejson.tmp)
  470. ADDRESS6=$(jq -r ".[$NUM].address6" ./nodejson.tmp)
  471. echo "wait 10 seconds for netclient to be ready"
  472. sleep 10
  473. NODE_ID=$(sudo cat /etc/netclient/nodes.yml | yq -r .$NETWORK.commonnode.id)
  474. echo "join complete. New node ID: $NODE_ID"
  475. if [[ $NUM -eq 0 ]]; then
  476. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  477. echo "For first join, making host a default"
  478. echo "Host ID: $HOST_ID"
  479. # set as a default host
  480. set +e
  481. ./nmctl host update $HOST_ID --default
  482. sleep 2
  483. set -e
  484. fi
  485. # create an egress if necessary
  486. if [[ $HAS_EGRESS == "yes" ]]; then
  487. echo "creating egress"
  488. ./nmctl node create_egress $NETWORK $NODE_ID $EGRESS_RANGES
  489. sleep 2
  490. fi
  491. echo "HAS INGRESS: $HAS_INGRESS"
  492. # create an ingress if necessary
  493. if [[ $HAS_INGRESS == "yes" ]]; then
  494. if [[ $HAS_FAILOVER == "yes" ]]; then
  495. echo "creating ingress and failover..."
  496. ./nmctl node create_ingress $NETWORK $NODE_ID --failover
  497. sleep 2
  498. else
  499. echo "creating ingress..."
  500. ./nmctl node create_ingress $NETWORK $NODE_ID
  501. sleep 2
  502. fi
  503. fi
  504. # relay
  505. if [[ $HAS_RELAY == "yes" ]]; then
  506. echo "cannot recreate relay; relay functionality moved to host"
  507. # ./nmctl node create_relay $NETWORK $NODE_ID $RELAY_ADDRS
  508. # sleep 2
  509. fi
  510. done
  511. echo "=================="
  512. else
  513. echo "no networks to join"
  514. fi
  515. }
  516. cat << "EOF"
  517. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  518. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  519. The Netmaker Upgrade Script: Upgrading to v0.18 so you don't have to!
  520. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  521. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  522. EOF
  523. set -e
  524. if [ $(id -u) -ne 0 ]; then
  525. echo "This script must be run as root"
  526. exit 1
  527. fi
  528. echo "...installing dependencies for script"
  529. install_dependencies
  530. echo "...confirming version is correct"
  531. check_version
  532. echo "...collecting necessary server settings"
  533. collect_server_settings
  534. echo "...setup nmctl"
  535. setup_nmctl
  536. echo "...retrieving current server node settings"
  537. collect_node_settings
  538. echo "...backing up docker compose to docker-compose.yml.backup"
  539. cp /root/docker-compose.yml /root/docker-compose.yml.backup
  540. echo "...setting Caddyfile values"
  541. setup_caddy
  542. echo "...setting docker-compose values"
  543. set_compose
  544. echo "...starting containers"
  545. start_containers
  546. echo "...remove old mosquitto data"
  547. # TODO - yq is not removing volume from docker compose
  548. # docker volume rm root_mosquitto_data
  549. wait_seconds 3
  550. echo "..testing Caddy proxy"
  551. test_caddy
  552. echo "..testing Netmaker health"
  553. # TODO, implement health check
  554. # netmaker_health_check
  555. # wait_seconds 2
  556. wait_seconds 2
  557. echo "...setup netclient"
  558. join_networks
  559. echo "-----------------------------------------------------------------"
  560. echo "-----------------------------------------------------------------"
  561. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  562. echo "Visit dashboard.$SERVER_NAME to log in"
  563. echo "-----------------------------------------------------------------"
  564. echo "-----------------------------------------------------------------"