nm-upgrade.sh 26 KB

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