nm-quick.sh 29 KB

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