nm-upgrade.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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.4/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. set +e
  382. netclient uninstall
  383. set -e
  384. wget -O /tmp/netclient https://fileserver.netmaker.org/$LATEST/netclient
  385. chmod +x /tmp/netclient
  386. /tmp/netclient install
  387. netclient register -t $KEY
  388. echo "waiting for client to become available"
  389. wait_seconds 10
  390. }
  391. # setup_nmctl - pulls nmctl and makes it executable
  392. setup_nmctl() {
  393. # DEV_TEMP - Temporary instructions for testing
  394. wget https://fileserver.netmaker.org/testing/nmctl
  395. # RELEASE_REPLACE - Use this once release is ready
  396. # wget https://github.com/gravitl/netmaker/releases/download/v0.17.1/nmctl
  397. chmod +x nmctl
  398. echo "using server $SERVER_HTTP_HOST"
  399. echo "using master key $MASTER_KEY"
  400. ./nmctl context set default --endpoint="https://$SERVER_HTTP_HOST" --master_key="$MASTER_KEY"
  401. ./nmctl context use default
  402. RESP=$(./nmctl network list)
  403. if [[ $RESP == *"unauthorized"* ]]; then
  404. echo "Unable to properly configure NMCTL, exiting..."
  405. exit 1
  406. fi
  407. }
  408. # join_networks - joins netclient into the networks using old settings
  409. join_networks() {
  410. NODE_LEN=$(jq length nodejson.tmp)
  411. if [ "$NODE_LEN" -gt 0 ]; then
  412. for i in $(seq 1 $NODE_LEN); do
  413. HAS_INGRESS="no"
  414. HAS_EGRESS="no"
  415. EGRESS_RANGES=""
  416. HAS_RELAY="no"
  417. RELAY_ADDRS=""
  418. HAS_FAILOVER="no"
  419. NUM=$(($i-1))
  420. NETWORK=$(jq -r ".[$NUM].network" ./nodejson.tmp)
  421. echo " joining network $NETWORK with following settings. Please confirm:"
  422. echo " network: $(jq -r ".[$NUM].network" ./nodejson.tmp)"
  423. echo " name: $(jq -r ".[$NUM].name" ./nodejson.tmp)"
  424. echo " private ipv4: $(jq -r ".[$NUM].address" ./nodejson.tmp)"
  425. echo " private ipv6: $(jq -r ".[$NUM].address6" ./nodejson.tmp)"
  426. echo " is egress: $(jq -r ".[$NUM].isegressgateway" ./nodejson.tmp)"
  427. if [[ $(jq -r ".[$NUM].isegressgateway" ./nodejson.tmp) == "yes" ]]; then
  428. HAS_EGRESS="yes"
  429. echo " egress ranges: $(jq -r ".[$NUM].egressgatewayranges" ./nodejson.tmp | tr -d '[]\n"[:space:]')"
  430. EGRESS_RANGES=$(jq -r ".[$NUM].egressgatewayranges" ./nodejson.tmp | tr -d '[]\n"[:space:]')
  431. fi
  432. echo " is ingress: $(jq -r ".[$NUM].isingressgateway" ./nodejson.tmp)"
  433. if [[ $(jq -r ".[$NUM].isingressgateway" ./nodejson.tmp) == "yes" ]]; then
  434. HAS_INGRESS="yes"
  435. fi
  436. echo " is relay: $(jq -r ".[$NUM].isrelay" ./nodejson.tmp)"
  437. if [[ $(jq -r ".[$NUM].isrelay" ./nodejson.tmp) == "yes" ]]; then
  438. HAS_RELAY="yes"
  439. RELAY_ADDRS=$(jq -r ".[$NUM].relayaddrs" ./nodejson.tmp | tr -d '[]\n"[:space:]')
  440. fi
  441. echo " is failover: $(jq -r ".[$NUM].failover" ./nodejson.tmp)"
  442. if [[ $(jq -r ".[$NUM].failover" ./nodejson.tmp) == "yes" ]]; then
  443. HAS_FAILOVER="yes"
  444. fi
  445. echo " ------------"
  446. confirm
  447. if [[ $NUM -eq 0 ]]; then
  448. echo "running command: ./nmctl enrollment_key create --uses 1 --networks $NETWORK"
  449. KEY_JSON=$(./nmctl enrollment_key create --uses 1 --networks $NETWORK)
  450. KEY=$(jq -r '.token' <<< ${KEY_JSON})
  451. echo "enrollment key created: $KEY"
  452. setup_netclient
  453. else
  454. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  455. ./nmctl host add_network $HOST_ID $NETWORK
  456. fi
  457. NAME=$(jq -r ".[$NUM].name" ./nodejson.tmp)
  458. ADDRESS=$(jq -r ".[$NUM].address" ./nodejson.tmp)
  459. ADDRESS6=$(jq -r ".[$NUM].address6" ./nodejson.tmp)
  460. echo "wait 10 seconds for netclient to be ready"
  461. sleep 10
  462. NODE_ID=$(sudo cat /etc/netclient/nodes.yml | yq -r .$NETWORK.commonnode.id)
  463. echo "join complete. New node ID: $NODE_ID"
  464. if [[ $NUM -eq 0 ]]; then
  465. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  466. echo "For first join, making host a default"
  467. echo "Host ID: $HOST_ID"
  468. # set as a default host
  469. set +e
  470. ./nmctl host update $HOST_ID --default
  471. sleep 2
  472. set -e
  473. fi
  474. # create an egress if necessary
  475. if [[ $HAS_EGRESS == "yes" ]]; then
  476. echo "creating egress"
  477. ./nmctl node create_egress $NETWORK $NODE_ID $EGRESS_RANGES
  478. sleep 2
  479. fi
  480. echo "HAS INGRESS: $HAS_INGRESS"
  481. # create an ingress if necessary
  482. if [[ $HAS_INGRESS == "yes" ]]; then
  483. if [[ $HAS_FAILOVER == "yes" ]]; then
  484. echo "creating ingress and failover..."
  485. ./nmctl node create_ingress $NETWORK $NODE_ID --failover
  486. sleep 2
  487. else
  488. echo "creating ingress..."
  489. ./nmctl node create_ingress $NETWORK $NODE_ID
  490. sleep 2
  491. fi
  492. fi
  493. # relay
  494. if [[ $HAS_RELAY == "yes" ]]; then
  495. echo "cannot recreate relay; relay functionality moved to host"
  496. # ./nmctl node create_relay $NETWORK $NODE_ID $RELAY_ADDRS
  497. # sleep 2
  498. fi
  499. done
  500. echo "=================="
  501. else
  502. echo "no networks to join"
  503. fi
  504. }
  505. cat << "EOF"
  506. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  507. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  508. The Netmaker Upgrade Script: Upgrading to v0.18 so you don't have to!
  509. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  510. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  511. EOF
  512. set -e
  513. if [ $(id -u) -ne 0 ]; then
  514. echo "This script must be run as root"
  515. exit 1
  516. fi
  517. set +e
  518. echo "...installing dependencies for script"
  519. install_dependencies
  520. echo "...installing yq if necessary"
  521. install_yq
  522. set -e
  523. echo "...confirming version is correct"
  524. check_version
  525. echo "...collecting necessary server settings"
  526. collect_server_settings
  527. echo "...setup nmctl"
  528. setup_nmctl
  529. echo "...retrieving current server node settings"
  530. collect_node_settings
  531. echo "...backing up docker compose to docker-compose.yml.backup"
  532. cp /root/docker-compose.yml /root/docker-compose.yml.backup
  533. echo "...setting Caddyfile values"
  534. setup_caddy
  535. echo "...setting docker-compose values"
  536. set_compose
  537. echo "...starting containers"
  538. start_containers
  539. echo "...remove old mosquitto data"
  540. # TODO - yq is not removing volume from docker compose
  541. # docker volume rm root_mosquitto_data
  542. wait_seconds 3
  543. echo "..testing Caddy proxy"
  544. test_caddy
  545. echo "..testing Netmaker health"
  546. # TODO, implement health check
  547. # netmaker_health_check
  548. # wait_seconds 2
  549. wait_seconds 2
  550. echo "...setup netclient"
  551. join_networks
  552. echo "-----------------------------------------------------------------"
  553. echo "-----------------------------------------------------------------"
  554. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  555. echo "Visit dashboard.$SERVER_NAME to log in"
  556. echo "-----------------------------------------------------------------"
  557. echo "-----------------------------------------------------------------"