nm-quick.sh 28 KB

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