nm-quick.sh 30 KB

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