Skip to content

Instantly share code, notes, and snippets.

@pkutaj
Created February 24, 2021 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkutaj/ea4e57d26ebe8db0ed25da094d84b5e6 to your computer and use it in GitHub Desktop.
Save pkutaj/ea4e57d26ebe8db0ed25da094d84b5e6 to your computer and use it in GitHub Desktop.
refactoredNomad
function get-nomadLogs ([string]$jobId, [string]$featureFlag) {
function get-stderr ($jobId) {
$nomadLogs = nomad alloc logs -stderr -job $jobId
if (nomad status $jobID | Select-String status.*dead) {
Out-Host -InputObject $nomadLogs
}
}
function get-logs($jobId) {
Out-Host -InputObject $nomadLogs
}
function get-logsPaged($jobId) {
Out-Host -InputObject $nomadLogs -Paging
}
function get-logsIntoFile($jobId) {
$slogFolder = "C:\Users\Admin\Documents\workspace\SNOW\SNOW-logs\_posts"
$logFile = "$slogFolder\$jobId.log"
Out-File -InputObject $nomadLogs -FilePath $logFile
Invoke-Item $logFile
}
function get-logsIfDead($jobId) {
#todo: regex to remove ascii escape chars
if (nomad status $jobID | Select-String -Pattern "(?=.*\d)failed") {
Write-Host "Job Failed! Check webUI && stderr below" -ForegroundColor DarkRed
Pause
get-stderr -jobId $jobId
}
elseif (nomad status $jobID | Select-String -Pattern "status.*dead") {
Out-Host -InputObject $nomadLogs
}
else {
nomad status $jobID | Select-String -Pattern "status.*=.*"
}
}
$nomadLogs = nomad alloc logs -job $jobId
switch ($featureFlag) {
"e" { get-stderr -jobId $jobId }
"l" { get-logs -jobId $jobId }
"p" { get-logsPaged -jobId $jobId }
"f" { get-logsIntoFile -jobId $jobId }
Default {
get-logsIfDead -jobId $jobId
}
}
<#
.SYNOPSIS
Function helping to get proper log output from HashiCorp Nomad CLI
.DESCRIPTION
Note that 4 possible flags are used
Default is none — will show logs only if a job is dead; will show stderr if the job has failed
"l" for getting logs no matter what the status is
"p" for getting logs no matter what the status is + in paging form for readability
"e" for getting the output of stderr no matter what the status is
"f" for getting the output into the file (note ASCII escape characters, sorry)
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment