nm-quick.sh 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. save_config_item FRONTEND_URL "dashboard.$NETMAKER_BASE_DOMAIN"
  206. fi
  207. save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
  208. # version-specific entries
  209. if [ "$INSTALL_TYPE" = "pro" ]; then
  210. save_config_item NETMAKER_TENANT_ID "$TENANT_ID"
  211. save_config_item LICENSE_KEY "$LICENSE_KEY"
  212. if [ "$UPGRADE_FLAG" = "yes" ];then
  213. save_config_item METRICS_EXPORTER "on"
  214. save_config_item PROMETHEUS "on"
  215. fi
  216. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG-ee"
  217. else
  218. save_config_item METRICS_EXPORTER "off"
  219. save_config_item PROMETHEUS "off"
  220. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG"
  221. fi
  222. # copy entries from the previous config
  223. local toCopy=("SERVER_HOST" "MASTER_KEY" "MQ_USERNAME" "MQ_PASSWORD"
  224. "INSTALL_TYPE" "NODE_ID" "DNS_MODE" "NETCLIENT_AUTO_UPDATE" "API_PORT"
  225. "CORS_ALLOWED_ORIGIN" "DISPLAY_KEYS" "DATABASE" "SERVER_BROKER_ENDPOINT" "VERBOSITY"
  226. "DEBUG_MODE" "REST_BACKEND" "DISABLE_REMOTE_IP_CHECK" "TELEMETRY" "ALLOWED_EMAIL_DOMAINS" "AUTH_PROVIDER" "CLIENT_ID" "CLIENT_SECRET"
  227. "FRONTEND_URL" "AZURE_TENANT" "OIDC_ISSUER" "EXPORTER_API_PORT" "JWT_VALIDITY_DURATION" "RAC_AUTO_DISABLE" "CACHING_ENABLED" "ENDPOINT_DETECTION")
  228. for name in "${toCopy[@]}"; do
  229. save_config_item $name "${!name}"
  230. done
  231. # preserve debug entries
  232. if test -n "$NM_SKIP_BUILD"; then
  233. save_config_item NM_SKIP_BUILD "$NM_SKIP_BUILD"
  234. fi
  235. if test -n "$NM_SKIP_CLONE"; then
  236. save_config_item NM_SKIP_CLONE "$NM_SKIP_CLONE"
  237. fi
  238. if test -n "$NM_SKIP_DEPS"; then
  239. save_config_item NM_SKIP_DEPS "$NM_SKIP_DEPS"
  240. fi
  241. ); }
  242. save_config_item() { (
  243. local NAME="$1"
  244. local VALUE="$2"
  245. #echo "$NAME=$VALUE"
  246. if test -z "$VALUE"; then
  247. # load the default for empty values
  248. VALUE=$(awk -F'=' "/^$NAME/ { print \$2}" "$SCRIPT_DIR/netmaker.default.env")
  249. # trim quotes for docker
  250. VALUE=$(echo "$VALUE" | sed -E "s|^(['\"])(.*)\1$|\2|g")
  251. #echo "Default for $NAME=$VALUE"
  252. fi
  253. # TODO single quote passwords
  254. if grep -q "^$NAME=" "$CONFIG_PATH"; then
  255. # TODO escape | in the value
  256. sed -i "s|$NAME=.*|$NAME=$VALUE|" "$CONFIG_PATH"
  257. else
  258. echo "$NAME=$VALUE" >>"$CONFIG_PATH"
  259. fi
  260. ); }
  261. # install_dependencies - install necessary packages to run netmaker
  262. install_dependencies() {
  263. if test -n "$NM_SKIP_DEPS"; then
  264. return
  265. fi
  266. echo "checking dependencies..."
  267. OS=$(uname)
  268. if [ -f /etc/debian_version ]; then
  269. dependencies="git wireguard-tools dnsutils jq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  270. update_cmd='apt update'
  271. install_cmd='apt-get install -y'
  272. elif [ -f /etc/alpine-release ]; then
  273. dependencies="git wireguard jq docker.io docker-compose grep gawk"
  274. update_cmd='apk update'
  275. install_cmd='apk --update add'
  276. elif [ -f /etc/centos-release ]; then
  277. dependencies="wget git wireguard-tools jq bind-utils docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  278. update_cmd='yum updateinfo'
  279. install_cmd='yum install -y'
  280. elif [ -f /etc/amazon-linux-release ]; then
  281. dependencies="git wireguard-tools bind-utils jq docker grep gawk"
  282. update_cmd='yum updateinfo'
  283. install_cmd='yum install -y'
  284. elif [ -f /etc/fedora-release ]; then
  285. dependencies="wget git wireguard-tools bind-utils jq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  286. update_cmd='dnf updateinfo'
  287. install_cmd='dnf install -y'
  288. elif [ -f /etc/redhat-release ]; then
  289. dependencies="wget git wireguard-tools jq bind-utils docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  290. update_cmd='yum updateinfo'
  291. install_cmd='yum install -y'
  292. elif [ -f /etc/arch-release ]; then
  293. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  294. update_cmd='pacman -Sy'
  295. install_cmd='pacman -S --noconfirm'
  296. elif [ "${OS}" = "FreeBSD" ]; then
  297. dependencies="git wireguard wget jq docker.io docker-compose grep gawk"
  298. update_cmd='pkg update'
  299. install_cmd='pkg install -y'
  300. else
  301. echo "-----------------------nm-quick.sh----------------------------------------------"
  302. echo "OS supported and tested include:"
  303. echo " Debian"
  304. echo " Ubuntu"
  305. echo " Fedora"
  306. echo " Centos"
  307. echo " Redhat"
  308. echo " Amazon Linux"
  309. echo " Rocky Linux"
  310. echo " AlmaLinux"
  311. echo "Your OS system is not in the support list, please chanage to an OS in the list"
  312. echo "--------------------------------------------------------------------------------"
  313. exit 1
  314. fi
  315. if [ -z "${install_cmd}" ]; then
  316. echo "OS unsupported for automatic dependency install"
  317. # TODO shouldnt exit, check if deps available, if not
  318. # ask the user to install manually and continue when ready
  319. exit 1
  320. fi
  321. # TODO add other supported architectures
  322. ARCH=$(uname -m)
  323. if [ "$ARCH" = "x86_64" ]; then
  324. ARCH=amd64
  325. elif [ "$ARCH" = "aarch64" ]; then
  326. ARCH=arm64
  327. else
  328. echo "Unsupported architechure"
  329. # exit 1
  330. fi
  331. # setup docker repository
  332. if [ "$(cat /etc/*-release |grep ubuntu |wc -l)" -gt 0 ]; then
  333. apt update
  334. apt install -y ca-certificates curl
  335. install -m 0755 -d /etc/apt/keyrings
  336. curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
  337. chmod a+r /etc/apt/keyrings/docker.asc
  338. echo \
  339. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  340. $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  341. tee /etc/apt/sources.list.d/docker.list > /dev/null
  342. apt update
  343. elif [ "$(cat /etc/*-release |grep debian |wc -l)" -gt 0 ]; then
  344. apt update
  345. apt install -y ca-certificates curl
  346. install -m 0755 -d /etc/apt/keyrings
  347. curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
  348. chmod a+r /etc/apt/keyrings/docker.asc
  349. echo \
  350. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  351. $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  352. tee /etc/apt/sources.list.d/docker.list > /dev/null
  353. apt update
  354. elif [ -f /etc/fedora-release ]; then
  355. dnf -y install dnf-plugins-core
  356. dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
  357. elif [ -f /etc/centos-release ]; then
  358. yum install -y yum-utils
  359. yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  360. if [ "$(cat /etc/*-release |grep 'release 8' |wc -l)" -gt 0 ]; then
  361. yum install -y elrepo-release epel-release
  362. elif [ "$(cat /etc/*-release |grep 'release 7' |wc -l)" -gt 0 ]; then
  363. yum install -y elrepo-release epel-release
  364. yum install -y yum-plugin-elrepo
  365. fi
  366. elif [ -f /etc/redhat-release ]; then
  367. yum install -y yum-utils
  368. yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  369. if [ "$(cat /etc/*-release |grep 'release 8' |wc -l)" -gt 0 ]; then
  370. yum install -y elrepo-release epel-release
  371. fi
  372. fi
  373. set -- $dependencies
  374. ${update_cmd}
  375. while [ -n "$1" ]; do
  376. if [ "${OS}" = "FreeBSD" ]; then
  377. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  378. if [ "$is_installed" != "" ]; then
  379. echo " " $1 is installed
  380. else
  381. echo " " $1 is not installed. Attempting install.
  382. ${install_cmd} $1
  383. sleep 5
  384. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  385. if [ "$is_installed" != "" ]; then
  386. echo " " $1 is installed
  387. elif [ -x "$(command -v $1)" ]; then
  388. echo " " $1 is installed
  389. else
  390. echo " " FAILED TO INSTALL $1
  391. echo " " This may break functionality.
  392. fi
  393. fi
  394. else
  395. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  396. is_installed=$(opkg list-installed $1 | grep $1)
  397. elif [ -f /etc/debian_version ]; then
  398. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  399. else
  400. is_installed=$(yum list installed | grep $1)
  401. fi
  402. if [ "${is_installed}" != "" ]; then
  403. echo " " $1 is installed
  404. else
  405. echo " " $1 is not installed. Attempting install.
  406. ${install_cmd} $1
  407. sleep 5
  408. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  409. is_installed=$(opkg list-installed $1 | grep $1)
  410. elif [ -f /etc/debian_version ]; then
  411. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  412. else
  413. is_installed=$(yum list installed | grep $1)
  414. fi
  415. if [ "${is_installed}" != "" ]; then
  416. echo " " $1 is installed
  417. elif [ -x "$(command -v $1)" ]; then
  418. echo " " $1 is installed
  419. else
  420. echo " " FAILED TO INSTALL $1
  421. echo " " This may break functionality.
  422. fi
  423. fi
  424. fi
  425. shift
  426. done
  427. # Startup docker daemon for OS which does not start it automatically
  428. if [ -f /etc/fedora-release ]; then
  429. systemctl start docker
  430. systemctl enable docker
  431. elif [ -f /etc/amazon-linux-release ]; then
  432. systemctl start docker
  433. systemctl enable docker
  434. usermod -a -G docker ec2-user
  435. mkdir -p /usr/local/lib/docker/cli-plugins
  436. curl -sL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-$(uname -m) \
  437. -o /usr/local/lib/docker/cli-plugins/docker-compose
  438. chown root:root /usr/local/lib/docker/cli-plugins/docker-compose
  439. chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
  440. elif [ -f /etc/centos-release ]; then
  441. systemctl start docker
  442. systemctl enable docker
  443. elif [ -f /etc/redhat-release ]; then
  444. systemctl start docker
  445. systemctl enable docker
  446. fi
  447. echo "-----------------------------------------------------"
  448. echo "dependency check complete"
  449. echo "-----------------------------------------------------"
  450. }
  451. set -e
  452. # set_install_vars - sets the variables that will be used throughout installation
  453. set_install_vars() {
  454. IP_ADDR=$(dig -4 myip.opendns.com @resolver1.opendns.com +short)
  455. if [ "$IP_ADDR" = "" ]; then
  456. IP_ADDR=$(curl -s ifconfig.me)
  457. fi
  458. if [ "$NETMAKER_BASE_DOMAIN" = "" ]; then
  459. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  460. fi
  461. SERVER_HOST=$IP_ADDR
  462. if test -z "$MASTER_KEY"; then
  463. MASTER_KEY=$(
  464. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  465. echo ''
  466. )
  467. fi
  468. DOMAIN_TYPE=""
  469. echo "-----------------------------------------------------"
  470. echo "Would you like to use your own domain for netmaker, or an auto-generated domain?"
  471. echo "To use your own domain, add a Wildcard DNS record (e.x: *.netmaker.example.com) pointing to $SERVER_HOST"
  472. echo "IMPORTANT: Due to the high volume of requests, the auto-generated domain has been rate-limited by the certificate provider."
  473. 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."
  474. echo "-----------------------------------------------------"
  475. select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
  476. case $REPLY in
  477. 1)
  478. echo "using $NETMAKER_BASE_DOMAIN for base domain"
  479. DOMAIN_TYPE="auto"
  480. break
  481. ;;
  482. 2)
  483. read -p "Enter Custom Domain (make sure *.domain points to $SERVER_HOST first): " domain
  484. NETMAKER_BASE_DOMAIN=$domain
  485. echo "using $NETMAKER_BASE_DOMAIN"
  486. DOMAIN_TYPE="custom"
  487. break
  488. ;;
  489. *) echo "invalid option $REPLY" ;;
  490. esac
  491. done
  492. wait_seconds 2
  493. echo "-----------------------------------------------------"
  494. echo "The following subdomains will be used:"
  495. echo " dashboard.$NETMAKER_BASE_DOMAIN"
  496. echo " api.$NETMAKER_BASE_DOMAIN"
  497. echo " broker.$NETMAKER_BASE_DOMAIN"
  498. if [ "$UPGRADE_FLAG" = "yes" ]; then
  499. echo " prometheus.$NETMAKER_BASE_DOMAIN"
  500. echo " netmaker-exporter.$NETMAKER_BASE_DOMAIN"
  501. echo " grafana.$NETMAKER_BASE_DOMAIN"
  502. fi
  503. echo "-----------------------------------------------------"
  504. if [[ "$DOMAIN_TYPE" == "custom" ]]; then
  505. echo "before continuing, confirm DNS is configured correctly, with records pointing to $SERVER_HOST"
  506. confirm
  507. fi
  508. wait_seconds 1
  509. unset GET_EMAIL
  510. while [ -z "$GET_EMAIL" ]; do
  511. read -p "Email Address for Domain Registration: " GET_EMAIL
  512. done
  513. EMAIL="$GET_EMAIL"
  514. wait_seconds 1
  515. unset GET_MQ_USERNAME
  516. unset GET_MQ_PASSWORD
  517. unset CONFIRM_MQ_PASSWORD
  518. echo "Enter Credentials For MQ..."
  519. read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
  520. if [ -z "$GET_MQ_USERNAME" ]; then
  521. echo "using default username for mq"
  522. MQ_USERNAME="netmaker"
  523. else
  524. MQ_USERNAME="$GET_MQ_USERNAME"
  525. fi
  526. if test -z "$MQ_PASSWORD"; then
  527. MQ_PASSWORD=$(
  528. tr -dc A-Za-z0-9 </dev/urandom | head -c 30
  529. echo ''
  530. )
  531. fi
  532. select domain_option in "Auto Generated / Config Password" "Input Your Own Password"; do
  533. case $REPLY in
  534. 1)
  535. echo "using random password for mq"
  536. break
  537. ;;
  538. 2)
  539. while true; do
  540. echo "Enter your Password For MQ: "
  541. read -s GET_MQ_PASSWORD
  542. echo "Enter your password again to confirm: "
  543. read -s CONFIRM_MQ_PASSWORD
  544. if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
  545. echo "wrong password entered, try again..."
  546. continue
  547. fi
  548. MQ_PASSWORD="$GET_MQ_PASSWORD"
  549. echo "MQ Password Saved Successfully!!"
  550. break
  551. done
  552. break
  553. ;;
  554. *) echo "invalid option $REPLY" ;;
  555. esac
  556. done
  557. wait_seconds 2
  558. echo "-----------------------------------------------------------------"
  559. echo " SETUP ARGUMENTS"
  560. echo "-----------------------------------------------------------------"
  561. echo " domain: $NETMAKER_BASE_DOMAIN"
  562. echo " email: $EMAIL"
  563. echo " public ip: $SERVER_HOST"
  564. echo "-----------------------------------------------------------------"
  565. echo "Confirm Settings for Installation"
  566. echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
  567. confirm
  568. }
  569. # install_netmaker - sets the config files and starts docker-compose
  570. install_netmaker() {
  571. echo "-----------------------------------------------------------------"
  572. echo "Beginning installation..."
  573. echo "-----------------------------------------------------------------"
  574. wait_seconds 3
  575. echo "Pulling config files..."
  576. local BASE_URL="https://raw.githubusercontent.com/gravitl/netmaker/$BUILD_TAG"
  577. local COMPOSE_URL="$BASE_URL/compose/docker-compose.yml"
  578. local CADDY_URL="$BASE_URL/docker/Caddyfile"
  579. if [ "$INSTALL_TYPE" = "pro" ]; then
  580. local COMPOSE_OVERRIDE_URL="$BASE_URL/compose/docker-compose.pro.yml"
  581. local CADDY_URL="$BASE_URL/docker/Caddyfile-pro"
  582. fi
  583. wget -qO "$SCRIPT_DIR"/docker-compose.yml $COMPOSE_URL
  584. if [ "$UPGRADE_FLAG" = "yes" ]; then
  585. wget -qO "$SCRIPT_DIR"/docker-compose.override.yml $COMPOSE_OVERRIDE_URL
  586. elif [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
  587. rm -f "$SCRIPT_DIR"/docker-compose.override.yml
  588. fi
  589. wget -qO "$SCRIPT_DIR"/Caddyfile "$CADDY_URL"
  590. wget -qO "$SCRIPT_DIR"/netmaker.default.env "$BASE_URL/scripts/netmaker.default.env"
  591. wget -qO "$SCRIPT_DIR"/mosquitto.conf "$BASE_URL/docker/mosquitto.conf"
  592. wget -qO "$SCRIPT_DIR"/wait.sh "$BASE_URL/docker/wait.sh"
  593. chmod +x "$SCRIPT_DIR"/wait.sh
  594. mkdir -p /etc/netmaker
  595. # link .env to the user config
  596. ln -fs "$SCRIPT_DIR/netmaker.env" "$SCRIPT_DIR/.env"
  597. save_config
  598. echo "Starting containers..."
  599. # start docker and rebuild containers / networks
  600. cd "${SCRIPT_DIR}"
  601. if [ -f /etc/debian_version ]; then
  602. docker compose up -d --force-recreate
  603. elif [ -f /etc/fedora-release ]; then
  604. docker compose up -d --force-recreate
  605. elif [ -f /etc/amazon-linux-release ]; then
  606. docker compose up -d --force-recreate
  607. elif [ -f /etc/centos-release ]; then
  608. docker compose up -d --force-recreate
  609. elif [ -f /etc/redhat-release ]; then
  610. docker compose up -d --force-recreate
  611. else
  612. docker-compose up -d --force-recreate
  613. fi
  614. cd -
  615. wait_seconds 2
  616. }
  617. # test_connection - tests to make sure Caddy has proper SSL certs
  618. test_connection() {
  619. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  620. for i in 1 2 3 4 5 6 7 8; do
  621. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  622. if [[ "$i" == 8 ]]; then
  623. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  624. echo " Exiting..."
  625. exit 1
  626. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  627. echo " Certificates not yet configured, retrying..."
  628. elif [[ "$curlresponse" == *"left intact"* ]]; then
  629. echo " Certificates ok"
  630. break
  631. else
  632. secs=$(($i * 5 + 10))
  633. echo " Issue establishing connection...retrying in $secs seconds..."
  634. fi
  635. sleep $secs
  636. done
  637. }
  638. # setup_mesh - sets up a default mesh network on the server
  639. setup_mesh() {
  640. wait_seconds 5
  641. local networkCount=$(nmctl network list -o json | jq '. | length')
  642. # add a network if none present
  643. if [ "$networkCount" -lt 1 ]; then
  644. echo "Creating netmaker network (10.101.0.0/16)"
  645. # TODO causes "Error Status: 400 Response: {"Code":400,"Message":"could not find any records"}"
  646. nmctl network create --name netmaker --ipv4_addr 10.101.0.0/16
  647. wait_seconds 5
  648. fi
  649. echo "Obtaining a netmaker enrollment key..."
  650. local tokenJson=$(nmctl enrollment_key create --tags netmaker --unlimited --networks netmaker)
  651. TOKEN=$(jq -r '.token' <<<${tokenJson})
  652. if test -z "$TOKEN"; then
  653. echo "Error creating an enrollment key"
  654. exit 1
  655. else
  656. echo "Enrollment key ready"
  657. fi
  658. wait_seconds 3
  659. }
  660. # print_success - prints a success message upon completion
  661. print_success() {
  662. echo "-----------------------------------------------------------------"
  663. echo "-----------------------------------------------------------------"
  664. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  665. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  666. echo "-----------------------------------------------------------------"
  667. echo "-----------------------------------------------------------------"
  668. }
  669. cleanup() {
  670. # remove the existing netclient's instance from the existing network
  671. if ! command -v netclient >/dev/null 2>&1; then
  672. return
  673. fi
  674. if command -v nmctl >/dev/null 2>&1; then
  675. local node_id=$(netclient list | jq '.[0].node_id' 2>/dev/null)
  676. # trim doublequotes
  677. node_id="${node_id//\"/}"
  678. if test -n "$node_id"; then
  679. echo "De-registering the existing netclient..."
  680. nmctl node delete netmaker $node_id >/dev/null 2>&1
  681. fi
  682. fi
  683. stop_services
  684. }
  685. stop_services(){
  686. echo "Stopping all containers, this will take a while please wait..."
  687. local containers=("mq" "netmaker-ui" "coredns" "turn" "caddy" "netmaker" "netmaker-exporter" "prometheus" "grafana")
  688. for name in "${containers[@]}"; do
  689. local running=$(docker ps | grep -w "$name")
  690. local exists=$(docker ps -a | grep -w "$name")
  691. if test -n "$running"; then
  692. docker stop "$name" 1>/dev/null
  693. fi
  694. if test -n "$exists"; then
  695. docker rm "$name" 1>/dev/null
  696. fi
  697. done
  698. }
  699. upgrade() {
  700. print_logo
  701. unset IMAGE_TAG
  702. unset BUILD_TAG
  703. IMAGE_TAG=$UI_IMAGE_TAG
  704. semver=$(chsv_check_version_ex "$UI_IMAGE_TAG")
  705. if [[ ! "$semver" ]]; then
  706. BUILD_TAG=$LATEST
  707. else
  708. BUILD_TAG=$UI_IMAGE_TAG
  709. fi
  710. echo "-----------------------------------------------------"
  711. echo "Provide Details for pro installation:"
  712. echo " 1. Log into https://app.netmaker.io"
  713. echo " 2. follow instructions to get a license at: https://docs.netmaker.io/pro/pro-setup.html"
  714. echo " 3. Retrieve License and Tenant ID"
  715. echo "-----------------------------------------------------"
  716. unset LICENSE_KEY
  717. while [ -z "$LICENSE_KEY" ]; do
  718. read -p "License Key: " LICENSE_KEY
  719. done
  720. unset TENANT_ID
  721. while [ -z ${TENANT_ID} ]; do
  722. read -p "Tenant ID: " TENANT_ID
  723. done
  724. save_config
  725. # start docker and rebuild containers / networks
  726. stop_services
  727. install_netmaker
  728. }
  729. downgrade () {
  730. print_logo
  731. unset IMAGE_TAG
  732. unset BUILD_TAG
  733. IMAGE_TAG=$UI_IMAGE_TAG
  734. semver=$(chsv_check_version_ex "$UI_IMAGE_TAG")
  735. if [[ ! "$semver" ]]; then
  736. BUILD_TAG=$LATEST
  737. else
  738. BUILD_TAG=$UI_IMAGE_TAG
  739. fi
  740. save_config
  741. if [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
  742. rm -f "$SCRIPT_DIR"/docker-compose.override.yml
  743. fi
  744. # start docker and rebuild containers / networks
  745. stop_services
  746. install_netmaker
  747. }
  748. function chsv_check_version() {
  749. 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
  750. echo "$1"
  751. else
  752. echo ""
  753. fi
  754. }
  755. function chsv_check_version_ex() {
  756. if [[ $1 =~ ^v.+$ ]]; then
  757. chsv_check_version "${1:1}"
  758. else
  759. chsv_check_version "${1}"
  760. fi
  761. }
  762. main (){
  763. # read the config
  764. if [ -f "$CONFIG_PATH" ]; then
  765. echo "Using config: $CONFIG_PATH"
  766. source "$CONFIG_PATH"
  767. fi
  768. INSTALL_TYPE="pro"
  769. while getopts :cudv flag; do
  770. case "${flag}" in
  771. c)
  772. INSTALL_TYPE="ce"
  773. ;;
  774. u)
  775. echo "upgrading to pro version..."
  776. INSTALL_TYPE="pro"
  777. UPGRADE_FLAG="yes"
  778. upgrade
  779. exit 0
  780. ;;
  781. d)
  782. echo "downgrading to community version..."
  783. INSTALL_TYPE="ce"
  784. downgrade
  785. exit 0
  786. ;;
  787. v)
  788. usage
  789. exit 0
  790. ;;
  791. esac
  792. done
  793. # 1. print netmaker logo
  794. print_logo
  795. # 2. setup the build instructions
  796. set_buildinfo
  797. set +e
  798. # 3. install necessary packages
  799. install_dependencies
  800. # 4. install yq if necessary
  801. install_yq
  802. set -e
  803. # 6. get user input for variables
  804. set_install_vars
  805. set +e
  806. cleanup
  807. set -e
  808. # 7. get and set config files, startup docker-compose
  809. install_netmaker
  810. set +e
  811. # 8. make sure Caddy certs are working
  812. test_connection
  813. # 9. install the netmaker CLI
  814. setup_nmctl
  815. # 10. create a default mesh network for netmaker
  816. setup_mesh
  817. set -e
  818. # 11. add netclient to docker-compose and start it up
  819. setup_netclient
  820. # 12. make the netclient a default host and ingress gw
  821. configure_netclient
  822. # 13. print success message
  823. print_success
  824. }
  825. main "${@}"