github.com/alexander-fernandez/go-group@v0.0.0-20220317235734-2d15c54e6f5f/UtmStack-Setup-Linux.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"encoding/json"
     6  	"fmt"
     7  	"math/rand"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  )
    15  
    16  // To handle the license structure
    17  type licenseDataJSON struct {
    18  	ID                int
    19  	IrderID           int
    20  	ProductID         int
    21  	UserID            int
    22  	LicenseKey        string
    23  	ExpiresAt         string
    24  	ValidFor          int
    25  	Source            int
    26  	Status            int
    27  	TimesActivated    int
    28  	TimesActivatedMax int
    29  	CreatedAt         string
    30  	CreatedBy         int
    31  	UpdatedAt         string
    32  	UpdatedBy         int
    33  }
    34  
    35  // To handle the license structure
    36  type licenseJSON struct {
    37  	Success bool
    38  	Code    string
    39  	Message string
    40  	Data    licenseDataJSON
    41  }
    42  
    43  // To be used by the progress bar
    44  type myBar struct {
    45  	percent int64  // progress percentage
    46  	cur     int64  // current progress
    47  	total   int64  // total value for progress
    48  	rate    string // the actual progress bar to be printed
    49  	graph   string // the fill value for progress bar
    50  }
    51  
    52  // Global variables
    53  var adminuser, adminpass, domain, prefix, licAct, licExp, licName, licMail, licKey, version string
    54  var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
    55  var licensed bool
    56  
    57  func main() {
    58  	// Checking if user is root
    59  	if os.Geteuid() != 0 {
    60  		fmt.Println("[ERROR] => [UTMSTACK]: This Console installer must be run as ROOT user. Exiting ...")
    61  		os.Exit(1)
    62  	} else {
    63  		fmt.Println("# [UTMSTACK]: ROOT user ... Ok.")
    64  	}
    65  
    66  	// TEST if APT is Busy
    67  	// lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'
    68  	outPID, _ := exec.Command("sh", "-c", `lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'`).Output()
    69  	strPID := string(outPID)
    70  	trimPID := strings.TrimRight(strPID, "\n")
    71  
    72  	if trimPID != "" {
    73  		fmt.Println("[ERROR] => [UTMSTACK]: APT-GET Services are busy, PID:", trimPID)
    74  		os.Exit(1)
    75  	} else {
    76  		fmt.Println("# [UTMSTACK]: APT-GET Services ... Ok.")
    77  	}
    78  
    79  	// Section to select
    80  	version = selectInstallType()
    81  	if version == "u" {
    82  		fmt.Println("# [UTMSTACK]: Uninstalling UTMStack Deployment ...")
    83  		utmUninstall()
    84  		fmt.Println("# [UTMSTACK]: Uninstaller is Done ... Ok.")
    85  		os.Exit(0)
    86  	}
    87  
    88  	choice := selectProduct()
    89  
    90  	// Create elasticsearch user if user 1000 doesn't exists
    91  	outUser, _ := exec.Command("getent", "passwd", "1000").Output()
    92  	strUser := string(outUser)
    93  	trimUser := strings.TrimRight(strUser, "\n")
    94  
    95  	if trimUser == "" {
    96  		err := exec.Command("useradd", "-u", "1000", "elasticsearch").Run()
    97  		if err != nil {
    98  			fmt.Println("[ERROR]: Creating user elasticsearch =>", err)
    99  		}
   100  	}
   101  
   102  	// mkdir /etc/utmstack/
   103  	if err := os.Mkdir("/etc/utmstack", 0755); err != nil {
   104  		fmt.Println("[ERROR] => [UTMSTACK]: There was a problem creating: /etc/utmstack/", "| Error:", err)
   105  	} else {
   106  		fmt.Println("# [UTMSTACK]: Folder: /etc/utmstack Created ... Ok.")
   107  	}
   108  
   109  	// Copy End User License Agreement (EULA)| Legal and Privacy Information
   110  	copyLicense()
   111  
   112  	// Menu tu accept EULA
   113  	agreeEULA()
   114  
   115  	// Adding Ubuntu repositories
   116  
   117  	// echo "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs) main restricted universe multiverse" > /etc/apt/sources.list
   118  	fmt.Println("# [UTMSTACK]: Setting up the Ubuntu repository, branch UBUNTU ...")
   119  	cmd := exec.Command("sh", "-c", `echo "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs) main restricted universe multiverse" > /etc/apt/sources.list`)
   120  	cmd.Run()
   121  
   122  	// echo "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-updates main restricted universe multiverse" >> /etc/apt/sources.list
   123  	fmt.Println("# [UTMSTACK]: Setting up the Ubuntu repository, branch UBUNTU-UPDATES ...")
   124  	cmd = exec.Command("sh", "-c", `echo "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-updates main restricted universe multiverse" >> /etc/apt/sources.list`)
   125  	cmd.Run()
   126  
   127  	// echo "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-security main restricted universe multiverse" >> /etc/apt/sources.list
   128  	fmt.Println("# [UTMSTACK]: Setting up the Ubuntu repository, branch UBUNTU-SECURITY ...")
   129  	cmd = exec.Command("sh", "-c", `echo "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-security main restricted universe multiverse" >> /etc/apt/sources.list`)
   130  	cmd.Run()
   131  
   132  	// echo "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-backports main restricted universe multiverse" >> /etc/apt/sources.list
   133  	fmt.Println("# [UTMSTACK]: Setting up the Ubuntu repository, branch UBUNTU-BACKPORTS ...")
   134  	cmd = exec.Command("sh", "-c", `echo "deb http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-backports main restricted universe multiverse" >> /etc/apt/sources.list`)
   135  	cmd.Run()
   136  
   137  	// Updating repository index
   138  	fmt.Println("# [UTMSTACK]: Updating repository index ...")
   139  	cmd = exec.Command("apt", "-y", "update")
   140  	cmd.Stdout = os.Stdout
   141  	cmd.Run()
   142  
   143  	// TEST if APT is Busy
   144  	// lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'
   145  	outPID, _ = exec.Command("sh", "-c", `lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'`).Output()
   146  	strPID = string(outPID)
   147  	trimPID = strings.TrimRight(strPID, "\n")
   148  
   149  	if trimPID != "" {
   150  		fmt.Println("[ERROR] => [UTMSTACK]: APT-GET Services are busy, PID:", trimPID)
   151  		os.Exit(1)
   152  	} else {
   153  		fmt.Println("# [UTMSTACK]: APT-GET Services ... Ok.")
   154  	}
   155  
   156  	// Install curl
   157  	fmt.Println("# [UTMSTACK]: Installing: curl ...")
   158  	cmd = exec.Command("apt", "-y", "install", "curl")
   159  	cmd.Stdout = os.Stdout
   160  	cmd.Run()
   161  
   162  	// Testing Access to FrontEnd, BackEnd (war) and Docker Registry
   163  	testAccess()
   164  
   165  	// Create configuration files (application-prod.yml, compose.yml, config.json, utm_client.sql)
   166  	copyFiles()
   167  
   168  	if choice == "p" {
   169  		fmt.Println("Installing Proxy for Data Collection (Just Probe).")
   170  		proxyProbe()
   171  		fmt.Println("Data Collection Proxy Installation is Done ... Ok.")
   172  		os.Exit(0)
   173  	}
   174  
   175  	if choice == "m" {
   176  		fmt.Println("Installing Master and Infrastructure ...")
   177  		inputData()
   178  		genCert()
   179  		utmInstall()
   180  		fmt.Println("Master and Infrastructure Installer is Done ... Ok.")
   181  		os.Exit(0)
   182  	}
   183  }
   184  
   185  func utmInstall() { // Installer
   186  	// free --mega | awk 'NR==2{printf "%.i", $2}'
   187  	outRAM, _ := exec.Command("sh", "-c", `free --mega | awk 'NR==2{printf "%.i", $2}'`).Output()
   188  	strRAM := string(outRAM)
   189  	trimRAM := strings.TrimRight(strRAM, "\n")
   190  
   191  	// lscpu | grep -i 'CPU(s):' | awk '{printf "%.i", $2}'
   192  	outCPU, _ := exec.Command("sh", "-c", `lscpu | grep -i 'CPU(s):' | awk '{printf "%.i", $2}'`).Output()
   193  	strCPU := string(outCPU)
   194  	trimCPU := strings.TrimRight(strCPU, "\n")
   195  
   196  	// Selecting Server Size
   197  	intRAM, _ := strconv.Atoi(trimRAM)
   198  	intCPU, _ := strconv.Atoi(trimCPU)
   199  	max := 0
   200  	for {
   201  		fmt.Println("# [UTMSTACK]: Your resources, RAM:", trimRAM, "MB, CPU:", trimCPU, "UNITS")
   202  		fmt.Println("# [UTMSTACK]: According to this, you can install:")
   203  		max = 1
   204  		fmt.Println("(1) Undersized Server deployment")
   205  		if intRAM >= 16384 && intCPU >= 4 {
   206  			fmt.Println("(2) Tiny Server deployment (at least RAM:16Gb, CPU:4 units)")
   207  			max = 2
   208  		}
   209  		if intRAM >= 32768 && intCPU >= 8 {
   210  			fmt.Println("(3) Small Server deployment (at least RAM:32Gb, CPU:8 units)")
   211  			max = 3
   212  		}
   213  		if intRAM >= 65536 && intCPU >= 12 {
   214  			fmt.Println("(4) Medium Server deployment (at least RAM:64Gb, CPU:12 units)")
   215  			max = 4
   216  		}
   217  
   218  		fmt.Println("(Q) Quit the installation")
   219  		fmt.Print("Your answer is?: ")
   220  		size := ""
   221  		fmt.Scanln(&size)
   222  		opt := string(size)
   223  		if opt == "0" {
   224  			fmt.Println("# [UTMSTACK]: Exiting the installation ...")
   225  			os.Exit(1)
   226  		}
   227  
   228  		if opt == "1" {
   229  			intSize, _ := strconv.Atoi(size)
   230  			if intSize <= max {
   231  				err := exec.Command("sed", "-i", "s|JVM_CORD|2048|g", "/etc/utmstack/compose.yml").Run()
   232  				err = exec.Command("sed", "-i", "s|JAVA_MIN|512|g", "/etc/utmstack/compose.yml").Run()
   233  				err = exec.Command("sed", "-i", "s|JAVA_MAX|1024|g", "/etc/utmstack/compose.yml").Run()
   234  				if err != nil {
   235  					fmt.Println("[ERROR]:", err)
   236  				}
   237  				break
   238  			} else {
   239  				fmt.Println("[ERROR] => [UTMSTACK]: You CAN'T use this option.")
   240  				continue
   241  			}
   242  		}
   243  
   244  		if opt == "2" {
   245  			intSize, _ := strconv.Atoi(size)
   246  			if intSize <= max {
   247  				err := exec.Command("sed", "-i", "s|JVM_CORD|8192|g", "/etc/utmstack/compose.yml").Run()
   248  				err = exec.Command("sed", "-i", "s|JAVA_MIN|512|g", "/etc/utmstack/compose.yml").Run()
   249  				err = exec.Command("sed", "-i", "s|JAVA_MAX|1024|g", "/etc/utmstack/compose.yml").Run()
   250  				if err != nil {
   251  					fmt.Println("[ERROR]:", err)
   252  				}
   253  				break
   254  			} else {
   255  				fmt.Println("[ERROR] => [UTMSTACK]: You CAN'T use this option.")
   256  				continue
   257  			}
   258  		}
   259  
   260  		if opt == "3" {
   261  			intSize, _ := strconv.Atoi(size)
   262  			if intSize <= max {
   263  				err := exec.Command("sed", "-i", "s|JVM_CORD|16384|g", "/etc/utmstack/compose.yml").Run()
   264  				err = exec.Command("sed", "-i", "s|JAVA_MIN|1024|g", "/etc/utmstack/compose.yml").Run()
   265  				err = exec.Command("sed", "-i", "s|JAVA_MAX|2048|g", "/etc/utmstack/compose.yml").Run()
   266  				if err != nil {
   267  					fmt.Println("[ERROR]:", err)
   268  				}
   269  				break
   270  			} else {
   271  				fmt.Println("[ERROR] => [UTMSTACK]: You CAN'T use this option.")
   272  				continue
   273  			}
   274  		}
   275  
   276  		if opt == "4" {
   277  			intSize, _ := strconv.Atoi(size)
   278  			if intSize <= max {
   279  				err := exec.Command("sed", "-i", "s|JVM_CORD|32768|g", "/etc/utmstack/compose.yml").Run()
   280  				err = exec.Command("sed", "-i", "s|JAVA_MIN|1024|g", "/etc/utmstack/compose.yml").Run()
   281  				err = exec.Command("sed", "-i", "s|JAVA_MAX|2048|g", "/etc/utmstack/compose.yml").Run()
   282  				if err != nil {
   283  					fmt.Println("[ERROR]:", err)
   284  				}
   285  				break
   286  			} else {
   287  				fmt.Println("[ERROR] => [UTMSTACK]: You CAN'T use this option.")
   288  				continue
   289  			}
   290  		}
   291  
   292  		if opt == "q" || opt == "Q" {
   293  			fmt.Println("# [UTMSTACK]: Exiting the installation ...")
   294  			os.Exit(1)
   295  		}
   296  		fmt.Println("[ERROR] => [UTMSTACK]: (", opt, ") is NOT a valid choice.")
   297  	}
   298  
   299  	storage := []string{
   300  		"/utmstack/data",
   301  		"/utmstack/repo",
   302  	}
   303  
   304  	// Checking if "/utmstack/XXXX" exists
   305  	for _, s := range storage {
   306  		if _, err := os.Stat(s); os.IsNotExist(err) {
   307  			if err := os.MkdirAll(s, 0755); err != nil {
   308  				fmt.Println("[ERROR] => [UTMSTACK]: There was a problem creating:", s, "| Error:", err)
   309  			} else {
   310  				fmt.Println("# [UTMSTACK]: Folder:", s, "Created ... Ok.")
   311  			}
   312  		} else {
   313  			fmt.Println("# [UTMSTACK]: Checking:", s, "... Ok.")
   314  		}
   315  	}
   316  
   317  	if err := os.Mkdir("/utmstack/data/single", 0755); err != nil {
   318  		fmt.Println("[ERROR] => [UTMSTACK]: There was a problem creating: '/utmstack/data/single' | Error:", err)
   319  	} else {
   320  		fmt.Println("# [UTMSTACK]: Folder: '/utmstack/data/single' Created ... Ok.")
   321  	}
   322  
   323  	if err := os.Mkdir("/utmstack/repo/utm-geoip", 0755); err != nil {
   324  		fmt.Println("[ERROR] => [UTMSTACK]: There was a problem creating: '/utmstack/repo/utm-geoip' | Error:", err)
   325  	} else {
   326  		fmt.Println("# [UTMSTACK]: Folder: '/utmstack/repo/utm-geoip' Created ... Ok.")
   327  	}
   328  
   329  	// Download geoip.zip
   330  	// wget -O /opt/geoip.zip --ftp-user=upgrade --ftp-password='y#K=[swzk.(DQ7+[w;=#na/958yU$' ftp://registry.utmstack.com/geoip.zip
   331  	fmt.Println("# [UTMSTACK]: Downloading geoip.zip... (~211.2MB, it takes some minutes depending on your bandwidth)")
   332  	strCMD := "wget -O /opt/geoip.zip --ftp-user=upgrade --ftp-password='y#K=[swzk.(DQ7+[w;=#na/958yU$' ftp://registry.utmstack.com/geoip.zip"
   333  	cmd := exec.Command("/bin/bash", "-c", strCMD)
   334  	cmd.Stdout = os.Stdout
   335  	cmd.Run()
   336  
   337  	// Checking and copying geoip.zip
   338  	// unzip /opt/geoip.zip
   339  	// cp -R /opt/utm-geoip/* /utmstack/repo/utm-geoip
   340  	// rm -Rf /opt/utm-geoip/
   341  	if _, err := os.Stat("/opt/geoip.zip"); os.IsNotExist(err) {
   342  		fmt.Println("[ERROR] => [UTMSTACK]: Checking: /opt/geoip.zip failed, unable to extract it")
   343  	} else {
   344  		err = exec.Command("unzip", "/opt/geoip.zip", "-d", "/opt").Run()
   345  		strCMD := "cp -R /opt/utm-geoip/* /utmstack/repo/utm-geoip"
   346  		err = exec.Command("/bin/bash", "-c", strCMD).Run()
   347  		err = exec.Command("rm", "-Rf", "/opt/utm-geoip/").Run()
   348  		fmt.Println("# [UTMSTACK]: Extracting geoip.zip ... Ok.")
   349  	}
   350  
   351  	filepath.Walk("/utmstack", func(path string, f os.FileInfo, err error) error {
   352  		if e := os.Chown(path, 1000, 1000); e != nil {
   353  			fmt.Println("[ERROR] => [UTMSTACK]: There was a problem setting owner to:", path, "| Error:", e)
   354  		} else {
   355  			fmt.Println("# [UTMSTACK]: Setting owner to:", path, "... Ok.")
   356  		}
   357  		return nil
   358  	})
   359  
   360  	// TEST if APT is Busy
   361  	// lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'
   362  	outPID, _ := exec.Command("sh", "-c", `lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'`).Output()
   363  	strPID := string(outPID)
   364  	trimPID := strings.TrimRight(strPID, "\n")
   365  
   366  	if trimPID != "" {
   367  		fmt.Println("[ERROR] => [UTMSTACK]: APT-GET Services are busy, PID:", trimPID)
   368  		os.Exit(1)
   369  	} else {
   370  		fmt.Println("# [UTMSTACK]: APT-GET Services ... Ok.")
   371  	}
   372  
   373  	// Installing software-properties-common
   374  	fmt.Println("# [UTMSTACK]: Installing: software-properties-common ...")
   375  	cmd = exec.Command("apt", "-y", "install", "software-properties-common")
   376  	cmd.Stdout = os.Stdout
   377  	cmd.Run()
   378  
   379  	// wget --no-check-certificate --quiet https://download.docker.com/linux/ubuntu/gpg -O - | apt-key add -
   380  	strCMD = "wget --no-check-certificate --quiet https://download.docker.com/linux/ubuntu/gpg -O - | apt-key add -"
   381  	err := exec.Command("/bin/bash", "-c", strCMD).Run()
   382  	if err != nil {
   383  		fmt.Println("[ERROR]: Adding Docker's official GPG key to apt =>", err)
   384  	} else {
   385  		fmt.Println("# [UTMSTACK]: Add Docker's official GPG key to apt ... Ok.")
   386  	}
   387  
   388  	// add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
   389  	fmt.Println("# [UTMSTACK]: Setting up the stable repository for Docker ... Ok.")
   390  	cmd = exec.Command("sh", "-c", `add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"`)
   391  	cmd.Run()
   392  
   393  	// Updating repository index with Docker included
   394  	fmt.Println("# [UTMSTACK]: Updating repository index ...")
   395  	cmd = exec.Command("apt", "-y", "update")
   396  	cmd.Stdout = os.Stdout
   397  	cmd.Run()
   398  
   399  	// TEST if APT is Busy
   400  	// lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'
   401  	outPID, _ = exec.Command("sh", "-c", `lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'`).Output()
   402  	strPID = string(outPID)
   403  	trimPID = strings.TrimRight(strPID, "\n")
   404  
   405  	if trimPID != "" {
   406  		fmt.Println("[ERROR] => [UTMSTACK]: APT-GET Services are busy, PID:", trimPID)
   407  		os.Exit(1)
   408  	} else {
   409  		fmt.Println("# [UTMSTACK]: APT-GET Services ... Ok.")
   410  	}
   411  
   412  	// Installing apt-transport-https ca-certificates curl gnupg-agent software-properties-common zip unzip apache2-utils
   413  	fmt.Println("# [UTMSTACK]: Installing: apt-transport-https ca-certificates gnupg-agent software-properties-common zip unzip apache2-utils ...")
   414  	cmd = exec.Command("apt", "-y", "install", "apt-transport-https", "ca-certificates", "gnupg-agent", "software-properties-common", "zip", "unzip", "apache2-utils")
   415  	cmd.Stdout = os.Stdout
   416  	cmd.Run()
   417  
   418  	//htpasswd -nb $adminuser $adminpass
   419  	// out, _ := exec.Command("htpasswd", "-nb", adminuser, adminpass).Output()
   420  	// str := string(out)
   421  	// trim := strings.TrimRight(str, "\n")
   422  	//traefikPass := strings.Replace(trim, "$", "$$", -1)
   423  
   424  	// sed -i "s|utmstack_user|$adminuser|g" compose.yml application-prod.yml
   425  	patternUser := "s|utmstack_user|" + adminuser + "|g"
   426  	err = exec.Command("sed", "-i", patternUser, "/etc/utmstack/compose.yml", "/etc/utmstack/application-prod.yml", "/etc/utmstack/config.json").Run()
   427  	if err != nil {
   428  		fmt.Println("[ERROR]: Setting utmstack_user =>", err)
   429  	} else {
   430  		fmt.Println("# [UTMSTACK]: Setting Admin username ... Ok.")
   431  	}
   432  
   433  	// sed -i "s|utmstack_pass|$adminpass|g" compose.yml application-prod.yml
   434  	patternPass := "s|utmstack_pass|" + adminpass + "|g"
   435  	err = exec.Command("sed", "-i", patternPass, "/etc/utmstack/compose.yml", "/etc/utmstack/application-prod.yml", "/etc/utmstack/config.json").Run()
   436  	if err != nil {
   437  		fmt.Println("[ERROR]: Setting utmstack_pass =>", err)
   438  	} else {
   439  		fmt.Println("# [UTMSTACK]: Setting Admin password ... Ok.")
   440  	}
   441  
   442  	// sed -i "s|utmclient.utmstack.com|$domain|g" compose.yml application-prod.yml
   443  	patternDomain := "s|utmclient.utmstack.com|" + domain + "|g"
   444  	err = exec.Command("sed", "-i", patternDomain, "/etc/utmstack/compose.yml", "/etc/utmstack/application-prod.yml", "/etc/utmstack/vhost-elastic", "/etc/utmstack/vhost-utmstack", "/etc/utmstack/vhost-cerebro").Run()
   445  	if err != nil {
   446  		fmt.Println("[ERROR]: Setting domain =>", err)
   447  	} else {
   448  		fmt.Println("# [UTMSTACK]: Setting Domain FQDN ... Ok.")
   449  	}
   450  
   451  	// sysctl -w vm.swappiness=0
   452  	err = exec.Command("sysctl", "-w", "vm.swappiness=0").Run()
   453  	if err != nil {
   454  		fmt.Println("[ERROR]: Setting vm.swappiness =>", err)
   455  	} else {
   456  		fmt.Println("# [UTMSTACK]: Setting vm.swappiness=0 ... Ok.")
   457  	}
   458  
   459  	// sysctl -w vm.overcommit_memory=1
   460  	err = exec.Command("sysctl", "-w", "vm.overcommit_memory=1").Run()
   461  	if err != nil {
   462  		fmt.Println("[ERROR]: Setting vm.overcommit_memory", err)
   463  	} else {
   464  		fmt.Println("# [UTMSTACK]: Setting vm.overcommit_memory=1 ... Ok.")
   465  	}
   466  
   467  	// sysctl -w vm.max_map_count=4194304
   468  	err = exec.Command("sysctl", "-w", "vm.max_map_count=4194304").Run()
   469  	if err != nil {
   470  		fmt.Println("[ERROR]: vm.max_map_count", err)
   471  	} else {
   472  		fmt.Println("# [UTMSTACK]: Setting vm.max_map_count=4194304 ... Ok.")
   473  	}
   474  
   475  	// echo 'vm.swappiness=0' >> /etc/sysctl.conf
   476  	err = exec.Command("sh", "-c", `echo 'vm.swappiness=0' >> /etc/sysctl.conf`).Run()
   477  	if err != nil {
   478  		fmt.Println("[ERROR]:", err)
   479  	}
   480  
   481  	// echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf
   482  	err = exec.Command("sh", "-c", `echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf`).Run()
   483  	if err != nil {
   484  		fmt.Println("[ERROR]:", err)
   485  	}
   486  
   487  	// echo 'vm.max_map_count=4194304' >> /etc/sysctl.conf
   488  	err = exec.Command("sh", "-c", `echo 'vm.max_map_count=4194304' >> /etc/sysctl.conf`).Run()
   489  	if err != nil {
   490  		fmt.Println("[ERROR]:", err)
   491  	}
   492  
   493  	// TEST if APT is Busy
   494  	// lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'
   495  	outPID, _ = exec.Command("sh", "-c", `lsof /var/lib/dpkg/lock | awk 'NR==2{printf "%.i", $2}'`).Output()
   496  	strPID = string(outPID)
   497  	trimPID = strings.TrimRight(strPID, "\n")
   498  
   499  	if trimPID != "" {
   500  		fmt.Println("[ERROR] => [UTMSTACK]: APT-GET Services are busy, PID:", trimPID)
   501  		os.Exit(1)
   502  	} else {
   503  		fmt.Println("# [UTMSTACK]: APT-GET Services ... Ok.")
   504  	}
   505  
   506  	// apt -y install docker-ce docker-ce-cli containerd.io
   507  	fmt.Println("# [UTMSTACK]: Installing Docker Community Edition packages ...")
   508  	cmd = exec.Command("sh", "-c", `apt -y install docker-ce docker-ce-cli containerd.io`)
   509  	cmd.Stdout = os.Stdout
   510  	cmd.Run()
   511  
   512  	// systemctl enable docker.service
   513  
   514  	err = exec.Command("systemctl", "enable", "docker.service").Run()
   515  	if err != nil {
   516  		fmt.Println("[ERROR]:", err)
   517  	} else {
   518  		fmt.Println("# [UTMSTACK]: Enabling Docker as a permanent service ... Ok.")
   519  	}
   520  
   521  	// Pulling services images from Docker Hub
   522  	fmt.Println("# [UTMSTACK]: Pulling services images from UTMStack Registry ... ~4.168 Gigabytes in total")
   523  	// docker pull amazon/opendistro-for-elasticsearch:1.9.0
   524  	fmt.Println("# [UTMSTACK]: Pulling Elasticsearch ... (~1.12GB, it takes some minutes depending on your bandwidth)")
   525  	cmd = exec.Command("docker", "pull", "registry.utmstack.com:443/utm-elastic-aws")
   526  	cmd.Stdout = os.Stdout
   527  	cmd.Run()
   528  	// docker pull registry.utmstack.com:443/utm-scanner11
   529  	fmt.Println("# [UTMSTACK]: Pulling Scanner ... (~2.46GB, it takes some minutes depending on your bandwidth)")
   530  	cmd = exec.Command("docker", "pull", "registry.utmstack.com:443/utm-scanner11")
   531  	cmd.Stdout = os.Stdout
   532  	cmd.Run()
   533  	// docker pull utmstack/bionic-omp4war
   534  	fmt.Println("# [UTMSTACK]: Pulling Ubuntu Bionic ... (~219MB, it takes some minutes depending on your bandwidth)")
   535  	cmd = exec.Command("docker", "pull", "registry.utmstack.com:443/utm-bionic")
   536  	cmd.Stdout = os.Stdout
   537  	cmd.Run()
   538  	// docker pull postgres:12-alpine
   539  	fmt.Println("# [UTMSTACK]: Pulling PostgreSQL ... (~159MB, it takes some minutes depending on your bandwidth)")
   540  	cmd = exec.Command("docker", "pull", "registry.utmstack.com:443/utm-postgres")
   541  	cmd.Stdout = os.Stdout
   542  	cmd.Run()
   543  	// docker pull nginx:stable-alpine
   544  	fmt.Println("# [UTMSTACK]: Pulling Nginx ... (~21.8MB, it takes some minutes depending on your bandwidth)")
   545  	cmd = exec.Command("docker", "pull", "registry.utmstack.com:443/utm-nginx")
   546  	cmd.Stdout = os.Stdout
   547  	cmd.Run()
   548  
   549  	// TEST if tomcat.tar.gz EXISTS then remove
   550  	if fileExists("/opt/tomcat.tar.gz") {
   551  		strCMD = "rm /opt/tomcat.tar.gz /opt/tomcat.tar.gz.*"
   552  		exec.Command("/bin/bash", "-c", strCMD).Run()
   553  		fmt.Println("# [UTMSTACK]: Removed old version of file: tomcat.tar.gz ... Ok.")
   554  	}
   555  
   556  	// TEST if utm-stack.zip EXISTS then remove
   557  	if fileExists("/opt/utm-stack.zip") {
   558  		strCMD = "rm /opt/utm-stack.zip /opt/utm-stack.zip.*"
   559  		exec.Command("/bin/bash", "-c", strCMD).Run()
   560  		fmt.Println("# [UTMSTACK]: Removed old version of file: /opt/utm-stack.zip ... Ok.")
   561  	}
   562  
   563  	// Download tomcat.tar.gz
   564  	// wget -O /opt/tomcat.tar.gz --ftp-user=upgrade --ftp-password='y#K=[swzk.(DQ7+[w;=#na/958yU$' ftp://registry.utmstack.com/tomcat.tar.gz
   565  	fmt.Println("# [UTMSTACK]: Downloading tomcat.tar.gz... (~165MB, it takes some minutes depending on your bandwidth)")
   566  	strCMD = "wget -O /opt/tomcat.tar.gz --ftp-user=upgrade --ftp-password='y#K=[swzk.(DQ7+[w;=#na/958yU$' ftp://registry.utmstack.com/tomcat.tar.gz"
   567  	cmd = exec.Command("/bin/bash", "-c", strCMD)
   568  	cmd.Stdout = os.Stdout
   569  	cmd.Run()
   570  
   571  	// Download utm-stack.zip
   572  	// wget -O /opt/utm-stack.zip --ftp-user=upgrade --ftp-password='y#K=[swzk.(DQ7+[w;=#na/958yU$' ftp://registry.utmstack.com/utm-stack.zip
   573  	fmt.Println("# [UTMSTACK]: Downloading utm-stack.zip... (~7.8MB, it takes some minutes depending on your bandwidth)")
   574  	strCMD = "wget -O /opt/utm-stack.zip --ftp-user=upgrade --ftp-password='y#K=[swzk.(DQ7+[w;=#na/958yU$' ftp://registry.utmstack.com/utm-stack.zip"
   575  	cmd = exec.Command("/bin/bash", "-c", strCMD)
   576  	cmd.Stdout = os.Stdout
   577  	cmd.Run()
   578  
   579  	// Download updateUTM
   580  	// wget -O /opt/updateUTM --ftp-user=upgrade --ftp-password='y#K=[swzk.(DQ7+[w;=#na/958yU$' ftp://registry.utmstack.com/updateUTM
   581  	fmt.Println("# [UTMSTACK]: Downloading utm-stack.zip... (~2.2MB, it takes some minutes depending on your bandwidth)")
   582  	strCMD = "wget -O /opt/updateUTM --ftp-user=upgrade --ftp-password='y#K=[swzk.(DQ7+[w;=#na/958yU$' ftp://registry.utmstack.com/updateUTM"
   583  	cmd = exec.Command("/bin/bash", "-c", strCMD)
   584  	cmd.Stdout = os.Stdout
   585  	cmd.Run()
   586  
   587  	// docker swarm init --advertise-addr 127.0.0.1
   588  	fmt.Println("# [UTMSTACK]: Initializing Docker Swarm Mode ...")
   589  	cmd = exec.Command("docker", "swarm", "init", "--advertise-addr", "127.0.0.1")
   590  	cmd.Stdout = os.Stdout
   591  	cmd.Run()
   592  
   593  	// Pulling services images from Docker Hub
   594  	fmt.Println("# [UTMSTACK]: Creating Stack Infrastructure ...")
   595  
   596  	//   docker volume create vol_scanner_data
   597  	fmt.Println("# [UTMSTACK]: Creating Volume: vol_scanner_data ...")
   598  	cmd = exec.Command("docker", "volume", "create", "vol_scanner_data")
   599  	cmd.Stdout = os.Stdout
   600  	cmd.Run()
   601  
   602  	// docker volume create vol_postgres_data
   603  	fmt.Println("# [UTMSTACK]: Creating Volume: vol_postgres_data ...")
   604  	cmd = exec.Command("docker", "volume", "create", "vol_postgres_data")
   605  	cmd.Stdout = os.Stdout
   606  	cmd.Run()
   607  
   608  	// docker network create --driver=overlay --attachable net_utmstack
   609  	fmt.Println("# [UTMSTACK]: Creating Network: net_utmstack ...")
   610  	cmd = exec.Command("docker", "network", "create", "--driver=overlay", "--attachable", "net_utmstack")
   611  	cmd.Stdout = os.Stdout
   612  	cmd.Run()
   613  
   614  	// tar -C /opt/ -zxf /opt/tomcat.tar.gz
   615  	if fileExists("/opt/tomcat.tar.gz") {
   616  		strCMD = "tar -C /opt/ -zxf /opt/tomcat.tar.gz"
   617  		exec.Command("/bin/bash", "-c", strCMD).Run()
   618  		fmt.Println("# [UTMSTACK]: Extracted tomcat.tar.gz ... Ok.")
   619  	}
   620  
   621  	// Tunning /opt/tomcat before deploy
   622  	// ### Default root page configuration
   623  	// sed '150i\ \ \ \ \ \ \ \ <Context docBase="${catalina.home}/webapps" path="" debug="0" reloadable="true"/>' /opt/tomcat/conf/server.xml > content.xml
   624  	strCMD = "sed '150i\\ \\ \\ \\ \\ \\ \\ \\ <Context docBase=\"${catalina.home}/webapps\" path=\"\" debug=\"0\" reloadable=\"true\"/>' /opt/tomcat/conf/server.xml > content.xml"
   625  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   626  	if err != nil {
   627  		fmt.Println("[ERROR]:", err)
   628  	}
   629  
   630  	// cat content.xml > /opt/tomcat/conf/server.xml
   631  	strCMD = "cat content.xml > /opt/tomcat/conf/server.xml"
   632  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   633  	if err != nil {
   634  		fmt.Println("[ERROR]:", err)
   635  	}
   636  
   637  	// rm content.xml
   638  	err = exec.Command("rm", "content.xml").Run()
   639  	if err != nil {
   640  		fmt.Println("[ERROR]:", err)
   641  	} else {
   642  		fmt.Println("# [UTMSTACK]: Default root page configuration ... Ok.")
   643  	}
   644  
   645  	// ### Default error page configuration
   646  	// sed '4709i\ \ <error-page>' /opt/tomcat/conf/web.xml > error1.xml
   647  	strCMD = "sed '4709i\\ \\ <error-page>' /opt/tomcat/conf/web.xml > error1.xml"
   648  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   649  	if err != nil {
   650  		fmt.Println("[ERROR]:", err)
   651  	}
   652  
   653  	// sed '4710i\ \ \ \ <error-code>404</error-code>' error1.xml > error2.xml
   654  	strCMD = "sed '4710i\\ \\ \\ \\ <error-code>404</error-code>' error1.xml > error2.xml"
   655  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   656  	if err != nil {
   657  		fmt.Println("[ERROR]:", err)
   658  	}
   659  
   660  	// sed '4711i\ \ \ \ <location>/index.html</location>' error2.xml > error3.xml
   661  	strCMD = "sed '4711i\\ \\ \\ \\ <location>/index.html</location>' error2.xml > error3.xml"
   662  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   663  	if err != nil {
   664  		fmt.Println("[ERROR]:", err)
   665  	}
   666  
   667  	// sed '4712i\ \ </error-page>' error3.xml > error4.xml
   668  	strCMD = "sed '4712i\\ \\ </error-page>' error3.xml > error4.xml"
   669  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   670  	if err != nil {
   671  		fmt.Println("[ERROR]:", err)
   672  	}
   673  
   674  	// cat error4.xml > /opt/tomcat/conf/web.xml
   675  	strCMD = "cat error4.xml > /opt/tomcat/conf/web.xml"
   676  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   677  	if err != nil {
   678  		fmt.Println("[ERROR]:", err)
   679  	}
   680  
   681  	// rm error1.xml error2.xml error3.xml error4.xml
   682  	err = exec.Command("rm", "error1.xml", "error2.xml", "error3.xml", "error4.xml").Run()
   683  	if err != nil {
   684  		fmt.Println("[ERROR]:", err)
   685  	} else {
   686  		fmt.Println("# [UTMSTACK]: Default error page configuration ... Ok.")
   687  	}
   688  
   689  	// ### Adding Roles and Manager User
   690  	// sed '44i\ \ <role rolename="admin-gui"/>' /opt/tomcat/conf/tomcat-users.xml > role1.xml
   691  	strCMD = "sed '44i\\ \\ <role rolename=\"admin-gui\"/>' /opt/tomcat/conf/tomcat-users.xml > role1.xml"
   692  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   693  	if err != nil {
   694  		fmt.Println("[ERROR]:", err)
   695  	}
   696  
   697  	// sed '45i\ \ <role rolename="admin-script"/>' role1.xml > role2.xml
   698  	strCMD = "sed '45i\\ \\ <role rolename=\"admin-script\"/>' role1.xml > role2.xml"
   699  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   700  	if err != nil {
   701  		fmt.Println("[ERROR]:", err)
   702  	}
   703  
   704  	// sed '46i\ \ <role rolename="manager-gui"/>' role2.xml > role3.xml
   705  	strCMD = "sed '46i\\ \\ <role rolename=\"manager-gui\"/>' role2.xml > role3.xml"
   706  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   707  	if err != nil {
   708  		fmt.Println("[ERROR]:", err)
   709  	}
   710  
   711  	// sed '47i\ \ <role rolename="manager-script"/>' role4.xml > role4.xml
   712  	strCMD = "sed '47i\\ \\ <role rolename=\"manager-script\"/>' role3.xml > role4.xml"
   713  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   714  	if err != nil {
   715  		fmt.Println("[ERROR]:", err)
   716  	}
   717  
   718  	// sed '48i\ \ <role rolename="manager-status"/>' role4.xml > role5.xml
   719  	strCMD = "sed '48i\\ \\ <role rolename=\"manager-status\"/>' role4.xml > role5.xml"
   720  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   721  	if err != nil {
   722  		fmt.Println("[ERROR]:", err)
   723  	}
   724  
   725  	// sed '49i\ \ <user username="admin" password="blehblehbleh" roles="admin-gui,admin-script,manager-gui,manager-script,manager-status"/>' role5.xml > role6.xml
   726  	strCMD = "sed '49i\\ \\ <user username=\"" + adminuser + "\" password=\"" + adminpass + "\" roles=\"admin-gui,admin-script,manager-gui,manager-script,manager-status\"/>' role5.xml > role6.xml"
   727  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   728  	if err != nil {
   729  		fmt.Println("[ERROR]:", err)
   730  	}
   731  
   732  	// cat role6.xml > /opt/tomcat/conf/tomcat-users.xml
   733  	strCMD = "cat role6.xml > /opt/tomcat/conf/tomcat-users.xml"
   734  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   735  	if err != nil {
   736  		fmt.Println("[ERROR]:", err)
   737  	}
   738  
   739  	// rm role1.xml role2.xml ...
   740  	err = exec.Command("rm", "role1.xml", "role2.xml", "role3.xml", "role4.xml", "role5.xml", "role6.xml").Run()
   741  	if err != nil {
   742  		fmt.Println("[ERROR]:", err)
   743  	} else {
   744  		fmt.Println("# [UTMSTACK]: Adding Roles and Manager User ... Ok.")
   745  	}
   746  
   747  	// Allowing Manager Web from everywhere
   748  	// sed -i 's#127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1#.*#g' /opt/tomcat/webapps/manager/META-INF/context.xml
   749  	strCMD = "sed -i 's#127\\\\.\\\\d+\\\\.\\\\d+\\\\.\\\\d+|::1|0:0:0:0:0:0:0:1#.*#g' /opt/tomcat/webapps/manager/META-INF/context.xml"
   750  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   751  	if err != nil {
   752  		fmt.Println("[ERROR]:", err)
   753  	} else {
   754  		fmt.Println("# [UTMSTACK]: Allowing Manager Web from everywhere ... Ok.")
   755  	}
   756  
   757  	// cp /etc/utmstack/application-prod.yml /opt/tomcat/webapps/utmstack/WEB-INF/classes/config/application-prod.yml
   758  	err = exec.Command("cp", "/etc/utmstack/application-prod.yml", "/opt/tomcat/webapps/utmstack/WEB-INF/classes/config/application-prod.yml").Run()
   759  	if err != nil {
   760  		fmt.Println("[ERROR]:", err)
   761  	}
   762  
   763  	// rm -Rf /opt/tomcat/webapps/docs /opt/tomcat/webapps/examples /opt/tomcat/webapps/host-manager /opt/tomcat/webapps/ROOT
   764  	err = exec.Command("rm", "-Rf", "/opt/tomcat/webapps/docs", "/opt/tomcat/webapps/examples", "/opt/tomcat/webapps/host-manager", "/opt/tomcat/webapps/ROOT").Run()
   765  	if err != nil {
   766  		fmt.Println("[ERROR]:", err)
   767  	} else {
   768  		fmt.Println("# [UTMSTACK]: Cleaning Tomcat webapps ... Ok.")
   769  	}
   770  
   771  	// Checking and copying utm-stack.zip
   772  	// unzip utm-stack.zip
   773  	// cp -R utm-stack/* /opt/tomcat/webapps/
   774  	// rm -Rf utm-stack/
   775  	if _, err := os.Stat("/opt/utm-stack.zip"); os.IsNotExist(err) {
   776  		fmt.Println("[ERROR] => [UTMSTACK]: Checking: /opt/utm-stack.zip failed, unable to deploy it")
   777  	} else {
   778  		err = exec.Command("unzip", "/opt/utm-stack.zip", "-d", "/opt").Run()
   779  		strCMD = "cp -R /opt/utm-stack/* /opt/tomcat/webapps/"
   780  		err = exec.Command("/bin/bash", "-c", strCMD).Run()
   781  		err = exec.Command("rm", "-Rf", "/opt/utm-stack/").Run()
   782  		fmt.Println("# [UTMSTACK]: Deploying utm-stack.zip ... Ok.")
   783  	}
   784  
   785  	// Creating & Deploying Stack Services
   786  	fmt.Println("# [UTMSTACK]: Creating & Deploying Stack Services ...")
   787  	// docker stack deploy --compose-file=/etc/utmstack/compose.yml utm
   788  	cmd = exec.Command("docker", "stack", "deploy", "--compose-file=/etc/utmstack/compose.yml", "utm")
   789  	cmd.Stdout = os.Stdout
   790  	cmd.Run()
   791  
   792  	// Wait 120 seconds to let services to deploy
   793  	fmt.Println("# [UTMSTACK]: Waiting 2 minutes (120 seconds) to let services to deploy ...")
   794  	var bar myBar
   795  	bar.newBar(0, 120)
   796  	for i := 0; i <= 120; i++ {
   797  		time.Sleep(1 * time.Second)
   798  		bar.playBar(int64(i))
   799  	}
   800  	bar.finishBar()
   801  
   802  	fmt.Println("Starting Post-Install Configuration ...")
   803  
   804  	// ########################################## POSTCONFIG ##########################################
   805  
   806  	verified := ""
   807  	if licensed {
   808  		verified = "t"
   809  	} else {
   810  		verified = "f"
   811  	}
   812  
   813  	// INSERT utm_client TABLE
   814  	// INSERT INTO "public"."utm_client" (client_name, client_domain, client_prefix, client_mail, client_user, client_pass, client_licence_creation, client_licence_expire, client_licence_id, client_licence_verified) VALUES (install_name, domain, install_prefix, install_mail, install_user, install_pass, install_licence_id, install_licence_verified);
   815  	tmpAct, tmpExp := "", ""
   816  	//strCMD = "echo \"INSERT INTO \"public\".\"utm_client\" (client_name,client_domain,client_prefix,client_mail,client_user,client_pass,client_licence_creation,client_licence_expire,client_licence_id,client_licence_verified) VALUES ('" + licName + "','" + domain + "','" + prefix + "','" + licMail + "','" + adminuser + "','" + adminpass + "','" + licAct + "','" + licExp + "','" + licKey + "','" + verified + "');\" >> /etc/utmstack/utm_client.sql"
   817  	strCMD1 := "echo \"INSERT INTO \"public\".\"utm_client\" (client_name,client_domain,client_prefix,client_mail,client_user,client_pass,client_licence_creation,client_licence_expire,client_licence_id,client_licence_verified) VALUES ('" + licName + "','" + domain + "','" + prefix + "','" + licMail + "','" + adminuser + "','" + adminpass + "',"
   818  	strCMD2 := ",'" + licKey + "','" + verified + "');\" >> /etc/utmstack/utm_client.sql"
   819  
   820  	if licAct == "" {
   821  		tmpAct = "NULL"
   822  	} else {
   823  		tmpAct = "'" + licAct + "'"
   824  	}
   825  	if licExp == "" {
   826  		tmpExp = "NULL"
   827  	} else {
   828  		tmpExp = "'" + licExp + "'"
   829  	}
   830  	strCMD = strCMD1 + tmpAct + "," + tmpExp + strCMD2
   831  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   832  	if err != nil {
   833  		fmt.Println("[ERROR]:", err)
   834  	}
   835  
   836  	// cp /etc/utmstack/utm_client.sql /var/lib/docker/volumes/vol_postgres_data/_data/utm_client.sql
   837  	cmd = exec.Command("cp", "/etc/utmstack/utm_client.sql", "/var/lib/docker/volumes/vol_postgres_data/_data/utm_client.sql")
   838  	cmd.Run()
   839  
   840  	// chown 70:70 /var/lib/docker/volumes/vol_postgres_data/_data/utm_client.sql
   841  	strCMD = "chown 70:70 /var/lib/docker/volumes/vol_postgres_data/_data/utm_client.sql"
   842  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   843  	if err != nil {
   844  		fmt.Println("[ERROR]:", err)
   845  	}
   846  
   847  	// docker ps -lqf "name=^utm_postgres"
   848  	outPSQL, _ := exec.Command("docker", "ps", "-lqf", "name=^utm_postgres").Output()
   849  	strPSQL := string(outPSQL)
   850  	trimPSQL := strings.TrimRight(strPSQL, "\n")
   851  
   852  	// docker exec $postgresID psql -h localhost -d utmstack -U <user> -f /var/lib/postgresql/data/utm_client.sql
   853  	strCMD = "docker exec " + trimPSQL + " psql -h localhost -d utmstack -U " + adminuser + " -f /var/lib/postgresql/data/utm_client.sql"
   854  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
   855  	if err != nil {
   856  		fmt.Println("[ERROR]:", err)
   857  	} else {
   858  		fmt.Println("# [UTMSTACK]: Creating utm_client table ... Ok.")
   859  	}
   860  
   861  	// PUT _template/main_index
   862  	// {"index_patterns":["index-*"],"settings":{"index.mapping.total_fields.limit":10000,"opendistro.index_state_management.policy_id":"main_index_policy","opendistro.index_state_management.rollover_alias":"index-prefix","number_of_shards":3,"number_of_replicas":0}}
   863  	cfgCMD := "curl -X PUT 'http://localhost/_index_template/main_index' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"index_patterns\":[\"index-*\"],\"template\":{\"settings\":{\"index.mapping.total_fields.limit\":10000,\"opendistro.index_state_management.policy_id\":\"main_index_policy\",\"opendistro.index_state_management.rollover_alias\":\"index-" + string(prefix) + "\",\"number_of_shards\":3,\"number_of_replicas\":0}}}'"
   864  	fmt.Println("Main Index String:", cfgCMD)
   865  	outCfg, errCfg := exec.Command("/bin/bash", "-c", cfgCMD).Output()
   866  	fmt.Println("Output:", string(outCfg))
   867  	if errCfg != nil {
   868  		fmt.Println("[ERROR]:", errCfg)
   869  	} else {
   870  		fmt.Println("# [UTMSTACK]: PUT _index_template/main_index ... Ok.")
   871  	}
   872  
   873  	// PUT _template/generic_index
   874  	// {"index_patterns":["generic-*"],"settings":{"index.mapping.total_fields.limit":10000,"number_of_shards":1,"number_of_replicas":0}}
   875  	cfgCMD = "curl -X PUT 'http://localhost/_index_template/generic_index' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"index_patterns\":[\"generic-*\"],\"template\":{\"settings\":{\"index.mapping.total_fields.limit\":10000,\"number_of_shards\":1,\"number_of_replicas\":0}}}'"
   876  	fmt.Println("Generic String:", cfgCMD)
   877  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   878  	fmt.Println("Output:", string(outCfg))
   879  	if errCfg != nil {
   880  		fmt.Println("[ERROR]:", errCfg)
   881  	} else {
   882  		fmt.Println("# [UTMSTACK]: PUT _index_template/generic_index ... Ok.")
   883  	}
   884  
   885  	// PUT _template/dc_index
   886  	// {"index_patterns":["dc-*"],"settings":{"number_of_shards":1,"number_of_replicas":0}}
   887  	cfgCMD = "curl -X PUT 'http://localhost/_index_template/dc_index' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"index_patterns\":[\"dc-*\"],\"template\":{\"settings\":{\"number_of_shards\":1,\"number_of_replicas\":0}}}'"
   888  	fmt.Println("DC String:", cfgCMD)
   889  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   890  	fmt.Println("Output:", string(outCfg))
   891  	if errCfg != nil {
   892  		fmt.Println("[ERROR]:", errCfg)
   893  	} else {
   894  		fmt.Println("# [UTMSTACK]: PUT _index_template/dc_index ... Ok.")
   895  	}
   896  
   897  	// PUT _template/utmstack_index
   898  	// {"index_patterns":["utmstack-*"],"settings":{"number_of_shards":1,"number_of_replicas":0}}
   899  	cfgCMD = "curl -X PUT 'http://localhost/_index_template/utmstack_index' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"index_patterns\":[\"utmstack-*\"],\"template\":{\"settings\":{\"number_of_shards\":1,\"number_of_replicas\":0}}}'"
   900  	fmt.Println("UtmStack String:", cfgCMD)
   901  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   902  	fmt.Println("Output:", string(outCfg))
   903  	if errCfg != nil {
   904  		fmt.Println("[ERROR]:", errCfg)
   905  	} else {
   906  		fmt.Println("# [UTMSTACK]: PUT _index_template/utmstack_index ... Ok.")
   907  	}
   908  
   909  	// PUT _snapshot/main_index
   910  	// {"type":"fs","settings":{"location":"main_index"}}
   911  	cfgCMD = "curl -X PUT 'http://localhost/_snapshot/main_index' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"type\":\"fs\",\"settings\":{\"location\":\"main_index\"}}'"
   912  	fmt.Println("Repo String:", cfgCMD)
   913  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   914  	fmt.Println("Output:", string(outCfg))
   915  	if errCfg != nil {
   916  		fmt.Println("[ERROR]:", errCfg)
   917  	} else {
   918  		fmt.Println("# [UTMSTACK]: PUT _snapshot/main_index ... Ok.")
   919  	}
   920  
   921  	// PUT _opendistro/_ism/policies/main_index_policy
   922  	// {"policy":{"description":"MainIndexLifecycle","default_state":"ingest","states":[{"name":"ingest","actions":[{"rollover":{"min_doc_count":30000000,"min_size":"15gb"}}],"transitions":[{"state_name":"search"}]},{"name":"search","actions":[{"snapshot":{"repository":"main_index","snapshot":"incremental"}}],"transitions":[{"state_name":"read","conditions":{"min_index_age":"30d"}}]},{"name":"read","actions":[{"force_merge":{"max_num_segments":1}},{"snapshot":{"repository":"main_index","snapshot":"incremental"}}],"transitions":[]}]}}
   923  	cfgCMD = "curl -X PUT 'http://localhost/_opendistro/_ism/policies/main_index_policy' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"policy\":{\"description\":\"MainIndexLifecycle\",\"default_state\":\"ingest\",\"states\":[{\"name\":\"ingest\",\"actions\":[{\"rollover\":{\"min_doc_count\":30000000,\"min_size\":\"15gb\"}}],\"transitions\":[{\"state_name\":\"search\"}]},{\"name\":\"search\",\"actions\":[{\"snapshot\":{\"repository\":\"main_index\",\"snapshot\":\"incremental\"}}],\"transitions\":[{\"state_name\":\"read\",\"conditions\":{\"min_index_age\":\"30d\"}}]},{\"name\":\"read\",\"actions\":[{\"force_merge\":{\"max_num_segments\":1}},{\"snapshot\":{\"repository\":\"main_index\",\"snapshot\":\"incremental\"}}],\"transitions\":[]}]}}'"
   924  	fmt.Println("ISM String:", cfgCMD)
   925  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   926  	fmt.Println("Output:", string(outCfg))
   927  	if errCfg != nil {
   928  		fmt.Println("[ERROR]:", errCfg)
   929  	} else {
   930  		fmt.Println("# [UTMSTACK]: PUT _opendistro/_ism/policies/main_index_policy ... Ok.")
   931  	}
   932  
   933  	// PUT index-pnb-000001 {}
   934  	cfgCMD = "curl -X PUT 'http://localhost/index-" + string(prefix) + "-000001' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{}'"
   935  	fmt.Println("Index String:", cfgCMD)
   936  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   937  	fmt.Println("Output:", string(outCfg))
   938  	if errCfg != nil {
   939  		fmt.Println("[ERROR]:", errCfg)
   940  	} else {
   941  		fmt.Println("# [UTMSTACK]: PUT index-" + string(prefix) + "-000001 ... Ok.")
   942  	}
   943  
   944  	// POST _aliases
   945  	// {"actions":[{"add":{"index":"index-prefix-000001","alias":"index-prefix"}}]}
   946  	cfgCMD = "curl -X POST 'http://localhost/_aliases' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"actions\":[{\"add\":{\"index\":\"index-" + string(prefix) + "-000001\",\"alias\":\"index-" + prefix + "\"}}]}'"
   947  	fmt.Println("Alias String:", cfgCMD)
   948  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   949  	fmt.Println("Output:", string(outCfg))
   950  	if errCfg != nil {
   951  		fmt.Println("[ERROR]:", errCfg)
   952  	} else {
   953  		fmt.Println("# [UTMSTACK]: POST _aliases ... Ok.")
   954  	}
   955  
   956  	// Adding UTM-GEOIP Snapshot.
   957  	// PUT _snapshot/repo-utm-geoip
   958  	// {"type":"fs","settings":{"location":"utm-geoip"}}
   959  	cfgCMD = "curl -X PUT 'http://localhost/_snapshot/repo-utm-geoip' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"type\":\"fs\",\"settings\":{\"location\":\"utm-geoip\"}}'"
   960  	fmt.Println("Repo String:", cfgCMD)
   961  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   962  	fmt.Println("Output:", string(outCfg))
   963  	if errCfg != nil {
   964  		fmt.Println("[ERROR]:", errCfg)
   965  	} else {
   966  		fmt.Println("# [UTMSTACK]: PUT _snapshot/utm-geoip ... Ok.")
   967  	}
   968  
   969  	// Restoring UTM-GEOIP Snapshot.
   970  	// POST _snapshot/repo-utm-geoip/utm-geoip/_restore
   971  	// {"include_global_state": false}
   972  	cfgCMD = "curl -X POST 'http://localhost/_snapshot/repo-utm-geoip/utm-geoip/_restore' -H 'Host:elastic." + string(domain) + "' -H 'Content-Type: application/json' -d '{\"include_global_state\": false}'"
   973  	fmt.Println("Repo String:", cfgCMD)
   974  	outCfg, errCfg = exec.Command("/bin/bash", "-c", cfgCMD).Output()
   975  	fmt.Println("Output:", string(outCfg))
   976  	if errCfg != nil {
   977  		fmt.Println("[ERROR]:", errCfg)
   978  	} else {
   979  		fmt.Println("# [UTMSTACK]: PUT _snapshot/utm-geoip ... Ok.")
   980  	}
   981  
   982  	// DOUBLE CHECK for WAR DEPLOYMENT
   983  	// curl -ku admin:AdminPass https://www.utmclient.utmstack.com/manager/text/list
   984  	// curl -ku admin:AdminPass https://www.utmclient.utmstack.com/manager/text/start?path=/utmstack
   985  	fmt.Println("# [UTMSTACK]: Verify and/or redeploy Application ...")
   986  	startCMD := "curl -ku " + adminuser + ":" + adminpass + " https://www.utmclient.utmstack.com/manager/text/start?path=/utmstack"
   987  	cmd = exec.Command("/bin/bash", "-c", startCMD)
   988  	cmd.Stdout = os.Stdout
   989  	cmd.Run()
   990  
   991  	//(M)aster and Infrastructure (UTMStack)
   992  	fmt.Println("# [UTMSTACK]: Infrastructure and Services are installed, time for Master Data Collector ...")
   993  	answer := ""
   994  	opt := ""
   995  	fmt.Println("# [UTMSTACK]: Are you sure to proceed?")
   996  	for {
   997  		fmt.Println("(Y)es to continue with Master Data Collector.")
   998  		fmt.Println("(N)o to abort doing nothing.")
   999  		fmt.Print("Your answer is?: ")
  1000  		fmt.Scanln(&answer)
  1001  		opt = string(answer)
  1002  		if opt == "y" || opt == "Y" {
  1003  			break
  1004  		}
  1005  		if opt == "n" || opt == "N" {
  1006  			fmt.Println("# [UTMSTACK]: Exiting the installer ...")
  1007  			os.Exit(1)
  1008  		}
  1009  		answer = ""
  1010  		fmt.Println("[ERROR] => [UTMSTACK]: (", opt, ") is NOT a valid choice.")
  1011  	}
  1012  
  1013  	fmt.Println("# [UTMSTACK]: Installing Master Data Collector ...")
  1014  	// stack install [master|probe|aio]
  1015  	stackCMD := "/usr/local/bin/stack install aio"
  1016  
  1017  	// cp -R .aws /root/
  1018  	strCMD = "cp -R .aws /root/"
  1019  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
  1020  	if err != nil {
  1021  		fmt.Println("[ERROR]:", err)
  1022  	} else {
  1023  		fmt.Println("# [UTMSTACK]: Copying .aws configuration ... Ok.")
  1024  	}
  1025  
  1026  	// wget -O /usr/local/bin/stack https://updates.utmstack.com/assets/stack
  1027  	fmt.Println("# [UTMSTACK]: Downloading installer ...")
  1028  	cmd = exec.Command("wget", "-O", "/usr/local/bin/stack", "https://updates.utmstack.com/assets/stack")
  1029  	cmd.Stdout = os.Stdout
  1030  	cmd.Run()
  1031  
  1032  	// chmod +x /usr/local/bin/stack
  1033  	strCMD = "chmod +x /usr/local/bin/stack"
  1034  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
  1035  	if err != nil {
  1036  		fmt.Println("[ERROR]:", err)
  1037  	} else {
  1038  		fmt.Println("# [UTMSTACK]: Setting executable permission to installer ... Ok.")
  1039  	}
  1040  
  1041  	cmd = exec.Command("/bin/bash", "-c", stackCMD)
  1042  	cmd.Stdout = os.Stdout
  1043  	cmd.Run()
  1044  	return
  1045  }
  1046  
  1047  func utmUninstall() { // Uninstaller function.
  1048  	answer := ""
  1049  	opt := ""
  1050  	fmt.Println("# [UTMSTACK]: This is the UTM Stack UNINSTALLER ...")
  1051  	fmt.Println("# [UTMSTACK]: Are you sure to proceed?")
  1052  	for {
  1053  		fmt.Println("(Y)es to continue with the uninstallation.")
  1054  		fmt.Println("(N)o to abort doing nothing.")
  1055  		fmt.Print("Your answer is?: ")
  1056  		fmt.Scanln(&answer)
  1057  		opt = string(answer)
  1058  		if opt == "y" || opt == "Y" {
  1059  			fmt.Println("# [UTMSTACK]: The uninstaller proceeds ...")
  1060  			break
  1061  		}
  1062  		if opt == "n" || opt == "N" {
  1063  			fmt.Println("# [UTMSTACK]: Exiting the unistaller ...")
  1064  			os.Exit(1)
  1065  		}
  1066  		answer = ""
  1067  		fmt.Println("[ERROR] => [UTMSTACK]: (", opt, ") is NOT a valid choice.")
  1068  	}
  1069  
  1070  	// docker swarm leave --force
  1071  	fmt.Println("# [UTMSTACK]: Leaving Docker Swarm Stack ...")
  1072  	cmd := exec.Command("docker", "swarm", "leave", "--force")
  1073  	cmd.Stdout = os.Stdout
  1074  	cmd.Run()
  1075  
  1076  	//docker volume rm vol_scanner_data vol_postgres_data
  1077  	fmt.Println("# [UTMSTACK]: Removing Docker Volumes ...")
  1078  	cmd = exec.Command("docker", "volume", "rm", "vol_scanner_data", "vol_postgres_data")
  1079  	cmd.Stdout = os.Stdout
  1080  	cmd.Run()
  1081  
  1082  	// rm -Rf /var/lib/docker/volumes/vol_* /var/lib/docker/volumes/utm_*
  1083  	strCMD := "rm -Rf /var/lib/docker/volumes/vol_* /var/lib/docker/volumes/utm_*"
  1084  	cmd = exec.Command("/bin/bash", "-c", strCMD)
  1085  	cmd.Run()
  1086  
  1087  	// // add-apt-repository -r "deb [arch=amd64] http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs) main restricted universe multiverse"
  1088  	// fmt.Println("# [UTMSTACK]: Removing the Ubuntu repository, branch UBUNTU ...")
  1089  	// cmd = exec.Command("sh", "-c", `add-apt-repository -r "deb [arch=amd64] http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs) main restricted universe multiverse"`)
  1090  	// cmd.Run()
  1091  
  1092  	// // add-apt-repository -r "deb [arch=amd64] http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-updates main restricted universe multiverse"
  1093  	// fmt.Println("# [UTMSTACK]: Removing the Ubuntu repository, branch UBUNTU-UPDATES ...")
  1094  	// cmd = exec.Command("sh", "-c", `add-apt-repository -r "deb [arch=amd64] http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-updates main restricted universe multiverse"`)
  1095  	// cmd.Run()
  1096  
  1097  	// // add-apt-repository -r "deb [arch=amd64] http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-security main restricted universe multiverse"
  1098  	// fmt.Println("# [UTMSTACK]: Removing the Ubuntu repository, branch UBUNTU-SECURITY ...")
  1099  	// cmd = exec.Command("sh", "-c", `add-apt-repository -r "deb [arch=amd64] http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-security main restricted universe multiverse"`)
  1100  	// cmd.Run()
  1101  
  1102  	// // add-apt-repository -r "deb [arch=amd64] http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-backports main restricted universe multiverse"
  1103  	// fmt.Println("# [UTMSTACK]: Removing the Ubuntu repository, branch UBUNTU-BACKPORTS ...")
  1104  	// cmd = exec.Command("sh", "-c", `add-apt-repository -r "deb [arch=amd64] http://us.archive.ubuntu.com/ubuntu $(lsb_release -cs)-backports main restricted universe multiverse"`)
  1105  	// cmd.Run()
  1106  
  1107  	// // add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  1108  	// fmt.Println("# [UTMSTACK]: Removing the stable repository for Docker ...")
  1109  	// cmd = exec.Command("sh", "-c", `add-apt-repository -r "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"`)
  1110  	// cmd.Run()
  1111  
  1112  	// apt -y remove docker-ce docker-ce-cli containerd.io && apt -y purge docker-ce docker-ce-cli containerd.io && apt autoremove
  1113  	fmt.Println("# [UTMSTACK]: Removing Docker packages ...")
  1114  	strCMD = "apt -y remove docker-ce docker-ce-cli containerd.io && apt -y purge docker-ce docker-ce-cli containerd.io"
  1115  	cmd = exec.Command("/bin/bash", "-c", strCMD)
  1116  	cmd.Stdout = os.Stdout
  1117  	cmd.Stdin = os.Stdin
  1118  	cmd.Run()
  1119  
  1120  	// apt -y remove apt-transport-https ca-certificates curl gnupg-agent software-properties-common && apt -y purge apt-transport-https ca-certificates curl gnupg-agent software-properties-common && apt autoremove
  1121  	fmt.Println("# [UTMSTACK]: Removing Support packages ...")
  1122  	strCMD = "apt -y remove apt-transport-https ca-certificates curl gnupg-agent software-properties-common && apt -y purge apt-transport-https ca-certificates curl gnupg-agent software-properties-common && apt autoremove"
  1123  	cmd = exec.Command("/bin/bash", "-c", strCMD)
  1124  	cmd.Stdout = os.Stdout
  1125  	cmd.Stdin = os.Stdin
  1126  	cmd.Run()
  1127  
  1128  	err := exec.Command("rm", "/opt/tomcat.tar.gz").Run()
  1129  	if err != nil {
  1130  		fmt.Println("[ERROR]:", err)
  1131  	} else {
  1132  		fmt.Println("# [UTMSTACK]: Removing /opt/tomcat.tar.gz ... Ok.")
  1133  	}
  1134  
  1135  	err = exec.Command("rm", "/opt/utm-stack.zip").Run()
  1136  	if err != nil {
  1137  		fmt.Println("[ERROR]:", err)
  1138  	} else {
  1139  		fmt.Println("# [UTMSTACK]: Removing /opt/utm-stack.zip ... Ok.")
  1140  	}
  1141  
  1142  	err = exec.Command("rm", "-Rf", "/opt/tomcat").Run()
  1143  	if err != nil {
  1144  		fmt.Println("[ERROR]:", err)
  1145  	} else {
  1146  		fmt.Println("# [UTMSTACK]: Removing /opt/tomcat ... Ok.")
  1147  	}
  1148  
  1149  	fmt.Println("# [UTMSTACK]: There will be a remnant inside some folders and files:")
  1150  	fmt.Println("# - /utmstack/data (Data from Nodes)")
  1151  	fmt.Println("# - /utmstack/repo (Backups of your Data)")
  1152  	fmt.Println("# - /opt/tomcat (Application Folder)")
  1153  	fmt.Println("# - /etc/apt/sources.list (Docker repository)")
  1154  	fmt.Println("# - /var/lib/docker/* (Docker Folders)")
  1155  	fmt.Println("# [UTMSTACK]: If you are absolutely sure you won't need this content for the future, just delete these folders and remove lines concerning Docker Repository.")
  1156  	return
  1157  }
  1158  
  1159  func proxyProbe() { // Data Collector
  1160  	scanner := bufio.NewScanner(os.Stdin)
  1161  	// Ask for PostgreSQL credentials
  1162  	fmt.Println("# [UTMSTACK]: Enter credentials for PostgreSQL user.")
  1163  	fmt.Print("Your PostgreSQL Username: ")
  1164  	scanner.Scan()
  1165  	adminuser := scanner.Text()
  1166  
  1167  	fmt.Print("Your PostgreSQL Password: ")
  1168  	scanner.Scan()
  1169  	adminpass := scanner.Text()
  1170  
  1171  	// stack install [master|probe|aio]
  1172  	stackCMD := "/usr/local/bin/stack install probe"
  1173  
  1174  	// mkdir /etc/utmstack/
  1175  	if err := os.Mkdir("/etc/utmstack", 0755); err != nil {
  1176  		fmt.Println("[ERROR] => [UTMSTACK]: There was a problem creating: /etc/utmstack/", "| Error:", err)
  1177  	} else {
  1178  		fmt.Println("# [UTMSTACK]: Folder: /etc/utmstack Created ... Ok.")
  1179  	}
  1180  
  1181  	// sed -i "s|utmstack_user|$adminuser|g" compose.yml application-prod.yml
  1182  	patternUser := "s|utmstack_user|" + adminuser + "|g"
  1183  	err := exec.Command("sed", "-i", patternUser, "/etc/utmstack/config.json").Run()
  1184  	if err != nil {
  1185  		fmt.Println("[ERROR]:", err)
  1186  	} else {
  1187  		fmt.Println("# [UTMSTACK]: Setting Admin username ... Ok.")
  1188  	}
  1189  
  1190  	// sed -i "s|utmstack_pass|$adminpass|g" compose.yml application-prod.yml
  1191  	patternPass := "s|utmstack_pass|" + adminpass + "|g"
  1192  	err = exec.Command("sed", "-i", patternPass, "/etc/utmstack/config.json").Run()
  1193  	if err != nil {
  1194  		fmt.Println("[ERROR]:", err)
  1195  	} else {
  1196  		fmt.Println("# [UTMSTACK]: Setting Admin password ... Ok.")
  1197  	}
  1198  
  1199  	// cp -R .aws /root/
  1200  	strCMD := "cp -R .aws /root/"
  1201  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
  1202  	if err != nil {
  1203  		fmt.Println("[ERROR]:", err)
  1204  	} else {
  1205  		fmt.Println("# [UTMSTACK]: Copying .aws configuration ... Ok.")
  1206  	}
  1207  
  1208  	// wget -O /usr/local/bin/stack https://updates.utmstack.com/assets/stack
  1209  	fmt.Println("# [UTMSTACK]: Downloading installer ...")
  1210  	cmd := exec.Command("wget", "-O", "/usr/local/bin/stack", "https://updates.utmstack.com/assets/stack")
  1211  	cmd.Stdout = os.Stdout
  1212  	cmd.Run()
  1213  
  1214  	// chmod +x /usr/local/bin/stack
  1215  	strCMD = "chmod +x /usr/local/bin/stack"
  1216  	err = exec.Command("/bin/bash", "-c", strCMD).Run()
  1217  	if err != nil {
  1218  		fmt.Println("[ERROR]:", err)
  1219  	} else {
  1220  		fmt.Println("# [UTMSTACK]: Setting executable permission to installer ... Ok.")
  1221  	}
  1222  
  1223  	cmd = exec.Command("/bin/bash", "-c", stackCMD)
  1224  	cmd.Stdout = os.Stdout
  1225  	cmd.Run()
  1226  	return
  1227  }
  1228  
  1229  func agreeEULA() {
  1230  	answer := ""
  1231  	opt := ""
  1232  	for {
  1233  		fmt.Println("# ----------------------------------------------")
  1234  		fmt.Println("# [UTMSTACK]: End User License Agreement (EULA).")
  1235  		fmt.Println("# ----------------------------------------------")
  1236  		fmt.Println("# [UTMSTACK]: How do you want to proceed?")
  1237  		fmt.Println("(A)ccept the agreement and continue.")
  1238  		fmt.Println("(R)eview the UTMStack License. (Hit 'Q' or 'q' to leave the editor)")
  1239  		fmt.Println("(C)ancel the Setup and do Nothing.")
  1240  		fmt.Print("Your answer is?: ")
  1241  		fmt.Scanln(&answer)
  1242  		opt = string(answer)
  1243  		if opt == "a" || opt == "A" {
  1244  			fmt.Println("# [UTMSTACK]: Since you accept the EULA, the setup proceeds ...")
  1245  			break
  1246  		}
  1247  		if opt == "r" || opt == "R" {
  1248  			cmd := exec.Command("less", "/etc/utmstack/License-Private-UTMStack.md")
  1249  			cmd.Stdout = os.Stdout
  1250  			cmd.Run()
  1251  			continue
  1252  		}
  1253  		if opt == "c" || opt == "C" {
  1254  			fmt.Println("# [UTMSTACK]: Exiting the setup ...")
  1255  			os.Exit(0)
  1256  		}
  1257  		answer = ""
  1258  		fmt.Println("[ERROR] => [UTMSTACK]: (", opt, ") is NOT a valid choice.")
  1259  	}
  1260  }
  1261  
  1262  func selectProduct() string { // Select Installation Option
  1263  	answer := ""
  1264  	for {
  1265  		fmt.Println("# ----------------------------------------")
  1266  		fmt.Println("# [UTMSTACK]: Select Product Setup Option.")
  1267  		fmt.Println("# ----------------------------------------")
  1268  		fmt.Println("(M)aster and Infrastructure (UTMStack).")
  1269  		fmt.Println("(P)roxy for Data Collection (Just Probe).")
  1270  		fmt.Println("(C)ancel the Setup and do Nothing.")
  1271  		fmt.Println("# ----------------------------------------")
  1272  		fmt.Print("Your answer is?: ")
  1273  		fmt.Scanln(&answer)
  1274  		opt := string(answer)
  1275  		if opt == "m" || opt == "M" {
  1276  			return "m"
  1277  		}
  1278  		if opt == "p" || opt == "P" {
  1279  			return "p"
  1280  		}
  1281  		if opt == "c" || opt == "C" {
  1282  			os.Exit(0)
  1283  		}
  1284  		answer = ""
  1285  		fmt.Println("[ERROR] => [UTMSTACK]: (", opt, ") is NOT a valid choice.")
  1286  	}
  1287  }
  1288  
  1289  func inputData() { // Inputs for adminuser, adminpass, domain, prefix, licName, licMail, licKey
  1290  	scanner := bufio.NewScanner(os.Stdin)
  1291  	rawprefix := ""
  1292  	install := ""
  1293  	left := 0
  1294  	for {
  1295  		adminuser, adminpass, domain, rawprefix, licName, licMail, licKey, install = "", "", "", "", "", "", "", ""
  1296  		licensed = false
  1297  
  1298  		// Ask for credentials, domain and License
  1299  		fmt.Println("# [UTMSTACK]: Enter credentials for administrator user.")
  1300  		fmt.Print("Your Admin Username: ")
  1301  		scanner.Scan()
  1302  		adminuser = scanner.Text()
  1303  
  1304  		fmt.Print("Your Admin Password: ")
  1305  		scanner.Scan()
  1306  		adminpass = scanner.Text()
  1307  
  1308  		fmt.Print("Your Domain FQDN is: ")
  1309  		scanner.Scan()
  1310  		domain = scanner.Text()
  1311  
  1312  		fmt.Print("Your Name Prefix (ex. mycompany) (10 chars MAX) is: ")
  1313  		scanner.Scan()
  1314  		rawprefix = scanner.Text()
  1315  
  1316  		if version == "l" {
  1317  			fmt.Println("# [UTMSTACK]: ----------- LICENSE DETAILS ----------")
  1318  			fmt.Print("Your LICENSE Key is: ")
  1319  			scanner.Scan()
  1320  			licKey = scanner.Text()
  1321  		}
  1322  
  1323  		if rawprefix == "" {
  1324  			prefix = randomString(10)
  1325  		} else {
  1326  			prefix = trimPrefix(rawprefix)
  1327  		}
  1328  
  1329  		if adminuser == "" || adminpass == "" || domain == "" {
  1330  			fmt.Print("[ERROR] => [UTMSTACK]: Empty Fields: | ")
  1331  			if adminuser == "" {
  1332  				fmt.Print("adminuser | ")
  1333  			}
  1334  			if adminpass == "" {
  1335  				fmt.Print("adminpass | ")
  1336  			}
  1337  			if domain == "" {
  1338  				fmt.Print("domain | ")
  1339  			}
  1340  			fmt.Println(" ")
  1341  			fmt.Println("# [UTMSTACK]: These inputs MUST have a non-empty value. Starting over ...")
  1342  			fmt.Println("# [UTMSTACK]: -----------------------------------------------------------")
  1343  			continue
  1344  		}
  1345  
  1346  		if version == "l" {
  1347  			// Checking days left on License
  1348  			left = licenseDays(licKey)
  1349  		}
  1350  
  1351  		if left > 0 {
  1352  			install = "LICENSED VERSION"
  1353  			licensed = true
  1354  
  1355  		} else {
  1356  			install = "FREE VERSION"
  1357  			licensed = false
  1358  		}
  1359  
  1360  		for {
  1361  			fmt.Println("# [UTMSTACK]: ---------------------------------------")
  1362  			fmt.Println("# [UTMSTACK]: VERIFY YOUR INPUTS ...")
  1363  			fmt.Println("# [UTMSTACK]: ---------------------------------------")
  1364  			fmt.Println("# [UTMSTACK]: Admin Username:", adminuser)
  1365  			fmt.Println("# [UTMSTACK]: Admin Password:", adminpass)
  1366  			fmt.Println("# [UTMSTACK]: Domain FQDN is:", domain)
  1367  			fmt.Println("# [UTMSTACK]: Company Prefix:", prefix)
  1368  			if licensed == true {
  1369  				fmt.Println("# [UTMSTACK]: ---------------------------------------")
  1370  				fmt.Println("# [UTMSTACK]: LICENSE Key is:", licKey)
  1371  				fmt.Println("# [UTMSTACK]: ---------------------------------------")
  1372  				fmt.Println("# [UTMSTACK]: LICENSE Validity:", left, "days left.")
  1373  				fmt.Println("# [UTMSTACK]: Install type is:", install)
  1374  			}
  1375  			fmt.Println("# [UTMSTACK]: ---------------------------------------")
  1376  			fmt.Println("# [UTMSTACK]: Is this correct to proceed?")
  1377  			fmt.Println("(Y)es to continue")
  1378  			fmt.Println("(N)o to repeat the INPUTS")
  1379  			fmt.Println("(C)ancel the Setup and do Nothing.")
  1380  			fmt.Print("Your answer is?: ")
  1381  			answer := ""
  1382  			fmt.Scanln(&answer)
  1383  			opt := string(answer)
  1384  			if opt == "y" || opt == "Y" {
  1385  				return
  1386  			}
  1387  			if opt == "n" || opt == "N" {
  1388  				fmt.Println("# [UTMSTACK]: Ok. Starting over ...")
  1389  				break
  1390  			}
  1391  			if opt == "c" || opt == "C" {
  1392  				os.Exit(0)
  1393  			}
  1394  			fmt.Println("[ERROR] => [UTMSTACK]: (", opt, ") is NOT a valid choice.")
  1395  			continue
  1396  		}
  1397  	}
  1398  }
  1399  
  1400  func licenseGet(key string) licenseJSON {
  1401  	var result licenseJSON
  1402  	// curl -k "https://utmstack.com/wp-json/lmfwc/v2/licenses/[key]?consumer_key=ck_ad89e93e4d5ea9435c5736cd5c7a2ebcf0e26f3c&consumer_secret=cs_c5fd29d6a453bb0d3f318620e53a00ea1fb555aa"
  1403  	verify := `curl -k "https://utmstack.com/wp-json/lmfwc/v2/licenses/` + key + `?consumer_key=ck_ad89e93e4d5ea9435c5736cd5c7a2ebcf0e26f3c&consumer_secret=cs_c5fd29d6a453bb0d3f318620e53a00ea1fb555aa"`
  1404  	outChk, errChk := exec.Command("/bin/bash", "-c", verify).Output()
  1405  	errJSON := json.Unmarshal(outChk, &result)
  1406  	if errChk != nil {
  1407  		fmt.Println("[ERROR] => [UTMSTACK]: There was an error trying to verify License:", errChk)
  1408  	}
  1409  	if errJSON != nil {
  1410  		fmt.Println("[ERROR] => [UTMSTACK]: There was an error trying to decode License:", errJSON)
  1411  	}
  1412  	return result
  1413  }
  1414  
  1415  func licenseDays(key string) int {
  1416  	live := 0
  1417  	now := time.Now()
  1418  	nowDate := now.Format("2006-01-02 15:04:05")
  1419  	license := licenseGet(key)
  1420  	if license.Success {
  1421  		if license.Data.Status == 2 {
  1422  			license.printString()
  1423  			live = dateSub(nowDate, license.Data.ExpiresAt)
  1424  		} else {
  1425  			fmt.Println("[ERROR] => [UTMSTACK]: License Status Invalid:", string(live))
  1426  		}
  1427  	} else {
  1428  		fmt.Println("[ERROR] => [UTMSTACK]: License Validity Check Failed.")
  1429  		fmt.Println("   Code:", string(license.Code))
  1430  		fmt.Println("Message:", string(license.Message))
  1431  	}
  1432  	return live
  1433  }
  1434  
  1435  func dateSub(active string, expire string) int {
  1436  	dateActive, _ := time.Parse("2006-01-02 15:04:05", active)
  1437  	dateExpire, _ := time.Parse("2006-01-02 15:04:05", expire)
  1438  	days := dateExpire.Sub(dateActive).Hours() / 24
  1439  	return int(days)
  1440  }
  1441  
  1442  func (l licenseJSON) printString() {
  1443  	licAct = l.Data.CreatedAt
  1444  	licExp = l.Data.ExpiresAt
  1445  	status := ""
  1446  	switch l.Data.Status {
  1447  	case 2:
  1448  		status = "Delivered"
  1449  	case 3:
  1450  		status = "Active"
  1451  	case 4:
  1452  		status = "Inactive"
  1453  	default:
  1454  		status = "UnKnown"
  1455  	}
  1456  	fmt.Println("    License Key:", string(l.Data.LicenseKey))
  1457  	fmt.Println("     Expires At:", string(l.Data.ExpiresAt))
  1458  	fmt.Println("      Valid For:", strconv.Itoa(l.Data.ValidFor))
  1459  	fmt.Println("         Status:", status)
  1460  	fmt.Println("Times Activated:", strconv.Itoa(l.Data.TimesActivated))
  1461  	fmt.Println("     Created At:", string(l.Data.CreatedAt))
  1462  	fmt.Println("     Updated At:", string(l.Data.UpdatedAt))
  1463  }
  1464  
  1465  func (bar *myBar) newBar(start, total int64) { // Progress NewBar
  1466  	bar.cur = start
  1467  	bar.total = total
  1468  	if bar.graph == "" {
  1469  		bar.graph = "#"
  1470  	}
  1471  	bar.percent = bar.getPercent()
  1472  	for i := 0; i < int(bar.percent); i += 2 {
  1473  		bar.rate += bar.graph // initial progress position
  1474  	}
  1475  }
  1476  
  1477  func (bar *myBar) getPercent() int64 { // Progress getPercent
  1478  	return int64(float32(bar.cur) / float32(bar.total) * 100)
  1479  }
  1480  
  1481  func (bar *myBar) playBar(cur int64) { // Progress Play
  1482  	bar.cur = cur
  1483  	last := bar.percent
  1484  	bar.percent = bar.getPercent()
  1485  	if bar.percent != last && bar.percent%2 == 0 {
  1486  		bar.rate += bar.graph
  1487  	}
  1488  	fmt.Printf("\r[%-50s]%3d%% %8d/%d", bar.rate, bar.percent, bar.cur, bar.total)
  1489  }
  1490  
  1491  func (bar *myBar) finishBar() { // Progress Finish
  1492  	bar.percent = 0
  1493  	bar.cur = 0
  1494  	bar.total = 0
  1495  	bar.rate = ""
  1496  	bar.graph = ""
  1497  	fmt.Println()
  1498  }
  1499  
  1500  func trimPrefix(prefix string) string {
  1501  	trimPrefix := strings.Replace(prefix, " ", "", -1)
  1502  	lowerPrefix := strings.ToLower(trimPrefix)
  1503  	if len(lowerPrefix) > 10 {
  1504  		return lowerPrefix[:10]
  1505  	}
  1506  	return lowerPrefix
  1507  }
  1508  
  1509  func fileExists(filename string) bool {
  1510  	info, err := os.Stat(filename)
  1511  	if os.IsNotExist(err) {
  1512  		return false
  1513  	}
  1514  	return !info.IsDir()
  1515  }
  1516  
  1517  func copyFiles() { // Create installation files
  1518  	// File utmstack_utm_client.sql
  1519  	utmClient := `CREATE TABLE IF NOT EXISTS "public"."utm_client" ("id" SERIAL PRIMARY KEY, "client_name" varchar(100), "client_domain" varchar(100), "client_prefix" varchar(10), "client_mail" varchar(100), "client_user" varchar(50), "client_pass" varchar(50), "client_licence_creation" timestamp(0), "client_licence_expire" timestamp(0), "client_licence_id" varchar(100), "client_licence_verified" bool NOT NULL);
  1520  `
  1521  	// File utmstack_compose.yml
  1522  	utmCompose := `version: "3.7"
  1523  ### STACK VOLUMES
  1524  volumes:
  1525    # POSTGRESQL
  1526    vol_postgres_data:
  1527      external: true
  1528    # Scanner
  1529    vol_scanner_data:
  1530      external: true
  1531  ### STACK NETWORKS
  1532  networks:
  1533    net_utmstack:
  1534      external: true
  1535  ### STACK SERVICES
  1536  services:
  1537  # -------------------------------------------------- NEW SERVICE -------------------------------------------------- #
  1538    ### NGINX, LOAD BALANCER & REVERSE PROXY
  1539    nginx:
  1540      image: registry.utmstack.com:443/utm-nginx
  1541      volumes:
  1542        - /etc/utmstack/vhost-elastic:/etc/nginx/conf.d/default.conf
  1543        - /etc/utmstack/vhost-utmstack:/etc/nginx/conf.d/utmstack.conf
  1544        - /etc/utmstack/vhost-cerebro:/etc/nginx/conf.d/cerebro.conf
  1545        - /etc/utmstack/single.crt:/etc/nginx/single.crt
  1546        - /etc/utmstack/single.key:/etc/nginx/single.key
  1547      networks:
  1548        - net_utmstack
  1549      ports:
  1550        - "80:80"     # HTTP
  1551        - "443:443"   # HTTPS
  1552      deploy:
  1553        placement:
  1554          constraints: [node.role==manager]
  1555        mode: global
  1556  # -------------------------------------------------- NEW SERVICE -------------------------------------------------- #
  1557    ### ELASTIC SEARCH, Single Nodes
  1558    es-elastic:
  1559      image: registry.utmstack.com:443/utm-elastic-aws
  1560      volumes:
  1561        - /utmstack/data/single:/usr/share/elasticsearch/data
  1562        - /utmstack/repo:/usr/share/elasticsearch/backup
  1563        - /etc/localtime:/etc/localtime:ro
  1564      networks:
  1565        - net_utmstack
  1566      environment:
  1567        - node.name=es-elastic
  1568        - cluster.name=es-cluster
  1569        - network.host=_eth0_
  1570        - opendistro_security.disabled=true
  1571        - discovery.type=single-node
  1572        - path.repo=/usr/share/elasticsearch/backup
  1573        - "ES_JAVA_OPTS=-XmsJVM_CORDm -XmxJVM_CORDm"
  1574      deploy:
  1575        endpoint_mode: dnsrr
  1576        mode: global
  1577        placement:
  1578          constraints: [node.role==manager]
  1579  # -------------------------------------------------- NEW SERVICE -------------------------------------------------- #
  1580    ### CEREBRO, ELASTICSEARCH WEB ADMIN TOOL
  1581    cerebro:
  1582      image: registry.utmstack.com:443/utm-cerebro
  1583      volumes:
  1584        - /etc/localtime:/etc/localtime:ro
  1585      networks:
  1586        - net_utmstack
  1587      command:
  1588        - -Dhosts.0.host=http://utm_es-elastic:9200
  1589      deploy:
  1590        placement:
  1591          constraints: [node.role==manager]
  1592        mode: global
  1593  # -------------------------------------------------- NEW SERVICE -------------------------------------------------- #
  1594    ### POSTGRES, DATABASE SERVER
  1595    postgres:
  1596      image: registry.utmstack.com:443/utm-postgres
  1597      ports:
  1598        - 5432:5432
  1599      volumes:
  1600        - vol_postgres_data:/var/lib/postgresql/data
  1601        - /etc/localtime:/etc/localtime:ro
  1602      environment:
  1603        POSTGRES_USER: utmstack_user
  1604        POSTGRES_PASSWORD: utmstack_pass
  1605        POSTGRES_DB: utmstack
  1606      command: ["-c", "max_connections=1000"]
  1607      networks:
  1608        - net_utmstack
  1609      deploy:
  1610        mode: global
  1611        placement:
  1612          constraints: [node.role==manager]
  1613  # -------------------------------------------------- NEW SERVICE -------------------------------------------------- #
  1614    ### SCANNER & MANAGER
  1615    scanner:
  1616      image: registry.utmstack.com:443/utm-scanner11
  1617      volumes:
  1618        - vol_scanner_data:/data
  1619        - /etc/localtime:/etc/localtime:ro
  1620      networks:
  1621        - net_utmstack
  1622      ports:
  1623        - "8888:5432"
  1624      environment:
  1625        USERNAME: utmstack_user
  1626        PASSWORD: utmstack_pass
  1627        DB_PASSWORD: utmstack_pass
  1628        HTTPS: 0
  1629      deploy:
  1630        resources:
  1631          limits:
  1632            cpus: '2.0'
  1633        mode: global
  1634        placement:
  1635          constraints: [node.role==manager]
  1636  # -------------------------------------------------- NEW SERVICE -------------------------------------------------- #
  1637    ### TOMCAT, JAVA WEBSERVER
  1638    tomcat:
  1639      image: registry.utmstack.com:443/utm-bionic
  1640      volumes:
  1641        - /opt/tomcat:/opt/tomcat
  1642        - /etc/localtime:/etc/localtime:ro
  1643      networks:
  1644        - net_utmstack
  1645      environment:
  1646        JRE_HOME: "/opt/tomcat/bin/jre"
  1647        JAVA_HOME: "/opt/tomcat/bin/jre"
  1648        CATALINA_BASE: "/opt/tomcat"
  1649        CATALINA_HOME: "/opt/tomcat"
  1650        LD_LIBRARY_PATH: "/usr/lib/x86_64-linux-gnu"
  1651        CATALINA_OPTS: "-XmsJAVA_MINm -XmxJAVA_MAXm -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -XX:+CMSPermGenSweepingEnabled"
  1652      command: ["/opt/tomcat/bin/catalina.sh", "run"]
  1653      deploy:
  1654        placement:
  1655          constraints: [node.role==manager]
  1656        mode: global`
  1657  
  1658  	// File utmstack_application-prod.yml
  1659  	appProd := `logging:
  1660    level:
  1661      ROOT: INFO
  1662      com.park.utmstack: INFO
  1663      io.github.jhipster: INFO
  1664  spring:
  1665    devtools:
  1666      restart:
  1667        enabled: false
  1668      livereload:
  1669        enabled: false
  1670    datasource: # For Postgresql database
  1671      type: com.zaxxer.hikari.HikariDataSource
  1672      url: jdbc:postgresql://utm_postgres:5432/utmstack
  1673      username: utmstack_user
  1674      password: utmstack_pass
  1675      hikari:
  1676        poolName: Hikari
  1677        auto-commit: false
  1678    jpa:
  1679      database-platform: io.github.jhipster.domain.util.FixedPostgreSQL82Dialect # For Postgresql database
  1680      database: POSTGRESQL # For Postgresql database
  1681      show-sql: false
  1682      properties:
  1683        hibernate.id.new_generator_mappings: true
  1684        hibernate.connection.provider_disables_autocommit: true
  1685        hibernate.cache.use_second_level_cache: false
  1686        hibernate.cache.use_query_cache: false
  1687        hibernate.generate_statistics: true
  1688        hibernate.jdbc.batch_size: 15
  1689        hibernate.order_inserts: true
  1690    elasticsearch:
  1691      rest:
  1692        uris: http://utm_es-elastic:9200
  1693      jest:
  1694        uris: http://utm_es-elastic:9200
  1695    liquibase:
  1696      contexts: prod
  1697    mail:
  1698      host: ${utmstack.mail.host} #mail.atlasinside.com
  1699      port: ${utmstack.mail.port} #587
  1700      username: ${utmstack.mail.username} #demo@utmvault.com
  1701      password: ${utmstack.mail.password} #e235g7b558bxbwd7
  1702      protocol: ${utmstack.mail.protocol} #smtp
  1703      properties.mail.smtp:
  1704        auth: ${utmstack.mail.properties.mail.smtp.auth} #true
  1705        starttls.enable: ${utmstack.mail.properties.mail.smtp.starttls.enable} #true
  1706        ssl.trust: ${utmstack.mail.properties.mail.smtp.ssl.trust} #mail.atlasinside.com
  1707    thymeleaf:
  1708      cache: true
  1709  server:
  1710    port: 8080
  1711    compression:
  1712      enabled: true
  1713      mime-types: text/html,text/xml,text/plain,text/css, application/javascript, application/json
  1714      min-response-size: 1024
  1715  jhipster:
  1716    http:
  1717      version: V_1_1 # To use HTTP/2 you will need SSL support (see above the "server.ssl" configuration)
  1718      cache: # Used by the CachingHttpHeadersFilter
  1719        timeToLiveInDays: 1461
  1720    cors:
  1721      allowed-origins: "*"
  1722      allowed-methods: "*"
  1723      allowed-headers: "*"
  1724      exposed-headers: "X-utmvaultApp-code,X-utmvaultApp-error,Origin,X-Requested-With,Content-Type,Accept,x-auth-token,Authorization,Link,X-Total-Count"
  1725      allow-credentials: true
  1726      max-age: 1800
  1727    security:
  1728      authentication:
  1729        jwt:
  1730          base64-secret: MzY5Mzg0ZTViYzUwMzc0ODhlYmE2ZDUxN2Y5OGVmNDM3ZDE4ZmY1YWUwMzliNmM4ZWU2NDU1ZTc2YTg0MWRmZjcyZDQxOTNhMTJkODgwZjg2YWVhNWNlZjk5ZmY5MzYwOTQxOTI4MDRhMDE5ZGUzYjliYTYzN2Y1OTU0YTQzNjU=
  1731          token-validity-in-seconds: 86400
  1732          token-validity-in-seconds-for-remember-me: 2592000
  1733    mail: # specific JHipster mail property, for standard properties see MailProperties
  1734      from: ${utmstack.mail.from}
  1735      base-url: ${utmstack.mail.baseUrl}
  1736    metrics:
  1737      logs: # Reports metrics in the logs
  1738        enabled: true
  1739        report-frequency: 60 # in seconds
  1740    logging:
  1741      logstash: # Forward logs to logstash over a socket, used by LoggingConfiguration
  1742        enabled: false
  1743        host: localhost
  1744        port: 5000
  1745        queue-size: 512
  1746  application:
  1747    alert-notification-mails: ${utmstack.mail.addressToNotify}
  1748    openvas: # Scan Manager credentials
  1749      host: utm_scanner
  1750      port: 9390
  1751      user: utmstack_user
  1752      password: utmstack_pass
  1753      datasource:
  1754        url: jdbc:postgresql://utm_scanner:8888/gvmd
  1755        username: gvm
  1756        password: utmstack_pass
  1757    chart-builder: # Chart builder configuration
  1758      elasticsearch-url: http://utm_es-elastic:9200
  1759      ip-info-index-name: utm-geoip
  1760    active-directory:
  1761      winlogbeat-index-pattern: index-*
  1762      active-directory-index-pattern: dc-*
  1763    application-event:
  1764      index: utmstack-logs
  1765    tools:
  1766      - name: "Vulnerability Management System"
  1767        signature-amount: ""
  1768        last-update: ""
  1769      - name: "HIPS"
  1770        signature-amount: ""
  1771        last-update: ""
  1772      - name: "NIDS"
  1773        signature-amount: ""
  1774        last-update: ""
  1775      - name: "Correlation Engine"
  1776        signature-amount: ""
  1777        last-update: ""
  1778    incident-response:
  1779      asset-verification-interval: 300
  1780    notification:
  1781      twilio:
  1782        accountSID: ${utmstack.twilio.accountSid}
  1783        authToken: ${utmstack.twilio.authToken}
  1784        phoneNumber: ${utmstack.twilio.number}`
  1785  
  1786  	// File config.json
  1787  	probeJSON := `{
  1788    "postgresql": {
  1789      "host": "localhost",
  1790      "user": "utmstack_user",
  1791      "password": "utmstack_pass",
  1792      "database": "utmstack",
  1793      "port": 5432
  1794    }
  1795  }`
  1796  
  1797  	// File vhost-elastic
  1798  	vhostElastic := `server {
  1799    listen 80;
  1800    server_name elastic.utmclient.utmstack.com;
  1801    resolver 127.0.0.11 valid=600s ipv6=off;
  1802    set $elastic http://utm_es-elastic:9200;
  1803    client_max_body_size 0;
  1804    chunked_transfer_encoding on;
  1805    location / {
  1806      proxy_pass $elastic;
  1807      proxy_set_header Host $http_host;   # required for docker client's sake
  1808      proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP
  1809      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  1810      proxy_set_header X-Forwarded-Proto $scheme;
  1811      proxy_read_timeout 900;
  1812    }
  1813  }`
  1814  
  1815  	// File vhost-cerebro
  1816  	vhostCerebro := `server {
  1817      listen 80;
  1818      server_name cerebro.utmclient.utmstack.com;
  1819      resolver 127.0.0.11 valid=600s ipv6=off;
  1820      set $cerebro http://utm_cerebro:9000;
  1821      client_max_body_size 0;
  1822      chunked_transfer_encoding on;
  1823      location / {
  1824        proxy_pass $cerebro;
  1825        proxy_set_header Host $http_host;   # required for docker client's sake
  1826        proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP
  1827        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  1828        proxy_set_header X-Forwarded-Proto $scheme;
  1829        proxy_read_timeout 900;
  1830      }
  1831    }`
  1832  
  1833  	// File vhost-utmstack
  1834  	vhostUtmStack := `server {
  1835    listen 80 default_server;
  1836    server_name utmclient.utmstack.com;
  1837    return 301 https://utmclient.utmstack.com$request_uri;
  1838  }
  1839  server {
  1840    listen 443 ssl default_server;
  1841    server_name utmclient.utmstack.com;
  1842    resolver 127.0.0.11 valid=600s ipv6=off;
  1843    set $tomcat http://utm_tomcat:8080;
  1844    ssl_certificate /etc/nginx/single.crt;
  1845    ssl_certificate_key /etc/nginx/single.key;
  1846    ssl_protocols TLSv1.1 TLSv1.2;
  1847    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
  1848    ssl_prefer_server_ciphers on;
  1849    ssl_session_cache shared:SSL:10m;
  1850    client_max_body_size 0;
  1851    chunked_transfer_encoding on;
  1852    location / {
  1853      proxy_pass $tomcat;
  1854      proxy_set_header Host $http_host;   # required for docker client's sake
  1855      proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP
  1856      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  1857      proxy_set_header X-Forwarded-Proto $scheme;
  1858      proxy_read_timeout 900;
  1859    }
  1860  }`
  1861  
  1862  	// utm_client.sql
  1863  	file, err := os.Create("/etc/utmstack/utm_client.sql")
  1864  	if err != nil {
  1865  		fmt.Println(err)
  1866  	} else {
  1867  		file.WriteString(utmClient)
  1868  		fmt.Println("File: '/etc/utmstack/utm_client.sql' ... Ok.")
  1869  	}
  1870  	file.Close()
  1871  
  1872  	// compose.yml
  1873  	file, err = os.Create("/etc/utmstack/compose.yml")
  1874  	if err != nil {
  1875  		fmt.Println(err)
  1876  	} else {
  1877  		file.WriteString(utmCompose)
  1878  		fmt.Println("File: '/etc/utmstack/compose.yml' ... Ok.")
  1879  	}
  1880  	file.Close()
  1881  
  1882  	// application-prod.yml
  1883  	file, err = os.Create("/etc/utmstack/application-prod.yml")
  1884  	if err != nil {
  1885  		fmt.Println(err)
  1886  	} else {
  1887  		file.WriteString(appProd)
  1888  		fmt.Println("File: '/etc/utmstack/application-prod.yml' ... Ok.")
  1889  	}
  1890  	file.Close()
  1891  
  1892  	// config.json
  1893  	file, err = os.Create("/etc/utmstack/config.json")
  1894  	if err != nil {
  1895  		fmt.Println(err)
  1896  	} else {
  1897  		file.WriteString(probeJSON)
  1898  		fmt.Println("File: '/etc/utmstack/config.json' ... Ok.")
  1899  	}
  1900  	file.Close()
  1901  
  1902  	// vhost-utmstack
  1903  	file, err = os.Create("/etc/utmstack/vhost-utmstack")
  1904  	if err != nil {
  1905  		fmt.Println(err)
  1906  	} else {
  1907  		file.WriteString(vhostUtmStack)
  1908  		fmt.Println("File: '/etc/utmstack/vhost-utmstack' ... Ok.")
  1909  	}
  1910  	file.Close()
  1911  
  1912  	// vhost-elastic
  1913  	file, err = os.Create("/etc/utmstack/vhost-elastic")
  1914  	if err != nil {
  1915  		fmt.Println(err)
  1916  	} else {
  1917  		file.WriteString(vhostElastic)
  1918  		fmt.Println("File: '/etc/utmstack/vhost-elastic' ... Ok.")
  1919  	}
  1920  	file.Close()
  1921  
  1922  	// vhost-cerebro
  1923  	file, err = os.Create("/etc/utmstack/vhost-cerebro")
  1924  	if err != nil {
  1925  		fmt.Println(err)
  1926  	} else {
  1927  		file.WriteString(vhostCerebro)
  1928  		fmt.Println("File: '/etc/utmstack/vhost-cerebro' ... Ok.")
  1929  	}
  1930  	file.Close()
  1931  
  1932  }
  1933  
  1934  func copyLicense() {
  1935  	eulaUTM := `Atlas Inside Technology LLC - UTMStack License
  1936  
  1937  By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Atlas Inside Technology LLC - UTMStack License ("UTMStack License"). To the extent this UTMStack License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions.
  1938  
  1939  # Section 1 – Definitions.
  1940  
  1941  Adapted Material means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor.
  1942  
  1943  Copyright and Similar Rights means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this UTMStack License, the rights specified in Section 2 are not Copyright and Similar Rights.
  1944  
  1945  Effective Technological Measures means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements.
  1946  
  1947  Exceptions and Limitations means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material.
  1948  
  1949  Licensed Material means the source code, compiled software, database, or other material to which the Licensor applied this UTMStack License.
  1950  
  1951  Licensed Rights means the rights granted to You subject to the terms and conditions of this UTMStack License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license.
  1952  
  1953  Licensor means the individual(s) or entity(ies) granting rights under this UTMStack License.
  1954  
  1955  NonCommercial means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this UTMStack License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange.
  1956  
  1957  Share means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them.
  1958  
  1959  Sui Generis Database Rights means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world.
  1960  
  1961  You means the individual or entity exercising the Licensed Rights under this UTMStack License. Your has a corresponding meaning.
  1962  
  1963  # Section 2 – Scope.
  1964  
  1965  ## License grant.
  1966  
  1967  Subject to the terms and conditions of this UTMStack License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to:
  1968  
  1969  * Use and Share the Licensed Material, in whole or in part, for NonCommercial purposes only.
  1970  
  1971  Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this UTMStack License does not apply, and You do not need to comply with its terms and conditions.
  1972  
  1973  ## Term.
  1974  
  1975  The term of this UTMStack License is specified in Section 6.
  1976  
  1977  ## Downstream recipients.
  1978  
  1979  Offer from the Licensor – Licensed Material. Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this UTMStack License.
  1980  
  1981  No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material.
  1982  
  1983  No endorsement. Nothing in this UTMStack License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution.
  1984  Other rights.
  1985  
  1986  Moral rights, such as the right of integrity, are not licensed under this UTMStack License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise.
  1987  
  1988  Patent and trademark rights are not licensed under this UTMStack License.
  1989  
  1990  To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes.
  1991  
  1992  # Section 3 – License Conditions.
  1993  
  1994  Your exercise of the Licensed Rights is expressly made subject to the following conditions.
  1995  
  1996  ## Attribution.
  1997  
  1998  If You Share the Licensed Material, You must:
  1999  retain the following if it is supplied by the Licensor with the Licensed Material:
  2000  identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated);
  2001  a copyright notice;
  2002  a notice that refers to this UTMStack License;
  2003  a notice that refers to the disclaimer of warranties;
  2004  a URI or hyperlink to the Licensed Material to the extent reasonably practicable; and
  2005  indicate the Licensed Material is licensed under this UTMStack License, and include the text of, or the URI or hyperlink to, this UTMStack License.
  2006  
  2007  For the avoidance of doubt, You do not have permission under this UTMStack License to make or Share Adapted Material.
  2008  
  2009  You may satisfy the conditions in this section in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information.
  2010  
  2011  If requested by the Licensor, You must remove any of the information required by this section to the extent reasonably practicable.
  2012  
  2013  # Section 4 – Sui Generis Database Rights.
  2014  
  2015  Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material:
  2016  for the avoidance of doubt, Section 2 grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only and provided You do not Share Adapted Material;
  2017  if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material; and
  2018  You must comply with the conditions in Section 3 if You Share all or a substantial portion of the contents of the database.
  2019  For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this UTMStack License where the Licensed Rights include other Copyright and Similar Rights.
  2020  
  2021  # Section 5 – Disclaimer of Warranties and Limitation of Liability.
  2022  
  2023  Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.
  2024  
  2025  To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this UTMStack License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.
  2026  The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability.
  2027  
  2028  # Section 6 – Term and Termination.
  2029  
  2030  This UTMStack License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this UTMStack License, then Your rights under this UTMStack License terminate automatically.
  2031  
  2032  Where Your right to use the Licensed Material has terminated under Section 6, it reinstates:
  2033  automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or
  2034  upon express reinstatement by the Licensor.
  2035  
  2036  For the avoidance of doubt, this Section 6 does not affect any right the Licensor may have to seek remedies for Your violations of this UTMStack License.
  2037  
  2038  For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this UTMStack License.
  2039  
  2040  Sections 1, 5, 6, 7, and 8 survive termination of this UTMStack License.
  2041  
  2042  # Section 7 – Other Terms and Conditions.
  2043  
  2044  The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed.
  2045  Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this UTMStack License.
  2046  
  2047  # Section 8 – Interpretation.
  2048  
  2049  For the avoidance of doubt, this UTMStack License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this UTMStack License.
  2050  To the extent possible, if any provision of this UTMStack License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this UTMStack License without affecting the enforceability of the remaining terms and conditions.
  2051  No term or condition of this UTMStack License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor.
  2052  Nothing in this UTMStack License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority.
  2053  
  2054  # Section 9 - Third Party Materials
  2055  UTMStack uses materials from third party licensors.
  2056  If you use those materials, you accept the terms expressed in their licenses for the use, modification and distribution of those third-party materials.
  2057  
  2058  ## Third Party Materials List
  2059  
  2060  | Material | License |
  2061  |---|---|
  2062  | psycopg2-binary | LGPL |
  2063  | elasticsearch | Apache-2.0 |
  2064  | geoip2 | Apache-2.0 |
  2065  | grpcio | Apache-2.0 |
  2066  | boto3 | Apache-2.0 |
  2067  | requests | Apache-2.0 |
  2068  | Jinja2 | BSD-3-Clause |
  2069  | hug | MIT |
  2070  | flask | BSD-3-Clause |
  2071  | ldap3 | LGPLv3 |
  2072  | pyparsing | MIT |
  2073  | python-dateutil | Apache |
  2074  | pyasn1 | BSD |
  2075  | echart | Apache-2.0 |
  2076  | echart-gl | BSD |
  2077  | echart-stat | BSD |
  2078  | echarts-wordcloud | ISC |
  2079  | echartslayer | ISC |
  2080  | flat | BSD-3-Clause |
  2081  | jsoneditor | Apache-2.0 |
  2082  | leaflet | BSD-2-Clause |
  2083  | jhipster | Apache-2.0 |
  2084  | ngx-drag-drop | BSD-3-Clause |`
  2085  
  2086  	// License-Private-UTMStack.md
  2087  	file, err := os.Create("/etc/utmstack/License-Private-UTMStack.md")
  2088  	if err != nil {
  2089  		fmt.Println(err)
  2090  	} else {
  2091  		file.WriteString(eulaUTM)
  2092  		fmt.Println("File: '/etc/utmstack/License-Private-UTMStack.md' ... Ok.")
  2093  	}
  2094  	file.Close()
  2095  }
  2096  
  2097  func selectInstallType() string {
  2098  	answer := ""
  2099  	for {
  2100  		fmt.Println("# -------------------------------------")
  2101  		fmt.Println("# [UTMSTACK]: Select Installation Type.")
  2102  		fmt.Println("# -------------------------------------")
  2103  		fmt.Println("(1) Evaluation Version (free trial up).")
  2104  		fmt.Println("(2) Enterprise Version (paid license).")
  2105  		fmt.Println("(U)ninstall UTMStack Deployment.")
  2106  		fmt.Println("(C)ancel the Setup and do Nothing.")
  2107  		fmt.Println("# -------------------------------------")
  2108  		fmt.Print("Your answer is?: ")
  2109  		fmt.Scanln(&answer)
  2110  		opt := string(answer)
  2111  		if opt == "1" {
  2112  			return "f"
  2113  		}
  2114  		if opt == "2" {
  2115  			return "l"
  2116  		}
  2117  		if opt == "u" || opt == "U" {
  2118  			return "u"
  2119  		}
  2120  		if opt == "c" || opt == "C" {
  2121  			os.Exit(0)
  2122  		}
  2123  		answer = ""
  2124  		fmt.Println("[ERROR] => [UTMSTACK]: (", opt, ") is NOT a valid choice.")
  2125  	}
  2126  }
  2127  
  2128  func testAccess() { // Testing Product Source access
  2129  	fmt.Println("# [UTMSTACK]: Testing Product Source Access ...")
  2130  	// curl -u "upgrade:y#K=[swzk.(DQ7+[w;=#na/958yU$" --head ftp://registry.utmstack.com/utm-stack.zip
  2131  	fmt.Println("")
  2132  	fmt.Println("# [UTMSTACK]: ---------- utm-stack.zip ----------")
  2133  	outFront, errFront := exec.Command("sh", "-c", `curl -u "upgrade:y#K=[swzk.(DQ7+[w;=#na/958yU$" --head ftp://registry.utmstack.com/utm-stack.zip`).Output()
  2134  	strFront := string(outFront)
  2135  	trimFront := strings.TrimRight(strFront, "\n")
  2136  	if trimFront == "" {
  2137  		fmt.Println("Error:", curlError(errFront.Error()))
  2138  		fmt.Println("# [UTMSTACK]: Exiting UTMStack Setup ...")
  2139  		os.Exit(1)
  2140  	} else {
  2141  		fmt.Println(trimFront)
  2142  		fmt.Println("File-Status: utm-stack.zip is Ok.")
  2143  	}
  2144  
  2145  	// curl -u "upgrade:y#K=[swzk.(DQ7+[w;=#na/958yU$" --head ftp://registry.utmstack.com/tomcat.tar.gz
  2146  	fmt.Println("")
  2147  	fmt.Println("# [UTMSTACK]: ---------- tomcat.tar.gz ----------")
  2148  	outWar, errWar := exec.Command("sh", "-c", `curl -u "upgrade:y#K=[swzk.(DQ7+[w;=#na/958yU$" --head ftp://registry.utmstack.com/tomcat.tar.gz`).Output()
  2149  	strWar := string(outWar)
  2150  	trimWar := strings.TrimRight(strWar, "\n")
  2151  	if trimFront == "" {
  2152  		fmt.Println("Error:", curlError(errWar.Error()))
  2153  		fmt.Println("# [UTMSTACK]: Exiting UTMStack Setup ...")
  2154  		os.Exit(1)
  2155  	} else {
  2156  		fmt.Println(trimWar)
  2157  		fmt.Println("File-Status: tomcat.tar.gz is Ok.")
  2158  	}
  2159  
  2160  	// curl -k -X GET https://registry.utmstack.com:443/v2/_catalog
  2161  	fmt.Println("")
  2162  	fmt.Println("# [UTMSTACK]: ---------- Docker Registry ----------")
  2163  	outReg, errReg := exec.Command("sh", "-c", `curl -k -X GET https://registry.utmstack.com:443/v2/_catalog`).Output()
  2164  	strReg := string(outReg)
  2165  	trimReg := strings.TrimRight(strReg, "\n")
  2166  	if trimReg == "" {
  2167  		fmt.Println("Error:", curlError(errReg.Error()))
  2168  		fmt.Println("# [UTMSTACK]: Exiting UTMStack Setup ...")
  2169  		os.Exit(1)
  2170  	} else {
  2171  		fmt.Println(trimReg)
  2172  		fmt.Println("Repo-Status: Docker Registry is Ok.")
  2173  	}
  2174  
  2175  	// Verify each image
  2176  	// curl -k -X GET https://registry.utmstack.com:443/v2/utm-bionic/tags/list
  2177  
  2178  	// curl -k -X GET https://registry.utmstack.com:443/v2/utm-cerebro/tags/list
  2179  
  2180  	// curl -k -X GET https://registry.utmstack.com:443/v2/utm-nginx/tags/list
  2181  
  2182  	// curl -k -X GET https://registry.utmstack.com:443/v2/utm-elastic-aws/tags/list
  2183  
  2184  	// curl -k -X GET https://registry.utmstack.com:443/v2/utm-postgres/tags/list
  2185  
  2186  	// curl -k -X GET https://registry.utmstack.com:443/v2/utm-scanner11/tags/list
  2187  
  2188  	// curl -k --head https://updates.utmstack.com/assets/stack
  2189  	fmt.Println("")
  2190  	fmt.Println("# [UTMSTACK]: ---------- Proxy Probe ----------")
  2191  	outProbe, errProbe := exec.Command("sh", "-c", `curl -k --head https://updates.utmstack.com/assets/stack`).Output()
  2192  	strProbe := string(outProbe)
  2193  	trimProbe := strings.TrimRight(strProbe, "\n")
  2194  	if trimProbe == "" {
  2195  		fmt.Println("Error:", curlError(errProbe.Error()))
  2196  		fmt.Println("# [UTMSTACK]: Exiting UTMStack Setup ...")
  2197  		os.Exit(1)
  2198  	} else {
  2199  		fmt.Println(trimProbe)
  2200  		fmt.Println("File-Status: Proxy Probe is Ok.")
  2201  	}
  2202  	fmt.Println("")
  2203  	fmt.Println("# [UTMSTACK]: Testing Product Source Access is Done.")
  2204  	fmt.Println("")
  2205  
  2206  	// curl -u "upgrade:y#K=[swzk.(DQ7+[w;=#na/958yU$" --head ftp://registry.utmstack.com/geoip.zip
  2207  	fmt.Println("")
  2208  	fmt.Println("# [UTMSTACK]: ---------- geoip.zip ----------")
  2209  	outWar, errWar = exec.Command("sh", "-c", `curl -u "upgrade:y#K=[swzk.(DQ7+[w;=#na/958yU$" --head ftp://registry.utmstack.com/geoip.zip`).Output()
  2210  	strWar = string(outWar)
  2211  	trimWar = strings.TrimRight(strWar, "\n")
  2212  	if trimFront == "" {
  2213  		fmt.Println("Error:", curlError(errWar.Error()))
  2214  		fmt.Println("# [UTMSTACK]: Exiting UTMStack Setup ...")
  2215  		os.Exit(1)
  2216  	} else {
  2217  		fmt.Println(trimWar)
  2218  		fmt.Println("File-Status: geoip.zip is Ok.")
  2219  	}
  2220  
  2221  	// curl -u "upgrade:y#K=[swzk.(DQ7+[w;=#na/958yU$" --head ftp://registry.utmstack.com/updateUTM
  2222  	fmt.Println("")
  2223  	fmt.Println("# [UTMSTACK]: ---------- updateUTM ----------")
  2224  	outWar, errWar = exec.Command("sh", "-c", `curl -u "upgrade:y#K=[swzk.(DQ7+[w;=#na/958yU$" --head ftp://registry.utmstack.com/updateUTM`).Output()
  2225  	strWar = string(outWar)
  2226  	trimWar = strings.TrimRight(strWar, "\n")
  2227  	if trimFront == "" {
  2228  		fmt.Println("Error:", curlError(errWar.Error()))
  2229  		fmt.Println("# [UTMSTACK]: Exiting UTMStack Setup ...")
  2230  		os.Exit(1)
  2231  	} else {
  2232  		fmt.Println(trimWar)
  2233  		fmt.Println("File-Status: updateUTM is Ok.")
  2234  	}
  2235  
  2236  }
  2237  
  2238  func curlError(err string) string {
  2239  	trimErr := strings.TrimRight(err, "\n")
  2240  	switch trimErr {
  2241  	case "exit status 6":
  2242  		return "Couldn't resolve host. The given remote host's address was not resolved."
  2243  	case "exit status 7":
  2244  		return "Wrong port number, wrong host name, wrong protocol or there is network equipment blocking the traffic."
  2245  	case "exit status 19":
  2246  		return "Got an error from the server when trying to download/access the file."
  2247  	case "exit status 51":
  2248  		return "The server's SSL/TLS certificate or SSH fingerprint failed verification."
  2249  	case "exit status 67":
  2250  		return "The user name, password, or similar was not accepted and failed to log in."
  2251  	default:
  2252  		return err
  2253  	}
  2254  }
  2255  
  2256  func genCert() {
  2257  	// openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/utmstack/single.key -out /etc/utmstack/single.crt -subj '/C=US/ST=Florida/L=Doral/O=UTMStack/OU=Org/CN=utmclient.utmstack.com'
  2258  	strCMD := "openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/utmstack/single.key -out /etc/utmstack/single.crt -subj '/C=US/ST=Florida/L=Doral/O=UTMStack/OU=Org/CN=" + domain + "'"
  2259  	err := exec.Command("/bin/bash", "-c", strCMD).Run()
  2260  	if err != nil {
  2261  		fmt.Println("[ERROR]:", err)
  2262  	} else {
  2263  		fmt.Println("# [UTMSTACK]: Generating self-singed certificate ... Ok.")
  2264  	}
  2265  }
  2266  
  2267  func randomString(n int) string {
  2268  	var letters = []rune("abcdefghijklmnopqrstuvwxyz")
  2269  	s := make([]rune, n)
  2270  	for i := range s {
  2271  		s[i] = letters[seededRand.Intn(len(letters))]
  2272  	}
  2273  	return string(s)
  2274  }