<commit_before>﻿<#
	.SYNOPSIS
		Installs elastic search on the given nodes.
	.DESCRIPTION
		This script runs as a VM extension and installs elastic search on the cluster nodes.
	.PARAMETER SomeParam
		Description of the parameter.
	.EXAMPLE
		SetupElasticVm.ps1 -elasticVersion '1.7.2' -elasticClusterName 'poltergeist'
	.LINK
		<<demo url>>
#>
Param(
    [Parameter(Mandatory=$true)][string]$elasticSearchVersion,
    [string]$jdkDownloadLocation,
	[string]$elasticSearchBaseFolder,
	[string]$elasticClusterName,
	[switch]$masterOnlyNode,
	[switch]$clientOnlyNode,
	[switch]$dataOnlyNode
)

# To set the env vars permanently, need to use registry location
Set-Variable regEnvPath -Option Constant -Value 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment'

function Log-Output(){
	$args | Write-Host -ForegroundColor Green
}

function Log-Error(){
	$args | Write-Host -ForegroundColor Red
}

Set-Alias -Name lmsg -Value Log-Output -Description "Displays an informational message in green color" 
Set-Alias -Name lerr -Value Log-Error -Description "Displays an error message in red color" 

function Initialize-Disks{
	
    # Get raw disks
    $disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number
    
    # Get letters starting from F
    $label = 'datadisk-'
    $letters = 70..89 | ForEach-Object { ([char]$_) }
    $letterIndex = 0
	if($disks -ne $null)
	{
        lmsg 'Found attached VHDs with raw partition...' $disks
        try{
            foreach($disk in $disks){
                $driveLetter = $letters[$letterIndex].ToString()
                lmsg 'Formatting disk...' $driveLetter
		        $disk | Initialize-Disk -PartitionStyle MBR -PassThru |	New-Partition -UseMaximumSize -DriveLetter $driveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "$label$letterIndex" -Confirm:$false -Force
                $letterIndex++
            }
        }catch [System.Exception]{
			lerr $_.Exception.Message
            lerr $_.Exception.StackTrace
			Break
		}
	}
    
    #return $letters[0].ToString()
}

function Download-Jdk($targetDrive, $downloadLocation){
	# download JDK from a given source URL to destination folder
	try{
			$destination = if ($targetDrive -eq $null) {"$env:HOMEDRIVE\Downloads\Java\Jdk-Installer.exe"} else {"$targetDrive`:\Downloads\Java\Jdk-Installer.exe"}
			$source = if ($downloadLocation -eq $null) {"http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-i586.exe"} else {$downloadLocation}
            
            # create folder if doesn't exists and suppress the output
            $folder = split-path $destination
            if (!(Test-Path $folder)) {
                New-Item -Path $folder -ItemType Directory | Out-Null
            }

			$client = new-object System.Net.WebClient 
			$cookie = "oraclelicense=accept-securebackup-cookie"

            lmsg 'Downloading JDK from ' $source ' to ' $destination

			$client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie) 
			$client.downloadFile($source, $destination)
		}catch [System.Net.WebException],[System.Exception]{
			lerr $_.Exception.Message
            lerr $_.Exception.StackTrace
			Break
		}

	return $destination
}

function Install-Jdk($sourceLoc, $targetDrive)
{
	$installPath = if($targetDrive -eq $null) {"$env:HOMEDRIVE\Program Files\JAVA\JDK"} else {"$targetDrive`:\Program Files\JAVA\JDK"}

	try{
        lmsg "Installing java on the box under $installPath"
		$proc = Start-Process -FilePath $sourceLoc -ArgumentList "/s INSTALLDIR=`"$installPath`"" -PassThru -Wait 
        #if($proc.ExitCode -ne 0){
            #THROW "JDK installation error"
        #}
		
    }catch [System.Exception]{
		lerr $_.Exception.Message
        lerr $_.Exception.StackTrace
	    Break
	}
	
	return $installPath
}

function Download-ElasticSearch($elasticVersion, $targetDrive){
	# download ElasticSearch from a given source URL to destination folder
	try{
			$source = if ($elasticVersion -eq $null) {"https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.zip"} else {"https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-$elasticVersion.zip"}
			$destination = if ($targetDrive -eq $null) {"$env:HOMEDRIVE\Downloads\ElasticSearch\Elastic-Search.zip"} else {"$targetDrive`:\Downloads\ElasticSearch\Elastic-Search.zip"}
            
            # create folder if doesn't exists and suppress the output
            $folder = split-path $destination
            if (!(Test-Path $folder)) {
                New-Item -Path $folder -ItemType Directory | Out-Null
            }

			$client = new-object System.Net.WebClient <commit_msg>Updated Java version and added a wait for installation exit<commit_after>﻿<#
	.SYNOPSIS
		Installs elastic search on the given nodes.
	.DESCRIPTION
		This script runs as a VM extension and installs elastic search on the cluster nodes.
	.PARAMETER SomeParam
		Description of the parameter.
	.EXAMPLE
		SetupElasticVm.ps1 -elasticVersion '1.7.2' -elasticClusterName 'poltergeist'
	.LINK
		<<demo url>>
#>
Param(
    [Parameter(Mandatory=$true)][string]$elasticSearchVersion,
    [string]$jdkDownloadLocation,
	[string]$elasticSearchBaseFolder,
	[string]$elasticClusterName,
	[switch]$masterOnlyNode,
	[switch]$clientOnlyNode,
	[switch]$dataOnlyNode
)

# To set the env vars permanently, need to use registry location
Set-Variable regEnvPath -Option Constant -Value 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment'

function Log-Output(){
	$args | Write-Host -ForegroundColor Green
}

function Log-Error(){
	$args | Write-Host -ForegroundColor Red
}

Set-Alias -Name lmsg -Value Log-Output -Description "Displays an informational message in green color" 
Set-Alias -Name lerr -Value Log-Error -Description "Displays an error message in red color" 

function Initialize-Disks{
	
    # Get raw disks
    $disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number
    
    # Get letters starting from F
    $label = 'datadisk-'
    $letters = 70..89 | ForEach-Object { ([char]$_) }
    $letterIndex = 0
	if($disks -ne $null)
	{
        lmsg 'Found attached VHDs with raw partition...' $disks
        try{
            foreach($disk in $disks){
                $driveLetter = $letters[$letterIndex].ToString()
                lmsg 'Formatting disk...' $driveLetter
		        $disk | Initialize-Disk -PartitionStyle MBR -PassThru |	New-Partition -UseMaximumSize -DriveLetter $driveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "$label$letterIndex" -Confirm:$false -Force
                $letterIndex++
            }
        }catch [System.Exception]{
			lerr $_.Exception.Message
            lerr $_.Exception.StackTrace
			Break
		}
	}
    
    #return $letters[0].ToString()
}

function Download-Jdk($targetDrive, $downloadLocation){
	# download JDK from a given source URL to destination folder
	try{
			$destination = if ($targetDrive -eq $null) {"$env:HOMEDRIVE\Downloads\Java\Jdk-Installer.exe"} else {"$targetDrive`:\Downloads\Java\Jdk-Installer.exe"}
			$source = if ($downloadLocation -eq $null) {"http://download.oracle.com/otn-pub/java/jdk/8u65-b17/jdk-8u65-windows-i586.exe"} else {$downloadLocation}
            
            # create folder if doesn't exists and suppress the output
            $folder = split-path $destination
            if (!(Test-Path $folder)) {
                New-Item -Path $folder -ItemType Directory | Out-Null
            }

			$client = new-object System.Net.WebClient 
			$cookie = "oraclelicense=accept-securebackup-cookie"

            lmsg 'Downloading JDK from ' $source ' to ' $destination

			$client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie) 
			$client.downloadFile($source, $destination)
		}catch [System.Net.WebException],[System.Exception]{
			lerr $_.Exception.Message
            lerr $_.Exception.StackTrace
			Break
		}

	return $destination
}

function Install-Jdk($sourceLoc, $targetDrive)
{
	$installPath = if($targetDrive -eq $null) {"$env:HOMEDRIVE\Program Files (x86)\JAVA\JDK"} else {"$targetDrive`:\Program Files (x86)\JAVA\JDK"}

	try{
        lmsg "Installing java on the box under $installPath..."
		$proc = Start-Process -FilePath $sourceLoc -ArgumentList "/s INSTALLDIR=`"$installPath`"" -Wait -PassThru
        $proc.WaitForExit()
        lmsg "JDK installed under $installPath!"
        
        #if($proc.ExitCode -ne 0){
            #THROW "JDK installation error"
        #}
		
    }catch [System.Exception]{
		lerr $_.Exception.Message
        lerr $_.Exception.StackTrace
	    Break
	}
	
	return $installPath
}

function Download-ElasticSearch($elasticVersion, $targetDrive){
	# download ElasticSearch from a given source URL to destination folder
	try{
			$source = if ($elasticVersion -eq $null) {"https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.zip"} else {"https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-$elasticVersion.zip"}
			$destination = if ($targetDrive -eq $null) {"$env:HOMEDRIVE\Downloads\ElasticSearch\Elastic-Search.zip"} else {"$targetDrive`:\Downloads\ElasticSearch\Elastic-Search.zip"}
            
            # create folder if doesn't exists and suppress the output
            $folder = split-path $destination
            if (!(Test-Path $folder)) {
                New-Item -Path $folder -ItemType Directory | Out-Null
            }

			$client = new-object System.Net.WebClient 
{'fraction_non_alphanumeric': 0.1182125501795901, 'fraction_numerical': 0.005493344601732516, 'mean_word_length': 3.226339285714286, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4034925', 'n_tokens_mistral': 3003, 'n_tokens_neox': 2780, 'n_words': 723}