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