nm-quick.sh 31 KB

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