abhishek9686 пре 1 година
родитељ
комит
83eed08449
4 измењених фајлова са 213 додато и 154 уклоњено
  1. 21 12
      controllers/server.go
  2. 19 13
      pro/initialize.go
  3. 9 8
      pro/trial.go
  4. 164 121
      scripts/nm-quick.sh

+ 21 - 12
controllers/server.go

@@ -110,11 +110,12 @@ func getUsage(w http.ResponseWriter, _ *http.Request) {
 //				200: serverConfigResponse
 //				200: serverConfigResponse
 func getStatus(w http.ResponseWriter, r *http.Request) {
 func getStatus(w http.ResponseWriter, r *http.Request) {
 	type status struct {
 	type status struct {
-		DB           bool      `json:"db_connected"`
-		Broker       bool      `json:"broker_connected"`
-		LicenseError string    `json:"license_error"`
-		IsPro        bool      `json:"is_pro"`
-		TrialEndDate time.Time `json:"trial_end_date"`
+		DB               bool      `json:"db_connected"`
+		Broker           bool      `json:"broker_connected"`
+		LicenseError     string    `json:"license_error"`
+		IsPro            bool      `json:"is_pro"`
+		TrialEndDate     time.Time `json:"trial_end_date"`
+		IsOnTrialLicense bool      `json:"is_on_trial_license"`
 	}
 	}
 
 
 	licenseErr := ""
 	licenseErr := ""
@@ -122,15 +123,23 @@ func getStatus(w http.ResponseWriter, r *http.Request) {
 		licenseErr = servercfg.ErrLicenseValidation.Error()
 		licenseErr = servercfg.ErrLicenseValidation.Error()
 	}
 	}
 	var trialEndDate time.Time
 	var trialEndDate time.Time
-	if servercfg.GetLicenseKey() == "" {
-		trialEndDate, _ = logic.GetTrialEndDate()
+	var err error
+	isOnTrial := false
+	if servercfg.IsPro && (servercfg.GetLicenseKey() == "" || servercfg.GetNetmakerTenantID() == "") {
+		trialEndDate, err = logic.GetTrialEndDate()
+		if err != nil {
+			slog.Error("failed to get trial end date", "error", err)
+		} else {
+			isOnTrial = true
+		}
 	}
 	}
 	currentServerStatus := status{
 	currentServerStatus := status{
-		DB:           database.IsConnected(),
-		Broker:       mq.IsConnected(),
-		LicenseError: licenseErr,
-		IsPro:        servercfg.IsPro,
-		TrialEndDate: trialEndDate,
+		DB:               database.IsConnected(),
+		Broker:           mq.IsConnected(),
+		LicenseError:     licenseErr,
+		IsPro:            servercfg.IsPro,
+		TrialEndDate:     trialEndDate,
+		IsOnTrialLicense: isOnTrial,
 	}
 	}
 
 
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Content-Type", "application/json")

+ 19 - 13
pro/initialize.go

@@ -4,6 +4,8 @@
 package pro
 package pro
 
 
 import (
 import (
+	"time"
+
 	controller "github.com/gravitl/netmaker/controllers"
 	controller "github.com/gravitl/netmaker/controllers"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logic"
 	"github.com/gravitl/netmaker/logic"
@@ -41,32 +43,36 @@ func InitPro() {
 		if !enableLicenseHook {
 		if !enableLicenseHook {
 			err := initTrial()
 			err := initTrial()
 			if err != nil {
 			if err != nil {
-				logger.FatalLog0("failed to init trail", err.Error())
+				logger.Log(0, "failed to init trial", err.Error())
+				enableLicenseHook = true
+			}
+			trialEndDate, err := getTrialEndDate()
+			if err != nil {
+				slog.Error("failed to get trial end date", "error", err)
+				enableLicenseHook = true
+			} else {
+				// check if trial ended
+				if time.Now().After(trialEndDate) {
+					// trial ended already
+					enableLicenseHook = true
+				}
 			}
 			}
-			// trialEndDate, err := getTrialEndDate()
-			// if err != nil {
-			// 	slog.Error("failed to get trial end date", "error", err)
-			// 	enableLicenseHook = true
-			// }
-			// // check if trial ended
-			// if time.Now().After(trialEndDate) {
-			// 	// trial ended already
-			// 	enableLicenseHook = true
-			// }
+
 		}
 		}
 
 
 		if enableLicenseHook {
 		if enableLicenseHook {
-			slog.Info("starting license checker")
+			logger.Log(0, "starting license checker")
 			ClearLicenseCache()
 			ClearLicenseCache()
 			if err := ValidateLicense(); err != nil {
 			if err := ValidateLicense(); err != nil {
 				slog.Error(err.Error())
 				slog.Error(err.Error())
 				return
 				return
 			}
 			}
-			slog.Info("proceeding with Paid Tier license")
+			logger.Log(0, "proceeding with Paid Tier license")
 			logic.SetFreeTierForTelemetry(false)
 			logic.SetFreeTierForTelemetry(false)
 			// == End License Handling ==
 			// == End License Handling ==
 			AddLicenseHooks()
 			AddLicenseHooks()
 		} else {
 		} else {
+			logger.Log(0, "starting trial license hook")
 			addTrialLicenseHook()
 			addTrialLicenseHook()
 		}
 		}
 
 

+ 9 - 8
pro/trial.go

@@ -1,6 +1,3 @@
-//go:build ee
-// +build ee
-
 package pro
 package pro
 
 
 import (
 import (
@@ -14,6 +11,7 @@ import (
 	"github.com/gravitl/netmaker/logic"
 	"github.com/gravitl/netmaker/logic"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 	"github.com/gravitl/netmaker/netclient/ncutils"
+	"github.com/gravitl/netmaker/servercfg"
 	"golang.org/x/crypto/nacl/box"
 	"golang.org/x/crypto/nacl/box"
 )
 )
 
 
@@ -39,14 +37,14 @@ const trial_table_name = "trial"
 
 
 const trial_data_key = "trialdata"
 const trial_data_key = "trialdata"
 
 
-// store trial date
+// stores trial end date
 func initTrial() error {
 func initTrial() error {
 	telData, err := logic.FetchTelemetryData()
 	telData, err := logic.FetchTelemetryData()
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
 	if telData.Hosts > 0 || telData.Networks > 0 || telData.Users > 0 {
 	if telData.Hosts > 0 || telData.Networks > 0 || telData.Users > 0 {
-		return nil
+		return nil // database is already populated, so skip creating trial
 	}
 	}
 	database.CreateTable(trial_table_name)
 	database.CreateTable(trial_table_name)
 	records, err := database.FetchRecords(trial_table_name)
 	records, err := database.FetchRecords(trial_table_name)
@@ -72,7 +70,7 @@ func initTrial() error {
 	}
 	}
 	trialDates := TrialDates{
 	trialDates := TrialDates{
 		TrialStartedAt: time.Now(),
 		TrialStartedAt: time.Now(),
-		TrialEndsAt:    time.Now().Add(time.Minute),
+		TrialEndsAt:    time.Now().Add(time.Minute * 5),
 	}
 	}
 	t := TrialInfo{
 	t := TrialInfo{
 		PrivKey: tPriv,
 		PrivKey: tPriv,
@@ -107,14 +105,17 @@ func initTrial() error {
 	return nil
 	return nil
 }
 }
 
 
+// TrialLicenseHook - hook func to check if pro trial has ended
 func TrialLicenseHook() error {
 func TrialLicenseHook() error {
 	endDate, err := getTrialEndDate()
 	endDate, err := getTrialEndDate()
 	if err != nil {
 	if err != nil {
 		logger.FatalLog0("failed to trial end date", err.Error())
 		logger.FatalLog0("failed to trial end date", err.Error())
 	}
 	}
 	if time.Now().After(endDate) {
 	if time.Now().After(endDate) {
-		logger.FatalLog0("***IMPORTANT: Your Trial Has Ended, to continue using pro version, please visit https://app.netmaker.io/ and create on-prem tenant to obtain a license***\nIf you wish to downgrade to community version, please run this command `/root/nm-quick.sh -d`")
-
+		logger.Log(0, "***IMPORTANT: Your Trial Has Ended, to continue using pro version, please visit https://app.netmaker.io/ and create on-prem tenant to obtain a license***\nIf you wish to downgrade to community version, please run this command `/root/nm-quick.sh -d`")
+		err = errors.New("your trial has ended")
+		servercfg.ErrLicenseValidation = err
+		return err
 	}
 	}
 	return nil
 	return nil
 }
 }

+ 164 - 121
scripts/nm-quick.sh

@@ -15,31 +15,18 @@ fi
 unset INSTALL_TYPE
 unset INSTALL_TYPE
 unset BUILD_TAG
 unset BUILD_TAG
 unset IMAGE_TAG
 unset IMAGE_TAG
-unset AUTO_BUILD
 unset NETMAKER_BASE_DOMAIN
 unset NETMAKER_BASE_DOMAIN
-INSTALL_TYPE="pro"
-UPGRADE_FLAG="yes"
 # usage - displays usage instructions
 # usage - displays usage instructions
 usage() {
 usage() {
 	echo "nm-quick.sh v$NM_QUICK_VERSION"
 	echo "nm-quick.sh v$NM_QUICK_VERSION"
 	echo "usage: ./nm-quick.sh [-c]"
 	echo "usage: ./nm-quick.sh [-c]"
-	echo "  -c      if specified, will install netmaker community version"
-
+	echo " -c  if specified, will install netmaker community version"
+	echo " -u  if specified, will upgrade netmaker to pro version"
+	echo " -d if specified, will downgrade netmaker to community version"
 	exit 1
 	exit 1
 }
 }
 
 
-while getopts evab:d:t: flag; do
-	case "${flag}" in
-	c)
-		INSTALL_TYPE="ce"
-		UPGRADE_FLAG="no"
-		;;
-	v)
-		usage
-		exit 0
-		;;
-	esac
-done
+
 
 
 # print_logo - prints the netmaker logo
 # print_logo - prints the netmaker logo
 print_logo() {
 print_logo() {
@@ -69,15 +56,7 @@ set_buildinfo() {
 	BUILD_TAG=$LATEST
 	BUILD_TAG=$LATEST
 	IMAGE_TAG=$(sed 's/\//-/g' <<<"$BUILD_TAG")
 	IMAGE_TAG=$(sed 's/\//-/g' <<<"$BUILD_TAG")
 
 
-	if [ "$1" = "ce" ]; then
-		INSTALL_TYPE="ce"
-	elif [ "$1" = "pro" ]; then
-		INSTALL_TYPE="pro"
-	fi
-
-	if [ "$AUTO_BUILD" = "on" ] && [ -z "$INSTALL_TYPE" ]; then
-		INSTALL_TYPE="ce"
-	elif [ -z "$INSTALL_TYPE" ]; then
+	if [ -z "$INSTALL_TYPE" ]; then
 		echo "-----------------------------------------------------"
 		echo "-----------------------------------------------------"
 		echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (pro)?"
 		echo "Would you like to install Netmaker Community Edition (CE), or Netmaker Enterprise Edition (pro)?"
 		echo "pro will require you to create an account at https://app.netmaker.io"
 		echo "pro will require you to create an account at https://app.netmaker.io"
@@ -217,9 +196,6 @@ wait_seconds() { (
 
 
 # confirm - get user input to confirm that they want to perform the next step
 # confirm - get user input to confirm that they want to perform the next step
 confirm() { (
 confirm() { (
-	if [ "$AUTO_BUILD" = "on" ]; then
-		return 0
-	fi
 	while true; do
 	while true; do
 		read -p 'Does everything look right? [y/n]: ' yn
 		read -p 'Does everything look right? [y/n]: ' yn
 		case $yn in
 		case $yn in
@@ -237,11 +213,16 @@ confirm() { (
 	done
 	done
 ) }
 ) }
 
 
+
 save_config() { (
 save_config() { (
 	echo "Saving the config to $CONFIG_PATH"
 	echo "Saving the config to $CONFIG_PATH"
 	touch "$CONFIG_PATH"
 	touch "$CONFIG_PATH"
-	save_config_item NM_EMAIL "$EMAIL"
-	save_config_item NM_DOMAIN "$NETMAKER_BASE_DOMAIN"
+	if [ -n "$EMAIL" ]; then 
+		save_config_item NM_EMAIL "$EMAIL"
+	fi
+	if [ -n "$NETMAKER_BASE_DOMAIN" ]; then
+		save_config_item NM_DOMAIN "$NETMAKER_BASE_DOMAIN"
+	fi
 	save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
 	save_config_item UI_IMAGE_TAG "$IMAGE_TAG"
 	# version-specific entries
 	# version-specific entries
 	if [ "$INSTALL_TYPE" = "pro" ]; then
 	if [ "$INSTALL_TYPE" = "pro" ]; then
@@ -440,27 +421,23 @@ set_install_vars() {
 	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."
 	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."
 	echo "-----------------------------------------------------"
 	echo "-----------------------------------------------------"
 
 
-	if [ "$AUTO_BUILD" = "on" ]; then
-		DOMAIN_TYPE="auto"
-	else
-		select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
-			case $REPLY in
-			1)
-				echo "using $NETMAKER_BASE_DOMAIN for base domain"
-				DOMAIN_TYPE="auto"
-				break
-				;;
-			2)
-				read -p "Enter Custom Domain (make sure  *.domain points to $SERVER_HOST first): " domain
-				NETMAKER_BASE_DOMAIN=$domain
-				echo "using $NETMAKER_BASE_DOMAIN"
-				DOMAIN_TYPE="custom"
-				break
-				;;
-			*) echo "invalid option $REPLY" ;;
-			esac
-		done
-	fi
+	select domain_option in "Auto Generated ($NETMAKER_BASE_DOMAIN)" "Custom Domain (e.x: netmaker.example.com)"; do
+		case $REPLY in
+		1)
+			echo "using $NETMAKER_BASE_DOMAIN for base domain"
+			DOMAIN_TYPE="auto"
+			break
+			;;
+		2)
+			read -p "Enter Custom Domain (make sure  *.domain points to $SERVER_HOST first): " domain
+			NETMAKER_BASE_DOMAIN=$domain
+			echo "using $NETMAKER_BASE_DOMAIN"
+			DOMAIN_TYPE="custom"
+			break
+			;;
+		*) echo "invalid option $REPLY" ;;
+		esac
+	done
 
 
 	wait_seconds 2
 	wait_seconds 2
 
 
@@ -491,9 +468,7 @@ set_install_vars() {
 	RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
 	RAND_EMAIL="$(echo $RANDOM | md5sum | head -c 16)@email.com"
 	# suggest the prev email or a random one
 	# suggest the prev email or a random one
 	EMAIL_SUGGESTED=${NM_EMAIL:-$RAND_EMAIL}
 	EMAIL_SUGGESTED=${NM_EMAIL:-$RAND_EMAIL}
-	if [ -z $AUTO_BUILD ]; then
-		read -p "Email Address for Domain Registration (click 'enter' to use $EMAIL_SUGGESTED): " GET_EMAIL
-	fi
+	read -p "Email Address for Domain Registration (click 'enter' to use $EMAIL_SUGGESTED): " GET_EMAIL
 	if [ -z "$GET_EMAIL" ]; then
 	if [ -z "$GET_EMAIL" ]; then
 		EMAIL="$EMAIL_SUGGESTED"
 		EMAIL="$EMAIL_SUGGESTED"
 		if [ "$EMAIL" = "$NM_EMAIL" ]; then
 		if [ "$EMAIL" = "$NM_EMAIL" ]; then
@@ -511,9 +486,8 @@ set_install_vars() {
 	unset GET_MQ_PASSWORD
 	unset GET_MQ_PASSWORD
 	unset CONFIRM_MQ_PASSWORD
 	unset CONFIRM_MQ_PASSWORD
 	echo "Enter Credentials For MQ..."
 	echo "Enter Credentials For MQ..."
-	if [ -z $AUTO_BUILD ]; then
-		read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
-	fi
+	
+	read -p "MQ Username (click 'enter' to use 'netmaker'): " GET_MQ_USERNAME
 	if [ -z "$GET_MQ_USERNAME" ]; then
 	if [ -z "$GET_MQ_USERNAME" ]; then
 		echo "using default username for mq"
 		echo "using default username for mq"
 		MQ_USERNAME="netmaker"
 		MQ_USERNAME="netmaker"
@@ -528,33 +502,33 @@ set_install_vars() {
 		)
 		)
 	fi
 	fi
 
 
-	if [ -z $AUTO_BUILD ]; then
-		select domain_option in "Auto Generated / Config Password" "Input Your Own Password"; do
-			case $REPLY in
-			1)
-				echo "using random password for mq"
-				break
-				;;
-			2)
-				while true; do
-					echo "Enter your Password For MQ: "
-					read -s GET_MQ_PASSWORD
-					echo "Enter your password again to confirm: "
-					read -s CONFIRM_MQ_PASSWORD
-					if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
-						echo "wrong password entered, try again..."
-						continue
-					fi
-					MQ_PASSWORD="$GET_MQ_PASSWORD"
-					echo "MQ Password Saved Successfully!!"
-					break
-				done
+
+	select domain_option in "Auto Generated / Config Password" "Input Your Own Password"; do
+		case $REPLY in
+		1)
+			echo "using random password for mq"
+			break
+			;;
+		2)
+			while true; do
+				echo "Enter your Password For MQ: "
+				read -s GET_MQ_PASSWORD
+				echo "Enter your password again to confirm: "
+				read -s CONFIRM_MQ_PASSWORD
+				if [ ${GET_MQ_PASSWORD} != ${CONFIRM_MQ_PASSWORD} ]; then
+					echo "wrong password entered, try again..."
+					continue
+				fi
+				MQ_PASSWORD="$GET_MQ_PASSWORD"
+				echo "MQ Password Saved Successfully!!"
 				break
 				break
-				;;
-			*) echo "invalid option $REPLY" ;;
-			esac
-		done
-	fi
+			done
+			break
+			;;
+		*) echo "invalid option $REPLY" ;;
+		esac
+	done
+	
 
 
 	wait_seconds 2
 	wait_seconds 2
 
 
@@ -690,6 +664,9 @@ print_success() {
 
 
 cleanup() {
 cleanup() {
 	# remove the existing netclient's instance from the existing network
 	# remove the existing netclient's instance from the existing network
+	if ! command -v netclient >/dev/null 2>&1; then
+		return
+	fi
 	if command -v nmctl >/dev/null 2>&1; then
 	if command -v nmctl >/dev/null 2>&1; then
 		local node_id=$(netclient list | jq '.[0].node_id' 2>/dev/null)
 		local node_id=$(netclient list | jq '.[0].node_id' 2>/dev/null)
 		# trim doublequotes
 		# trim doublequotes
@@ -700,7 +677,11 @@ cleanup() {
 		fi
 		fi
 	fi
 	fi
 
 
-	echo "Stopping all containers..."
+	stop_services
+}
+
+stop_services(){
+	echo "Stopping all containers, this will take a while please wait..."
 	local containers=("mq" "netmaker-ui" "coredns" "turn" "caddy" "netmaker" "netmaker-exporter" "prometheus" "grafana")
 	local containers=("mq" "netmaker-ui" "coredns" "turn" "caddy" "netmaker" "netmaker-exporter" "prometheus" "grafana")
 	for name in "${containers[@]}"; do
 	for name in "${containers[@]}"; do
 		local running=$(docker ps | grep -w "$name")
 		local running=$(docker ps | grep -w "$name")
@@ -714,57 +695,119 @@ cleanup() {
 	done
 	done
 }
 }
 
 
-# 1. print netmaker logo
-print_logo
+upgrade() {
+	print_logo
+	set_buildinfo
+	stop_services
+	echo "-----------------------------------------------------"
+	echo "Provide Details for pro installation:"
+	echo "    1. Log into https://app.netmaker.io"
+	echo "    2. follow instructions to get a license at: https://docs.netmaker.io/ee/ee-setup.html"
+	echo "    3. Retrieve License and Tenant ID"
+	echo "    4. note email address"
+	echo "-----------------------------------------------------"
+	unset LICENSE_KEY
+	while [ -z "$LICENSE_KEY" ]; do
+		read -p "License Key: " LICENSE_KEY
+	done
+	unset TENANT_ID
+	while [ -z ${TENANT_ID} ]; do
+		read -p "Tenant ID: " TENANT_ID
+	done
+	save_config
+	install_netmaker
+}
+
+downgrade () {
+	print_logo
+	set_buildinfo
+	stop_services
+	save_config
+	if [ -a "$SCRIPT_DIR"/docker-compose.override.yml ]; then
+		rm -f "$SCRIPT_DIR"/docker-compose.override.yml
+	fi
+	install_netmaker
+}
 
 
-# read the config
-if [ -f "$CONFIG_PATH" ]; then
-	echo "Using config: $CONFIG_PATH"
-	source "$CONFIG_PATH"
-fi
 
 
-# 2. setup the build instructions
-set_buildinfo
+main (){
+
+	# read the config
+	if [ -f "$CONFIG_PATH" ]; then
+		echo "Using config: $CONFIG_PATH"
+		source "$CONFIG_PATH"
+	fi
+
+	INSTALL_TYPE="pro"
+	while getopts :cudv flag; do
+	case "${flag}" in
+	c)
+		INSTALL_TYPE="ce"
+		;;
+	u)
+		echo "upgrading to pro version..."
+		INSTALL_TYPE="pro"
+		upgrade
+		exit 0
+		;;
+	d) 
+		echo "downgrading to community version..."
+		INSTALL_TYPE="ce"
+		downgrade
+		exit 0
+		;;
+	v)
+		usage
+		exit 0
+		;;
+	esac
+done
 
 
-set +e
+	# 1. print netmaker logo
+	print_logo
 
 
-# 3. install necessary packages
-install_dependencies
+	# 2. setup the build instructions
+	set_buildinfo
+	set +e
+	# 3. install necessary packages
+	install_dependencies
 
 
-# 4. install yq if necessary
-install_yq
+	# 4. install yq if necessary
+	install_yq
 
 
-set -e
+	set -e
 
 
-# 6. get user input for variables
-set_install_vars
+	# 6. get user input for variables
+	set_install_vars
 
 
-set +e
-cleanup
-set -e
+	set +e
+	cleanup
+	set -e
 
 
-# 7. get and set config files, startup docker-compose
-install_netmaker
+	# 7. get and set config files, startup docker-compose
+	install_netmaker
 
 
-set +e
+	set +e
 
 
-# 8. make sure Caddy certs are working
-test_connection
+	# 8. make sure Caddy certs are working
+	test_connection
 
 
-# 9. install the netmaker CLI
-setup_nmctl
+	# 9. install the netmaker CLI
+	setup_nmctl
 
 
-# 10. create a default mesh network for netmaker
-setup_mesh
+	# 10. create a default mesh network for netmaker
+	setup_mesh
 
 
-set -e
+	set -e
 
 
-# 11. add netclient to docker-compose and start it up
-setup_netclient
+	# 11. add netclient to docker-compose and start it up
+	setup_netclient
 
 
-# 12. make the netclient a default host and ingress gw
-configure_netclient
+	# 12. make the netclient a default host and ingress gw
+	configure_netclient
 
 
-# 13. print success message
-print_success
+	# 13. print success message
+	print_success
+}
 
 
+main "${@}"