<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>YAML Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/category/cloud-init/yaml/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/category/cloud-init/yaml/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Thu, 24 Dec 2020 06:58:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>YAML Archives - LucD notes</title>
	<link>https://www.lucd.info/category/cloud-init/yaml/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>A Hitchhikers Guide to SRS 1.0.0</title>
		<link>https://www.lucd.info/2020/12/23/a-hitchhikers-guide-to-srs-1-0-0/</link>
					<comments>https://www.lucd.info/2020/12/23/a-hitchhikers-guide-to-srs-1-0-0/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Wed, 23 Dec 2020 14:08:53 +0000</pubDate>
				<category><![CDATA[Cloud-init]]></category>
		<category><![CDATA[Photon]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[REST API]]></category>
		<category><![CDATA[SRS]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[vSphere]]></category>
		<category><![CDATA[YAML]]></category>
		<category><![CDATA[Appliance]]></category>
		<guid isPermaLink="false">https://www.lucd.info/?p=7381</guid>

					<description><![CDATA[Sometimes announcements tend to disappear in the cracks of time. When the Script [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Sometimes announcements tend to disappear in the cracks of time. When the <a href="https://github.com/vmware/script-runtime-service-for-vsphere/releases/tag/v1.0.0" target="_blank" rel="noopener">Script Runtime Service for vSphere (SRS) 1.0.0</a> was announced, I had the feeling just that happened.</p>
<p>When version <strong>1.0.0</strong> of this open-sourced (!) product was released, I had expected much more buzz on social media from <a href="https://code.vmware.com/web/tool/12.1.0/vmware-powercli" target="_blank" rel="noopener">VMware PowerCLI</a> users.&nbsp;</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1000" height="426" src="https://www.lucd.info/wp-content/uploads/2020/12/SRS_1.png" alt="" class="wp-image-7400" srcset="https://www.lucd.info/wp-content/uploads/2020/12/SRS_1.png 1000w, https://www.lucd.info/wp-content/uploads/2020/12/SRS_1-300x128.png 300w, https://www.lucd.info/wp-content/uploads/2020/12/SRS_1-768x327.png 768w, https://www.lucd.info/wp-content/uploads/2020/12/SRS_1-720x307.png 720w" sizes="(max-width: 1000px) 100vw, 1000px" /></figure>



<p>This appliance does in fact bring an answer to a wish that many PowerShell/PowerCLI users have had for years: a &#8220;<em><strong>Scripting Host</strong></em>&#8220;!</p>



<p>This <strong>Hitchhikers Guide to SRS 1.0.0</strong> post will show how I build my own customised SRS appliance, and how I use it to run PowerShell/PowerCLI scripts.</p>



<span id="more-7381"></span>



<h2 class="wp-block-heading">Introduction</h2>



<p>When you visit the <a href="https://github.com/vmware/script-runtime-service-for-vsphere" target="_blank" rel="noopener">Script Runtime Service for vSphere (SRS) repository</a>, you&#8217;ll notice that this <strong>open-sourced</strong> project comes with extensive <strong>documentation</strong>.</p>



<p>Always a great characteristic for an open-sourced project!</p>



<p>The installation as a VM&nbsp; (from the <a href="https://github.com/vmware/script-runtime-service-for-vsphere/releases/tag/v1.0.0" target="_blank" rel="noopener">downloadable OVF files</a>) or in a Kubernetes cluster is well documented.</p>



<p>The available documentation in the SRS repo contains instructions on how to build, and customise, your own SRS appliance under the <a href="https://github.com/vmware/script-runtime-service-for-vsphere/blob/master/BUILD_AND_RUN.md" target="_blank" rel="noopener">Build and Run</a> page.</p>
<p>This blog post documents how I did exactly that.</p>
<p>I wanted to have a fully automated, self-documenting, and repeatable method, think CI, to roll out my own SRS station.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="892" src="https://www.lucd.info/wp-content/uploads/2020/12/SRS-Build-1024x892.png" alt="" class="wp-image-7405" srcset="https://www.lucd.info/wp-content/uploads/2020/12/SRS-Build-1024x892.png 1024w, https://www.lucd.info/wp-content/uploads/2020/12/SRS-Build-300x261.png 300w, https://www.lucd.info/wp-content/uploads/2020/12/SRS-Build-768x669.png 768w, https://www.lucd.info/wp-content/uploads/2020/12/SRS-Build-720x627.png 720w, https://www.lucd.info/wp-content/uploads/2020/12/SRS-Build.png 1394w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<ol class="wp-block-list"><li>Roll out the Builder station and install the prerequisites.</li><li>Start the Build of the SRS Appliance on the Builder station</li><li>Retrieve the OVF/OVA files, created from the SRS Appliance, that came out of the build</li><li>Use the OVF/OVA to deploy your SRS station</li></ol>



<h2 class="wp-block-heading">Creating the SRS Builder</h2>



<h3 class="wp-block-heading">The Prerequisites</h3>



<p>To create an SRS OVA/OVF yourself you need a &#8220;builder&#8221; station. And on that station there need to be a number of packages installed.&nbsp;</p>
<p>I included the installation of these packages in the &#8220;build&#8221; I describe in the next section.</p>
<p>The <a href="https://github.com/vmware/script-runtime-service-for-vsphere/blob/master/appliance/buildappliance.sh" target="_blank" rel="noopener">buildappliance.sh</a> script, which you will need to run on the builder station, will use the packages to setup and configure an SRS appliance (VMware Photon based), and as the last step will generate an OVA file from the appliance.</p>
<p>That OVA file can then be used to deploy your SRS VM.</p>



<p>This is also the place where you can install extra applications and extra PowerShell modules that will be included in your DIY SRS OVA.</p>



<h3 class="wp-block-heading">Create the Builder station</h3>



<h4>Preparation</h4>



<p>I used an <a href="https://cloud-images.ubuntu.com/bionic/current/" target="_blank" rel="noopener">Ubuntu 18.04 LTS</a> station as the Builder station. As the download of the ISO is also automated, I use the <strong>Daily Build</strong>.</p>



<pre class="lang:ps decode:true  ">
#region Get the Ubuntu Bionic Cloud Image OVA Daily Build

$uri = 'https://cloud-images.ubuntu.com/bionic/current/'
$ovaName = 'bionic-server-cloudimg-amd64.ova'
$repository = 'D:\Repository\Linux\Ubuntu\'

$currentOVA = Get-ChildItem -Path "$repository\$ovaName" -ErrorAction SilentlyContinue
if (-not $currentOVA -or $currentOVA.LastWriteTime.Date -lt $now.Date) {
  $sWeb = @{
    Uri = "$uri$ovaName"
    OutFile = "$repository$ovaName"
    Verbose = $false
  }
  Invoke-WebRequest @sWeb
  $currentOVA = Get-ChildItem -Path "$repository\$ovaName"
}
#endregion
</code></pre>



<p>I also check if the <strong>DNS A</strong>&nbsp;and <strong>PTR</strong> records for the Builder station are present in DNS. The Builder station I used has an FQDN of <strong>srsbuilder.local.lab</strong> with an IP address of <strong>192.168.10.88</strong>.</p>



<pre class="lang:ps decode:true  ">
$hostName = 'srsbuilder'
$domain = 'local.lab'
$ipAddress = '192.168.10.88'

$if = Get-Netadapter
$sDns = @{
   AddressFamily = 'IPv4'
   InterfaceIndex = $if.ifIndex
}
$dns = Get-DnsClientServerAddress @sDns
$dnsServer = $dns.ServerAddresses | Get-Random
$ip = $ipAddress.Split('.')
$reverseZone = "$($ip[2]).$($ip[1]).$($ip[0]).in-addr.arpa"

try{
  Write-Verbose (Get-LogText -Text "Check DNS A record for $hostname.$domain with $ipAddress")
  $sQDns = @{
    Name = "$hostName.$domain"
    Server = $dnsServer
    ErrorAction = 'Stop'
    Verbose = $false
  }
  Resolve-DnsName @sQDns  | Out-Null
  Write-Verbose (Get-LogText -Text "DNS A record exists for $hostname.$domain with $ipAddress")
}
catch{
  Write-Verbose (Get-LogText -Text "Create A record for $hostname.$domain with $ipAddress")
  $sADns = @{
    Name = $hostName
    ZoneName = $domain
    IPv4Address = $ipAddress
    A = $true
    CreatePtr = $true
    ComputerName = $dnsServer
  }
  Add-DnsServerResourceRecord @sADns | Out-Null
}
try {
  Write-Verbose (Get-LogText -Text "Check DNS PTR record for $hostname.$domain with $ipAddress")
  $sQdns = @{
    Name = $ipAddress
    Type = 'PTR'
    Server = $dnsServer
    Verbose = $false
    ErrorAction = 'Stop'
  }
  Resolve-DnsName @sQdns | Out-Null
  Write-Verbose (Get-LogText -Text "DNS PTR record exists for $hostname.$domain with $ipAddress")
}
catch {
  Write-Verbose (Get-LogText -Text "Create PTR record for $hostname.$domain with $ipAddress")
  $sADns = @{
    Name = "$($ip[3])"
    PtrDomainName = "$hostname.$domain"
    ZoneName = $reverseZone
    ComputerName = $dnsServer
  }
  Add-DnsServerResourceRecordPtr @sADns | Out-Null
}
</code></pre>



<h4>Deploy the Builder</h4>



<p>For the rollout of this Builder station, I used the Cloud-Init method I described in <a href="https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/" target="_blank" rel="noopener">Cloud-Init – Part 2 – Advanced Ubuntu</a>. I had to make some small changes to the <strong>Install-CloudInitVM</strong> function from that post, so I was able to specify the memory size and the disk size. The default sizes were insufficient to run the SRS Appliance build.</p>



<p>The updated function now has <strong>MemoryGB</strong> and <strong>DiskGB</strong> parameters.</p>



<p>I also had to add a snippet to handle the issue with the $PSScriptRoot parameter when running the code from the Visual Studio Code editor.</p>



<pre class="lang:ps decode:true  ">
function Install-CloudInitVM {
  <#
.SYNOPSIS
  Deploy a VM from an OVA file and use cloud-init for the configuration
  .DESCRIPTION
  This function will deploy an OVA file.
  The function transfer the user-data to the cloud-init process on the VM with
  one of the OVF properties.
.NOTES
  Author:  Luc Dekens
  Version:
  1.0 05/12/19  Initial release
.PARAMETER OvaFile
  Specifies the path to the OVA file
.PARAMETER VmName
  The displayname of the VM
.PARAMETER ClusterName
  The cluster onto which the VM shall be deployed
.PARAMETER DsName
  The datastore on which the VM shall be deployed
.PARAMETER PgName
  The portgroupname to which the VM shall be connected
.PARAMETER CloudConfig
  The path to the YAML file containing the user-data
.PARAMETER Credential
  The credentials for a user in the VM's guest OS
.EXAMPLE
  $sCloudInitVM = @{
    OvaFile = '.\bionic-server-cloudimg-amd64.ova'
    VmName = $vmName
    ClusterName = $clusterName
    DsName = $dsName
    PgName = $pgName
    CloudConfig = '.\user-data.yaml'
    Credential = $cred
  }
  Install-CloudInitVM @sCloudInitVM
#>

  [cmdletbinding()]
  param(
    [string]$OvaFile,
    [string]$VmName,
    [string]$ClusterName,
    [string]$DsName,
    [string]$PgName,
    [string]$CloudConfig,
    [PSCredential[]]$Credential,
    [int]$MemoryGB,
    [int]$DiskGB
  )

#region Bypass for known issue in VSC ()
# See https://github.com/PowerShell/vscode-powershell/issues/633

  if ($psISE) {
    $dir = Split-Path -Path $psISE.CurrentFile.FullPath
  }
  else {
    if ($profile -match "VScode") {
      $dir = Split-Path $psEditor.GetEditorContext().CurrentFile.Path
    }
    else {
      $dir = $PSScriptRoot
    }
  }
#endregion

  $waitJob = (Get-Command -Name "$dir\Wait-Job.ps1").ScriptBlock
  $userData = Get-Content -Path "$dir\$CloudConfig" -Raw

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Starting deployment of $vmName"

  $start = Get-Date

  $vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
  if ($vm) {
    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Cleaning up"
    if ($vm.PowerState -eq 'PoweredOn') {
      Stop-VM -VM $vm -Confirm:$false | Out-Null
    }
    Remove-VM -VM $vm -DeletePermanently -Confirm:$false
  }

  $ovfProp = Get-OvfConfiguration -Ovf $ovaFile
  $ovfProp.Common.user_data.Value = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($userData))
  $ovfProp.NetworkMapping.VM_Network.Value = $pgName

  $sApp = @{
    Source = $ovaFile
    Name = $vmName
    Datastore = Get-Datastore -Name $dsName
    DiskStorageFormat = 'Thin'
    VMHost = Get-Cluster -Name $clusterName | Get-VMHost | Get-Random
    OvfConfiguration = $ovfProp
  }
  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Importing OVA"
  $vm = Import-VApp @sApp

  if($MemoryGB){
    $vm = Set-VM -VM $vm -MemoryGB $MemoryGB -Confirm:$false
  }
  if($DiskGB){
    $hd = Get-HardDisk -VM $vm | Set-HardDisk -CapacityGB $DiskGB -Confirm:$false
  }

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Starting the VM"
  Start-VM -VM $vm -Confirm:$false -RunAsync | Out-Null

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Waiting for cloud-init to finish"

  $User = $Credential.GetNetworkCredential().UserName
  $Password = $Credential.GetNetworkCredential().Password

  $sJob = @{
    Name = 'WaitForCloudInit'
    ScriptBlock = $waitJob
    ArgumentList = $vm.Name, $User, $Password, $global:DefaultVIServer.Name, $global:DefaultVIServer.SessionId
  }
  Start-Job @sJob | Receive-Job -Wait

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Deployment complete"

  Write-Verbose "<code>nDeployment took $([math]::Round((New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds,0)) seconds"
}
</code></pre>



<p>The call to the <strong>Install-CloudInitVM</strong> functions is rather straightforward.</p>



<pre class="lang:ps decode:true  ">
$vmName = 'SRSBuilder'

# Get credential for logging on to the guest
# Replace with your secrets manager application when
# running this on PSv6 or higher

$viCred = Get-VICredentialStoreItem -Host $vmName
$secPassword = ConvertTo-SecureString -String $viCred.Password -AsPlainText -Force
$cred = [Management.Automation.PSCredential]::new($viCred.User, $secPassword)

$sCloudInitVM = @{
  OvaFile = 'D:\Repository\Linux\Ubuntu\bionic-server-cloudimg-amd64.ova'
  VmName = $vmName
  ClusterName = 'cluster'
  DsName = 'vsanDatastore'
  PgName = 'vdPg1'
  CloudConfig = 'user-data-srsbuilder.yaml'
  Credential = $cred
  MemoryGB = 4
  DiskGB = 25
  Verbose = $true
}
Install-CloudInitVM @sCloudInitVM
</code></pre>



<p>Notice how I used the <strong>VICredentialStore</strong> to store and retrieve the credentials for the Builder station. When you run this from a PowerShell version greater than 5.1, you will have to replace that part with calls to the secrets manager of your choice.</p>



<p>The real strength of using this <strong>Cloud-Init</strong> method is that one can use a <strong>YAML</strong> file to specify how the target station, the Builder station, in this case, needs to be configured. And also specify which packages shall be installed on the target station as part of the deployment.</p>



<p>The following <strong>extract</strong> of my YAML file shows the packages that are required. It also includes pulling down the SRS appliance.</p>



<pre class="lang:yaml decode:true  ">
# SRS 1.0
# A) dotnet sdk
# 1) add the Microsoft package signing key
- wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
# 2) add the package repository
- dpkg -i packages-microsoft-prod.deb
# 3) donet sdk
- apt-get update
- apt-get install -y apt-transport-https
- apt-get update
- apt-get install -y dotnet-sdk-3.1
# B) docker
- curl -fsSL https://get.docker.com -o get-docker.sh
- sh get-docker.sh
# C) PowerShell
- apt-get install -y wget apt-transport-https software-properties-common
- wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
- dpkg -i packages-microsoft-prod.deb
- apt-get update
- add-apt-repository universe
- apt-get install -y powershell
# D) VMware PowerCLI
- pwsh -C '& {Install-Module -Name VMware.PowerCLI -Scope AllUsers -Force -Confirm:$false -AllowClobber}'
# E) OvfTool
- /root/ovftool-ftp.sh
- chmod 744 /root/VMware-ovftool-4.4.1-16812187-lin.x86_64.bundle
- /root/VMware-ovftool-4.4.1-16812187-lin.x86_64.bundle --eulas-agreed --required --console
# F) packer
- curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add -
- apt-get update
- apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
- apt-get update
- apt-get install packer
# X) Additional modules
- pwsh -C '& {Install-Module -Name ImportExcel -Scope AllUsers -Force -Confirm:$false -AllowClobber}'
- pwsh -C '& {Install-Module -Name Posh-Ssh -Scope AllUsers -Force -Confirm:$false -AllowClobber}'
# Y) Clone SRS repo
- mkdir /root/SRS
- git clone https://github.com/vmware/script-runtime-service-for-vsphere /root/SRS
# End SRS 1.0
</code></pre>



<h4>Verify the Builder</h4>



<p>Once the Builder station is deployed, it is useful to check that everything the creation of the SRS Appliance needs, is present.</p>



<p>A <strong>word of warning</strong>, the following code includes all <strong>versions as hard-coded</strong>. This is not ideal, and especially for products that have a daily build, this will require updating the code each time. Since this was, at the time of writing this post, not my top priority, I postponed looking for a more portable solution to a later date.</p>



<pre class="lang:ps decode:true  ">
#region Helper functions
function Get-LogText {
  param([string]$Text)

  $dt = (Get-Date).ToString('yyyyMMdd HH:mm:ss.fff')
  $app = Split-Path -Path $MyInvocation.ScriptName -Leaf
  "$dt - $app - $Text"
}
#endregion

#region Preamble

$vmName = 'SrsBuilder'

$now = Get-Date

$viCredObj = Get-VICredentialStoreItem -Host $vmName
$sObj = @{
  TypeName = 'PSCredential'
  ArgumentList = $viCredObj.User,(ConvertTo-SecureString -String $viCredObj.Password -AsPlainText -Force)
}
$cred = New-Object @sObj

$vm = Get-VM -Name $vmName
#endregion

#region dotnet SDK
$check_dotnet = @'
dotnet --version
'@
$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptText = $check_dotnet
  ScriptType = 'bash'
}
$result = Invoke-VMScript @sInvoke
if($result.ScriptOutput.Trim("</code>n") -ne '3.1.404'){
  Write-Host (Get-LoGText -Text "Dotnet SDK installation failure") -ForegroundColor red
}
else{
  Write-Host (Get-LogText -Text "Dotnet SDK $($result.ScriptOutput.Trim("<code>n")) installation OK") -ForegroundColor green
}
#endregion

#region Docker
$check_docker = @'
docker --version
'@
$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptText = $check_docker
  ScriptType = 'bash'
}
$result = Invoke-VMScript @sInvoke
if ($result.ScriptOutput.Trim("</code>n") -ne 'Docker version 20.10.1, build 831ebea') {
  Write-Host (Get-LoGText -Text "Docker installation failure") -ForegroundColor red
} else {
  Write-Host (Get-LogText -Text "$($result.ScriptOutput.Trim("<code>n")) installation OK") -ForegroundColor green
}
#endregion

#region PowerShell v7
$check_PSv7 = @'
pwsh -C '& {$PSVersionTable.PSVersion.ToString()}'
'@
$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptText = $check_PSv7
  ScriptType = 'bash'
}
$result = Invoke-VMScript @sInvoke
if ($result.ScriptOutput.Trim("</code>n") -ne '7.1.0') {
  Write-Host (Get-LoGText -Text "PowerShell installation failure") -ForegroundColor red
} else {
  Write-Host (Get-LogText -Text "PowerShell $($result.ScriptOutput.Trim("<code>n")) installation OK") -ForegroundColor green
}
#endregion

#region VMware PowerCLI
$check_PowerCLI = @'
pwsh -C '& {(Get-Module -Name VMware.PowerCLI -ListAvailable).Version.ToString()}'
'@
$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptText = $check_PowerCLI
  ScriptType = 'bash'
}
$result = Invoke-VMScript @sInvoke
if ($result.ScriptOutput.Trim("</code>n") -ne '12.1.0.17009493') {
  Write-Host (Get-LoGText -Text "VMware PowerCLI installation failure") -ForegroundColor red
} else {
  Write-Host (Get-LogText -Text "VMware PowerCLI $($result.ScriptOutput.Trim("<code>n")) installation OK") -ForegroundColor green
}
#endregion

#region VMware ovftool
$check_ovftool = @'
ovftool --version
'@
$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptText = $check_ovftool
  ScriptType = 'bash'
}
$result = Invoke-VMScript @sInvoke
if ($result.ScriptOutput.Trim("</code>n") -ne 'VMware ovftool 4.4.1 (build-16812187)') {
  Write-Host (Get-LoGText -Text "VMware OVFTool installation failure") -ForegroundColor red
} else {
  Write-Host (Get-LogText -Text "$($result.ScriptOutput.Trim("<code>n")) installation OK") -ForegroundColor green
}
#endregion

#region Packer
$check_packer = @'
packer --version
'@
$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptText = $check_packer
  ScriptType = 'bash'
}
$result = Invoke-VMScript @sInvoke
if ($result.ScriptOutput.Trim("</code>n") -ne '1.6.6') {
  Write-Host (Get-LoGText -Text "Packer installation failure") -ForegroundColor red
} else {
  Write-Host (Get-LogText -Text "Packer $($result.ScriptOutput.Trim("<code>n")) installation OK") -ForegroundColor green
}
#endregion

#region Additional

#region ImportExcel
$check_ImportExcel = @'
pwsh -C '& {(Get-Module -Name ImportExcel -ListAvailable).Version.ToString()}'
'@
$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptText = $check_ImportExcel
  ScriptType = 'bash'
}
$result = Invoke-VMScript @sInvoke
if ($result.ScriptOutput.Trim("</code>n") -ne '7.1.1') {
  Write-Host (Get-LoGText -Text "ImportExcel installation failure") -ForegroundColor red
} else {
  Write-Host (Get-LogText -Text "ImportExcel $($result.ScriptOutput.Trim("<code>n")) installation OK") -ForegroundColor green
}
#endregion

#region Posh-Ssh
$check_PoshSSH = @'
pwsh -C '& {(Get-Module -Name Posh-SSH -ListAvailable).Version.ToString()}'
'@
$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptText = $check_PoshSSH
  ScriptType = 'bash'
}
$result = Invoke-VMScript @sInvoke
if ($result.ScriptOutput.Trim("</code>n") -ne '2.3.0') {
  Write-Host (Get-LoGText -Text "Posh-SSH installation failure") -ForegroundColor red
} else {
  Write-Host (Get-LogText -Text "Posh-SSH $($result.ScriptOutput.Trim("`n")) installation OK") -ForegroundColor green
}
#endregion

#endregion
</code></pre>



<h3>Run the Build</h3>



<p>When the Builder station is deployed, and we verified that all prerequisites are in place, we can start the deployment of the SRS Applicance. From which, if all goes well, the OVA file will be generated.</p>



<p>During my experimenting with building the SRS Appliance, I stumbled on two what I suspect are issues.</p>



<p>As a bypass I build the SRS Appliance for now with <strong>Photon V3 Rev 2</strong>.</p>



<p>I did not succeed in building the SRS Appliance with <strong>Photon V3 Rev 3</strong> image. I opened an Issue for that, and I&#8217;m curious to know if the issue is a Photon issue or something in my environment.</p>
<p>Another issue, which seems to be a real, known issue, is that you apparently can not use a <strong>Distributed Switch Portgroup</strong> to build the SRS Appliance. The underlying issue seems to stem from the <strong>Packer</strong> application, more specifically the <strong>VMware-Iso</strong> builder. This builder uses <strong>VNC</strong> to &#8216;talk&#8217; with the ESXi node on which the SRS Appliance is created.</p>



<p>There are two issues here:</p>
<ul>
<li>VNC is not included anymore in ESXi since 6.7</li>
<li>With the ESXi API you can not interact with a VDS</li>
</ul>



<p>The VNC issue is resolved by changing some Packer settings. But for the VDS issue, there is no solution besides switching to the vSphere-Iso builder in the Packer step, so I had to build the SRS Appliance on a <strong>VSS Portgroup</strong>.</p>



<p>I packaged the preparation steps and the start of the SRS Appliance build in a function, named <strong>Invoke-SRSBuild</strong>.</p>



<pre class="lang:ps decode:true  ">
function Invoke-SRSBuild {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory)]
    [String]$BuilderName,
    [Parameter(Mandatory)]
    [PSCredential]$Credential,
    [Switch]$PrepOnly,
    [Switch]$BuildOnly,
    [Parameter(Mandatory)]
    [ValidateSet('V3Rev2', 'V3Rev3')]
    [String]$PhotonVersion,
    [Parameter(Mandatory)]
    [String]$Portgroup,
    [Switch]$PackerLog
  )

  #region Helper functions
  function Get-LogText {
    param([string]$Text)

    $dt = (Get-Date).ToString('yyyyMMdd HH:mm:ss.fff')
    $app = Split-Path -Path $MyInvocation.ScriptName -Leaf
    "$dt - $app - $Text"
  }

  function Remove-SRSFolder {
    Param(
      [Parameter(Mandatory = $true)]
      [VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.StorageResource]$Datastore
    )

    $dsBrowser = Get-View -Id $Datastore.ExtensionData.Browser
    $spec = New-Object -TypeName VMware.Vim.HostDatastoreBrowserSearchSpec
    $spec.MatchPattern = 'SRS_Appliance'
    $spec.Query = New-Object -TypeName Vmware.Vim.FolderFileQuery
    $spec.Details = New-Object -TypeName VMware.Vim.FileQueryFlags
    $result = $dsBrowser.SearchDatastore("[$($Datastore.Name)]", $spec)

    if ($result.File.Count -gt 0 -and $result.File[0]) {
      $dc = Get-VMHost -Datastore $Datastore | Get-Datacenter
      $fileMgr = Get-View FileManager
      $fileMgr.DeleteDatastoreFile("[$($Datastore.Name)] $($result.File[0].Path)", $dc.ExtensionData.MoRef)
    }
  }
  #endregion

  #region Bypass for known issue in VSC ()
  # See https://github.com/PowerShell/vscode-powershell/issues/633

  if ($psISE) {
    $dir = Split-Path -Path $psISE.CurrentFile.FullPath
  } else {
    if ($profile -match "VScode") {
      $dir = Split-Path $psEditor.GetEditorContext().CurrentFile.Path
    } else {
      $dir = $PSScriptRoot
    }
  }
  #endregion

  #region Preamble

  $vm = Get-VM -Name $BuilderName
  #endregion

  if (-not $BuildOnly.IsPresent) {

    #region SSH service
    $ssh = Get-VMHostService -VMHost $vm.VMHost | Where-Object { $_.Label -eq 'SSH' }
    if ($ssh.Running) {
      Write-Verbose (Get-LogText -Text "SSH is running on $($vm.VMHost.Name)")
    } else {
      Write-Verbose (Get-LogText -Text "Starting SSH on $($vm.VMHost.Name)")
      $ssh = Start-VMHostService -HostService $ssh -Confirm:$false
      if (-not $ssh.Running) {
        Write-Verbose (Get-LogText -Text "Could not start SSH on $($vm.VMHost.Name)")
        throw "SSH service not started on $($vm.VMHost.Name)"
      }
    }
    #endregion

    #region GuestIPHack
    $esxcli = Get-EsxCli -VMHost $vm.VMHost -V2
    $sOpt = @{
      option = '/Net/GuestIPHack'
    }
    $opt = $esxcli.system.settings.advanced.list.Invoke($sOpt)
    if ($opt.IntValue -eq 1) {
      Write-Verbose (Get-LogText -Text "GuestIPHack setting is correct on $($vm.VMHost.Name)")
    } else {
      Write-Verbose (Get-LogText -Text "GuestIPHack setting is not correct on $($vm.VMHost.Name)")
      $sOpt.Add('intvalue', [long]1)
      if ($esxcli.system.settings.advanced.set.Invoke($sOpt)) {
        Write-Verbose (Get-LogText -Text "GuestIPHack setting corrected on $($vm.VMHost.Name)")
      } else {
        Write-Verbose (Get-LogText -Text "Could not change GuestIPHack setting on $($vm.VMHost.Name)")
        throw "Could net set GuestIPHack"
      }
    }
    #endregion

    #region Make sure folder doesn't exist
    $ds = Get-Datastore -RelatedObject $vm
    Remove-SRSFolder($ds)
    #endregion

    #region Update photon-build.json
    # Suspected issue when using a vdPortgroup

    $jsonFile = New-TemporaryFile
    $photonBuilder = @{
      builder_host = $vm.VMHost.Name
      builder_host_username = $viCredObj.User
      builder_host_password = $viCredObj.Password
      builder_host_datastore = (Get-Datastore -RelatedObject $vm).Name
      builder_host_portgroup = $Portgroup
    }
    $photonBuilder | ConvertTo-Json | Set-Content -Path $jsonFile
    $sCopy = @{
      VM = $vm
      GuestCredential = $Credential
      LocalToGuest = $true
      Source = $jsonFile
      Destination = '/root/SRS/appliance/photon-builder.json'
      Force = $true
      Confirm = $false
    }
    Copy-VMGuestFile @sCopy
    Remove-Item -Path $jsonFile -Confirm:$false
    #endregion

    #region Update photon-version.json
    $photonTab = Get-Content -Path "$dir\Photon-version.json" | ConvertFrom-Json
    $photonSelected = $photonTab.Photon | Where-Object { $_.Label -eq $PhotonVersion }
    $jsonFile = New-TemporaryFile
    $sCopy = @{
      VM = $vm
      GuestCredential = $Credential
      GuestToLocal = $true
      Source = '/root/SRS/appliance/photon-version.json'
      Destination = $jsonFile
      Force = $true
      Confirm = $false
    }
    Copy-VMGuestFile @sCopy
    $photonBuilder = (Get-Content -Path $jsonFile | ConvertFrom-Json)[0]
    if ($photonBuilder.iso_url -ne $photonSelected.Uri -or $photonBuilder.iso_checksum -ne $photonSelected.CheckSum) {
      $photonBuilder.iso_url = $photonSelected.Uri
      $photonBuilder.iso_checksum = $photonSelected.CheckSum
      @($photonBuilder) | ConvertTo-Json | Set-Content -Path $jsonFile
      $sCopy = @{
        VM = $vm
        GuestCredential = $Credential
        LocalToGuest = $true
        Source = $jsonFile
        Destination = '/root/SRS/appliance/photon-version.json'
        Force = $true
        Confirm = $false
      }
      Copy-VMGuestFile @sCopy
      Remove-Item -Path $jsonFile -Confirm:$false

    }
    #endregion

    #region Handle VNC for ESXi 6.7 and later
    if ([Version]$vm.VMHost.Version -ge [Version]'6.7.0') {
      $updateJSON = 'sed' +
      ' -i.bak -e ''/vnc_disable_password/ i \      "vnc_over_websocket": true,''' +
      ' -e ''/vnc_disable_password/ i \      "insecure_connection": true,''' +
      ' -e ''/vnc_disable_password/d'' ~/SRS/appliance/photon.json'

      $sInvoke = @{
        VM = $vm
        GuestCredential = $Credential
        ScriptText = $updateJSON
        ScriptType = 'bash'
      }
      $result = Invoke-VMScript @sInvoke
    }
    #endregion

  }

  if (-not $PrepOnly.IsPresent) {

    #region Get PowerCLI folder path
    $findDir = 'pwsh -C ''& {Split-Path -Path (Split-Path -Path (Get-Module -Name VMware.PowerCLI -ListAvailable).ModuleBase)}'''
    $sInvoke = @{
      VM = $vm
      GuestCredential = $Credential
      ScriptText = $findDir
      ScriptType = 'bash'
    }
    $pcliPath = (Invoke-VMScript @sInvoke).ScriptOutput
    #endregion

    #region Start build
    $sInvoke = @{
      VM = $vm
      GuestCredential = $Credential
      ScriptText = "/root/SRS/build.sh $pcliPath"
      ScriptType = 'bash'
    }
    if ($PackerLog.IsPresent) {
      $packerLogName = 'packer.log'
      $sInvoke.ScriptText = "export PACKER_LOG=1;export PACKER_LOG_PATH='$($packerLogName)';$($sInvoke.ScriptText)"
    }
    $buildResult = Invoke-VMScript @sInvoke
    $buildResult.ScriptOutput > "$dir\Buildlog.txt"
    #endregion

    #region Rettrieve Packer log
    if($PackerLog.IsPresent){
      $sCopy = @{
        VM = $vm
        GuestCredential = $Credential
        GuestToLocal = $true
        Source = "/root/SRS/appliance/$($packerLogName)"
        Destination = "$dir\$($packerLogName)"
        Force = $true
        Confirm = $false
      }
      Copy-VMGuestFile @sCopy
    }
    #endregion

    #region Clean up
    # Packer leaves an orphaned entry in the VCSA behind

    $endpointVM = Get-VM -Name 'SRS_Appliance' -ErrorAction SilentlyContinue
    if($endpointVM){
      while($endpointVM.PowerState -ne 'PoweredOff'){
        sleep2
        $endpointVM = Get-VM -Name 'SRS_Appliance' -ErrorAction SilentlyContinue
      }
      Remove-VM -VM $endpointVM   -DeletePermanently -ErrorAction SilentlyContinue -Confirm:$false
    }
    #endregion
  }
}
</code></pre>



<p>The call to the <strong>Invoke-SRSBuilder</strong> function is again straight-forward.</p>



<pre class="lang:ps decode:true  ">
$vmName = 'SRSBuilder'

#region Credential
# Get the credentials for the SRS Builder station
# Alternative method required when using PSv6 or later

$viCredObj = Get-VICredentialStoreItem -Host $vmName
$sObj = @{
  TypeName = 'PSCredential'
  ArgumentList = $viCredObj.User, (ConvertTo-SecureString -String $viCredObj.Password -AsPlainText -Force)
}
$cred = New-Object @sObj
#endregion

$sBuild = @{
  BuilderName = $vmName
  Credential = $cred
  PhotonVersion = 'V3Rev2'
  Portgroup = 'PG1'
  PackerLog = $true
}
Invoke-SRSBuild @sBuild
</code></pre>



<h3 class="wp-block-heading">The OVA</h3>



<p>When the call to the <strong>build.sh</strong> script completes successfully, there will be <strong>OVA/OVF</strong> files created on the Builder station.</p>
<p>Since the Builder station is, in my setup, a temporary station, I need to download those files to a more permanent location. The following snippet uses the <strong>Get-ScpFile</strong> cmdlet from the <a href="https://www.powershellgallery.com/packages/Posh-SSH" target="_blank" rel="noopener">Posh-Ssh</a> module to do that.</p>



<pre class="lang:ps decode:true  ">
#requires -Modules Posh-Ssh

#region Preamble

$vmName = 'SrsBuilder'

$now = Get-Date

$viCredObj = Get-VICredentialStoreItem -Host $vmName
$sObj = @{
  TypeName = 'PSCredential'
  ArgumentList = $viCredObj.User, (ConvertTo-SecureString -String $viCredObj.Password -AsPlainText -Force)
}
$cred = New-Object @sObj

$vm = Get-VM -Name $vmName

$fqdn = (Resolve-DnsName -Name $vm.ExtensionData.Guest.IpAddress).NameHost
#endregion

#region Download OVA
$sSCP = @{
  ComputerName = $fqdn
  Credential = $cred
  RemoteFile = '/root/SRS/appliance/output-vmware-iso/SRS_Appliance_1.0.0.ova'
  LocalFile = 'D:\OVA\SRS\V1.0.0\Rev2-VSS\SRS_Appliance_1.0.0.ova'
  AcceptKey = $true
}
Get-SCPFile @sSCP
#endregion
</code></pre>



<h2 class="wp-block-heading">Deploy SRS</h2>



<p>Once we have the OVA available, it is a piece of cake to deploy our SRS VM with the <a href="https://vdc-repo.vmware.com/vmwb-repository/dcr-public/f17594eb-bbe1-44a7-b7ac-b2da546936c2/21da7740-996d-481e-83e4-05d8fa7db18d/doc/Get-OvfConfiguration.html" target="_blank" rel="noreferrer noopener">Get-OvfConfiguration</a> and <a href="https://vdc-repo.vmware.com/vmwb-repository/dcr-public/f17594eb-bbe1-44a7-b7ac-b2da546936c2/21da7740-996d-481e-83e4-05d8fa7db18d/doc/Import-VApp.html" target="_blank" rel="noreferrer noopener">Import-VApp</a> cmdlets.</p>



<p>Note that deploying the SRS OVA to a Distributed Switch Portgroup is perfectly possible. The VDS issue mentioned earlier only comes into play when Packer with the VMware-Iso builder is involved.&nbsp;</p>



<pre class="lang:ps decode:true  ">
$vmName = 'srs1'
$clusterName = 'cluster'
$numCPU = 2
$memoryGB = 4
$dsName = 'vsanDatastore'
$harddiskGB = 2
$ovaPath = 'D:\OVA\SRS\V1.0.0\Rev2-VSS\SRS_Appliance_1.0.0.ova'
$networkName = 'vdPg1'

$cluster = Get-Cluster -Name $clusterName
$esx = Get-VMHost -Location $cluster | Get-Random
$ds = Get-Datastore -Name $dsName
$ovfProp = Get-OvfConfiguration -Ovf $ovaPath
$ovfProp.Common.guestinfo.hostname.Value = $vmName
$ovfProp.Common.guestinfo.ipaddress.Value = '192.168.10.43'
$ovfProp.Common.guestinfo.netmask.Value = '24'
$ovfProp.Common.guestinfo.gateway.Value = '192.168.10.1'
$ovfProp.Common.guestinfo.root_password.Value = 'Welcome2020!'
$ovfProp.Common.guestinfo.dns.Value = '192.168.10.2'
$ovfProp.Common.guestinfo.domain.Value = 'local.lab'
$ovfProp.Common.srs.vcaddress.Value = 'vcsa7.local.lab'
$ovfProp.Common.srs.vcpassword.Value = 'Welcome2020!'
$ovfProp.Common.srs.vcuser.Value = 'administrator@vsphere.local'
$ovfProp.Common.srs.vcthumbprint.Value = '5bf3246fde90fd3b7ab84144f50105114c2826e1'
$ovfProp.NetworkMapping.PG1.Value = $networkName

$vm = Import-VApp -Name $vmName -Source $ovaPath -VMHost $esx -OvfConfiguration $ovfProp -Datastore $ds
Start-VM -VM $vm -Confirm:$false

</code></pre>



<p>Your first test to see if the deployment went ok, is to try and access the swagger page on your SRS VM. The URI is <strong>https://&lt;your-SRS-FQDN&gt;/swagger</strong>. When all goes well, you will see a page like this.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="797" src="https://www.lucd.info/wp-content/uploads/2020/12/swagger-1024x797.png" alt="" class="wp-image-7437" srcset="https://www.lucd.info/wp-content/uploads/2020/12/swagger-1024x797.png 1024w, https://www.lucd.info/wp-content/uploads/2020/12/swagger-300x234.png 300w, https://www.lucd.info/wp-content/uploads/2020/12/swagger-768x598.png 768w, https://www.lucd.info/wp-content/uploads/2020/12/swagger-720x560.png 720w, https://www.lucd.info/wp-content/uploads/2020/12/swagger.png 1471w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">Using SRS</h2>



<p>Now we can start using the SRS server to run our scripts. The way to do that is quite simple. The complete process is described in the <a href="https://github.com/vmware/script-runtime-service-for-vsphere/wiki/Run-Scripts" target="_blank" rel="noopener">Run Scripts</a> section in the SRS repository.</p>



<h3 class="wp-block-heading">A Simple Sample</h3>



<p>My test installation of SRS is done on a VM that is named <strong>SRS1</strong>.</p>
<p>A basic REST API call to verify that everything is working, is to call the <strong>api/about</strong> method.</p>



<pre class="lang:ps decode:true  ">
function Invoke-SrsMethod {
  [cmdletbinding()]
  param(
    [Parameter(Mandatory)]
    [String]$FQDN,
    [Parameter(Mandatory)]
    [String]$API,
    [Parameter(Mandatory)]
    [String]$Method
  )

  $sWeb = @{
    Uri = "https://$($FQDN)/$($API)"
    Method = $Method
  }

  try {
    $result = Invoke-WebRequest @sWeb
  }
  catch [System.Net.WebException] {
    switch ($error[0].Exception.Status) {
      ([System.Net.WebExceptionStatus]::TrustFailure) {
        if ($PSVersionTable.PSVersion.Major -lt 6) {
          if (-not ([System.Management.Automation.PSTypeName]"TrustAllCertsPolicy").Type) {
            Add-Type -TypeDefinition @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem)
    {
        return true;
    }
}
"@
          }
          if ([System.Net.ServicePointManager]::CertificatePolicy.ToString() -ne "TrustAllCertsPolicy") {
            [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
          }
        } else {
          $sWeb.Add('SkipCertificateCheck', $true)
        }
        $result = Invoke-WebRequest @sWeb
      }
      Default {
        Write-Error "Unhandled WebException $($error[0].Exception.Status)"
      }
    }
  }
  catch {
    Write-Error "Unhandled exception code $($error[0].Exception.gettype().Name)"
  }

  switch ($result.StatusCode) {
    200 {
      $result
    }
    Default {
      Write-Error "Unhandled StatusCode $($result.StatusCode)"
    }
  }
}

$vmName = 'srs1'

$vm = Get-VM -Name $vmName
$fqdn = (Resolve-DnsName -Name $vm.ExtensionData.Guest.IpAddress).NameHost

$about = Invoke-SrsMethod -FQDN $fqdn -API 'api/about' -Method 'Get'
$about.Content | ConvertFrom-Json
</code></pre>



<p>You notice that I created a function Invoke-SRSMethod instead of just calling the Invoke-WebRequest cmdlet directly. The reason for creating that function is that I needed to be able to bypass invalid certificates.</p>
<p>With PSv6 that has become easy, since the <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.1" target="_blank" rel="noopener">Invoke-WebRequest</a> cmdlet now has the <strong>SkipCertificateCheck</strong> switch.&nbsp;</p>
<p>But I wanted to have a function that would also work in a PSV5.1 environment, hence the function and the logic in there.</p>
<p>When all goes well, that snippet should return the following.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="672" height="91" src="https://www.lucd.info/wp-content/uploads/2020/12/srs-call.png" alt="" class="wp-image-7442" srcset="https://www.lucd.info/wp-content/uploads/2020/12/srs-call.png 672w, https://www.lucd.info/wp-content/uploads/2020/12/srs-call-300x41.png 300w" sizes="auto, (max-width: 672px) 100vw, 672px" /></figure>



<h3>A PowerCLI example</h3>



<p>The full sequence for running code on the SRS station is perfectly explained in the <a href="https://github.com/vmware/script-runtime-service-for-vsphere/wiki/Run-Scripts" target="_blank" rel="noopener">Run Scripts</a> section on the <a href="https://github.com/vmware/script-runtime-service-for-vsphere" target="_blank" rel="noopener">SRS repository</a>.</p>



<p>A full, scripted example, excluding the earlier <strong>Invoke-SRSMethod</strong> function, could look like this.</p>



<pre class="lang:ps decode:true  ">
#region Preamble
$vmName = 'srs1'

$vm = Get-VM -Name $vmName
$fqdn = (Resolve-DnsName -Name $vm.ExtensionData.Guest.IpAddress).NameHost

# Get credential for logging on to the guest OS
$viCred = Get-VICredentialStoreItem -Host $global:DefaultVIServer.Name
$secPassword = ConvertTo-SecureString -String $viCred.Password -AsPlainText -Force
$cred = [Management.Automation.PSCredential]::new($viCred.User, $secPassword)

$headers = @{
  "accept" = "application/json"
  "content-type" = "application/json"
}
#endregion

#region Login
$sLogon = @{
  FQDN = $fqdn
  API = '/api/auth/login'
  Method = 'Post'
  Credential = $cred
  Headers = $headers
}
$logon = Invoke-SrsMethod @sLogon
$headers.Add('X-SRS-API-KEY', $logon.Headers['X-SRS-API-KEY'])
#endregion

#region Create Runspace
$sRSCreate = @{
  FQDN = $fqdn
  API = '/api/runspaces'
  Method = 'Post'
  Credential = $cred
  Headers = $headers
  Body = @{
    name = 'MyRS'
    run_vc_connection_script = $true
  }
}
$createRS = Invoke-SrsMethod @sRSCreate
$rs = $createRS.Content | ConvertFrom-Json
#endregion

#region Wait till RS is ready
while ($rs.state -eq 'Creating') {
  $sRSGet = @{
    FQDN = $fqdn
    API = "/api/runspaces/$($rs.Id)"
    Method = 'Get'
    Headers = $headers
  }
  $getRS = Invoke-SrsMethod @sRSGet
  $rs = $getRS.Content | ConvertFrom-Json
}
#endregion

#region Run Script
$code = {
  Get-VM
}
$sSCRCreate = @{
  FQDN = $fqdn
  API = '/api/script-executions'
  Method = 'Post'
  Headers = $headers
  Body = @{
    runspace_id = $rs.id
    name = 'MyScript'
    script = $code.ToString()
    script_parameters = @()
  }
}
$createSCR = Invoke-SrsMethod @sSCRCreate
$scr = $createSCR.Content | ConvertFrom-Json
#endregion

#region Wait for Script to end
while($scr.state -eq 'running'){
$sSCRCreate = @{
  FQDN = $fqdn
  API = "/api/script-executions/$($scr.id)"
  Method = 'Get'
  Headers = $headers
}
$getSCR = Invoke-SrsMethod @sSCRCreate
$scr = $getSCR.Content | ConvertFrom-Json
}
#endregion

#region Retrieve Script output
$sSCROut = @{
  FQDN = $fqdn
  API = "/api/script-executions/$($scr.id)/output"
  Method = 'Get'
  Headers = $headers
}
$outSCR = Invoke-SrsMethod @sSCROut
$outSCR.Content | ConvertFrom-Json
#endregion

#region Retrieve Script streams
'information', 'error', 'warning', 'debug', 'verbose' |
ForEach-Object -Process {
  $sSCRStr = @{
    FQDN = $fqdn
    API = "/api/script-executions/$($scr.id)/streams/$($_)"
    Method = 'Get'
    Headers = $headers
  }
  $getStream = Invoke-SrsMethod @sScrStr
  if($getStream.Content -ne '[]'){
    Write-Host "--- $_ ---" -ForegroundColor Green
    $getStream.Content | ConvertFrom-Json | Out-Default
    Write-Host "------------" -ForegroundColor Green
  }
}
#endregion

#region Remove Runspace
$sRSDel = @{
  FQDN = $fqdn
  API = "/api/runspaces/$($rs.id)"
  Method = 'Delete'
  Headers = $headers
}
$rsDel = Invoke-SrsMethod @sRSDel
$rsDel.Content | ConvertFrom-Json
#endregion

#region Logout
$sLogout = @{
  FQDN = $fqdn
  API = "/api/auth/logout"
  Method = 'Post'
  Headers = $headers
}
$srsLogout = Invoke-SrsMethod @sLogout
#endregion
</code></pre>



<h3 class="wp-block-heading">A Function</h3>



<p>That turned out to be a whole lot of code to just run a simple Get-VM.</p>



<p>But since we will be using most of the time the same logic, we can easily turn that into a function. That will make submitting code to run on the SRS a lot simpler.</p>



<p>And while we are at it, let&#8217;s include an option to run the code from an existing .ps1 file.</p>



<pre class="lang:ps decode:true  ">
Function Invoke-SRSScript {
  [cmdletbinding()]
  param(
    [Parameter(Mandatory = $true)]
    [String]$SRSHost,
    [Parameter(ParameterSetName = 'ScriptBlock', Mandatory = $true)]
    [scriptblock]$Code,
    [Parameter(ParameterSetName = 'ScriptFile', Mandatory = $true)]
    [String]$Path,
    [Parameter(Mandatory = $true)]
    [PSCredential]$VCCredential
  )

  #region Helper functions
  function Invoke-SrsMethod {
    [cmdletbinding()]
    param(
      [Parameter(Mandatory)]
      [String]$FQDN,
      [Parameter(Mandatory)]
      [String]$API,
      [Parameter(Mandatory)]
      [String]$Method,
      [PSCredential]$Credential,
      [PSObject]$Headers,
      [PSObject]$Body
    )

    $sWeb = @{
      Uri = "https://$($FQDN)$($API)"
      Method = $Method
    }
    if ($Credential) {
      $sWeb.Add('Credential', $Credential)
    }
    if ($Headers) {
      $sWeb.Add('Headers', $Headers)
    }
    if ($Body) {
      $sWeb.Add('Body', ($Body | ConvertTo-Json))
    }

    try {
      $result = Invoke-WebRequest @sWeb
    } catch [System.Net.WebException] {
      switch ($error[0].Exception.Status) {
        ([System.Net.WebExceptionStatus]::TrustFailure) {
          if ($PSVersionTable.PSVersion.Major -lt 7) {
            if (-not ([System.Management.Automation.PSTypeName]"TrustAllCertsPolicy").Type) {
              Add-Type -TypeDefinition @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem)
    {
        return true;
    }
}
"@
            }
            if ([System.Net.ServicePointManager]::CertificatePolicy.ToString() -ne "TrustAllCertsPolicy") {
              [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
            }
          } else {
            $sWeb.Add('SkipCertificateCheck', $true)
          }
          $result = Invoke-WebRequest @sWeb
        }
        Default {
          Write-Error "Unhandled WebException $($error[0].Exception.Status)"
        }
      }
    } catch {
      Write-Error "Unhandled exception code $($error[0].Exception.gettype().Name)"
      $error[0].Exception | Format-Custom
    }

    switch ($result.StatusCode) {
      200 {
        $result
      }
      202 {
        $result
      }
      401 {
        Write-Error "Unauthorized $($result.StatusCode)"
      }
      500 {
        Write-Error "Server error $($result.StatusCode)"
      }
      Default {
        Write-Error "Unhandled StatusCode $($result.StatusCode)"
      }
    }
  }
  #endregion

  #region Preamble

  $vm = Get-VM -Name $SRSHost
  $fqdn = (Resolve-DnsName -Name $vm.ExtensionData.Guest.IpAddress).NameHost

  switch ($PSCmdlet.ParameterSetName){
    'ScriptBlock' {
      $strCode = $Code.ToString()
    }
    'ScriptFile' {
      $strCode = Get-Content -Path $Path | Out-String
    }
  }

  $headers = @{
    "accept" = "application/json"
    "content-type" = "application/json"
  }
  #endregion

  #region Login
  $sLogon = @{
    FQDN = $fqdn
    API = '/api/auth/login'
    Method = 'Post'
    Credential = $VCCredential
    Headers = $headers
  }
  $logon = Invoke-SrsMethod @sLogon
  $headers.Add('X-SRS-API-KEY', $logon.Headers['X-SRS-API-KEY'])
  #endregion

  #region Create Runspace
  $sRSCreate = @{
    FQDN = $fqdn
    API = '/api/runspaces'
    Method = 'Post'
    Credential = $cred
    Headers = $headers
    Body = @{
      name = 'MyRS'
      run_vc_connection_script = $true
    }
  }
  $createRS = Invoke-SrsMethod @sRSCreate
  $rs = $createRS.Content | ConvertFrom-Json
  #endregion

  #region Wait till RS is ready
  while ($rs.state -eq 'Creating') {
    $sRSGet = @{
      FQDN = $fqdn
      API = "/api/runspaces/$($rs.Id)"
      Method = 'Get'
      Headers = $headers
    }
    $getRS = Invoke-SrsMethod @sRSGet
    $rs = $getRS.Content | ConvertFrom-Json
  }
  #endregion

  #region Run Script
  $sSCRCreate = @{
    FQDN = $fqdn
    API = '/api/script-executions'
    Method = 'Post'
    Headers = $headers
    Body = @{
      runspace_id = $rs.id
      name = 'MyScript'
      script = $strCode
      script_parameters = @()
    }
  }
  $createSCR = Invoke-SrsMethod @sSCRCreate
  $scr = $createSCR.Content | ConvertFrom-Json
  #endregion

  #region Wait for Script to end
  while ($scr.state -eq 'running') {
    $sSCRCreate = @{
      FQDN = $fqdn
      API = "/api/script-executions/$($scr.id)"
      Method = 'Get'
      Headers = $headers
    }
    $getSCR = Invoke-SrsMethod @sSCRCreate
    $scr = $getSCR.Content | ConvertFrom-Json
  }
  #endregion

  #region Retrieve Script output
  $sSCROut = @{
    FQDN = $fqdn
    API = "/api/script-executions/$($scr.id)/output"
    Method = 'Get'
    Headers = $headers
  }
  $outSCR = Invoke-SrsMethod @sSCROut
  $outSCR.Content | ConvertFrom-Json
  #endregion

  #region Retrieve Script streams
  $streams = 'information','error','warning','debug','verbose'

  $streams | ForEach-Object -Process {
    $sSCRStr = @{
      FQDN = $fqdn
      API = "/api/script-executions/$($scr.id)/streams/$($_)"
      Method = 'Get'
      Headers = $headers
    }
    $getStream = Invoke-SrsMethod @sScrStr
    if ($getStream.Content -ne '[]') {
      $out = ($getStream.Content | ConvertFrom-Json).message | Out-String
      switch($_){
        'information' {
          Write-Information -MessageData $out
        }
        'error' {
          Write-Error -Message $out
        }
        'warning' {
          Write-Warning -Message $out
        }
        'debug' {
          Write-Debug -Message $out
        }
        'verbose' {
          Write-Verbose -Message $out
        }
      }
    }
  }
  #endregion

  #region Remove Runspace
  $sRSDel = @{
    FQDN = $fqdn
    API = "/api/runspaces/$($rs.id)"
    Method = 'Delete'
    Headers = $headers
  }
  $rsDel = Invoke-SrsMethod @sRSDel
  $rsDel.Content | ConvertFrom-Json
  #endregion

  #region Logout
  $sLogout = @{
    FQDN = $fqdn
    API = "/api/auth/logout"
    Method = 'Post'
    Headers = $headers
  }
  $srsLogout = Invoke-SrsMethod @sLogout
  #endregion

}
</code></pre>



<p>This <strong>Invoke-SRSScript</strong> function can be used in two variations (parametersets). With the <strong>Code</strong> parameter, you pass a ScriptBlock.</p>



<pre class="lang:ps decode:true  ">
$sScript = @{
  SRSHost = 'srs1'
  Code = { Get-VM }
  VCCredential = $cred
}
Invoke-SRSScript @sScript
</code></pre>



<p>With the <strong>Path</strong> parameter, you point to a .ps1 file.</p>



<pre class="lang:ps decode:true  ">$sScript = @{
  SRSHost = 'srs1'
  Path = 'D:\Git\SRS-Explore\Use\Sample.ps1'
  VCCredential = $cred
}
</code></pre>



<h2 class="wp-block-heading">Some Administration</h2>



<p>The SRS comes with a number of preset values, see the <a href="https://github.com/vmware/script-runtime-service-for-vsphere/wiki/Initial-Configuration" target="_blank" rel="noopener">Initial Configuration</a> documentation. In some situations, you might want to change one or more of these settings.</p>
<p>The obvious solution is to rebuild your SRS OVA with the desired settings, and re-deploy your SRS VM.&nbsp;</p>
<p>But you can also change these settings on the fly on an existing and running SRS. The following snippet is just an example where I change the <strong>script-runtime-service</strong> setting from the default 10 minutes to 20 minutes.</p>



<pre class="lang:ps decode:true  ">
$vmName = 'srs1'

# Get the SRS credentials

$viCred = Get-VICredentialStoreItem -Host $vmName
$secPassword = ConvertTo-SecureString -String $viCred.Password -AsPlainText -Force
$cred = [Management.Automation.PSCredential]::new($viCred.User, $secPassword)

$vm = Get-VM -Name $vmName

#region Change SRS setting

$code = @'
kubectl get cm service-settings -n script-runtime-service -o yaml | sed -e 's/\("MaxRunspaceIdleTimeMinutes": \).*/\120,/' | kubectl apply -f -
'@

$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptType = 'bash'
  ScriptText = $code
}
Invoke-VMScript @sInvoke
#endregion

#region Check new SRS settings

$code = @'
kubectl get cm service-settings -n script-runtime-service -o yaml
'@

$sInvoke = @{
  VM = $vm
  GuestCredential = $cred
  ScriptType = 'bash'
  ScriptText = $code
}
Invoke-VMScript @sInvoke

#endregion
</code></pre>



<p>Note that such a change will not be applied immediately, it might take some time (we are talking minutes).</p>



<h2 class="wp-block-heading">Epilogue</h2>



<p>This concludes, for now, my somewhat lengthy <strong>Hitchhikers Guide to SR</strong>S<strong> 1.0.0</strong>, which resulted from my playing/testing/exploring the Script Runtime Service for vSphere (SRS) 1.0.0.</p>



<p>Is this something to should have gotten more attention when it was released?</p>
<p>Definitely!</p>



<p>I will surely be doing some more experimenting with the new and shining tool.</p>
<p>Enjoy!</p>


]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2020/12/23/a-hitchhikers-guide-to-srs-1-0-0/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Cloud-init &#8211; Part 3 &#8211; Photon OS</title>
		<link>https://www.lucd.info/2019/12/08/cloud-init-part-3-photon-os/</link>
					<comments>https://www.lucd.info/2019/12/08/cloud-init-part-3-photon-os/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sun, 08 Dec 2019 21:52:03 +0000</pubDate>
				<category><![CDATA[Cloud-init]]></category>
		<category><![CDATA[Deploy]]></category>
		<category><![CDATA[OVA]]></category>
		<category><![CDATA[Photon]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[powershellv6]]></category>
		<category><![CDATA[YAML]]></category>
		<category><![CDATA[cloud-init]]></category>
		<category><![CDATA[seedISO]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=6695</guid>

					<description><![CDATA[In Part 1 and Part 2 of this series we used Ubuntu as [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In <a rel="noreferrer noopener" aria-label="Part 1 (opens in a new tab)" href="https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/" target="_blank">Part 1</a> and <a href="https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/">Part 2</a> of this series we used Ubuntu as the guest OS of our target instances. In Part 3 we will show how to use VMware&#8217;s <a rel="noreferrer noopener" aria-label="Photon OS (opens in a new tab)" href="https://vmware.github.io/photon/" target="_blank">Photon OS</a> as our guest OS.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="693" height="295" src="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-3.png" alt="" class="wp-image-6784" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-3.png 693w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-3-300x128.png 300w" sizes="auto, (max-width: 693px) 100vw, 693px" /></figure>



<p>The main reason to use Photon OS is that it is <strong>open-sourced</strong>, it has a <strong>small footprint</strong> and it is <strong>optimised for VMware vSphere</strong>.</p>



<span id="more-6695"></span>



<h2 class="wp-block-heading">Some Background</h2>



<p>Does this mean that <strong>Photon OS</strong> is the ideal guest OS for our &#8216;<em>cattle</em>&#8216; stations where we want to run our PowerShell scripts?</p>



<p>Not really, the major flaw with Photon OS, imho, is that the available packages are <strong>limited</strong> and rather <strong>infrequently maintained</strong>. </p>



<p>In fact, until recently the available <strong>PowerShell package</strong> was an outdated, <strong>not supported </strong>anymore version. But the worst part of the deal, it didn&#8217;t even allow to perform an <a rel="noreferrer noopener" aria-label="Install-Module (opens in a new tab)" href="https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-6" target="_blank">Install-Module</a>. Which, again imho, is a basic concept in PowerShell. And which didn&#8217;t allow me to install, for example, <a rel="noreferrer noopener" aria-label="VMware PowerCLI (opens in a new tab)" href="https://www.vmware.com/support/developer/PowerCLI/" target="_blank">VMware PowerCLI</a> on a Photon station.</p>



<p>But then recently this happened!</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="521" height="363" src="https://www.lucd.info/wp-content/uploads/2019/12/ps6-photon.png" alt="" class="wp-image-6788" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ps6-photon.png 521w, https://www.lucd.info/wp-content/uploads/2019/12/ps6-photon-300x209.png 300w" sizes="auto, (max-width: 521px) 100vw, 521px" /></figure>



<p>The available PowerShell v6 <a href="https://vmware.github.io/photon/assets/files/html/3.0/photon_admin/tdnf.html" target="_blank" rel="noopener noreferrer">TDNF</a> package was upgraded to a currently supported version, and more importantly, the <strong>Import-Module</strong> cmdlet&nbsp; worked!&nbsp;</p>
<p>Allowing us to install VMware PowerCLI on a Photon instance.</p>



<p>Is this the end of all the PowerShell woes on Photon OS?</p>



<p>Not really! The PowerShell version in this TDNF package will also run out of support in a not so far future and a PowerShell TDNF package upgrade is (still) not automated and somewhat of a black art. </p>



<p>But for now, let&#8217;s be happy with what we have and &#8216;cloud-init&#8217; our Photon &#8216;cattle&#8217; station!</p>



<h2 class="wp-block-heading">Issues and some Solutions</h2>



<h3 class="wp-block-heading">No UserData Property</h3>



<p>While the Ubuntu cloud image, as we saw in <a rel="noreferrer noopener" aria-label="Part 1 (opens in a new tab)" href="https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/" target="_blank">Part 1</a> of the series, has the option to pass the <strong>user-data</strong> to cloud-init via an <strong>OVF property</strong>, this feature is unfortunately not available in the Photon OVA.</p>



<p>There are some solutions available.</p>



<ul class="wp-block-list"><li>Roll your own OVA image</li><li>Use the <a rel="noreferrer noopener" aria-label="NoCloud (opens in a new tab)" href="https://cloudinit.readthedocs.io/en/latest/topics/datasources/nocloud.html" target="_blank">NoCloud</a> option, which means creating an ISO containing the user-data and attaching this ISO to the instance before cloud-init runs.</li></ul>



<p>Since I prefer to use the &#8216;standard&#8217; OVA, I opt for creating such a seed ISO.</p>



<h3 class="wp-block-heading">The Root Password</h3>



<p>This is somewhat of a <a rel="noreferrer noopener" aria-label="chicken-and-the-egg (opens in a new tab)" href="https://en.wikipedia.org/wiki/Chicken_or_the_egg" target="_blank">chicken-and-the-egg</a> dilemma.</p>



<p>In the Photon OVA image, the root account is set to force a password change on first use. But unfortunately, this also disallows setting the root password via the user-data. See <a href="https://github.com/vmware/photon/issues/931">Issue#931</a> for more details.</p>



<p>Again, there are two options available.</p>



<ul class="wp-block-list"><li>Roll your own OVA image</li><li>Use a two-stage process, wherein the first stage we use <a rel="noreferrer noopener" aria-label="William Lam (opens in a new tab)" href="https://twitter.com/lamw" target="_blank">William Lam</a>&#8216;s  <a rel="noreferrer noopener" aria-label="Set-VMKeystrokes (opens in a new tab)" href="https://www.virtuallyghetto.com/2017/09/automating-vm-keystrokes-using-the-vsphere-api-powercli.html" target="_blank">Set-VMKeystrokes</a> function. Then in the second stage, we attach the seed ISO and start the instance. And thus kicking off the cloud-init process.</li></ul>



<p>And again, since I prefer to use the &#8216;standard&#8217; OVA, I opt for the Set-VMKeystrokes solution.</p>



<h2 class="wp-block-heading">Install-CloudInitVM Revisited</h2>



<pre class="lang:ps decode:true  ">function Install-CloudInitVM
{
  &lt;#
.SYNOPSIS
  Deploy a VM from an OVA file and use cloud-init for the configuration
  .DESCRIPTION
  This function will deploy an OVA file.
  The function transfer the user-data to the cloud-init process on the VM with
  one of the OVF properties.
.NOTES
  Author:  Luc Dekens
  Version:
  1.0 05/12/19  Initial release
  1.1 07/12/19  Added Photon deployment
                Added seed ISO option
.PARAMETER OvaFile
  Specifies the path to the OVA file
.PARAMETER VmName
  The displayname of the VM
.PARAMETER ClusterName
  The cluster onto which the VM shall be deployed
.PARAMETER DsName
  The datastore on which the VM shall be deployed
.PARAMETER PgName
  The portgroupname to which the VM shall be connected
.PARAMETER CloudConfig
  The path to the YAML file containing the user-data
.PARAMETER CloudConfigISO
  The ISO seed file, containing the meta-data and user-data
.PARAMETER Credential
  The credentials for a user in the VM's guest OS
.PARAMETER Photon
  Switch to indicate a Photon is deployed. This will add an
  additional step to reset the root password before starting
  the actual cloud-init based deployment
.EXAMPLE
  $sCloudInitVM = @{
    OvaFile = '.\bionic-server-cloudimg-amd64.ova'
    VmName = $vmName
    ClusterName = $clusterName
    DsName = $dsName
    PgName = $pgName
    CloudConfig = '.\user-data.yaml'
    Credential = $cred
  }
  Install-CloudInitVM @sCloudInitVM
.EXAMPLE
  $sCloudInitVM = @{
    OvaFile = '.\photon-hw13_uefi-3.0-9355405.ova'
    VmName = $vmName
    ClusterName = $clusterName
    DsName = $dsName
    PgName = $pgName
    CloudConfigISO = '.\seed.iso'
    Credential = $cred
    Photon = $true
  }
  Install-CloudInitVM @sCloudInitVM
#&gt;

  [cmdletbinding()]
  param(
    [string]$OvaFile,
    [string]$VmName,
    [string]$ClusterName,
    [string]$DsName,
    [string]$PgName,
    [Parameter(ParameterSetName = 'YAML')]
    [string]$CloudConfig,
    [Parameter(ParameterSetName = 'SeedISO')]
    [string]$CloudConfigISO,
    [PSCredential[]]$Credential,
    [Switch]$Photon
  )

  $waitJob = (Get-Command -Name .\Wait-Job.ps1 ).ScriptBlock
  if ($PSCmdlet.ParameterSetName -eq 'YAML')
  {
    $userData = Get-Content -Path $CloudConfig -Raw
  }

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Starting deployment of $vmName"
  if ($Photon)
  {
    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - This is a Photon deployment"
  }

  $start = Get-Date

  $vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue -Verbose:$false
  if ($vm)
  {
    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Cleaning up"
    if ($vm.PowerState -eq 'PoweredOn')
    {
      Stop-VM -VM $vm -Confirm:$false -Verbose:$false | Out-Null
    }
    Remove-VM -VM $vm -DeletePermanently -Confirm:$false -Verbose:$false
  }

  $ovfProp = Get-OvfConfiguration -Ovf $ovaFile -Verbose:$false
  if (-not $Photon)
  {
    $ovfProp.Common.user_data.Value = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($userData))
    $ovfProp.NetworkMapping.VM_Network.Value = $pgName
  }
  else
  {
    $ovfProp.NetworkMapping.None.Value = $pgName
  }
  $sApp = @{
    Source = $ovaFile
    Name = $vmName
    Datastore = Get-Datastore -Name $dsName -Verbose:$false
    DiskStorageFormat = 'Thin'
    VMHost = Get-Cluster -Name $clusterName -Verbose:$false | Get-VMHost  -Verbose:$false | Get-Random
    OvfConfiguration = $ovfProp
    Verbose = $false
  }
  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Importing OVA"
  $vm = Import-VApp @sApp

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Starting the VM"
  Start-VM -VM $vm -Confirm:$false -RunAsync -Verbose:$false | Out-Null

  if ($Photon)
  {
    while (-not $vm.ExtensionData.Guest.GuestOperationsReady)
    {
      Start-Sleep 2
      $vm.ExtensionData.UpdateViewData('Guest')
    }

    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Resetting password"

    $PswdIsReset = $false
    while (-not $PswdIsReset)
    {
      Set-VMKeystrokes -VMName $VM.Name -StringInput 'root' -ReturnCarriage $true | Out-Null
      Start-Sleep 1
      Set-VMKeystrokes -VMName $VM.Name -StringInput 'changeme' -ReturnCarriage $true | Out-Null
      Start-Sleep 1
      Set-VMKeystrokes -VMName $VM.Name -StringInput 'changeme' -ReturnCarriage $true | Out-Null
      Start-Sleep 1
      Set-VMKeystrokes -VMName $VM.Name -StringInput 'Welcome2019!' -ReturnCarriage $true | Out-Null
      Start-Sleep 1
      Set-VMKeystrokes -VMName $VM.Name -StringInput 'Welcome2019!' -ReturnCarriage $true | Out-Null
      Start-Sleep 1
      Set-VMKeystrokes -VMName $VM.Name -StringInput "exit" -ReturnCarriage $true | Out-Null

      Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Testing new password"

      $sInvoke = @{
        VM = $vm
        ScriptType = 'bash'
        ScriptText = 'whoami'
        GuestUser = $Credential.GetNetworkCredential().UserName
        GuestPassword = $Credential.GetNetworkCredential().Password
        Verbose = $false
      }
      try
      {
        Invoke-VMScript @sInvoke -ErrorAction Stop | Out-Null
        $PswdIsReset = $true
      }
      catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]
      {
        Start-Sleep -Seconds 1
      }
      catch
      {
        Write-Error "Exception $($error[0].Exception.GetType().FullName)"
        return
      }
    }

    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Shutdown VM"

    Shutdown-VMGuest -VM $vm -Confirm:$false -Verbose:$false | Out-Null
    while ($vm.ExtensionData.Runtime.PowerState -ne 'poweredOff')
    {
      Start-Sleep 2
      $vm.ExtensionData.UpdateViewData('Runtime.PowerState')
    }

    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Attaching seed ISO"

    $isoName = Split-Path -Path $CloudConfigISO -Leaf
    $isoFile = "DS:\$($vm.ExtensionData.Summary.Config.VmPathName.Split(' ')[1].Split('/')[0])\$($isoName)"
    Get-PSDrive -Name DS -ErrorAction SilentlyContinue -Verbose:$false | Remove-PSDrive -Confirm:$false -Verbose:$false
    New-PSDrive -Location (Get-Datastore -VM $vm) -Name DS -PSProvider VimDatastore -Root '\' -Verbose:$false | Out-Null
    Get-Item -Path $CloudConfigISO |
    Copy-DatastoreItem -Destination $isoFile -Force -Confirm:$false -Verbose:$false
    $isoDSPath = $vm.ExtensionData.Summary.Config.VmPathName.Replace("$vmName.vmx", $isoName)
    Get-CDDrive -VM $vm -Verbose:$false | Set-CDDrive -IsoPath $isoDSPath -StartConnected:$true -Confirm:$false -Verbose:$false | Out-Null

    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Starting the VM"
    Start-VM -VM $vm -Confirm:$false -Verbose:$false | Out-Null
  }

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Waiting for cloud-init to finish"

  $User = $Credential.GetNetworkCredential().UserName
  $Password = $Credential.GetNetworkCredential().Password

  $sJob = @{
    Name = 'WaitForCloudInit'
    ScriptBlock = $waitJob
    ArgumentList = $vm.Name, $User, $Password, $global:DefaultVIServer.Name, $global:DefaultVIServer.SessionId
  }
  Start-Job @sJob | Receive-Job -Wait

  if ($Photon)
  {
    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Restart OS"
    $vmGuest = Stop-VMGuest -VM $vm -Confirm:$false -Verbose:$false
    while ($vmGuest.VM.ExtensionData.Runtime.PowerState -ne 'poweredOff')
    {
      Start-Sleep 2
      $vmGuest.VM.ExtensionData.UpdateViewData('Runtime.PowerState')
    }
    $vm = Start-VM -VM $vmName -Confirm:$false -Verbose:$false
    while ($vm.PowerState -ne 'PoweredOn')
    {
      Start-Sleep 2
      $vm = Get-VM -Name $VmName -Verbose:$false
    }
    while (-not $vm.ExtensionData.Guest.GuestOperationsReady)
    {
      Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Waiting for GuestOperations ready"
      Start-Sleep 2
      $vm.ExtensionData.UpdateViewData('Guest')
    }
    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Eject CDROM"
    $sInvoke = @{
      VM = $vm
      ScriptType = 'bash'
      ScriptText = 'eject cdrom'
      GuestUser = $Credential.GetNetworkCredential().UserName
      GuestPassword = $Credential.GetNetworkCredential().Password
      Verbose = $false
    }
    Invoke-VMScript @sInvoke | Out-Null
    Get-CDDrive -VM $vm -Verbose:$false | Set-CDDrive -NoMedia -Confirm:$false -Verbose:$false | Out-Null
  }

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Deployment complete"
  Write-Verbose "`nDeployment took $([math]::Round((New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds,0)) seconds"
}</pre>
<p>&nbsp;</p>



<h3 class="wp-block-heading">Annotations</h3>



<p>The v1.0 version of this function has already been annotated in <a href="https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/" target="_blank" rel="noreferrer noopener" aria-label="Part 1 (opens in a new tab)">Part 1</a> of this series. Those annotations will not be repeated here.</p>



<p><strong>Line 68-71</strong>: To distinguish between the way the user-data is passed, the function now has two mutually exclusive parameters, CloudConfig and CloudConfigIso.</p>



<p><strong>Line 73</strong>: Since Photon requires some special actions, the function now has a switch, obviously named Photon, that specifies if the cloud-init deployment is intended for a Photon instance or not.</p>



<p><strong>Line 77-80</strong>: When the user-data is provided as a YAML file, the function reads the file.</p>



<p><strong>Line 83-86</strong>: A small reminder in the verbose output that the function will handle this as a Photon deployment.</p>



<p><strong>Line 102-110</strong>: The Ubuntu and the Photon OVA files have different OVF properties. This If-Then-Else handles the differences.</p>



<p><strong>Line 126-199</strong>: This part of the function handles most of the peculiarities of a Photon deployment.</p>



<p><strong>Line 136-175</strong>: This part handles the reset of the root password. As mentioned earlier, it uses William&#8217;s <a rel="noreferrer noopener" aria-label="Set-VMKeystrokes (opens in a new tab)" href="https://www.virtuallyghetto.com/2017/09/automating-vm-keystrokes-using-the-vsphere-api-powercli.html" target="_blank">Set-VMKeystrokes</a> function. I experienced intermittent race conditions in sending the keystrokes. Hence the loop with the retries. As a test, to check if the root password was reset, the command &#8216;<em>whoami</em>&#8216; is executed trough <a rel="noreferrer noopener" aria-label="Invoke-VMScript (opens in a new tab)" href="https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6fb85470-f6ca-4341-858d-12ffd94d975e/4bee17f3-579b-474e-b51c-898e38cc0abb/doc/Invoke-VMScript.html" target="_blank">Invoke-VMScript</a> on the instance.</p>



<p><strong>Line 179-184</strong>: Before attaching the seed ISO, the Photon OS is shutdown gracefully. In a While-loop the script test until the instance is actually powered off.</p>



<p><strong>Line 188-195</strong>: The seed ISO is copied to the VM&#8217;s folder, and then attached.</p>



<p><strong>Line 198</strong>: The VM is started with the seed ISO attached. As a result, cloud-init will now use the YAML files it finds on the CD drive as datasource.</p>



<p><strong>Line 216-221</strong>: Since the cloud-init implementation on Photon is <a rel="noreferrer noopener" aria-label="not configured (opens in a new tab)" href="https://github.com/vmware/photon/issues/950" target="_blank">not configured</a> to use the power-state module, the test we used on the Ubuntu instance, can not be used for Photon. As a bypass, the function stops/starts the guest OS on the instance.</p>



<p><strong>Line 235-244</strong>: The function uses the stop/start sequence to disconnect the seed ISO. To avoid having a question on the VM, the function first runs the &#8216;<em>eject cdrom</em>&#8216; command inside the instance.</p>



<h2 class="wp-block-heading">The Seed ISO</h2>



<p>As mentioned earlier, I prefer to use a seed ISO file to solve the issue of not having an OVF property for the user-data.</p>



<p>To create this seed ISO file, we need a Linux station that has the &#8216;<a rel="noreferrer noopener" aria-label="genisoimage (opens in a new tab)" href="https://linux.die.net/man/1/genisoimage" target="_blank">genisoimage</a>&#8216; command available. In the following sample script, I use an  Ubuntu station just for that purpose.</p>



<pre class="lang:ps decode:true  ">$workVM = 'UbuntuPS'
$isoName = 'genisoimage-ps.iso'
$isoVolId = 'cidata'
$isoFiles = @{
    'meta-data' = 'meta-data.yaml'
    'user-data' = 'user-data.yaml'
}

$code1 = @'
sudo apt-get install genisoimage -y &gt; null
genisoimage -output $isoName -volid $isoVolId -joliet -rock $($isoFiles.Keys -join ' ')
'@

$workCred = Get-VICredentialStoreItem -Host $workVM

$isoFiles.GetEnumerator() | ForEach-Object -Process {
    Copy-Item -Path $_.Value -Destination $_.Name
}

$sInvoke = @{
    VM = $workVM
    ScriptType = 'Bash'
    ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code1)
    GuestUser = $workCred.User
    GuestPassword = ConvertTo-SecureString -String $workCred.Password -AsPlainText -Force
    GuestOSType = 'Linux'
    Verbose = $true
    NoIPinCert = $true
    InFile = $isoFiles.Keys
    OutFile = $isoName
}
Invoke-VMScriptPlus @sInvoke

Remove-Item -Path @($isoFiles.Keys) -Confirm:$false
Move-Item -Path $isoName -Destination "..\$isoName" -Force -Confirm:$false</pre>



<h3 class="wp-block-heading">Annotations</h3>



<p><strong>Line 4-7, 16-18</strong>: The script converts (copies) the provided YAML files to standard named YAML files. This allows the script to be used with different versions of the data and only needs a change in the <strong>$isoFiles</strong> hash table.</p>



<p><strong>Line 32</strong>: This script uses the latest version of my <a rel="noreferrer noopener" aria-label="Invoke-VMScriptPlus (opens in a new tab)" href="https://www.lucd.info/2019/11/17/invoke-vmscriptplus-v3/" target="_blank">Invoke-VMScriptPlus</a> function. The reason is twofold: the bash script is multi-line and the script used the InFile/OutFile parameters to copy the YAML files to the Linux box and the ISO file back to the calling station.</p>



<h2 class="wp-block-heading">User-Data</h2>



<p>The user-data that is used for a Photon based instance is, for now, rather simple and straightforward. </p>



<p>Note the installation of the <strong>tdnf</strong> <strong>PowerShell</strong> package (as mentioned earlier in this post) in the <strong>runcmd</strong> section.</p>



<pre class="lang:yaml decode:true ">#cloud-config
fqdn: PhotonPS.local.lab
timezone: Europe/Brussels
hostname: PhotonPS
write_files:
    - path: /etc/systemd/network/10-static.network
      permissions: 0644
      content: |
        [Match]
        Name=eth0

        [Network]
        Address=192.168.10.81/24
        Gateway=192.168.10.1
        DNS=192.168.10.2 192.168.10.3
        DHCP=no
        Domains=local.lab
        NTP=192.168.10.2 192.168.10.3
        LinkLocalAddressing=no
        LLDP=true
    - path: /etc/systemd/network/99-dhcp-en.network
      permissions: 0644
      content: |
        [Match]
        Name=e*

        [Network]
        DHCP=no
    - path: /etc/gnutls/default-priorities
      content: |
        SYSTEM=NONE:!VERS-SSL3.0:!VERS-TLS1.0:+VERS-TLS1.1:+VERS-TLS1.2:+AES-128-CBC:+RSA:+SHA1:+COMP-NULL
runcmd:
- tdnf distro-sync
- tdnf update -y
- tdnf install bindutils -y
- tdnf install powershell -y
- tdnf install gnuTLS -y
- touch /etc/gnutls/default-priorities
package_upgrade: true
package_reboot_if_required: true
</pre>



<h2 class="wp-block-heading">Meta-data</h2>



<p>If you plan on deploying multiple instance, just make sure that the <strong>instance-id</strong> has a unique value.</p>



<pre class="lang:yaml decode:true ">instance-id: iid-local01
local-hostname: photonps
</pre>



<h2 class="wp-block-heading">Sample Run</h2>



<p>For a Photon instance the function is called like this.</p>



<pre class="lang:ps decode:true  ">$vmName = 'PhotonPS'

# Get credential for logging on to the guest OS
$viCred = Get-VICredentialStoreItem -Host $vmName
$secPassword = ConvertTo-SecureString -String $viCred.Password -AsPlainText -Force
$cred = [Management.Automation.PSCredential]::new($viCred.User, $secPassword)

$sCloudInitVM = @{
  OvaFile = 'D:\Repository\VMware\Photon\photon-hw13_uefi-3.0-9355405.ova'
  VmName = $vmName
  ClusterName = 'cluster'
  DsName = 'vsanDatastore'
  PgName = 'vdPg1'
  CloudConfigISO = '..\genisoimage-ps.iso'
  Photon = $true
  Credential = $cred
  Verbose = $true
}
Install-CloudInitVM @sCloudInitVM</pre>



<p>The result of this, since we used the Verbose switch, looks like this.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="687" height="533" src="https://www.lucd.info/wp-content/uploads/2019/12/photon-deploy-1.png" alt="" class="wp-image-6832" srcset="https://www.lucd.info/wp-content/uploads/2019/12/photon-deploy-1.png 687w, https://www.lucd.info/wp-content/uploads/2019/12/photon-deploy-1-300x233.png 300w" sizes="auto, (max-width: 687px) 100vw, 687px" /></figure>



<p>Again, don&#8217;t give too much importance to the total execution time. This was a run in my lab environment, which has limited resources.</p>



<p>On a side-note, notice how the PowerCLI cmdlets used in the function all show this, imho, annoying &#8216;<em><strong>Finished execution</strong></em>&#8216; message.<br>I would love to be able to suppress these verbose messages from the PowerCLI cmdlets. That is why I launched <a rel="noreferrer noopener" aria-label="PowerCLI Idea #225 (opens in a new tab)" href="https://powercli.ideas.aha.io/ideas/PCLI-I-225?utm_source=idea_mailer&amp;utm_medium=email&amp;utm_campaign=submitted_idea" target="_blank">PowerCLI Idea #225</a> &#8220;<em>Get rid of the over-eager verbosity on PowerCLI cmdlets</em>&#8220;.</p>



<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2019/12/08/cloud-init-part-3-photon-os/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Cloud-init &#8211; Part 2 &#8211; Advanced Ubuntu</title>
		<link>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/</link>
					<comments>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sat, 07 Dec 2019 07:43:24 +0000</pubDate>
				<category><![CDATA[Cloud-init]]></category>
		<category><![CDATA[Deploy]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[powershellv6]]></category>
		<category><![CDATA[powershellv7]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Visual Studio Code]]></category>
		<category><![CDATA[YAML]]></category>
		<category><![CDATA[cloud-init]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[repository]]></category>
		<category><![CDATA[v6]]></category>
		<category><![CDATA[v7]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=6693</guid>

					<description><![CDATA[In Cloud-init – Part 1 – The Basics, we laid the groundwork for [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In <a href="https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/" target="_blank" rel="noopener noreferrer">Cloud-init – Part 1 – The Basics</a>, we laid the groundwork for using <strong>cloud-init</strong> in a <strong>vSphere</strong> environment. In this post we will go into more <strong>advanced Ubuntu</strong> setups. This includes deploying PowerShell, v6 and v7, using repositories and if needed, a GUI with Visual Studio Code.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="679" height="296" src="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2.png" alt="" class="wp-image-6708" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2.png 679w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2-300x131.png 300w" sizes="auto, (max-width: 679px) 100vw, 679px" /></figure>



<span id="more-6693"></span>



<p>For the installation of software on the instance we use the <strong>user-data</strong> YAML file. In the following sections, we discuss some of the more obvious candidates you want to have on your station. Remember that the basic idea of the series is to have a &#8220;<em>cattle</em>&#8221; station to test/run (and optionally develop) your scripts. </p>



<h2 class="wp-block-heading">Scripting Packages</h2>



<h3 class="wp-block-heading">PowerShell</h3>



<p>Let&#8217;s start by installing <strong>PowerShell</strong> on the instance. This comes down to adding a couple of instructions to the YAML file we used in Part 1.</p>



<pre class="lang:yaml decode:true ">#cloud-config
hostname: ubuntubionicps
fqdn: ubuntubionicps.local.lab
write_files:
- path: /etc/netplan/50-cloud-init.yaml
  content: |
    network:
     version: 2
     ethernets:
      ens192:
       addresses: [192.168.10.82/24]
       gateway4: 192.168.10.1
       dhcp6: false
       nameservers:
         addresses:
           - 192.168.10.2
           - 192.168.10.3
         search:
           - local.lab
       dhcp4: false
       optional: true
- path: /etc/sysctl.d/60-disable-ipv6.conf
  owner: root
  content: |
    net.ipv6.conf.all.disable_ipv6=1
    net.ipv6.conf.default.disable_ipv6=1
runcmd:
- netplan --debug apply
- sysctl -w net.ipv6.conf.all.disable_ipv6=1
- sysctl -w net.ipv6.conf.default.disable_ipv6=1
- apt-get -y update
- add-apt-repository universe
# PowerShell
- wget https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/install-powershell.sh
- chmod 755 install-powershell.sh
# PowerShell v6
- ./install-powershell.sh
# PowerShell v7
- ./install-powershell.sh -preview
- apt-get -y clean
- apt-get -y autoremove --purge
timezone: Europe/Brussels
system_info:
  default_user:
    name: default-user
    lock_passwd: false
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
disable_root: false
ssh_pwauth: yes
users:
  - default
  - name: luc
    gecos: LucD
    lock_passwd: false
    groups: sudo, users, admin
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
chpasswd:
  list: |
    default-user:$6$aPp//e2ueP$ETEXcAhAyQuJ4qNCbqxmmSYGZbg2wFwpP/YITvoXdgxwZBnf32drePKi2OIn5fLqtH5pHO03yRdPXK3ToLG6b0
    luc:$6$kW1WwJ2K$M6415du1BZd.qt92SvR6X.RuyDhEZmgR4hz4NcKH9XHn2850Vc6zHpubXM6uUeqMUaJQ740ogROB74gfBEhn9.
    root:$6$Js9CVr06$br9qf0VxuBsdY7Vtg/0pk9jLlycYBDLVsvbKwLDleCK7dSDheOxWaFOWdjkiqSPRrWG./N8V5RgCVwugZGnTc1
  expire: false
package_upgrade: true
package_reboot_if_required: true
power_state:
  delay: now
  mode: reboot
  message: Rebooting the OS
  condition: if [ -e /var/run/reboot-required ]; then exit 0; else exit 1; fi</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 33-39</strong>: The instructions to install PowerShell are added to the <strong>runcmd</strong> section.</p>



<p><strong>Line 33</strong>: You can use comments in your YAML file. I highly recommend doing that. It will help others, and probably yourself later on.</p>



<p><strong>Line 34-35</strong>: The method uses the <a rel="noreferrer noopener" aria-label="install.sh (opens in a new tab)" href="https://github.com/PowerShell/PowerShell/blob/master/tools/install-powershell.sh" target="_blank">install-powershell.sh</a> bash script to install the latest PowerShell v6 and the latest PowerShell v7 Preview. I personally prefer this method vs the apt-get method, since it is globally usable on multiple Linux distributions. The <strong>install-powershell.sh</strong> script determines on which distro it is running, and launches the appropriate command(s).</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="550" height="288" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps.png" alt="" class="wp-image-6744" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps.png 550w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps-300x157.png 300w" sizes="auto, (max-width: 550px) 100vw, 550px" /></figure>



<h3 class="wp-block-heading">Git &amp; repo cloning</h3>



<p>For your scripts, you, of course, use a repository. That means you will need to configure <a rel="noreferrer noopener" aria-label="git (opens in a new tab)" href="https://www.linux.com/tutorials/introduction-using-git/" target="_blank">git</a>, and a way to clone repositories to your instance.</p>



<p>The following extract of the YAML file only shows the lines in the <strong>runcmd</strong> section.</p>



<pre class="lang:yaml decode:true  ">runcmd:
- netplan --debug apply
- sysctl -w net.ipv6.conf.all.disable_ipv6=1
- sysctl -w net.ipv6.conf.default.disable_ipv6=1
- apt-get -y update
- add-apt-repository universe
# PowerShell
- wget https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/install-powershell.sh
- chmod 755 install-powershell.sh
# PowerShell v6
- ./install-powershell.sh
# PowerShell v7
- ./install-powershell.sh -preview
# Git
- git config --global user.name "LucD"
- git config --global user.email "lucd@lucd.info"
- mkdir /home/luc/vCheck-vSphere
- chown luc:luc /home/luc/vCheck-vSphere
- git clone https://github.com/alanrenouf/vCheck-vSphere /home/luc/vCheck-vSphere
- apt-get -y clean
- apt-get -y autoremove --purge</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 14-16</strong>: Since git is by default installed in the Ubuntu 18.04 LTS Cloud Image OVA file, we only need to configure some settings. In this we update some configuration settings, primarily to be able to correct sign off commits.</p>



<p><strong>Line 17-19</strong>: As part of the deployment process through cloud-init, you can clone specific repositories to the instance. In these lines I used <a rel="noreferrer noopener" aria-label="Alan Renouf (opens in a new tab)" href="https://twitter.com/alanrenouf" target="_blank">Alan Renouf</a>&#8216;s <a href="https://github.com/alanrenouf/vCheck-vSphere" target="_blank" rel="noreferrer noopener" aria-label="vCheck-vSphere (opens in a new tab)">vCheck-vSphere</a> repository as an example.</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="517" height="293" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck.png" alt="" class="wp-image-6747" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck.png 517w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck-300x170.png 300w" sizes="auto, (max-width: 517px) 100vw, 517px" /></figure>



<h2 class="wp-block-heading">Going GUI</h2>



<p>There might be occasions when you need to provide a temporary station with a GUI. That can also be done with cloud-init&#8217;s user-data YAML file.</p>



<h3 class="wp-block-heading">The Desktop</h3>



<p>The choice of which desktop and which desktop manager is completely up to you. Just be aware that some of these might require more resources to be assigned to the instance.</p>



<p>The following sample is intended for an Ubuntu 18.04 LTS instance. Again this extract only shows the <strong>runcmd</strong> section of the YAML file.</p>



<pre class="lang:yaml decode:true   "># Install GUI
- apt-get install -y tasksel
- tasksel install lubuntu-core
# ==&gt; know issue
- apt-get --purge remove -y light-locker
- service lightdm start
# Install RDP
- apt-get install -y apt-transport-https
- apt-get install -y xrdp
- systemctl enable xrdp</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 1-10</strong>: In this example, I use <a rel="noreferrer noopener" aria-label="lubuntu (opens in a new tab)" href="https://lubuntu.net/" target="_blank">lubuntu</a> as the desktop. Primarily because it is fast and resource-friendly.</p>



<p><strong>Line </strong>2: We use <a rel="noreferrer noopener" aria-label="tasksel (opens in a new tab)" href="https://manpages.ubuntu.com/manpages/xenial/man8/tasksel.8.html" target="_blank">tasksel</a> to install the desktop, so we have to make sure that it is available.</p>



<p><strong>Line 5</strong>: There is a known issue with the <a rel="noreferrer noopener" aria-label="light-locker (opens in a new tab)" href="https://launchpad.net/ubuntu/+source/light-locker" target="_blank">light-locker</a> package. Until a final fix is available, the easiest solution is to just uninstall it. If you absolutely need a screen-saver, install one of the alternatives. Like for example <a rel="noreferrer noopener" aria-label="xscreensaver (opens in a new tab)" href="https://packages.ubuntu.com/search?keywords=xscreensaver" target="_blank">xscreensaver</a>.</p>



<p><strong>Line 6</strong>: Next we start the display manager <a rel="noreferrer noopener" aria-label="lightdm (opens in a new tab)" href="https://wiki.ubuntu.com/LightDM" target="_blank">lightdm</a>.</p>



<p><strong>Line 7-</strong>10: We need a way to connect to the station we are deploying. In this example, I opted for RDP as my protocol.</p>



<p><strong>Line 8</strong>: The <a rel="noreferrer noopener" aria-label="apt-transport-https (opens in a new tab)" href="https://manpages.ubuntu.com/manpages/bionic/man1/apt-transport-https.1.html" target="_blank">apt-transport-https</a> package is a pre-requisite.</p>



<p><strong>Line 10</strong>: Configure the <a rel="noreferrer noopener" aria-label="xrdp (opens in a new tab)" href="https://help.ubuntu.com/community/xrdp" target="_blank">xrdp</a> service to automatically start at the boot.</p>



<h3 class="wp-block-heading">Visual Studio Code</h3>



<p>Now that we have a GUI, we can use several GUI based applications. For working with PowerShell, the <a rel="noreferrer noopener" aria-label="Visual Studio Code (opens in a new tab)" href="https://code.visualstudio.com/" target="_blank">Visual Studio Code</a> editor with the <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell" target="_blank" rel="noreferrer noopener" aria-label="PowerShell extension (opens in a new tab)">PowerShell extension</a> is an obvious choice.</p>



<pre class="lang:yaml decode:true "># Install Visual Studio Code
- add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
- apt-get update
- apt-get install -y code
- mkdir /home/luc/.vscode
- chown luc:luc /home/luc/.vscode
- code --install-extension ms-vscode.powershell --user-data-dir /home/luc/.vscode/ --extensions-dir /home/luc/.vscode/extensions/
- code --install-extension eamodio.gitlens --user-data-dir /home/luc/.vscode --extensions-dir /home/luc/.vscode/extensions/</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 2-4</strong>: The standard <a href="https://code.visualstudio.com/docs/setup/linux" target="_blank" rel="noopener noreferrer">installation procedure</a> for Code on a Linux platform.</p>
<p><strong>Line 5-8</strong>: This installs the extensions for <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell" target="_blank" rel="noopener noreferrer">PowerShell</a> and <a href="https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens" target="_blank" rel="noopener noreferrer">GitLens</a>. Remember that the cloud-init stages run under the root account, if you need to install packages for a user, make sure to change the owner.</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="561" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1024x561.png" alt="" class="wp-image-6773" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1024x561.png 1024w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-300x164.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-768x421.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1536x842.png 1536w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-720x395.png 720w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI.png 1633w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">Conclusion</h2>



<p>We have now set the stage to actually start using these instances. In one of the next posts in this cloud-init series, we will show how to run scripts on these stations.</p>



<p>If you have suggestions or questions about specific additions on these instances, feel free to use the comments.</p>



<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>Cloud-init &#8211; Part 1 &#8211; The Basics</title>
		<link>https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/</link>
					<comments>https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Fri, 06 Dec 2019 12:16:08 +0000</pubDate>
				<category><![CDATA[Cloud-init]]></category>
		<category><![CDATA[Deploy]]></category>
		<category><![CDATA[OVA]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[YAML]]></category>
		<category><![CDATA[cloud-init]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[VM]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=6586</guid>

					<description><![CDATA[In this first part, we will introduce cloud-init and how you can use it from your PowerShell/PowerCLI scripts. Since the Ubuntu distribution is very popular...]]></description>
										<content:encoded><![CDATA[
<p>One of the important <a href="https://en.wikipedia.org/wiki/DevOps">DevOps</a> adagios in my book is “<em>Treat your servers as cattle, not as pets</em>”. Meaning that you roll out your stations when you need them, use them and throw them away after you used them. This series of posts will document one such way of deploying such &#8216;cattle&#8217; stations. The method is named <a href="https://cloud-init.io/" target="_blank" rel="noreferrer noopener" aria-label="cloud-init (opens in a new tab)">cloud-init</a>.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="678" height="295" src="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-1.png" alt="Post logo" class="wp-image-6589" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-1.png 678w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-1-300x131.png 300w" sizes="auto, (max-width: 678px) 100vw, 678px" /></figure>



<p>In this first part, we will introduce <strong>cloud-init</strong> and how you can use it from your <strong>PowerShell/PowerCLI</strong> scripts. Since the <a rel="noreferrer noopener" aria-label="Ubuntu (opens in a new tab)" href="https://en.wikipedia.org/wiki/Ubuntu" target="_blank">Ubuntu</a> distribution is very popular, on-premises and in the cloud, this introduction will focus on that distro to demonstrate the concept. In the following parts, we will tackle <a rel="noreferrer noopener" aria-label="Photon (opens in a new tab)" href="https://vmware.github.io/photon/" target="_blank">Photon</a>, containers and how to run your scripts on these stations.</p>



<span id="more-6586"></span>



<p>Before diving into the technical stuff, first a reminder why we should treat our <strong>servers as cattle</strong>.</p>



<p>A list with some of the major arguments (at least for me).</p>



<ul class="wp-block-list"><li>No maintenance required when using the latest distributions. Meaning no long-running updates and security patches before usage.</li><li>No “<em>less than 5% used</em>” machines anymore in your environment.</li><li>No risk of system tattooing from any previous usage.</li><li>An easy way to test new versions of your scripts, the OS, PowerShell and PowerCLI.</li></ul>



<h2 class="wp-block-heading">Ingredients</h2>



<p>To use cloud-init, the requirements, in the cases we will handle here, are minimal.</p>



<ul class="wp-block-list"><li>An <strong>OVA image</strong> that is configured to use cloud-init<ul><li>Several Linux distributions nowadays have such a &#8216;cloud&#8217; image: <a rel="noreferrer noopener" aria-label="Ubuntu (opens in a new tab)" href="https://cloud-images.ubuntu.com/" target="_blank">Ubuntu</a>, <a rel="noreferrer noopener" aria-label="Photon (opens in a new tab)" href="https://github.com/vmware/photon/wiki/Downloading-Photon-OS" target="_blank">Photon</a>&#8230;</li></ul></li><li>A <a rel="noreferrer noopener" aria-label="datasource (opens in a new tab)" href="https://cloudinit.readthedocs.io/en/latest/topics/datasources.html" target="_blank">datasource</a>, which is the configuration data provided by the user to the cloud-init process, and which defines how the resulting station will be configured.</li><li>A <strong>cloud</strong>, in it&#8217;s broadest sense, to run the VM. From the cloud-init documentation &#8220;&#8230; <em>all major public cloud providers, provisioning systems for private cloud infrastructure, and bare-metal installations</em>.&#8221;</li><li>A <strong>script</strong> to trigger and control it all. </li></ul>



<p><a rel="noreferrer noopener" aria-label="Canonical (opens in a new tab)" href="https://twitter.com/canonical" target="_blank">Canonical</a>, the company behind Ubuntu, maintains a document, named <a href="https://pages.ubuntu.com/rs/066-EOV-335/images/CloudInit_Whitepaper.pdf?utm_source=marketo&amp;utm_medium=landingpage&amp;utm_campaign=CY19_DC_Server_Whitepaper_CloudInit" target="_blank" rel="noreferrer noopener" aria-label="Cloud Instance Initialisation with cloud-init (opens in a new tab)">Cloud Instance Initialisation with cloud-init</a>, that has a schematic that captures the cloud-init process perfectly.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="923" height="487" src="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema.png" alt="Cloud-init schematic" class="wp-image-6600" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema.png 923w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema-300x158.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema-768x405.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema-720x380.png 720w" sizes="auto, (max-width: 923px) 100vw, 923px" /></figure>



<p>If we annotate and update this schema for the usage we have in mind, it becomes like this.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="860" height="487" src="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema-vsphere.png" alt="Cloud-init for a vSphere environment schematic" class="wp-image-6601" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema-vsphere.png 860w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema-vsphere-300x170.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema-vsphere-768x435.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-schema-vsphere-720x408.png 720w" sizes="auto, (max-width: 860px) 100vw, 860px" /></figure>



<h2 class="wp-block-heading">The OVA File</h2>



<p>Since our target cloud platform in this series is a vSphere environment, we will use OVA files as the source for the VM(s) we are going to deploy.</p>



<p>The <a rel="noreferrer noopener" aria-label="Ubuntu OVA image (opens in a new tab)" href="https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.ova" target="_blank">Ubuntu OVA image</a> that we will be using in this post is for <strong>Ubuntu Server 18.04 LTS</strong>, aka <strong>Bionic Beaver</strong>.</p>



<p>This OVA image allows using the <a rel="noreferrer noopener" aria-label="OVFProperties (opens in a new tab)" href="https://blogs.vmware.com/PowerCLI/2014/09/powercli-5-8-new-feature-get-ovfconfiguration-part-1-2.html" target="_blank">OVFProperties</a> as the datasource. Note that the user-data we pass in this way needs to be <a rel="noreferrer noopener" aria-label="Base64 (opens in a new tab)" href="https://en.wikipedia.org/wiki/Base64" target="_blank">Base64</a> encoded.</p>



<h2 class="wp-block-heading">The User-data</h2>



<p>As mentioned in the previous section, in this Ubuntu image we will use the <strong>OVFProperties</strong> as the datasource for our user-data.</p>



<p>The content of the <strong>user-data</strong> is provided to the script as a <a rel="noreferrer noopener" aria-label="YAML (opens in a new tab)" href="https://en.wikipedia.org/wiki/YAML" target="_blank">YAML</a> file.  The syntax for such a file and the available is defined in the <a href="https://readthedocs.org/projects/cloudinit/downloads/pdf/latest/" target="_blank" rel="noreferrer noopener" aria-label="cloud-init Documentation (opens in a new tab)">cloud-init Documentation</a>.</p>



<p>The following is the sample YAML file we will use in the rest of this post.</p>



<pre class="theme:vs2012-black lang:yaml decode:true ">#cloud-config
hostname: ubuntubionic
fqdn: ubuntubionic.local.lab
write_files:
- path: /etc/netplan/50-cloud-init.yaml
  content: |
    network:
     version: 2
     ethernets:
      ens192:
       addresses: [192.168.10.79/24]
       gateway4: 192.168.10.1
       dhcp6: false
       nameservers:
         addresses:
           - 192.168.10.2
           - 192.168.10.3
         search:
           - local.lab
       dhcp4: false
       optional: true
- path: /etc/sysctl.d/60-disable-ipv6.conf
  owner: root
  content: |
    net.ipv6.conf.all.disable_ipv6=1
    net.ipv6.conf.default.disable_ipv6=1
runcmd:
- netplan --debug apply
- sysctl -w net.ipv6.conf.all.disable_ipv6=1
- sysctl -w net.ipv6.conf.default.disable_ipv6=1
- apt-get -y update
- add-apt-repository universe
- apt-get -y clean
- apt-get -y autoremove --purge
timezone: Europe/Brussels
system_info:
  default_user:
    name: default-user
    lock_passwd: false
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
disable_root: false
ssh_pwauth: yes
users:
  - default
  - name: luc
    gecos: LucD
    lock_passwd: false
    groups: sudo, users, admin
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
chpasswd:
  list: |
    default-user:$6$aPp//e2ueP$ETEXcAhAyQuJ4qNCbqxmmSYGZbg2wFwpP/YITvoXdgxwZBnf32drePKi2OIn5fLqtH5pHO03yRdPXK3ToLG6b0
    luc:$6$kW1WwJ2K$M6415du1BZd.qt92SvR6X.RuyDhEZmgR4hz4NcKH9XHn2850Vc6zHpubXM6uUeqMUaJQ740ogROB74gfBEhn9.
    root:$6$Js9CVr06$br9qf0VxuBsdY7Vtg/0pk9jLlycYBDLVsvbKwLDleCK7dSDheOxWaFOWdjkiqSPRrWG./N8V5RgCVwugZGnTc1
  expire: false
package_upgrade: true
package_reboot_if_required: true
power_state:
  delay: now
  mode: reboot
  message: Rebooting the OS
  condition: if [ -e /var/run/reboot-required ]; then exit 0; else exit 1; fi
</pre>



<h3 class="wp-block-heading">Annotations</h3>



<p><strong>Line 1</strong>: the user-data file always has to start with the line &#8216;# cloud-config&#8217;</p>



<p><strong>Line 2</strong>-3: These lines follow the regular <a rel="noreferrer noopener" aria-label="YAML key-value syntax (opens in a new tab)" href="https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html" target="_blank">YAML syntax</a> of key-value entries.  The lines defines the <strong>hostname</strong> and the <strong>FQDN</strong> that the instance will get.</p>



<p><strong>Line 4-26</strong>: The <strong>write-files</strong> section instructs cloud-init to <strong>create files</strong>, with the specified properties and content on the instance. In this example file the files are used to define a static IPv4 address and to disable IPv6. Which files need to be used is dependent on the OS that runs in the VM. Note that this example will use <a href="https://netplan.io/" target="_blank" rel="noreferrer noopener" aria-label="netplan (opens in a new tab)">netplan</a> for the network configuration.</p>



<p><strong>Line 27-34</strong>: The <strong>runcmd</strong> section defines commands that will run on the instance during the <strong>first boot</strong> of the guest OS. In this example the commands are used to activate the network (it uses netplan), set some variables and set and update the application repositories.</p>



<p><strong>Line 35</strong>: This line defines which timezone needs to be configured.</p>



<p><strong>Line 36-40</strong>: An Ubuntu image comes configured with a <strong>default user. In the system_info section,</strong> the settings for this default user are specified.</p>



<p><strong>Line 43-50</strong>: The <strong>users</strong> section allows to define additional users on the instance. Note that the <strong>default</strong> user needs to come first in this list.</p>



<p><strong>Line 51-56</strong>: The <strong>chpasswd</strong> allows you to change and/or set the passwords for the users. The required format and how these entries are created will be discussed in one of the following sections.</p>



<p><strong>Line 57-58</strong>: These lines request an upgrade of the <strong>packages</strong> present on the instance. And perform a reboot when one of the package upgrades would require that.</p>



<p><strong>Line 59-63</strong>: The <strong>power-state</strong> section instructs cloud-init in which state the instance shall be left after cloud-init completes. In this example, the system is rebooted on condition that a specific file exists. This file is created by cloud-init when it has decided that a reboot of the instance is required.</p>



<h3 class="wp-block-heading">Password encoding</h3>



<p>In <strong>user-data,</strong> there are several places where a password can be provided. The value needs to be the hash of the password, not the password itself.  With the help of an existing Linux box (<strong>UbuntuWork</strong> in this case), I use the following script to create such SHA-512 encoded password hashes.  The script uses the <a rel="noreferrer noopener" aria-label="mkpasswd (opens in a new tab)" href="https://manpages.ubuntu.com/manpages/trusty/man1/mkpasswd.1.html" target="_blank">mkpasswd</a> command to generate the hash. </p>



<p>Automation rules!</p>



<pre class="lang:ps decode:true  ">function Get-PasswordHash
{
    param(
        [String]$VMName,
        [String]$GuestUser,
        [String]$GuestPassword,
        [String]$Password
    )

    $sInvoke = @{
        VM = $VMName
        ScriptType = 'bash'
        ScriptText = "mkpasswd -m SHA-512 $Password"
        GuestUser = $GuestUser
        GuestPassword = $GuestPassword
    }
    (Invoke-VMScript @sInvoke).ScriptOutput.Trim("<code>n")
}

$sHash = @{
    VM = 'UbuntuWork'
    GuestUser = 'root'
    GuestPassword = 'VMware1!'
    Password = 'VMware1!'
}
Get-PasswordHash @sHash
</pre>



<p>This returns something like this.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="944" height="81" src="https://www.lucd.info/wp-content/uploads/2019/12/hash.png" alt="Password hash" class="wp-image-6613" srcset="https://www.lucd.info/wp-content/uploads/2019/12/hash.png 944w, https://www.lucd.info/wp-content/uploads/2019/12/hash-300x26.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/hash-768x66.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/hash-720x62.png 720w" sizes="auto, (max-width: 944px) 100vw, 944px" /></figure>



<p>A word of warning, such hashed password are not super-safe. A decent password cracker with sufficient compute power and a good wordlist can most probably crack this in less than one minute.</p>



<h2 class="wp-block-heading">The Script</h2>



<pre class="lang:ps decode:true   ">function Install-CloudInitVM
{
  &lt;#
.SYNOPSIS
  Deploy a VM from an OVA file and use cloud-init for the configuration
  .DESCRIPTION
  This function will deploy an OVA file.
  The function transfer the user-data to the cloud-init process on the VM with
  one of the OVF properties.
.NOTES
  Author:  Luc Dekens
  Version:
  1.0 05/12/19  Initial release
.PARAMETER OvaFile
  Specifies the path to the OVA file
.PARAMETER VmName
  The displayname of the VM
.PARAMETER ClusterName
  The cluster onto which the VM shall be deployed
.PARAMETER DsName
  The datastore on which the VM shall be deployed
.PARAMETER PgName
  The portgroupname to which the VM shall be connected
.PARAMETER CloudConfig
  The path to the YAML file containing the user-data
.PARAMETER Credential
  The credentials for a user in the VM's guest OS
.EXAMPLE
  $sCloudInitVM = @{
    OvaFile = '.\bionic-server-cloudimg-amd64.ova'
    VmName = $vmName
    ClusterName = $clusterName
    DsName = $dsName
    PgName = $pgName
    CloudConfig = '.\user-data.yaml'
    Credential = $cred
  }
  Install-CloudInitVM @sCloudInitVM
  #&gt;

  [cmdletbinding()]
  param(
    [string]$OvaFile,
    [string]$VmName,
    [string]$ClusterName,
    [string]$DsName,
    [string]$PgName,
    [string]$CloudConfig,
    [PSCredential[]]$Credential
  )

  $waitJob = (Get-Command -Name .\Wait-Job.ps1).ScriptBlock
  $userData = Get-Content -Path $CloudConfig -Raw

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Starting deployment of $vmName"

  $start = Get-Date

  $vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
  if ($vm)
  {
    Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Cleaning up"
    if ($vm.PowerState -eq 'PoweredOn')
    {
      Stop-VM -VM $vm -Confirm:$false | Out-Null
    }
    Remove-VM -VM $vm -DeletePermanently -Confirm:$false
  }

  $ovfProp = Get-OvfConfiguration -Ovf $ovaFile
  $ovfProp.Common.user_data.Value = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($userData))
  $ovfProp.NetworkMapping.VM_Network.Value = $pgName

  $sApp = @{
    Source = $ovaFile
    Name = $vmName
    Datastore = Get-Datastore -Name $dsName
    DiskStorageFormat = 'Thin'
    VMHost = Get-Cluster -Name $clusterName | Get-VMHost | Get-Random
    OvfConfiguration = $ovfProp
  }
  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Importing OVA"
  $vm = Import-VApp @sApp

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Starting the VM"
  Start-VM -VM $vm -Confirm:$false -RunAsync | Out-Null

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Waiting for cloud-init to finish"

  $User = $Credential.GetNetworkCredential().UserName
  $Password = $Credential.GetNetworkCredential().Password

  $sJob = @{
    Name = 'WaitForCloudInit'
    ScriptBlock = $waitJob
    ArgumentList = $vm.Name, $User, $Password, $global:DefaultVIServer.Name, $global:DefaultVIServer.SessionId
  }
  Start-Job @sJob | Receive-Job -Wait

  Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') - Deployment complete"

  Write-Verbose "</code>nDeployment took $([math]::Round((New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds,0)) seconds"
}
</pre>



<h3 class="wp-block-heading">Annotations</h3>



<p><strong>Line 52</strong>: To find out when cloud-init has finished it's run, the function uses a background job. This background job is stored in a separate .ps1 file. More on the content and purpose of that 'wait' script later.</p>



<p><strong>Line 53</strong>: The user-data is read from the YAML file. It is important to use the <strong>Raw</strong> switch, otherwise, we would lose the line separators.</p>



<p><strong>Line 60-68</strong>: If a VM with the same Displayname is already present, it will be stopped (when powered on) and removed.</p>



<p><strong>Line 70-71</strong>: The user-data is assigned to the <strong>Common.user_data</strong> property of the OVF properties. The user-data needs to be converted to <strong>base64</strong>.</p>



<p><strong>Line 72</strong>: The Ubuntu OVA allows to pass the Portgroup to which the VM shall be connected.</p>



<p><strong>Line 74-83</strong>: The VM is installed from the OVA with the <a rel="noreferrer noopener" aria-label="Import-VApp (opens in a new tab)" href="https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6fb85470-f6ca-4341-858d-12ffd94d975e/4bee17f3-579b-474e-b51c-898e38cc0abb/doc/Import-VApp.html" target="_blank">Import-VApp</a> cmdlet.</p>



<p><strong>Line 86</strong>: The VM is powered on. During the boot process, the first phase of cloud-init will run.</p>



<p><strong>Line 90-98</strong>: The function uses a background job to check if the cloud-init process has completed. More on that later.</p>



<h3 class="wp-block-heading">The Wait job</h3>



<p>I decided to store the WaitJob script in a separate .ps1 file. Primarily because I can easily reuse it this way and it makes the code of scripts that use shorter.</p>



<p>The Wait Job is started as a background job. This allows the calling code to simply use the <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/wait-job?view=powershell-6" target="_blank" rel="noreferrer noopener" aria-label="Wait-Job (opens in a new tab)">Wait-Job</a> cmdlet.</p>



<pre class="lang:ps decode:true   ">[CmdletBinding()]
param(
    [string]$vmName,
    [string]$user,
    [string]$pswd,
    [string]$vcsaName,
    [string]$SessionId
)

$sleepTime = 5
Connect-VIServer -Server $vcsaName -Session $SessionId | Out-Null
$notFinished = $true
while ($notFinished)
{
    Try
    {
        $vm = Get-VM -Name $vmName -ErrorAction Stop
        while ($vm.PowerState -ne 'PoweredOn' -and -not $vm.ExtensionData.Guest.GuestOperationsReady)
        {
            Start-Sleep -Seconds $sleepTime
        }
        $fileExist = $false
        while (-not $fileExist)
        {
            $sInvoke = @{
                VM = $vm
                ScriptType = 'bash'
                ScriptText = '[ -f /var/lib/cloud/instance/boot-finished ] &amp;&amp; echo "File exist"'
                GuestUser = $user
                GuestPassword = $pswd
                ErrorAction = 'Stop'
            }
            try
            {
                $result = Invoke-VMScript @sInvoke
                $fileExist = [Boolean]$result.ScriptOutput
            }
            catch
            {
                Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') Exception:<code>tId: $($error[0].Exception.ErrorId)  Category: $($error[0].Exception.ErrorCategory)"
                Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff')</code>t<code>tLine: $($error[0].InvocationInfo.Line)"
            }
        }
        $sInvoke = @{
            VM = $vm
            ScriptType = 'bash'
            ScriptText = 'cat /var/lib/cloud/instance/boot-finished'
            GuestUser = $user
            GuestPassword = $pswd
        }
        $result = Invoke-VMScript @sInvoke
        $result.ScriptOutput
        $notFinished = $false
    }
    catch
    {
        Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff') Exception:</code>tId: $($error[0].Exception.ErrorId)  Category: $($error[0].Exception.ErrorCategory)"
        Write-Verbose "$(Get-Date -Format 'HH:mm:ss.fff')<code>t</code>tLine: $($error[0].InvocationInfo.Line)"
    }
    Start-Sleep -Seconds $sleepTime
}</pre>
<p>&nbsp;</p>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 7 + 11</strong>: In a background job we do not inherit any open connections to a vSphere Server. A script can use the SessionId of an open connection in the calling script, to open a connection, without having to provide credentials.</p>



<p><strong>Line 18</strong>: Before using the call to <a rel="noreferrer noopener" aria-label="Invoke-VMScript (opens in a new tab)" href="https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6fb85470-f6ca-4341-858d-12ffd94d975e/4bee17f3-579b-474e-b51c-898e38cc0abb/doc/Invoke-VMScript.html" target="_blank">Invoke-VMScript</a>, the Wait Job makes sure the VM and the VMware Tools in there, are ready to receive such a call.</p>



<p><strong>Line 28</strong>: Inside the guest OS, the function uses the simple bash expression to test for the presence of a file. The file <strong>/var/lib/cloud/instance/boot-finished</strong> is created by cloud-init when the process completes. </p>



<p><strong>Line 44-52</strong>: The Wait Job function returns the content of the file to the caller.</p>



<h2 class="wp-block-heading">Sample Run</h2>



<p>With the above code, we have everything in place to deploy a <strong>VM</strong> from an <strong>Ubuntu OVA</strong>, with the guest OS configuration is done through cloud-init, based on a <strong>YAML</strong> file.</p>



<p>A typical call could look something like this.</p>



<pre class="lang:ps decode:true  ">$vmName = 'UbuntuBionic'

# Get credential for logging on to the guest OS

$viCred = Get-VICredentialStoreItem -Host $vmName
$secPassword = ConvertTo-SecureString -String $viCred.Password -AsPlainText -Force
$cred = [Management.Automation.PSCredential]::new($viCred.User, $secPassword)

$sCloudInitVM = @{
  OvaFile = '.\bionic-server-cloudimg-amd64.ova'
  VmName = $vmName
  ClusterName = 'cluster'
  DsName = 'vsanDatastore'
  PgName = 'vdPg1'
  CloudConfig = '.\user-data-bionic.yaml'
  Credential = $cred
  Verbose = $true
}
Install-CloudInitVM @sCloudInitVM</pre>


<p>With the <strong>Verbose</strong> switch set to $true, the output of this call would look like this.</p>


<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="763" height="390" src="https://www.lucd.info/wp-content/uploads/2019/12/bionicVM.png" alt="Deployment - Verbose output" class="wp-image-6662" srcset="https://www.lucd.info/wp-content/uploads/2019/12/bionicVM.png 763w, https://www.lucd.info/wp-content/uploads/2019/12/bionicVM-300x153.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/bionicVM-720x368.png 720w" sizes="auto, (max-width: 763px) 100vw, 763px" /></figure>



<p>I highlighted with <mark><span style="background-color:#f78da7" class="tadv-background-color">red</span></mark> boxes the verbose messages from the function. The text in the <span style="background-color:#7bdcb5" class="tadv-background-color">green</span> box is the content of the  <strong>/var/lib/cloud/instance/boot-finished file</strong>.</p>



<p>Do not look too much at the timings. This run happened in my lab environment, which has limited resources. The main reason for showing this is to demonstrate how easy such a cloud-init based deployment can be incorporated in your pipelines.</p>



<p>This concludes Part 1 in the cloud-init series. It provides some functions and scripts to deploy a VM, starting from an OVA file, with the configuration of the guest OS done through cloud-init.</p>



<p>In the upcoming parts in this series, I will show some more advanced Ubuntu deployments, show how cloud-init can be used with Photon and show how you can use these deployed VMs as your '<em>cattle</em>'.</p>



<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
