nm-quick.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 --static-port -p 443
  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. save_config_item METRICS_EXPORTER "on"
  225. save_config_item PROMETHEUS "on"
  226. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG-ee"
  227. else
  228. save_config_item METRICS_EXPORTER "off"
  229. save_config_item PROMETHEUS "off"
  230. save_config_item SERVER_IMAGE_TAG "$IMAGE_TAG"
  231. fi
  232. # copy entries from the previous config
  233. local toCopy=("SERVER_HOST" "SERVER_HOST6" "MASTER_KEY" "MQ_USERNAME" "MQ_PASSWORD" "LICENSE_KEY" "NETMAKER_TENANT_ID"
  234. "INSTALL_TYPE" "NODE_ID" "DNS_MODE" "NETCLIENT_AUTO_UPDATE" "API_PORT"
  235. "CORS_ALLOWED_ORIGIN" "DISPLAY_KEYS" "DATABASE" "SERVER_BROKER_ENDPOINT" "VERBOSITY"
  236. "DEBUG_MODE" "REST_BACKEND" "DISABLE_REMOTE_IP_CHECK" "TELEMETRY" "ALLOWED_EMAIL_DOMAINS" "AUTH_PROVIDER" "CLIENT_ID" "CLIENT_SECRET"
  237. "FRONTEND_URL" "AZURE_TENANT" "OIDC_ISSUER" "EXPORTER_API_PORT" "JWT_VALIDITY_DURATION" "RAC_AUTO_DISABLE" "CACHING_ENABLED" "ENDPOINT_DETECTION"
  238. "SMTP_HOST" "SMTP_PORT" "EMAIL_SENDER_ADDR" "EMAIL_SENDER_USER" "EMAIL_SENDER_PASSWORD")
  239. for name in "${toCopy[@]}"; do
  240. save_config_item $name "${!name}"
  241. done
  242. # preserve debug entries
  243. if test -n "$NM_SKIP_BUILD"; then
  244. save_config_item NM_SKIP_BUILD "$NM_SKIP_BUILD"
  245. fi
  246. if test -n "$NM_SKIP_CLONE"; then
  247. save_config_item NM_SKIP_CLONE "$NM_SKIP_CLONE"
  248. fi
  249. if test -n "$NM_SKIP_DEPS"; then
  250. save_config_item NM_SKIP_DEPS "$NM_SKIP_DEPS"
  251. fi
  252. ); }
  253. save_config_item() { (
  254. local NAME="$1"
  255. local VALUE="$2"
  256. #echo "$NAME=$VALUE"
  257. if test -z "$VALUE"; then
  258. # load the default for empty values
  259. VALUE=$(awk -F'=' "/^$NAME/ { print \$2}" "$SCRIPT_DIR/netmaker.default.env")
  260. # trim quotes for docker
  261. VALUE=$(echo "$VALUE" | sed -E "s|^(['\"])(.*)\1$|\2|g")
  262. #echo "Default for $NAME=$VALUE"
  263. fi
  264. # TODO single quote passwords
  265. if grep -q "^$NAME=" "$CONFIG_PATH"; then
  266. # TODO escape | in the value
  267. sed -i "s|$NAME=.*|$NAME=$VALUE|" "$CONFIG_PATH"
  268. else
  269. echo "$NAME=$VALUE" >>"$CONFIG_PATH"
  270. fi
  271. ); }
  272. # install_dependencies - install necessary packages to run netmaker
  273. install_dependencies() {
  274. if test -n "$NM_SKIP_DEPS"; then
  275. return
  276. fi
  277. echo "checking dependencies..."
  278. OS=$(uname)
  279. if [ -f /etc/debian_version ]; then
  280. dependencies="git wireguard-tools dnsutils jq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  281. update_cmd='apt update'
  282. install_cmd='apt-get install -y'
  283. elif [ -f /etc/alpine-release ]; then
  284. dependencies="git wireguard jq docker.io docker-compose grep gawk"
  285. update_cmd='apk update'
  286. install_cmd='apk --update add'
  287. elif [ -f /etc/centos-release ]; then
  288. dependencies="wget git wireguard-tools jq bind-utils docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  289. update_cmd='yum updateinfo'
  290. install_cmd='yum install -y'
  291. elif [ -f /etc/amazon-linux-release ]; then
  292. dependencies="git wireguard-tools bind-utils jq docker grep gawk"
  293. update_cmd='yum updateinfo'
  294. install_cmd='yum install -y'
  295. elif [ -f /etc/fedora-release ]; then
  296. dependencies="wget git wireguard-tools bind-utils jq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  297. update_cmd='dnf updateinfo'
  298. install_cmd='dnf install -y'
  299. elif [ -f /etc/redhat-release ]; then
  300. dependencies="wget git wireguard-tools jq bind-utils docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin grep gawk"
  301. update_cmd='yum updateinfo'
  302. install_cmd='yum install -y'
  303. elif [ -f /etc/arch-release ]; then
  304. dependencies="git wireguard-tools dnsutils jq docker.io docker-compose grep gawk"
  305. update_cmd='pacman -Sy'
  306. install_cmd='pacman -S --noconfirm'
  307. elif [ "${OS}" = "FreeBSD" ]; then
  308. dependencies="git wireguard wget jq docker.io docker-compose grep gawk"
  309. update_cmd='pkg update'
  310. install_cmd='pkg install -y'
  311. else
  312. echo "-----------------------nm-quick.sh----------------------------------------------"
  313. echo "OS supported and tested include:"
  314. echo " Debian"
  315. echo " Ubuntu"
  316. echo " Fedora"
  317. echo " Centos"
  318. echo " Redhat"
  319. echo " Amazon Linux"
  320. echo " Rocky Linux"
  321. echo " AlmaLinux"
  322. echo "Your OS system is not in the support list, please chanage to an OS in the list"
  323. echo "--------------------------------------------------------------------------------"
  324. exit 1
  325. fi
  326. if [ -z "${install_cmd}" ]; then
  327. echo "OS unsupported for automatic dependency install"
  328. # TODO shouldnt exit, check if deps available, if not
  329. # ask the user to install manually and continue when ready
  330. exit 1
  331. fi
  332. # TODO add other supported architectures
  333. ARCH=$(uname -m)
  334. if [ "$ARCH" = "x86_64" ]; then
  335. ARCH=amd64
  336. elif [ "$ARCH" = "aarch64" ]; then
  337. ARCH=arm64
  338. else
  339. echo "Unsupported architechure"
  340. # exit 1
  341. fi
  342. # setup docker repository
  343. if [ "$(cat /etc/*-release |grep ubuntu |wc -l)" -gt 0 ]; then
  344. apt update
  345. apt install -y ca-certificates curl
  346. install -m 0755 -d /etc/apt/keyrings
  347. curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
  348. chmod a+r /etc/apt/keyrings/docker.asc
  349. echo \
  350. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  351. $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  352. tee /etc/apt/sources.list.d/docker.list > /dev/null
  353. apt update
  354. elif [ "$(cat /etc/*-release |grep debian |wc -l)" -gt 0 ]; then
  355. apt update
  356. apt install -y ca-certificates curl
  357. install -m 0755 -d /etc/apt/keyrings
  358. curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
  359. chmod a+r /etc/apt/keyrings/docker.asc
  360. echo \
  361. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  362. $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  363. tee /etc/apt/sources.list.d/docker.list > /dev/null
  364. apt update
  365. elif [ -f /etc/fedora-release ]; then
  366. dnf -y install dnf-plugins-core
  367. dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
  368. elif [ -f /etc/centos-release ]; then
  369. yum install -y yum-utils
  370. yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  371. if [ "$(cat /etc/*-release |grep 'release 8' |wc -l)" -gt 0 ]; then
  372. yum install -y elrepo-release epel-release
  373. elif [ "$(cat /etc/*-release |grep 'release 7' |wc -l)" -gt 0 ]; then
  374. yum install -y elrepo-release epel-release
  375. yum install -y yum-plugin-elrepo
  376. fi
  377. elif [ -f /etc/redhat-release ]; then
  378. yum install -y yum-utils
  379. yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  380. if [ "$(cat /etc/*-release |grep 'release 8' |wc -l)" -gt 0 ]; then
  381. yum install -y elrepo-release epel-release
  382. fi
  383. fi
  384. set -- $dependencies
  385. ${update_cmd}
  386. while [ -n "$1" ]; do
  387. if [ "${OS}" = "FreeBSD" ]; then
  388. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  389. if [ "$is_installed" != "" ]; then
  390. echo " " $1 is installed
  391. else
  392. echo " " $1 is not installed. Attempting install.
  393. ${install_cmd} $1
  394. sleep 5
  395. is_installed=$(pkg check -d $1 | grep "Checking" | grep "done")
  396. if [ "$is_installed" != "" ]; then
  397. echo " " $1 is installed
  398. elif [ -x "$(command -v $1)" ]; then
  399. echo " " $1 is installed
  400. else
  401. echo " " FAILED TO INSTALL $1
  402. echo " " This may break functionality.
  403. fi
  404. fi
  405. else
  406. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  407. is_installed=$(opkg list-installed $1 | grep $1)
  408. elif [ -f /etc/debian_version ]; then
  409. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  410. else
  411. is_installed=$(yum list installed | grep $1)
  412. fi
  413. if [ "${is_installed}" != "" ]; then
  414. echo " " $1 is installed
  415. else
  416. echo " " $1 is not installed. Attempting install.
  417. ${install_cmd} $1
  418. sleep 5
  419. if [ "${OS}" = "OpenWRT" ] || [ "${OS}" = "TurrisOS" ]; then
  420. is_installed=$(opkg list-installed $1 | grep $1)
  421. elif [ -f /etc/debian_version ]; then
  422. is_installed=$(dpkg-query -W --showformat='${Status}\n' $1 | grep "install ok installed")
  423. else
  424. is_installed=$(yum list installed | grep $1)
  425. fi
  426. if [ "${is_installed}" != "" ]; then
  427. echo " " $1 is installed
  428. elif [ -x "$(command -v $1)" ]; then
  429. echo " " $1 is installed
  430. else
  431. echo " " FAILED TO INSTALL $1
  432. echo " " This may break functionality.
  433. fi
  434. fi
  435. fi
  436. shift
  437. done
  438. # Startup docker daemon for OS which does not start it automatically
  439. if [ -f /etc/fedora-release ]; then
  440. systemctl start docker
  441. systemctl enable docker
  442. elif [ -f /etc/amazon-linux-release ]; then
  443. systemctl start docker
  444. systemctl enable docker
  445. usermod -a -G docker ec2-user
  446. mkdir -p /usr/local/lib/docker/cli-plugins
  447. curl -sL https://github.com/docker/compose/releases/latest/download/docker-compose-linux-$(uname -m) \
  448. -o /usr/local/lib/docker/cli-plugins/docker-compose
  449. chown root:root /usr/local/lib/docker/cli-plugins/docker-compose
  450. chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
  451. elif [ -f /etc/centos-release ]; then
  452. systemctl start docker
  453. systemctl enable docker
  454. elif [ -f /etc/redhat-release ]; then
  455. systemctl start docker
  456. systemctl enable docker
  457. fi
  458. echo "-----------------------------------------------------"
  459. echo "dependency check complete"
  460. echo "-----------------------------------------------------"
  461. }
  462. set -e
  463. # set_install_vars - sets the variables that will be used throughout installation
  464. set_install_vars() {
  465. IP_ADDR=$(curl -s -4 ifconfig.me)
  466. IP6_ADDR=$(curl -s -6 ifconfig.me)
  467. if [ "$NETMAKER_BASE_DOMAIN" = "" ]; then
  468. NETMAKER_BASE_DOMAIN=nm.$(echo $IP_ADDR | tr . -).nip.io
  469. fi
  470. SERVER_HOST=$IP_ADDR
  471. SERVER_HOST6=$IP6_ADDR
  472. if [ "$IP_ADDR" = "" ]; then
  473. SERVER_HOST=$IP6_ADDR
  474. fi
  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 [ "$INSTALL_TYPE" = "pro" ]; 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. wget -qO "$SCRIPT_DIR"/docker-compose.override.yml $COMPOSE_OVERRIDE_URL
  570. elif [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
  571. rm -f "$SCRIPT_DIR"/docker-compose.override.yml
  572. fi
  573. wget -qO "$SCRIPT_DIR"/docker-compose.yml $COMPOSE_URL
  574. wget -qO "$SCRIPT_DIR"/Caddyfile "$CADDY_URL"
  575. wget -qO "$SCRIPT_DIR"/netmaker.default.env "$BASE_URL/scripts/netmaker.default.env"
  576. wget -qO "$SCRIPT_DIR"/mosquitto.conf "$BASE_URL/docker/mosquitto.conf"
  577. wget -qO "$SCRIPT_DIR"/wait.sh "$BASE_URL/docker/wait.sh"
  578. chmod +x "$SCRIPT_DIR"/wait.sh
  579. mkdir -p /etc/netmaker
  580. # link .env to the user config
  581. ln -fs "$SCRIPT_DIR/netmaker.env" "$SCRIPT_DIR/.env"
  582. save_config
  583. echo "Starting containers..."
  584. # start docker and rebuild containers / networks
  585. cd "${SCRIPT_DIR}"
  586. if [ -f /etc/debian_version ]; then
  587. docker compose up -d --force-recreate
  588. elif [ -f /etc/fedora-release ]; then
  589. docker compose up -d --force-recreate
  590. elif [ -f /etc/amazon-linux-release ]; then
  591. docker compose up -d --force-recreate
  592. elif [ -f /etc/centos-release ]; then
  593. docker compose up -d --force-recreate
  594. elif [ -f /etc/redhat-release ]; then
  595. docker compose up -d --force-recreate
  596. else
  597. docker-compose up -d --force-recreate
  598. fi
  599. cd -
  600. wait_seconds 2
  601. }
  602. # test_connection - tests to make sure Caddy has proper SSL certs
  603. test_connection() {
  604. echo "Testing Caddy setup (please be patient, this may take 1-2 minutes)"
  605. for i in 1 2 3 4 5 6 7 8; do
  606. curlresponse=$(curl -vIs https://api.${NETMAKER_BASE_DOMAIN} 2>&1)
  607. if [[ "$i" == 8 ]]; then
  608. echo " Caddy is having an issue setting up certificates, please investigate (docker logs caddy)"
  609. echo " Exiting..."
  610. exit 1
  611. elif [[ "$curlresponse" == *"failed to verify the legitimacy of the server"* ]]; then
  612. echo " Certificates not yet configured, retrying..."
  613. elif [[ "$curlresponse" == *"left intact"* ]]; then
  614. echo " Certificates ok"
  615. break
  616. else
  617. secs=$(($i * 5 + 10))
  618. echo " Issue establishing connection...retrying in $secs seconds..."
  619. fi
  620. sleep $secs
  621. done
  622. }
  623. # setup_mesh - sets up a default mesh network on the server
  624. setup_mesh() {
  625. wait_seconds 5
  626. networks=$(nmctl network list -o json)
  627. if [[ ${networks} != "null" ]]; then
  628. netmakerNet=$(nmctl network list -o json | jq -r '.[] | .netid' | grep -w "netmaker")
  629. inetNet=$(nmctl network list -o json | jq -r '.[] | .netid' | grep -w "internet-access-vpn")
  630. fi
  631. # create netmaker network
  632. if [[ ${netmakerNet} = "" ]]; then
  633. echo "Creating netmaker network (100.64.0.0/16)"
  634. # TODO causes "Error Status: 400 Response: {"Code":400,"Message":"could not find any records"}"
  635. nmctl network create --name netmaker --ipv4_addr 100.64.0.0/16
  636. fi
  637. # create enrollment key for netmaker network
  638. local netmakerTag=$(nmctl enrollment_key list | jq -r '.[] | .tags[0]' | grep -w "netmaker")
  639. if [[ ${netmakerTag} = "" ]]; then
  640. nmctl enrollment_key create --tags netmaker --unlimited --networks netmaker
  641. fi
  642. # create internet-access-vpn
  643. if [ "$INSTALL_TYPE" = "pro" ]; then
  644. if [[ ${inetNet} = "" ]]; then
  645. echo "Creating internet-access-vpn network (100.65.0.0/16)"
  646. # TODO causes "Error Status: 400 Response: {"Code":400,"Message":"could not find any records"}"
  647. nmctl network create --name internet-access-vpn --ipv4_addr 100.65.0.0/16
  648. fi
  649. # create enrollment key for internet-access-vpn network
  650. local inetTag=$(nmctl enrollment_key list | jq -r '.[] | .tags[0]' | grep -w "internet-access-vpn")
  651. if [[ ${inetTag} = "" ]]; then
  652. nmctl enrollment_key create --tags internet-access-vpn --unlimited --networks internet-access-vpn
  653. fi
  654. # create enrollment key for both networks
  655. local netInetTag=$(nmctl enrollment_key list | jq -r '.[] | .tags[0]' | grep -w "netmaker-inet")
  656. if [[ ${netInetTag} = "" ]]; then
  657. nmctl enrollment_key create --tags netmaker-inet --unlimited --networks netmaker,internet-access-vpn
  658. fi
  659. fi
  660. if [ "$INSTALL_TYPE" = "pro" ]; then
  661. # create enrollment key for both setup networks
  662. echo "Obtaining enrollment key..."
  663. # key exists already, fetch token
  664. TOKEN=$(nmctl enrollment_key list | jq -r '.[] | select(.tags[0]=="netmaker-inet") | .token')
  665. else
  666. echo "Obtaining enrollment key..."
  667. # key exists already, fetch token
  668. TOKEN=$(nmctl enrollment_key list | jq -r '.[] | select(.tags[0]=="netmaker") | .token')
  669. fi
  670. wait_seconds 3
  671. }
  672. # print_success - prints a success message upon completion
  673. print_success() {
  674. echo "-----------------------------------------------------------------"
  675. echo "-----------------------------------------------------------------"
  676. echo "Netmaker setup is now complete. You are ready to begin using Netmaker."
  677. echo "Visit dashboard.$NETMAKER_BASE_DOMAIN to log in"
  678. echo "-----------------------------------------------------------------"
  679. echo "-----------------------------------------------------------------"
  680. }
  681. cleanup() {
  682. # remove the existing netclient's instance from the existing network
  683. if ! command -v netclient >/dev/null 2>&1; then
  684. return
  685. fi
  686. if command -v nmctl >/dev/null 2>&1; then
  687. local node_id=$(netclient list | jq '.[0].node_id' 2>/dev/null)
  688. # trim doublequotes
  689. node_id="${node_id//\"/}"
  690. if test -n "$node_id"; then
  691. echo "De-registering the existing netclient..."
  692. nmctl node delete netmaker $node_id >/dev/null 2>&1
  693. fi
  694. fi
  695. stop_services
  696. }
  697. stop_services(){
  698. echo "Stopping all containers, this will take a while please wait..."
  699. local containers=("mq" "netmaker-ui" "coredns" "turn" "caddy" "netmaker" "netmaker-exporter" "prometheus" "grafana")
  700. for name in "${containers[@]}"; do
  701. local running=$(docker ps | grep -w "$name")
  702. local exists=$(docker ps -a | grep -w "$name")
  703. if test -n "$running"; then
  704. docker stop "$name" 1>/dev/null
  705. fi
  706. if test -n "$exists"; then
  707. docker rm "$name" 1>/dev/null
  708. fi
  709. done
  710. }
  711. upgrade() {
  712. print_logo
  713. unset IMAGE_TAG
  714. unset BUILD_TAG
  715. IMAGE_TAG=$UI_IMAGE_TAG
  716. semver=$(chsv_check_version_ex "$UI_IMAGE_TAG")
  717. if [[ ! "$semver" ]]; then
  718. BUILD_TAG=$LATEST
  719. else
  720. BUILD_TAG=$UI_IMAGE_TAG
  721. fi
  722. echo "-----------------------------------------------------"
  723. echo "Provide Details for pro installation:"
  724. echo " 1. Log into https://app.netmaker.io"
  725. echo " 2. follow instructions to get a license at: https://docs.netmaker.io/docs/server-installation/netmaker-professional-setup"
  726. echo " 3. Retrieve License and Tenant ID"
  727. echo "-----------------------------------------------------"
  728. unset LICENSE_KEY
  729. while [ -z "$LICENSE_KEY" ]; do
  730. read -p "License Key: " LICENSE_KEY
  731. done
  732. unset NETMAKER_TENANT_ID
  733. while [ -z ${NETMAKER_TENANT_ID} ]; do
  734. read -p "Tenant ID: " NETMAKER_TENANT_ID
  735. done
  736. save_config
  737. # start docker and rebuild containers / networks
  738. stop_services
  739. install_netmaker
  740. }
  741. downgrade () {
  742. print_logo
  743. unset IMAGE_TAG
  744. unset BUILD_TAG
  745. IMAGE_TAG=$UI_IMAGE_TAG
  746. semver=$(chsv_check_version_ex "$UI_IMAGE_TAG")
  747. if [[ ! "$semver" ]]; then
  748. BUILD_TAG=$LATEST
  749. else
  750. BUILD_TAG=$UI_IMAGE_TAG
  751. fi
  752. save_config
  753. if [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
  754. rm -f "$SCRIPT_DIR"/docker-compose.override.yml
  755. fi
  756. # start docker and rebuild containers / networks
  757. stop_services
  758. install_netmaker
  759. }
  760. function chsv_check_version() {
  761. 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
  762. echo "$1"
  763. else
  764. echo ""
  765. fi
  766. }
  767. function chsv_check_version_ex() {
  768. if [[ $1 =~ ^v.+$ ]]; then
  769. chsv_check_version "${1:1}"
  770. else
  771. chsv_check_version "${1}"
  772. fi
  773. }
  774. main (){
  775. # read the config
  776. if [ -f "$CONFIG_PATH" ]; then
  777. echo "Using config: $CONFIG_PATH"
  778. source "$CONFIG_PATH"
  779. fi
  780. INSTALL_TYPE="ce"
  781. while getopts :cudpv flag; do
  782. case "${flag}" in
  783. c)
  784. INSTALL_TYPE="ce"
  785. ;;
  786. u)
  787. echo "upgrading to pro version..."
  788. INSTALL_TYPE="pro"
  789. UPGRADE_FLAG="yes"
  790. upgrade
  791. exit 0
  792. ;;
  793. d)
  794. echo "downgrading to community version..."
  795. INSTALL_TYPE="ce"
  796. downgrade
  797. exit 0
  798. ;;
  799. p)
  800. echo "installing pro version..."
  801. INSTALL_TYPE="pro"
  802. COLLECT_PRO_VARS="true"
  803. ;;
  804. v)
  805. usage
  806. exit 0
  807. ;;
  808. esac
  809. done
  810. # 1. print netmaker logo
  811. print_logo
  812. # 2. setup the build instructions
  813. set_buildinfo
  814. set +e
  815. # 3. install necessary packages
  816. install_dependencies
  817. # 4. install yq if necessary
  818. install_yq
  819. set -e
  820. # 6. get user input for variables
  821. set_install_vars
  822. set +e
  823. cleanup
  824. set -e
  825. # 7. get and set config files, startup docker-compose
  826. install_netmaker
  827. set +e
  828. # 8. make sure Caddy certs are working
  829. test_connection
  830. # 9. install the netmaker CLI
  831. setup_nmctl
  832. # 10. create a default mesh network for netmaker
  833. setup_mesh
  834. set -e
  835. # 11. add netclient to docker-compose and start it up
  836. setup_netclient
  837. # 12. make the netclient a default host and ingress gw
  838. configure_netclient
  839. # 13. print success message
  840. print_success
  841. }
  842. main "${@}"