nm-quick.sh 28 KB

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