TisaneOnprem.psm1

#Region '.\Public\Download-TisaneFromFTP.ps1' 0
function Download-TisaneFromFTP{
  [CmdletBinding()]
Param(
     [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="FTP user: ")][String] $user,
     [Parameter(Mandatory = $true, HelpMessage="FTP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="FTP host: ")][String] $ftp
   )

$DOWNLOAD_FOLDER = "C:\Users\Administrator\Downloads\Tisane"
$TISANE_ROOT = "C:\Tisane"

$webclient = New-Object System.Net.WebClient 

if(-not(Test-Path($DOWNLOAD_FOLDER))) {
    # file with path $path doesn't exist
  mkdir $DOWNLOAD_FOLDER
}


$webclient.Credentials = New-Object System.Net.NetworkCredential("$user@$ftp", $password)

$uri = New-Object System.Uri("ftp://ftp.$ftp/tisane_db.zip")
Write-Host "Downloading Tisane data to $DOWNLOAD_FOLDER..." -ForegroundColor Green
$webclient.DownloadFile($uri, "$DOWNLOAD_FOLDER\tisane_db.zip")
Write-Host "Downloading Tisane Web Service to $DOWNLOAD_FOLDER..." -ForegroundColor Green
$uri = New-Object System.Uri("ftp://ftp.$ftp/tisane_ws.zip")
$webclient.DownloadFile($uri, "$DOWNLOAD_FOLDER\tisane_ws.zip")

if(Test-Path($TISANE_ROOT)) {
  #mkdir $TISANE_ROOT
  #Remove-Item -Path "$TISANE_ROOT\" -Recurse
  # dir $TISANE_ROOT -Recurse | Where-Object { $_.FullName -imatch '^C:\\tisane\\([^\\]+)\\.+[.]sst' } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
}

#Add-Type -AssemblyName System.IO.Compression.FileSystem
#Add-Type -Assembly "System.IO.Compression.Filesystem"

Add-Type -Assembly "System.Io.Compression.FileSystem"

[System.IO.Compression.ZipFile]::ExtractToDirectory("$DOWNLOAD_FOLDER\tisane_db.zip", "$DOWNLOAD_FOLDER\db")
}
#EndRegion '.\Public\Download-TisaneFromFTP.ps1' 41
#Region '.\Public\Install-TisaneFromFTP.ps1' 0
function Install-TisaneFromFTP{
  [CmdletBinding()]
Param(
     [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="FTP user: ")][String] $user,
     [Parameter(Mandatory = $true, HelpMessage="FTP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="FTP host: ")][String] $ftp,
     [Parameter(Mandatory = $false, HelpMessage="Install .NET? ")][boolean] $dotnet,
     [Parameter(Mandatory = $true, HelpMessage="Instance count: ")][int] $instances,
     [Parameter(Mandatory = $false, HelpMessage="Windows user: ")][String] $winuser
   )

if (-not($winuser)) {
  $winuser = 'Administrator'
}
$DOWNLOAD_FOLDER = "C:\Users\$winuser\Downloads\Tisane"
$dotNETInstallerUrl = "https://go.microsoft.com/fwlink/?linkid=2088631"
$TISANE_ROOT = "C:\Tisane"


$webclient = New-Object System.Net.WebClient 

if(-not(Test-Path($DOWNLOAD_FOLDER))) {
    # file with path $path doesn't exist
  mkdir $DOWNLOAD_FOLDER
}

if ($dotnet) {
  Write-Host "Downloading .NET installer to $DOWNLOAD_FOLDER..." -ForegroundColor Green
  $webclient.DownloadFile($dotNETInstallerUrl, "$DOWNLOAD_FOLDER\dotnetinstaller.exe")
  Write-Host "Installing .NET..." -ForegroundColor Green
  Start-Process -FilePath $DOWNLOAD_FOLDER\dotnetinstaller.exe -ArgumentList "/q","/norestart" -Wait
}



$webclient.Credentials = New-Object System.Net.NetworkCredential("$user@tisane.ai", $password)

$uri = New-Object System.Uri("ftp://$ftp/tisane_db.zip")
Write-Host "Downloading Tisane data to $DOWNLOAD_FOLDER..." -ForegroundColor Green
$webclient.DownloadFile($uri, "$DOWNLOAD_FOLDER\tisane_db.zip")
Write-Host "Downloading Tisane Web Service to $DOWNLOAD_FOLDER..." -ForegroundColor Green
$uri = New-Object System.Uri("ftp://$ftp/tisane_ws.zip")
$webclient.DownloadFile($uri, "$DOWNLOAD_FOLDER\tisane_ws.zip")
$uri = New-Object System.Uri("ftp://$ftp/Tisane.Runtime.Service.exe.config")
$webclient.DownloadFile($uri, "$DOWNLOAD_FOLDER\Tisane.Runtime.Service.exe.config")


if(Test-Path($TISANE_ROOT)) {
  #mkdir $TISANE_ROOT
  #Remove-Item -Path $TISANE_ROOT -Recurse
}

#Add-Type -AssemblyName System.IO.Compression.FileSystem

#[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
Write-Host "Extracting Tisane data to $TISANE_ROOT..." -ForegroundColor Green
# WE HAVE TO USE Ionic Zip because the standard compression libraries crash with very large archives
[System.Reflection.Assembly]::LoadFrom("$DOWNLOAD_FOLDER\Ionic.Zip.dll")
$zipFile = New-Object Ionic.Zip.ZipFile("$DOWNLOAD_FOLDER\tisane_db.zip")

#Expand-Archive -LiteralPath "$DOWNLOAD_FOLDER\tisane_db.zip" -DestinationPath $TISANE_ROOT
$zipFile.ExtractAll($TISANE_ROOT, 1)

$portNumber = 81
For ($i=1; $i -le $instances; $i++) {
  $formattedI = $i | % tostring 00
  $portNumber = 79 + $i
  Write-Host "Creating instance under $TISANE_ROOT\instance$formattedI" -ForegroundColor Green
  Expand-Archive -LiteralPath "$DOWNLOAD_FOLDER\tisane_ws.zip" -DestinationPath "$TISANE_ROOT\instance$formattedI" -Force
  $webclient.DownloadFile($uri, "$TISANE_ROOT\instance$formattedI\Tisane.Runtime.Service.exe.config")
  ((Get-Content -path "$TISANE_ROOT\instance$formattedI\Tisane.Runtime.Service.exe.config" -Raw) -replace 'localhost:80',"localhost:$portNumber") | Set-Content -Path "$TISANE_ROOT\instance$formattedI\Tisane.Runtime.Service.exe.config"
  Start-Process -FilePath "$TISANE_ROOT\instance$formattedI\Tisane.Runtime.Service.exe" -ArgumentList "-i" -Wait
}
# now open firewall ports
netsh advfirewall firewall add rule name='Tisane ports' dir=in protocol=tcp localport="80-$portNumber" action=allow profile=any

# create a quarterly update task
SchTasks /Create /sc monthly /mo 3 /TN "Tisane update" /TR "$DOWNLOAD_FOLDER\UpdateTisaneFromFTP.ps1 -user $user -password $password -ftp $ftp" /ST 23:00

# start the services
Start-Service "Tisane Runtime *"
}
#EndRegion '.\Public\Install-TisaneFromFTP.ps1' 82
#Region '.\Public\Update-TisaneFromFTP.ps1' 0
function Update-TisaneFromFTP{
  [CmdletBinding()]
Param(
     [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="FTP user: ")][String] $user,
     [Parameter(Mandatory = $true, HelpMessage="FTP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="FTP host: ")][String] $ftp,
     [Parameter(Mandatory = $false, HelpMessage="Copy only, no need to reregister the service: ")][Boolean] $copyOnly
   )

$DOWNLOAD_FOLDER = "C:\Users\Administrator\Downloads\Tisane"
$TISANE_ROOT = "C:\Tisane"

$webclient = New-Object System.Net.WebClient 

if(-not(Test-Path($DOWNLOAD_FOLDER))) {
    # file with path $path doesn't exist
  mkdir $DOWNLOAD_FOLDER
}


$webclient.Credentials = New-Object System.Net.NetworkCredential("$user@$ftp", $password)

$uri = New-Object System.Uri("ftp://ftp.$ftp/tisane_db.zip")
Write-Host "Downloading Tisane data to $DOWNLOAD_FOLDER..." -ForegroundColor Green
$webclient.DownloadFile($uri, "$DOWNLOAD_FOLDER\tisane_db.zip")
Stop-Service "Tisane Runtime *"
Write-Host "Downloading Tisane Web Service to $DOWNLOAD_FOLDER..." -ForegroundColor Green
$uri = New-Object System.Uri("ftp://ftp.$ftp/tisane_ws.zip")
$webclient.DownloadFile($uri, "$DOWNLOAD_FOLDER\tisane_ws.zip")

if(Test-Path($TISANE_ROOT)) {
  #mkdir $TISANE_ROOT
  #Remove-Item -Path "$TISANE_ROOT\" -Recurse
  # dir $TISANE_ROOT -Recurse | Where-Object { $_.FullName -imatch '^C:\\tisane\\([^\\]+)\\.+[.]sst' } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
}

#Add-Type -AssemblyName System.IO.Compression.FileSystem
#Add-Type -Assembly "System.IO.Compression.Filesystem"


# WE HAVE TO USE Ionic Zip because the standard compression libraries crash with very large archives
[System.Reflection.Assembly]::LoadFrom("$DOWNLOAD_FOLDER\Ionic.Zip.dll")
$zipFile = New-Object Ionic.Zip.ZipFile("$DOWNLOAD_FOLDER\tisane_db.zip")

#[System.IO.Compression.ZipFile]::ExtractToDirectory("$DOWNLOAD_FOLDER\tisane_db.zip", $TISANE_ROOT, $true)
Write-Host "Extracting Tisane data to $TISANE_ROOT..." -ForegroundColor Green
#Expand-Archive -LiteralPath "$DOWNLOAD_FOLDER\tisane_db.zip" -DestinationPath $TISANE_ROOT -Force
(Get-Service "Tisane *").WaitForStatus('Stopped')
$zipFile.ExtractAll($TISANE_ROOT, 1)

# dir $TISANE_ROOT -Recurse | Where-Object { $_.FullName -imatch '^C:\\tisane\\([^\\]+)\\LOG[.]old.+' } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue


$services = Get-Service "Tisane Runtime *"
$instances = $services.length


For ($i=1; $i -le $instances; $i++) {
  $formattedI = $i | % tostring 00
  Write-Host "Updating instance under $TISANE_ROOT\instance$formattedI" -ForegroundColor Green
  Expand-Archive -LiteralPath "$DOWNLOAD_FOLDER\tisane_ws.zip" -DestinationPath "$TISANE_ROOT\instance$formattedI" -Force
  if (-not $copyOnly) {
    Start-Process -FilePath "$TISANE_ROOT\instance$formattedI\Tisane.Runtime.Service.exe" -ArgumentList "-r" -Wait
  }
}

Start-Service "Tisane Runtime *"
}
#EndRegion '.\Public\Update-TisaneFromFTP.ps1' 68
#Region '.\Public\Update-TisaneFromLocalZips.ps1' 0
function Update-TisaneFromLocalZips{
Stop-Service "Tisane Runtime *"

$DOWNLOAD_FOLDER = "C:\Users\Administrator\Downloads\Tisane"
$TISANE_ROOT = "C:\Tisane"

#Add-Type -AssemblyName System.IO.Compression.FileSystem
#Add-Type -Assembly "System.IO.Compression.Filesystem"


# WE HAVE TO USE Ionic Zip because the standard compression libraries crash with very large archives
[System.Reflection.Assembly]::LoadFrom("$DOWNLOAD_FOLDER\Ionic.Zip.dll")
$zipFile = New-Object Ionic.Zip.ZipFile("$DOWNLOAD_FOLDER\tisane_db.zip")


#[System.IO.Compression.ZipFile]::ExtractToDirectory("$DOWNLOAD_FOLDER\tisane_db.zip", $TISANE_ROOT, $true)
Write-Host "Extracting Tisane data to $TISANE_ROOT..." -ForegroundColor Green
#Expand-Archive -LiteralPath "$DOWNLOAD_FOLDER\tisane_db.zip" -DestinationPath $TISANE_ROOT -Force
$zipFile.ExtractAll($TISANE_ROOT, 1)

# dir $TISANE_ROOT -Recurse | Where-Object { $_.FullName -imatch '^C:\\tisane\\([^\\]+)\\LOG[.]old.+' } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue


$services = Get-Service "Tisane Runtime *"
$instances = $services.length


For ($i=1; $i -le $instances; $i++) {
  $formattedI = $i | % tostring 00
  Write-Host "Updating instance under $TISANE_ROOT\instance$formattedI" -ForegroundColor Green
  Expand-Archive -LiteralPath "$DOWNLOAD_FOLDER\tisane_ws.zip" -DestinationPath "$TISANE_ROOT\instance$formattedI" -Force
  Start-Process -FilePath "$TISANE_ROOT\instance$formattedI\Tisane.Runtime.Service.exe" -ArgumentList "-r" -Wait
}

Start-Service "Tisane Runtime *"
}
#EndRegion '.\Public\Update-TisaneFromLocalZips.ps1' 36