nm-upgrade.sh 23 KB

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