nm-quick.sh 29 KB

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