Skip to content

Commit

Permalink
Adding Custom Tasks to DevBox
Browse files Browse the repository at this point in the history
  • Loading branch information
Evilazaro committed Mar 27, 2024
1 parent 03b128a commit 755df17
Show file tree
Hide file tree
Showing 15 changed files with 1,350 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Deploy/Bash/DevBox/devCenter/createDevBoxDefinition.sh
Expand Up @@ -15,6 +15,9 @@ galleryName="$5"
imageName="$6"
networkConnectionName="$7"

echo "Updating DevCEnter..."
sleep 15

# Function to create development pools and boxes
createDevPoolsAndDevBoxes() {
local location="$1"
Expand Down
142 changes: 142 additions & 0 deletions Tasks/choco/Choco.ps1
@@ -0,0 +1,142 @@
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string] $Package,

[Parameter()]
[string] $Version,

[Parameter()]
[string] $IgnoreChecksums
)

if (-not $Package) {
throw "Package parameter is mandatory. Please provide a value for the Package parameter."
}

###################################################################################################
#
# PowerShell configurations
#

# Ensure we force use of TLS 1.2 for all downloads.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Expected path of the choco.exe file.
$Choco = "$Env:ProgramData/chocolatey/choco.exe"

###################################################################################################
#
# Functions used in this script.
#

function Ensure-Chocolatey
{
[CmdletBinding()]
param(
[string] $ChocoExePath
)

if (-not (Test-Path "$ChocoExePath"))
{
Set-ExecutionPolicy Bypass -Scope Process -Force
$installScriptPath = [System.IO.Path]::GetTempFileName() + ".ps1"
Invoke-WebRequest -Uri 'https://chocolatey.org/install.ps1' -OutFile $installScriptPath

try {
Execute -File $installScriptPath
} finally {
Remove-Item $installScriptPath
}

if ($LastExitCode -eq 3010)
{
Write-Host 'The recent changes indicate a reboot is necessary. Please reboot at your earliest convenience.'
}
}
}

function Install-Package
{
[CmdletBinding()]
param(
[string] $ChocoExePath,
[string] $Package,
[string] $Version,
[string] $IgnoreChecksums
)

$expression = "$ChocoExePath install $Package"

if ($Version){
$expression = "$expression --version $Version"
}

$expression = "$expression -y -f --acceptlicense --no-progress --stoponfirstfailure"

if ($IgnoreChecksums -eq "true") {
$expression = "$expression --ignorechecksums"
}

$expression = "$expression `nexit `$LASTEXITCODE"

Set-ExecutionPolicy Bypass -Scope Process -Force
$packageScriptPath = [System.IO.Path]::GetTempFileName() + ".ps1"
Set-Content -Value $expression -Path $packageScriptPath
Write-Host "File path $packageScriptPath"

Execute -File $packageScriptPath
Remove-Item $packageScriptPath
}

function Execute
{
[CmdletBinding()]
param(
$File
)

# Note we're calling powershell.exe directly, instead
# of running Invoke-Expression, as suggested by
# https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/avoid-using-invoke-expression?view=powershell-7.3
# Note that this will run powershell.exe
# even if the system has pwsh.exe.
powershell.exe -File $File

# capture the exit code from the process
$processExitCode = $LASTEXITCODE

# This check allows us to capture cases where the command we execute exits with an error code.
# In that case, we do want to throw an exception with whatever is in stderr. Normally, when
# Invoke-Expression throws, the error will come the normal way (i.e. $Error) and pass via the
# catch below.
if ($processExitCode -or $expError)
{
if ($processExitCode -eq 3010)
{
# Expected condition. The recent changes indicate a reboot is necessary. Please reboot at your earliest convenience.
}
elseif ($expError)
{
throw $expError
}
else
{
throw "Installation failed with exit code: $processExitCode. Please see the Chocolatey logs in %ALLUSERSPROFILE%\chocolatey\logs folder for details."
break
}
}
}

###################################################################################################
#
# Main execution block.
#

Write-Host 'Ensuring latest Chocolatey version is installed.'
Ensure-Chocolatey -ChocoExePath "$Choco"

Write-Host "Preparing to install Chocolatey package: $Package."
Install-Package -ChocoExePath "$Choco" -Package $Package -Version $Version -IgnoreChecksums $IgnoreChecksums

Write-Host "`nThe artifact was applied successfully.`n"
39 changes: 39 additions & 0 deletions Tasks/choco/task.yaml
@@ -0,0 +1,39 @@
# This is a Chocolatey package installation task for Dev Box.

$schema: 1.0
name: choco
description: Installs a Chocolatey package.
author: Microsoft Corporation
command: "./Choco.ps1 -Package {{package}} -Version {{version}} -IgnoreChecksums {{ignoreChecksums}}"
parameters:
package:
default: ""
type: string
required: true
description: |
The name of the Chocolatey package to install.
For example, "git".
Visit https://chocolatey.org/packages to learn
more about Chocolatey packages.
version:
default: ""
type: string
required: false
description: The version of the Chocolatey package to install.
ignoreChecksums:
default: false
type: boolean
required: false
description: Whether to ignore checksums when installing the package.
documentation:
notes: This task is used to install a Chocolatey package.
examples:
- name: choco
description: install OracleJDK 17.0.2
parameters:
package: oraclejdk
version: 17.0.2
- name: choco
description: install notepad++
parameters:
package: notepadplusplus
13 changes: 13 additions & 0 deletions Tasks/git-clone/cleanup.ps1
@@ -0,0 +1,13 @@
$CustomizationScriptsDir = "C:\DevBoxCustomizations"
$LockFile = "lockfile"
$SetVariablesScript = "setVariables.ps1"
$RunAsUserScript = "runAsUser.ps1"
$CleanupScript = "cleanup.ps1"
$RunAsUserTask = "DevBoxCustomizations"
$CleanupTask = "DevBoxCustomizationsCleanup"

if (!(Test-Path "$($CustomizationScriptsDir)\$($LockFile)")) {
Unregister-ScheduledTask -TaskName $RunAsUserTask -Confirm:$false
Unregister-ScheduledTask -TaskName $CleanupTask -Confirm:$false
Remove-Item $CustomizationScriptsDir -Force -Recurse
}

0 comments on commit 755df17

Please sign in to comment.