nm-quick.sh 28 KB

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