nm-quick.sh 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. #!/bin/bash
  2. CONFIG_FILE=netmaker.env
  3. # location of nm-quick.sh (usually `/root`)
  4. SCRIPT_DIR=$(dirname "$(realpath "$0")")
  5. CONFIG_PATH="$SCRIPT_DIR/$CONFIG_FILE"
  6. NM_QUICK_VERSION="0.1.1"
  7. LATEST=$(curl -s https://api.github.com/repos/gravitl/netmaker/releases/latest | grep "tag_name" | cut -d : -f 2,3 | tr -d [:space:],\")
  8. if [ $(id -u) -ne 0 ]; then
  9. echo "This script must be run as root"
  10. exit 1
  11. fi
  12. # increase the timeouts
  13. export DOCKER_CLIENT_TIMEOUT=120
  14. export COMPOSE_HTTP_TIMEOUT=120
  15. unset INSTALL_TYPE
  16. unset BUILD_TAG
  17. unset IMAGE_TAG
  18. unset NETMAKER_BASE_DOMAIN
  19. unset UPGRADE_FLAG
  20. # usage - displays usage instructions
  21. usage() {
  22. echo "nm-quick.sh v$NM_QUICK_VERSION"
  23. echo "usage: ./nm-quick.sh [-c]"
  24. echo " -c if specified, will install netmaker community version"
  25. echo " -u if specified, will upgrade netmaker to pro version"
  26. echo " -d if specified, will downgrade netmaker to community version"
  27. exit 1
  28. }
  29. # print_logo - prints the netmaker logo
  30. print_logo() {
  31. cat <<"EOF"
  32. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  33. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  34. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  35. __ __ ______ ______ __ __ ______ __ __ ______ ______
  36. /\ "-.\ \ /\ ___\ /\__ _\ /\ "-./ \ /\ __ \ /\ \/ / /\ ___\ /\ == \
  37. \ \ \-. \ \ \ __\ \/_/\ \/ \ \ \-./\ \ \ \ __ \ \ \ _"-. \ \ __\ \ \ __<
  38. \ \_\\"\_\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\ \_\ \ \_\ \_\ \ \_____\ \ \_\ \_\
  39. \/_/ \/_/ \/_____/ \/_/ \/_/ \/_/ \/_/\/_/ \/_/\/_/ \/_____/ \/_/ /_/
  40. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  41. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  42. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  43. EOF
  44. }
  45. # set_buildinfo - sets the information based on script input for how the installation should be run
  46. set_buildinfo() {
  47. BUILD_TAG=$LATEST
  48. IMAGE_TAG=$(sed 's/\//-/g' <<<"$BUILD_TAG")
  49. if [ -z "$INSTALL_TYPE" ]; then
  50. echo "-----------------------------------------------------"
  51. echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (pro)?"
  52. echo "pro will require you to create an account at https://app.netmaker.io"
  53. echo "-----------------------------------------------------"
  54. select install_option in "Community Edition" "Enterprise Edition"; do
  55. case $REPLY in
  56. 1)
  57. echo "installing Netmaker CE"
  58. INSTALL_TYPE="ce"
  59. break
  60. ;;
  61. 2)
  62. echo "installing Netmaker pro"
  63. INSTALL_TYPE="pro"
  64. break
  65. ;;
  66. *) echo "invalid option $REPLY" ;;
  67. esac
  68. done
  69. fi
  70. echo "-----------Build Options-----------------------------"
  71. echo " Pro or CE: $INSTALL_TYPE"
  72. echo " Build Tag: $BUILD_TAG"
  73. echo " Image Tag: $IMAGE_TAG"
  74. echo " Installer: v$NM_QUICK_VERSION"
  75. echo "-----------------------------------------------------"
  76. }
  77. # install_yq - install yq if not present
  78. install_yq() {
  79. if [ -f /etc/debian_version ]; then
  80. if ! command -v yq &>/dev/null; then
  81. wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_$(dpkg --print-architecture)
  82. chmod +x /usr/bin/yq
  83. fi
  84. fi
  85. set +e
  86. if ! command -v yq &>/dev/null; then
  87. set -e
  88. wget -qO /usr/bin/yq https://github.com/mikefarah/yq/releases/download/v4.31.1/yq_linux_amd64
  89. chmod +x /usr/bin/yq
  90. fi
  91. set -e
  92. if ! command -v yq &>/dev/null; then
  93. echo "failed to install yq. Please install yq and try again."
  94. echo "https://github.com/mikefarah/yq/#install"
  95. exit 1
  96. fi
  97. }
  98. # setup_netclient - adds netclient to docker-compose
  99. setup_netclient() {
  100. set +e
  101. if [ -x "$(command -v netclient)" ]; then
  102. netclient uninstall
  103. fi
  104. set -e
  105. wget -qO netclient https://github.com/gravitl/netclient/releases/download/$LATEST/netclient-linux-$ARCH
  106. chmod +x netclient
  107. ./netclient install
  108. echo "Register token: $TOKEN"
  109. sleep 2
  110. netclient register -t $TOKEN
  111. echo "waiting for netclient to become available"
  112. local found=false
  113. local file=/etc/netclient/nodes.yml
  114. for ((a = 1; a <= 90; a++)); do
  115. if [ -f "$file" ]; then
  116. found=true
  117. break
  118. fi
  119. sleep 1
  120. done
  121. if [ "$found" = false ]; then
  122. echo "Error - $file not present"
  123. exit 1
  124. fi
  125. }
  126. # configure_netclient - configures server's netclient as a default host and an ingress gateway
  127. configure_netclient() {
  128. sleep 2
  129. NODE_ID=$(sudo cat /etc/netclient/nodes.yml | yq -r .netmaker.commonnode.id)
  130. if [ "$NODE_ID" = "" ] || [ "$NODE_ID" = "null" ]; then
  131. echo "Error obtaining NODE_ID for the new network"
  132. exit 1
  133. fi
  134. echo "register complete. New node ID: $NODE_ID"
  135. HOST_ID=$(sudo cat /etc/netclient/netclient.yml | yq -r .host.id)
  136. if [ "$HOST_ID" = "" ] || [ "$HOST_ID" = "null" ]; then
  137. echo "Error obtaining HOST_ID for the new network"
  138. exit 1
  139. fi
  140. echo "making host a default"
  141. echo "Host ID: $HOST_ID"
  142. # set as a default host
  143. set +e
  144. nmctl host update $HOST_ID --default
  145. sleep 5
  146. nmctl node create_remote_access_gateway netmaker $NODE_ID
  147. #setup failOver
  148. sleep 5
  149. curl --location --request POST "https://api.${NETMAKER_BASE_DOMAIN}/api/v1/node/${NODE_ID}/failover" --header "Authorization: Bearer ${MASTER_KEY}"
  150. set -e
  151. }
  152. # setup_nmctl - pulls nmctl and makes it executable
  153. setup_nmctl() {
  154. local URL="https://github.com/gravitl/netmaker/releases/download/$LATEST/nmctl-linux-$ARCH"
  155. echo "Downloading nmctl..."
  156. wget -qO /usr/bin/nmctl "$URL"
  157. if [ ! -f /usr/bin/nmctl ]; then
  158. echo "Error downloading nmctl from '$URL'"
  159. exit 1
  160. fi
  161. chmod +x /usr/bin/nmctl
  162. echo "using server api.$NETMAKER_BASE_DOMAIN"
  163. echo "using master key $MASTER_KEY"
  164. nmctl context set default --endpoint="https://api.$NETMAKER_BASE_DOMAIN" --master_key="$MASTER_KEY"
  165. nmctl context use default
  166. RESP=$(nmctl network list)
  167. if [[ $RESP == *"unauthorized"* ]]; then
  168. echo "Unable to properly configure NMCTL, exiting..."
  169. exit 1
  170. fi
  171. }
  172. # wait_seconds - wait for the specified period of time
  173. wait_seconds() { (
  174. for ((a = 1; a <= $1; a++)); do
  175. echo ". . ."
  176. sleep 1
  177. done
  178. ); }
  179. # confirm - get user input to confirm that they want to perform the next step
  180. confirm() { (
  181. while true; do
  182. read -p 'Does everything look right? [y/n]: ' yn
  183. case $yn in
  184. [Yy]*)
  185. override="true"
  186. break
  187. ;;
  188. [Nn]*)
  189. echo "exiting..."
  190. exit 1
  191. # TODO start from the beginning instead
  192. ;;
  193. *) echo "Please answer yes or no." ;;
  194. esac
  195. done
  196. ) }
  197. save_config() { (
  198. echo "Saving the config to $CONFIG_PATH"
  199. touch "$CONFIG_PATH"
  200. if [ -n "$EMAIL" ]; then
  201. save_config_item NM_EMAIL "$EMAIL"
  202. fi
  203. if [ -n "$NETMAKER_BASE_DOMAIN" ]; then
  204. save_config_item NM_DOMAIN "$NETMAKER_BASE_DOMAIN"
  205. fi
  206. save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
  207. # version-specific entries
  208. if [ "$INSTALL_TYPE" = "pro" ]; then
  209. save_config_item NETMAKER_TENANT_ID "$TENANT_ID"
  210. save_config_item LICENSE_KEY "$LICENSE_KEY"
  211. if [ "$UPGRADE_FLAG" = "yes" ];then
  212. save_config_item METRICS_EXPORTER "on"
  213. save_config_item PROMETHEUS "on"
  214. fi
  215. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG-ee"
  216. else
  217. save_config_item METRICS_EXPORTER "off"
  218. save_config_item PROMETHEUS "off"
  219. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG"
  220. fi
  221. # copy entries from the previous config
  222. local toCopy=("SERVER_HOST" "MASTER_KEY" "MQ_USERNAME" "MQ_PASSWORD"
  223. "INSTALL_TYPE" "NODE_ID" "DNS_MODE" "NETCLIENT_AUTO_UPDATE" "API_PORT"
  224. "CORS_ALLOWED_ORIGIN" "DISPLAY_KEYS" "DATABASE" "SERVER_BROKER_ENDPOINT" "VERBOSITY"
  225. "DEBUG_MODE" "REST_BACKEND" "DISABLE_REMOTE_IP_CHECK" "TELEMETRY" "ALLOWED_EMAIL_DOMAINS" "AUTH_PROVIDER" "CLIENT_ID" "CLIENT_SECRET"
  226. "FRONTEND_URL" "AZURE_TENANT" "OIDC_ISSUER" "EXPORTER_API_PORT" "JWT_VALIDITY_DURATION" "RAC_AUTO_DISABLE" "CACHING_ENABLED" "ENDPOINT_DETECTION")
  227. for name in "${toCopy[@]}"; do
  228. save_config_item $name "${!name}"
  229. done
  230. # preserve debug entries
  231. if test -n "$NM_SKIP_BUILD"; then
  232. save_config_item NM_SKIP_BUILD "$NM_SKIP_BUILD"
  233. fi
  234. if test -n "$NM_SKIP_CLONE"; then
  235. save_config_item NM_SKIP_CLONE "$NM_SKIP_CLONE"
  236. fi
  237. if test -n "$NM_SKIP_DEPS"; then
  238. save_config_item NM_SKIP_DEPS "$NM_SKIP_DEPS"
  239. fi
  240. ); }
  241. save_config_item() { (
  242. local NAME="$1"
  243. local VALUE="$2"
  244. #echo "$NAME=$VALUE"
  245. if test -z "$VALUE"; then
  246. # load the default for empty values
  247. VALUE=$(awk -F'=' "/^$NAME/ { print \$2}" "$SCRIPT_DIR/netmaker.default.env")
  248. # trim quotes for docker
  249. VALUE=$(echo "$VALUE" | sed -E "s|^(['\"])(.*)\1$|\2|g")
  250. #echo "Default for $NAME=$VALUE"
  251. fi
  252. # TODO single quote passwords
  253. if grep -q "^$NAME=" "$CONFIG_PATH"; then
  254. # TODO escape | in the value
  255. sed -i "s|$NAME=.*|$NAME=$VALUE|" "$CONFIG_PATH"
  256. else
  257. echo "$NAME=$VALUE" >>"$CONFIG_PATH"
  258. fi
  259. ); }
  260. # install_dependencies - install necessary packages to run netmaker
  261. install_dependencies() {
  262. if test -n "$NM_SKIP_DEPS"; then
  263. return
  264. fi
  265. echo "checking dependencies..."
  266. OS=$(uname)
  267. if [ -f /etc/debian_version ]; then
  268. dependencies="git wireguard-tools dnsutils jq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  269. update_cmd='apt update'
  270. install_cmd='apt-get install -y'
  271. elif [ -f /etc/alpine-release ]; then
  272. dependencies="git wireguard jq docker.io docker-compose grep gawk"
  273. update_cmd='apk update'
  274. install_cmd='apk --update add'
  275. elif [ -f /etc/centos-release ]; then
  276. dependencies="wget git wireguard-tools jq bind-utils docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  277. update_cmd='yum updateinfo'
  278. install_cmd='yum install -y'
  279. elif [ -f /etc/amazon-linux-release ]; then
  280. dependencies="git wireguard-tools bind-utils jq docker grep gawk"
  281. update_cmd='yum updateinfo'
  282. install_cmd='yum install -y'
  283. elif [ -f /etc/fedora-release ]; then
  284. dependencies="wget git wireguard-tools bind-utils jq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  285. update_cmd='dnf updateinfo'
  286. install_cmd='dnf install -y'
  287. elif [ -f /etc/redhat-release ]; then
  288. dependencies="wget git wireguard-tools jq bind-utils docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  289. update_cmd='yum updateinfo'
  290. install_cmd='yum install -y'
  291. elif [ -f /etc/arch-release ]; then
  292. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  293. update_cmd='pacman -Sy'
  294. install_cmd='pacman -S --noconfirm'
  295. elif [ "${OS}" = "FreeBSD" ]; then
  296. dependencies="git wireguard wget jq docker.io docker-compose grep gawk"
  297. update_cmd='pkg update'
  298. install_cmd='pkg install -y'
  299. else
  300. echo "-----------------------nm-quick.sh----------------------------------------------"
  301. echo "OS supported and tested include:"
  302. echo " Debian"
  303. echo " Ubuntu"
  304. echo " Fedora"
  305. echo " Centos"
  306. echo " Redhat"
  307. echo " Amazon Linux"
  308. echo " Rocky Linux"
  309. echo " AlmaLinux"
  310. echo "Your OS system is not in the support list, please chanage to an OS in the list"
  311. echo "--------------------------------------------------------------------------------"
  312. exit 1
  313. fi
  314. if [ -z "${install_cmd}" ]; then
  315. echo "OS unsupported for automatic dependency install"
  316. # TODO shouldnt exit, check if deps available, if not
  317. # ask the user to install manually and continue when ready
  318. exit 1
  319. fi
  320. # TODO add other supported architectures
  321. ARCH=$(uname -m)
  322. if [ "$ARCH" = "x86_64" ]; then
  323. ARCH=amd64
  324. elif [ "$ARCH" = "aarch64" ]; then
  325. ARCH=arm64
  326. else
  327. echo "Unsupported architechure"
  328. # exit 1
  329. fi
  330. # setup docker repository
  331. if [ "$(cat /etc/*-release |grep ubuntu |wc -l)" -gt 0 ]; then
  332. apt update
  333. apt install -y ca-certificates curl
  334. install -m 0755 -d /etc/apt/keyrings
  335. curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
  336. chmod a+r /etc/apt/keyrings/docker.asc
  337. echo \
  338. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  339. $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  340. tee /etc/apt/sources.list.d/docker.list > /dev/null
  341. apt update
  342. elif [ "$(cat /etc/*-release |grep debian |wc -l)" -gt 0 ]; then
  343. apt update
  344. apt install -y ca-certificates curl
  345. install -m 0755 -d /etc/apt/keyrings
  346. curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
  347. chmod a+r /etc/apt/keyrings/docker.asc
  348. echo \
  349. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  350. $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  351. tee /etc/apt/sources.list.d/docker.list > /dev/null
  352. apt update
  353. elif [ -f /etc/fedora-release ]; then
  354. dnf -y install dnf-plugins-core
  355. dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
  356. elif [ -f /etc/centos-release ]; then
  357. yum install -y yum-utils
  358. yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  359. if [ "$(cat /etc/*-release |grep 'release 8' |wc -l)" -gt 0 ]; then
  360. yum install -y elrepo-release epel-release
  361. elif [ "$(cat /etc/*-release |grep 'release 7' |wc -l)" -gt 0 ]; then
  362. yum install -y elrepo-release epel-release
  363. yum install -y yum-plugin-elrepo
  364. fi
  365. elif [ -f /etc/redhat-release ]; then
  366. yum install -y yum-utils
  367. yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  368. if [ "$(cat /etc/*-release |grep 'release 8' |wc -l)" -gt 0 ]; then
  369. yum install -y elrepo-release epel-release
  370. fi
  371. fi
  372. set -- $dependencies
  373. ${update_cmd}
  374. while [ -n "$1" ]; do
  375. if [ "${OS}" = "FreeBSD" ]; then
  376. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  377. if [ "$is_installed" != "" ]; then
  378. echo " " $1 is installed
  379. else
  380. echo " " $1 is not installed. Attempting install.
  381. ${install_cmd} $1
  382. sleep 5
  383. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  384. if [ "$is_installed" != "" ]; then
  385. echo " " $1 is installed
  386. elif [ -x "$(command -v $1)" ]; then
  387. echo " " $1 is installed
  388. else
  389. echo " " FAILED TO INSTALL $1
  390. echo " " This may break functionality.
  391. fi
  392. fi
  393. else
  394. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  395. is_installed=$(opkg list-installed $1 | grep $1)
  396. elif [ -f /etc/debian_version ]; then
  397. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  398. else
  399. is_installed=$(yum list installed | grep $1)
  400. fi
  401. if [ "${is_installed}" != "" ]; then
  402. echo " " $1 is installed
  403. else
  404. echo " " $1 is not installed. Attempting install.
  405. ${install_cmd} $1
  406. sleep 5
  407. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  408. is_installed=$(opkg list-installed $1 | grep $1)
  409. elif [ -f /etc/debian_version ]; then
  410. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  411. else
  412. is_installed=$(yum list installed | grep $1)
  413. fi
  414. if [ "${is_installed}" != "" ]; then
  415. echo " " $1 is installed
  416. elif [ -x "$(command -v $1)" ]; then
  417. echo " " $1 is installed
  418. else
  419. echo " " FAILED TO INSTALL $1
  420. echo " " This may break functionality.
  421. fi
  422. fi
  423. fi
  424. shift
  425. done
  426. # Startup docker daemon for OS which does not start it automatically
  427. if [ -f /etc/fedora-release ]; then
  428. systemctl start docker
  429. systemctl enable docker
  430. elif [ -f /etc/amazon-linux-release ]; then
  431. systemctl start docker
  432. systemctl enable docker
  433. usermod -a -G docker ec2-user
  434. mkdir -p /usr/local/lib/docker/cli-plugins
  435. curl -sL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-$(uname -m) \
  436. -o /usr/local/lib/docker/cli-plugins/docker-compose
  437. chown root:root /usr/local/lib/docker/cli-plugins/docker-compose
  438. chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
  439. elif [ -f /etc/centos-release ]; then
  440. systemctl start docker
  441. systemctl enable docker
  442. elif [ -f /etc/redhat-release ]; then
  443. systemctl start docker
  444. systemctl enable docker
  445. fi
  446. echo "-----------------------------------------------------"
  447. echo "dependency check complete"
  448. echo "-----------------------------------------------------"
  449. }
  450. set -e
  451. # set_install_vars - sets the variables that will be used throughout installation
  452. set_install_vars() {
  453. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  454. if [ "$IP_ADDR" = "" ]; then
  455. IP_ADDR=$(curl -s ifconfig.me)
  456. fi
  457. if [ "$NETMAKER_BASE_DOMAIN" = "" ]; then
  458. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  459. fi
  460. SERVER_HOST=$IP_ADDR
  461. if test -z "$MASTER_KEY"; then
  462. MASTER_KEY=$(
  463. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  464. echo ''
  465. )
  466. fi
  467. DOMAIN_TYPE=""
  468. echo "-----------------------------------------------------"
  469. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  470. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_HOST"
  471. echo "IMPORTANT: Due to the high volume of requests, the auto-generated domain has been rate-limited by the certificate provider."
  472. echo "For this reason, we STRONGLY RECOMMEND using your own domain. Using the auto-generated domain may lead to a failed installation due to rate limiting."
  473. echo "-----------------------------------------------------"
  474. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  475. case $REPLY in
  476. 1)
  477. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  478. DOMAIN_TYPE="auto"
  479. break
  480. ;;
  481. 2)
  482. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_HOST first): " domain
  483. NETMAKER_BASE_DOMAIN=$domain
  484. echo "using $NETMAKER_BASE_DOMAIN"
  485. DOMAIN_TYPE="custom"
  486. break
  487. ;;
  488. *) echo "invalid option $REPLY" ;;
  489. esac
  490. done
  491. wait_seconds 2
  492. echo "-----------------------------------------------------"
  493. echo "The following subdomains will be used:"
  494. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  495. echo " api.$NETMAKER_BASE_DOMAIN"
  496. echo " broker.$NETMAKER_BASE_DOMAIN"
  497. if [ "$UPGRADE_FLAG" = "yes" ]; then
  498. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  499. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  500. echo " grafana.$NETMAKER_BASE_DOMAIN"
  501. fi
  502. echo "-----------------------------------------------------"
  503. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  504. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_HOST"
  505. confirm
  506. fi
  507. wait_seconds 1
  508. unset GET_EMAIL
  509. while [ -z "$GET_EMAIL" ]; do
  510. read -p "Email Address for Domain Registration: " GET_EMAIL
  511. done
  512. EMAIL="$GET_EMAIL"
  513. wait_seconds 1
  514. unset GET_MQ_USERNAME
  515. unset GET_MQ_PASSWORD
  516. unset CONFIRM_MQ_PASSWORD
  517. echo "Enter Credentials For MQ..."
  518. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  519. if [ -z "$GET_MQ_USERNAME" ]; then
  520. echo "using default username for mq"
  521. MQ_USERNAME="netmaker"
  522. else
  523. MQ_USERNAME="$GET_MQ_USERNAME"
  524. fi
  525. if test -z "$MQ_PASSWORD"; then
  526. MQ_PASSWORD=$(
  527. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  528. echo ''
  529. )
  530. fi
  531. select domain_option in "Auto Generated / Config Password" "Input Your Own Password"; do
  532. case $REPLY in
  533. 1)
  534. echo "using random password for mq"
  535. break
  536. ;;
  537. 2)
  538. while true; do
  539. echo "Enter your Password For MQ: "
  540. read -s GET_MQ_PASSWORD
  541. echo "Enter your password again to confirm: "
  542. read -s CONFIRM_MQ_PASSWORD
  543. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  544. echo "wrong password entered, try again..."
  545. continue
  546. fi
  547. MQ_PASSWORD="$GET_MQ_PASSWORD"
  548. echo "MQ Password Saved Successfully!!"
  549. break
  550. done
  551. break
  552. ;;
  553. *) echo "invalid option $REPLY" ;;
  554. esac
  555. done
  556. wait_seconds 2
  557. echo "-----------------------------------------------------------------"
  558. echo " SETUP ARGUMENTS"
  559. echo "-----------------------------------------------------------------"
  560. echo " domain: $NETMAKER_BASE_DOMAIN"
  561. echo " email: $EMAIL"
  562. echo " public ip: $SERVER_HOST"
  563. echo "-----------------------------------------------------------------"
  564. echo "Confirm Settings for Installation"
  565. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  566. confirm
  567. }
  568. # install_netmaker - sets the config files and starts docker-compose
  569. install_netmaker() {
  570. echo "-----------------------------------------------------------------"
  571. echo "Beginning installation..."
  572. echo "-----------------------------------------------------------------"
  573. wait_seconds 3
  574. echo "Pulling config files..."
  575. local BASE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG"
  576. local COMPOSE_URL="$BASE_URL/compose/docker-compose.yml"
  577. local CADDY_URL="$BASE_URL/docker/Caddyfile"
  578. if [ "$INSTALL_TYPE" = "pro" ]; then
  579. local COMPOSE_OVERRIDE_URL="$BASE_URL/compose/docker-compose.pro.yml"
  580. local CADDY_URL="$BASE_URL/docker/Caddyfile-pro"
  581. fi
  582. wget -qO "$SCRIPT_DIR"/docker-compose.yml $COMPOSE_URL
  583. if [ "$UPGRADE_FLAG" = "yes" ]; then
  584. wget -qO "$SCRIPT_DIR"/docker-compose.override.yml $COMPOSE_OVERRIDE_URL
  585. elif [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
  586. rm -f "$SCRIPT_DIR"/docker-compose.override.yml
  587. fi
  588. wget -qO "$SCRIPT_DIR"/Caddyfile "$CADDY_URL"
  589. wget -qO "$SCRIPT_DIR"/netmaker.default.env "$BASE_URL/scripts/netmaker.default.env"
  590. wget -qO "$SCRIPT_DIR"/mosquitto.conf "$BASE_URL/docker/mosquitto.conf"
  591. wget -qO "$SCRIPT_DIR"/wait.sh "$BASE_URL/docker/wait.sh"
  592. chmod +x "$SCRIPT_DIR"/wait.sh
  593. mkdir -p /etc/netmaker
  594. # link .env to the user config
  595. ln -fs "$SCRIPT_DIR/netmaker.env" "$SCRIPT_DIR/.env"
  596. save_config
  597. echo "Starting containers..."
  598. # start docker and rebuild containers / networks
  599. cd "${SCRIPT_DIR}"
  600. if [ -f /etc/debian_version ]; then
  601. docker compose up -d --force-recreate
  602. elif [ -f /etc/fedora-release ]; then
  603. docker compose up -d --force-recreate
  604. elif [ -f /etc/amazon-linux-release ]; then
  605. docker compose up -d --force-recreate
  606. elif [ -f /etc/centos-release ]; then
  607. docker compose up -d --force-recreate
  608. elif [ -f /etc/redhat-release ]; then
  609. docker compose up -d --force-recreate
  610. else
  611. docker-compose up -d --force-recreate
  612. fi
  613. cd -
  614. wait_seconds 2
  615. }
  616. # test_connection - tests to make sure Caddy has proper SSL certs
  617. test_connection() {
  618. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  619. for i in 1 2 3 4 5 6 7 8; do
  620. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  621. if [[ "$i" == 8 ]]; then
  622. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  623. echo " Exiting..."
  624. exit 1
  625. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  626. echo " Certificates not yet configured, retrying..."
  627. elif [[ "$curlresponse" == *"left intact"* ]]; then
  628. echo " Certificates ok"
  629. break
  630. else
  631. secs=$(($i * 5 + 10))
  632. echo " Issue establishing connection...retrying in $secs seconds..."
  633. fi
  634. sleep $secs
  635. done
  636. }
  637. # setup_mesh - sets up a default mesh network on the server
  638. setup_mesh() {
  639. wait_seconds 5
  640. local networkCount=$(nmctl network list -o json | jq '. | length')
  641. # add a network if none present
  642. if [ "$networkCount" -lt 1 ]; then
  643. echo "Creating netmaker network (10.101.0.0/16)"
  644. # TODO causes "Error Status: 400 Response: {"Code":400,"Message":"could not find any records"}"
  645. nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16
  646. wait_seconds 5
  647. fi
  648. echo "Obtaining a netmaker enrollment key..."
  649. local tokenJson=$(nmctl enrollment_key create --tags netmaker --unlimited --networks netmaker)
  650. TOKEN=$(jq -r '.token' <<<${tokenJson})
  651. if test -z "$TOKEN"; then
  652. echo "Error creating an enrollment key"
  653. exit 1
  654. else
  655. echo "Enrollment key ready"
  656. fi
  657. wait_seconds 3
  658. }
  659. # print_success - prints a success message upon completion
  660. print_success() {
  661. echo "-----------------------------------------------------------------"
  662. echo "-----------------------------------------------------------------"
  663. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  664. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  665. echo "-----------------------------------------------------------------"
  666. echo "-----------------------------------------------------------------"
  667. }
  668. cleanup() {
  669. # remove the existing netclient's instance from the existing network
  670. if ! command -v netclient >/dev/null 2>&1; then
  671. return
  672. fi
  673. if command -v nmctl >/dev/null 2>&1; then
  674. local node_id=$(netclient list | jq '.[0].node_id' 2>/dev/null)
  675. # trim doublequotes
  676. node_id="${node_id//\"/}"
  677. if test -n "$node_id"; then
  678. echo "De-registering the existing netclient..."
  679. nmctl node delete netmaker $node_id >/dev/null 2>&1
  680. fi
  681. fi
  682. stop_services
  683. }
  684. stop_services(){
  685. echo "Stopping all containers, this will take a while please wait..."
  686. local containers=("mq" "netmaker-ui" "coredns" "turn" "caddy" "netmaker" "netmaker-exporter" "prometheus" "grafana")
  687. for name in "${containers[@]}"; do
  688. local running=$(docker ps | grep -w "$name")
  689. local exists=$(docker ps -a | grep -w "$name")
  690. if test -n "$running"; then
  691. docker stop "$name" 1>/dev/null
  692. fi
  693. if test -n "$exists"; then
  694. docker rm "$name" 1>/dev/null
  695. fi
  696. done
  697. }
  698. upgrade() {
  699. print_logo
  700. unset IMAGE_TAG
  701. unset BUILD_TAG
  702. IMAGE_TAG=$UI_IMAGE_TAG
  703. semver=$(chsv_check_version_ex "$UI_IMAGE_TAG")
  704. if [[ ! "$semver" ]]; then
  705. BUILD_TAG=$LATEST
  706. else
  707. BUILD_TAG=$UI_IMAGE_TAG
  708. fi
  709. echo "-----------------------------------------------------"
  710. echo "Provide Details for pro installation:"
  711. echo " 1. Log into https://app.netmaker.io"
  712. echo " 2. follow instructions to get a license at: https://docs.netmaker.io/pro/pro-setup.html"
  713. echo " 3. Retrieve License and Tenant ID"
  714. echo "-----------------------------------------------------"
  715. unset LICENSE_KEY
  716. while [ -z "$LICENSE_KEY" ]; do
  717. read -p "License Key: " LICENSE_KEY
  718. done
  719. unset TENANT_ID
  720. while [ -z ${TENANT_ID} ]; do
  721. read -p "Tenant ID: " TENANT_ID
  722. done
  723. save_config
  724. # start docker and rebuild containers / networks
  725. stop_services
  726. install_netmaker
  727. }
  728. downgrade () {
  729. print_logo
  730. unset IMAGE_TAG
  731. unset BUILD_TAG
  732. IMAGE_TAG=$UI_IMAGE_TAG
  733. semver=$(chsv_check_version_ex "$UI_IMAGE_TAG")
  734. if [[ ! "$semver" ]]; then
  735. BUILD_TAG=$LATEST
  736. else
  737. BUILD_TAG=$UI_IMAGE_TAG
  738. fi
  739. save_config
  740. if [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
  741. rm -f "$SCRIPT_DIR"/docker-compose.override.yml
  742. fi
  743. # start docker and rebuild containers / networks
  744. stop_services
  745. install_netmaker
  746. }
  747. function chsv_check_version() {
  748. if [[ $1 =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]]; then
  749. echo "$1"
  750. else
  751. echo ""
  752. fi
  753. }
  754. function chsv_check_version_ex() {
  755. if [[ $1 =~ ^v.+$ ]]; then
  756. chsv_check_version "${1:1}"
  757. else
  758. chsv_check_version "${1}"
  759. fi
  760. }
  761. main (){
  762. # read the config
  763. if [ -f "$CONFIG_PATH" ]; then
  764. echo "Using config: $CONFIG_PATH"
  765. source "$CONFIG_PATH"
  766. fi
  767. INSTALL_TYPE="pro"
  768. while getopts :cudv flag; do
  769. case "${flag}" in
  770. c)
  771. INSTALL_TYPE="ce"
  772. ;;
  773. u)
  774. echo "upgrading to pro version..."
  775. INSTALL_TYPE="pro"
  776. UPGRADE_FLAG="yes"
  777. upgrade
  778. exit 0
  779. ;;
  780. d)
  781. echo "downgrading to community version..."
  782. INSTALL_TYPE="ce"
  783. downgrade
  784. exit 0
  785. ;;
  786. v)
  787. usage
  788. exit 0
  789. ;;
  790. esac
  791. done
  792. # 1. print netmaker logo
  793. print_logo
  794. # 2. setup the build instructions
  795. set_buildinfo
  796. set +e
  797. # 3. install necessary packages
  798. install_dependencies
  799. # 4. install yq if necessary
  800. install_yq
  801. set -e
  802. # 6. get user input for variables
  803. set_install_vars
  804. set +e
  805. cleanup
  806. set -e
  807. # 7. get and set config files, startup docker-compose
  808. install_netmaker
  809. set +e
  810. # 8. make sure Caddy certs are working
  811. test_connection
  812. # 9. install the netmaker CLI
  813. setup_nmctl
  814. # 10. create a default mesh network for netmaker
  815. setup_mesh
  816. set -e
  817. # 11. add netclient to docker-compose and start it up
  818. setup_netclient
  819. # 12. make the netclient a default host and ingress gw
  820. configure_netclient
  821. # 13. print success message
  822. print_success
  823. }
  824. main "${@}"