framework/Resources/Scripts/add_client_node.ps1


Param(
    [string]$CacheName,
    [Parameter(Mandatory = $true)]
    [string]$ClientNode,

    [Parameter(Mandatory = $true)]
    [string]$EnvironmentName,

    [switch]$AcquireServerMapping = $false,
    [switch]$NoLogo = $false,
    [switch]$UpdateServerConfig = $false,
    [int]$Port = 8250,
    [pscredential]$Credentials
)

$resourceGroupName = $null
$resourceGroup = $null
$script:vmName = $null
$script:tagsResult = $rg.Tags
$script:allVms = $null
$script:privateIpsList = $null
$script:vmToExecuteCommandOn = $null

function GetAzureVms {
    $script:allVms = Get-AzVM -ResourceGroupName $script:resourceGroupName -ErrorAction Stop
}


function GetPrivateIps {
    $result = @()
    foreach ($vm in $script:allVms) {
        $vmName = $vm.Name
        $net = $vm.NetworkProfile
        $nicId = $net.NetworkInterfaces[0].Id
        $nicName = ($nicId -split "/")[-1]

        $nic = Get-AzNetworkInterface -ResourceGroupName $script:resourceGroupName -Name $nicName -ErrorAction Stop
        $privateIp = $nic.IpConfigurations[0].PrivateIpAddress

        $result += [pscustomobject]@{
            VMName    = $vmName
            PrivateIp = $privateIp
        }
    }

    $script:privateIpsList = $result
    $script:vmToExecuteCommandOn = $script:privateIpsList[0]
}

function GetVmPrivateIp {
    param(
        $TheVm
    )
    $net = $TheVm.NetworkProfile
    $nicId = $net.NetworkInterfaces[0].Id
    $nicName = ($nicId -split "/")[-1]
    $nic = Get-AzNetworkInterface -ResourceGroupName $script:resourceGroupName -Name $nicName -ErrorAction Stop
    $privateIp = $nic.IpConfigurations[0].PrivateIpAddress
    return $privateIp
}


function InvokeCommandOnLinuxServer {
    $linuxScript = AddClientNodeOnAllCachesLinux
    Invoke-AzVMRunCommand -ResourceGroupName $resourceGroupName -VMName $($script:vmToExecuteCommandOn.VMName) -CommandId "RunShellScript" -ScriptString $linuxScript
}

function InvokeCommandOnWindowsServer {
    $windowsScript = AddClientNodeOnAllCachesWindows
    Invoke-AzVMRunCommand -ResourceGroupName $resourceGroupName -VMName $($script:vmToExecuteCommandOn.VMName) -CommandId "RunPowerShellScript" -ScriptString $windowsScript
}

function AddClientNodeOnAllCachesWindows {
    param(
        $TheVm,
        [string]$privateIp
    )
    $cmd = ""

    if (-not $CacheName) {
        $cachesList = $TheVm.Tags["Caches"].Split(",")
        foreach ($cache in $cachesList) {
            $cmd += "Add-ClientNode -CacheName $cache -ClientNode $ClientNode -Port $Port -Server $privateIp"
            if ($AcquireServerMapping) { $cmd += " -AcquireServerMapping" }
            if ($NoLogo) { $cmd += " -NoLogo" }
            if ($UpdateServerConfig) { $cmd += " -UpdateServerConfig" }
            if ($Credentials) { $cmd += " -Credentials `"$($Credentials.GetNetworkCredential().UserName):$($Credentials.GetNetworkCredential().Password)`"" }
            $cmd += "`n"
        }
    }
    else {
        $cmd = "Add-ClientNode -CacheName $CacheName -ClientNode $ClientNode -Port $Port -Server $privateIp"
    }
    Invoke-AzVMRunCommand -ResourceGroupName $script:resourceGroupName -VMName $($TheVm.Name) -CommandId "RunPowerShellScript" -ScriptString $cmd
    return $cmd;
}

function AddClientNodeOnAllCachesLinux {
    param(
        $TheVm,
        [string]$privateIp
    )
    $cmd = ""
    if (-not $CacheName) {
        $cachesList = $TheVm.Tags["Caches"].Split(",")
        if ($cachesList.count -gt 0) {
            foreach ($cache in $cachesList) {
                $cmd += "/opt/ncache/bin/tools/add-clientnode -cacheName $cache -clientNode $ClientNode -port $Port -Server $privateIp"
                if ($AcquireServerMapping) { $cmd += " -acquireServerMapping" }
                if ($NoLogo) { $cmd += " -noLogo" }
                if ($UpdateServerConfig) { $cmd += " -updateServerConfig" }
                if ($Credentials) { $cmd += " -credentials `"$($Credentials.GetNetworkCredential().UserName):$($Credentials.GetNetworkCredential().Password)`"" }
                if ($cmd) { $cmd += "; " }  
            }
        }
    }
    else {
        $cmd += "/opt/ncache/bin/tools/add-clientnode -cacheName $CacheName -clientNode $ClientNode -port $Port -Server $privateIp"
    }
    Invoke-AzVMRunCommand -ResourceGroupName $script:resourceGroupName -VMName $($TheVm.Name) -CommandId "RunShellScript" -ScriptString $cmd
    return $cmd
}

function InvokeCommandOnServer {

}

function GetAllCachesOnWindows {
    foreach ($vm in $script:allVms) {
        if ($vm.Tags.ContainsKey("Caches")) {
            $privateIp = GetVmPrivateIp -TheVm $vm
            AddClientNodeOnAllCachesWindows -TheVm $vm -privateIp $privateIp
        }
    }
}

function GetAllCachesOnLinux {
    foreach ($vm in $script:allVms) {
        if ($vm.Tags.ContainsKey("Caches")) {
            $privateIp = GetVmPrivateIp -TheVm $vm
            AddClientNodeOnAllCachesLinux -TheVm $vm -privateIp $privateIp
        }
    }
}

function ExecuteCommands {
    $resourceGroup = Get-AzResourceGroup -ErrorAction Stop | Where-Object { $_.Tags -and $_.Tags.Contains("EnvironmentName") -and $_.Tags["EnvironmentName"] -eq $EnvironmentName } 
    if (-not $resourceGroup) {
        throw "No such environment exists"
    }
    $script:tagsResult = $resourceGroup.Tags
    $script:resourceGroupName = $resourceGroup | Select-Object -ExpandProperty resourceGroupName
    GetAzureVms
    GetPrivateIps
    if ($resourceGroup.Tags["OsType"] -eq "Windows") {
        GetAllCachesOnWindows
    }
    else {
        GetAllCachesOnLinux
    }
}

    
try {
    if (-not (Get-AzContext)) {
        Connect-AzAccount
        if (Get-AzContext) {
            ExecuteCommands
        }
    }
    else {
        ExecuteCommands
    }
}
catch {
    Write-Error $($_.Exception.Message)
}