<#
NAME: Stackato_Deploy_Cloud_from_Template.ps1
AUTHOR: Chris Danielewski
DATE : 11/23/2016
PURPOSE: This script is used to deploy Stackato Cloud.
This is done by deploying a VM from template and applying extra configuration.
OUTPUT: N/A
REQUIRED UTILITIES: PowerCLI, SSH-Sessions module
==========================================================================
CHANGE HISTORY:
v1.0 11/18/2016 CD New script!
#>
if (!(Get-module | where {$_.Name -eq "SSH-Sessions"})) {
Import-Module SSH-Sessions
}
#Ask for VM Name
$cloudname = Read-Host "Please enter the desired name for the new Stackato cloud (eg. p4stackato_, do not append the role)"
#Ask for Odd / Even Cluster
$OddEven = Read-Host "Odd or even vAPP/Cluster? 1 - Odd , 2 - Even"
#Declere nodes to be built
Write-Host ""
if ($OddEven -eq "1") {
$nodestodeploy = @()
$nodestodeploy += $cloudname + "head01"
$nodestodeploy += $cloudname + "router01"
$nodestodeploy += $cloudname + "services01"
$nodestodeploy += $cloudname + "dea01"
$nodestodeploy += $cloudname + "dea03"
}
elseif ($OddEven -eq "2") {
$nodestodeploy = @()
$nodestodeploy += $cloudname + "head02"
$nodestodeploy += $cloudname + "router02"
$nodestodeploy += $cloudname + "services02"
$nodestodeploy += $cloudname + "dea02"
$nodestodeploy += $cloudname + "dea04"
}
else {Write-Host "Improper answer received."; Exit}
#Set some POD/DC variables
$search = "x.local"
if ($cloudname -like '*p1*'){
$pod = "1"
[int]$podOctet = x
$dns = "x"
$proxy = "x"
}
if ($cloudname -like '*p2*'){
$pod = "2"
[int]$podOctet = x
$dns = "x"
$proxy = "x"
}
if ($cloudname -like '*p3*'){
$pod = "3"
$dns = "x"
$proxy = "x"
[int]$podOctet = x
}
if ($cloudname -like '*p4*'){
$pod = "4"
[int]$podOctet = x
$dns = "x"
$proxy = "x"
}
if ($cloudname -eq $null) {Write-Host "No system name found."; Exit}
$InputvApp = $vApp = $Cluster = $NewVM = $RandHost = $RandDSC = $null
#Output the available vApps to choose from
Write-Host ""
$vapps = Get-vApp -server $DefaultVIServers[0] | sort Name
#Create Cluster Menu and Prompt user to Select Desired Cluster
if ($vapps -is [System.Array]) {
$vmenu = @{}
Write-Host "`nvApp selection for $($DefaultVIServers[0]):"
for ($i=1;$i -le $vapps.count; $i++) {
Write-Host "$i. $($vapps[$i-1].name)"
$vmenu.Add($i,($vapps[$i-1].name))
}
[int]$cans = Read-Host 'Enter desired vApp'
if ($cans -eq '0' -or $cans -gt $i) {Write-Host -ForegroundColor Red -Object "Invalid selection.`n";Exit}
#Set Cluster Variable to User's choice
$vApp = get-vApp ($vmenu.Item($cans))
}
#$InputvApp = Read-Host "What vApp should the new Stackato VM be placed in?"
$InputvApp = $vApp.name
Write-Host ""
Write-Host ""
Write-Host "Select one of the available Stackato versions to be installed:"
Write-Host ""
Write-Host "1) 3.4.2"
Write-Host "2) 3.6.2"
$InputVersion = Read-Host "Which version of Stackato should be installed?"
Write-Host ""
if ($InputVersion -eq "1") {$mySourceTemplate = Get-Template -Name "stackato-3.4.2-template-vm-tools"}
elseif ($InputVersion -eq "2") {$mySourceTemplate = Get-Template -Name "stackato-3.6.2-template-vm-tools"}
else {Write-Host "Improper answer received."; Exit}
#Prompt for production password to be set
$properpwd = Read-Host "Please enter stackato production password to be configured"
#Array to hold IPs
$SetLastOct = @()
#Loop through array and ask for last octet
for ($i=0; $i -lt $nodestodeploy.length; $i++) {
$SetLastOct += Read-Host "Please enter last IP address Octect for" $nodestodeploy[$i] "(172.$podOctet.x.x)"
}
for ($i=0; $i -lt $nodestodeploy.length; $i++) {
$svrname = $nodestodeploy[$i]
$lastOctet = $SetLastOct[$i]
$vApp = Get-VApp $vApp
$Cluster = Get-Cluster (Get-ResourcePool -Id $vApp.Parent.Id).Parent
$RandHost = $Cluster | Get-VMHost | where {$_.ConnectionState -eq "Connected"} | Get-Random
$StkPG = $RandHost | Get-VDSwitch | Get-VDPortgroup | where {$_.VlanConfiguration.VlanId -eq '<VLAN>'}
$RandDSC = Get-DatastoreCluster -Id ($RandHost | Get-Datastore | where {$_.ParentFolderId -like "StoragePod-group-*"} | Get-Random).ParentFolderId
###Deploying to Cluster with DVS Only###
$myDatastoreCluster = Get-DatastoreCluster -Name $RandDSC
$vmhost = Get-VMHost -Name $RandHost
Write-Host -foregroundcolor green "VMware-tools heartbeat detected"
New-VM -Name $svrname -Template $mySourceTemplate -Datastore $myDatastoreCluster -ResourcePool $vApp
###Deploy Advanced Configuration on New VM###
#Overwritte template settings if needed
$NewVM = $Cluster | Get-VM $svrname
$NewVM | Set-VM -MemoryGB 8 -NumCpu 2 -Confirm:$false | Out-Null
$NewVM | Get-NetworkAdapter | Set-NetworkAdapter -Portgroup $StkPG -Confirm:$false | Out-Null
New-AdvancedSetting -Entity $NewVM -Name keyboard.typematicMinDelay -Value '2000000' -Force:$true -Confirm:$false | Out-Null
$NewVM | Start-VM | Out-Null
$fqdn = $svrname + ".x.local"
#Wait for VM to boot
$tools = Wait-Tools -VM $svrname -TimeoutSeconds 240
$tools.ScriptOutput
write-host -foregroundcolor yellow ""
Write-Host -foregroundcolor green "VMware-tools heartbeat detected"
Write-Host ""
write-host -foregroundcolor yellow "Waiting 60sec - Stackato boot process"
Start-Sleep 60
#configure static IP
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'auto eth0' > /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "Updating values for /etc/network/interfaces"
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'iface eth0 inet static' >> /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "IP value set to static"
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'address 172.$podOctet.x.$lastOctet' >> /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "IPv4 address set"
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'netmask 255.255.255.0' >> /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "Netmask set"
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'network 172.$podOctet.x.0' >> /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "Network address set"
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'broadcast 172.$podOctet.x.255' >> /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "Broadcast address set"
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'gateway 172.$podOctet.x.x' >> /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "Gateway address set"
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'dns-nameservers $dns' >> /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "DNS set"
$output = Invoke-VMScript -VM $svrname -ScriptText "echo 'dns-search x.local' >> /etc/network/interfaces" -GuestUser stackato -GuestPassword stackato123 -WarningAction SilentlyContinue
$output.ScriptOutput
write-host -foregroundcolor yellow "Search domain set"
write-host -foregroundcolor yellow ""
write-host -foregroundcolor yellow "Static IP values written to /etc/network/interfaces"
write-host -foregroundcolor yellow ""
write-host -foregroundcolor green "Rebooting guest OS"
Get-VM $NewVM | Restart-VMGuest
$tools = Wait-Tools -VM $svrname -TimeoutSeconds 600
$tools.ScriptOutput
write-host -foregroundcolor yellow ""
Write-Host -foregroundcolor green "VMware-tools heartbeat detected"
write-host -foregroundcolor yellow ""
write-host -foregroundcolor yellow "Waiting 90sec - Stackato post boot configs"
Start-Sleep 90
write-host -foregroundcolor yellow ""
write-host -foregroundcolor Green "Invoking SSH session"
$sessionVC0 = $(New-SshSession -ComputerName $fqdn -Username "stackato" -Password "stackato123")
#write-host "session - $session"
if ($sessionVC0 -match "Successfully connected") {
#Disable password expiry for root
write-host -foregroundcolor yellow ""
write-host -foregroundcolor yellow "Updating hostname using kato"
Invoke-SshCommand -ComputerName $fqdn -Command "kato node rename $fqdn"
write-host -foregroundcolor Yellow "Hostname updated"
write-host ""
write-host -foregroundcolor yellow "Updating timezone"
write-host ""
Invoke-SshCommand -ComputerName $fqdn -Command "echo 'Etc/GMT+6' | sudo tee /etc/timezone;sudo dpkg-reconfigure -f noninteractive tzdata"
write-host -foregroundcolor Yellow "Timezone updated"
write-host ""
write-host -foregroundcolor yellow "Updating NTP settings"
write-host ""
Invoke-SshCommand -ComputerName $fqdn -Command "echo 'server NTPX' >> /etc/ntp.conf"
write-host -foregroundcolor Yellow "NTP settings updated"
write-host ""
write-host -foregroundcolor yellow "Updating Proxy"
write-host ""
#Export proxy settings to user profile - more perm
Invoke-SshCommand -ComputerName $fqdn -Command "echo 'export http_proxy=http://$proxy' >> /home/stackato/.bashrc"
write-host -foregroundcolor Yellow "Proxy updated"
write-host ""
#write-host -foregroundcolor yellow "Running updates"
#write-host ""
#Invoke-SshCommand -ComputerName $fqdn -Command "sudo apt-get update"
#Invoke-SshCommand -ComputerName $fqdn -Command "sudo unattended-upgrades -d"
#write-host -foregroundcolor Yellow "Updates complete"
#write-host ""
write-host -foregroundcolor yellow "Updating password to prod"
write-host ""
Invoke-SshCommand -ComputerName $fqdn -Command "echo 'stackato:'$properpwd | sudo chpasswd" -Verbose
write-host -foregroundcolor Yellow "Password has been changed"
write-host ""
write-host -foregroundcolor yellow "Rebooting the node"
Invoke-SshCommand -ComputerName $fqdn -Command "sudo reboot"
write-host "Connection to $fqdn is now closed"
}
Else
{
write-host -ForegroundColor Red "Unable to connect to $fqdn"
}
}