Skip to content

Instantly share code, notes, and snippets.

@jermdavis

jermdavis/up.ps1 Secret

Created September 5, 2021 13:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jermdavis/25655ee9c095d20d15caf42fa3d27ded to your computer and use it in GitHub Desktop.
Save jermdavis/25655ee9c095d20d15caf42fa3d27ded to your computer and use it in GitHub Desktop.
An example of checking license expiry before starting Sitecore docker containers
Param(
[switch]$build = $false,
[switch]$attach = $false
)
function Validate-LicenseData
{
Param (
$EnvironmentFile = ".env",
$EnvironmentKey = "SITECORE_LICENSE"
)
$file = Get-Content $EnvironmentFile -Encoding UTF8
$key = $file | ForEach-Object {
if($_ -imatch "^$EnvironmentKey=.*")
{
return $_.SubString($EnvironmentKey.Length + 1)
}
}
$data = [System.Convert]::FromBase64String($key)
$memory = [System.IO.MemoryStream]::new()
$memory.Write($data, 0, $data.Length)
$memory.Flush()
$memory.Seek(0, [System.IO.SeekOrigin]::Begin) | Out-Null
$gzip = [System.IO.Compression.GZipStream]::new($memory, [System.IO.Compression.CompressionMode]::Decompress)
$s = [System.IO.StreamReader]::new($gzip);
$xml = $s.ReadToEnd()
$s.Dispose();
$gzip.Dispose()
$memory.Dispose();
$xml -match '<expiration>(.*?)</expiration>' | Out-Null
$textExpiry = $Matches[1]
$expiry = [System.DateTime]::ParseExact($textExpiry, "yyyyMMddThhmmss", [System.Globalization.CultureInfo]::InvariantCulture)
if($expiry -lt [System.DateTime]::Now)
{
throw "Your Sitecore license has expired."
}
else
{
$daysLeft = [int]($expiry - [System.DateTime]::Now).TotalDays
Write-Host "You have $daysLeft days left on your license." -ForegroundColor Green
}
}
try
{
pushd ".\docker"
Validate-LicenseData
$buildFlag = ""
if($build)
{
$buildFlag = "--build"
}
Write-Host "Starting: XP=$xp, Build=$build, Attach=$attach"
$detachFlag = "--detach"
if($attach)
{
$detachFlag = ""
}
docker-compose up $buildFlag $detachFlag
}
finally
{
popd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment