-
Notifications
You must be signed in to change notification settings - Fork 5
Add uploaders of installation script(s) logs #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,14 @@ param ( | |
| # The API URL used to communicate with the SumoLogic backend | ||
| [string] $Api, | ||
|
|
||
| # DisableInstallationTelemetry is used to disable reporting the installation | ||
| # to Sumologic. | ||
| [bool] $DisableInstallationTelemetry, | ||
|
|
||
| # InstallationLogfileEndpoint is used to configure the endpoint where | ||
| # installation logs will be sent. | ||
| [string] $InstallationLogfileEndpoint, | ||
|
|
||
| # The OpAmp Endpoint used to communicate with the OpAmp backend | ||
| [string] $OpAmpApi, | ||
|
|
||
|
|
@@ -346,14 +354,60 @@ function Get-BinaryFromURI { | |
| Write-Host "Downloaded ${Path}" | ||
| } | ||
|
|
||
| ## | ||
| function Send-Installation-Logs { | ||
| param ( | ||
| [Parameter(Mandatory, Position=0)] | ||
| [HttpClient] $HttpClient, | ||
|
|
||
| [Parameter(Mandatory, Position=1)] | ||
| [string] $Endpoint, | ||
|
|
||
| [Parameter(Mandatory, Position=2)] | ||
| [string] $Path | ||
| ) | ||
|
|
||
| $Content = Get-Content -Path $Path | ||
| $StringContent = [System.Net.Http.StringContent]::new($Content) | ||
|
|
||
| $response = $HttpClient.PostAsync($Endpoint, $StringContent).GetAwaiter().GetResult() | ||
| if (!($response.IsSuccessStatusCode)) { | ||
| $statusCode = [int]$response.StatusCode | ||
| $reasonPhrase = $response.StatusCode.ToString() | ||
| $errMsg = "${statusCode} ${reasonPhrase}" | ||
|
|
||
| if ($response.Content -ne $null) { | ||
| $content = $response.Content.ReadAsStringAsync().GetAwaiter().GetResult() | ||
| $errMsg += ": ${content}" | ||
| } | ||
|
|
||
| } | ||
| }# | ||
| # | ||
| # Main code | ||
| ## | ||
|
|
||
| try { | ||
| $InstallationLogFile = New-TemporaryFile | ||
|
|
||
| if ($InstallationLogFileEndpoint -eq "") { | ||
| $InstallationLogFileEndpoint = "https://open-collectors.sumologic.com/api/v1/collector/installation/logs" | ||
| } | ||
|
|
||
| Start-Transcript $InstallationLogFile | Out-Null | ||
|
|
||
| $handler = New-Object HttpClientHandler | ||
| $handler.AllowAutoRedirect = $true | ||
|
|
||
| $httpClient = New-Object System.Net.Http.HttpClient($handler) | ||
| $userAgentHeader = New-Object System.Net.Http.Headers.ProductInfoHeaderValue("otelcol-sumo-installer", "0.1") | ||
| $httpClient.DefaultRequestHeaders.UserAgent.Add($userAgentHeader) | ||
|
|
||
| # set http client timeout to 30 seconds | ||
| $httpClient.Timeout = New-Object System.TimeSpan(0, 0, 30) | ||
|
|
||
| if ($InstallationToken -eq $null -or $InstallationToken -eq "") { | ||
| Write-Error "Installation token has not been provided. Please set the SUMOLOGIC_INSTALLATION_TOKEN environment variable." -ErrorAction Stop | ||
| } | ||
| } | ||
|
|
||
| $osName = Get-OSName | ||
| Write-Host "Detected OS type:`t${osName}" | ||
|
|
@@ -373,16 +427,6 @@ try { | |
| Write-Host "Architecture overridden: `t${archName}" | ||
| } | ||
|
|
||
| $handler = New-Object HttpClientHandler | ||
| $handler.AllowAutoRedirect = $true | ||
|
|
||
| $httpClient = New-Object System.Net.Http.HttpClient($handler) | ||
| $userAgentHeader = New-Object System.Net.Http.Headers.ProductInfoHeaderValue("otelcol-sumo-installer", "0.1") | ||
| $httpClient.DefaultRequestHeaders.UserAgent.Add($userAgentHeader) | ||
|
|
||
| # set http client timeout to 30 seconds | ||
| $httpClient.Timeout = New-Object System.TimeSpan(0, 0, 30) | ||
|
|
||
| if ($Fips -eq $true) { | ||
| if ($osName -ne "Win32NT" -or $archName -ne "x64") { | ||
| Write-Error "Error: The FIPS-approved binary is only available for windows/amd64" -ErrorAction Stop | ||
|
|
@@ -473,6 +517,17 @@ try { | |
| msiexec.exe /i "$msiPath" /passive $msiProperties | ||
| } catch [HttpRequestException] { | ||
| Write-Error $_.Exception.InnerException.Message -ErrorAction Stop | ||
| } finally { | ||
| Stop-Transcript | Out-Null | ||
| if ($InstallationToken -eq $true) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not too familiar with powershell, i'm assuming this line means if installation token equals to true? is installation token a boolean? |
||
| Add-Content -Path $InstallationLogFile.FullName -Value $InstallationToken | ||
| } | ||
| if ($DisableInstallationTelemetry -eq $false) { | ||
| Send-Installation-Logs -Endpoint $InstallationLogFileEndpoint -Path $InstallationLogFile -HttpClient $httpClient | ||
| } | ||
|
|
||
| Remove-Item $InstallationLogFile | ||
|
|
||
| } | ||
|
|
||
| Write-Host "Installation successful" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,10 @@ ARG_SHORT_CLOBBER='C' | |
| ARG_LONG_CLOBBER='clobber' | ||
| ARG_SHORT_PACKAGE_PATH='P' | ||
| ARG_LONG_PACKAGE_PATH='package-path' | ||
| ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY='S' | ||
| ARG_LONG_DISABLE_INSTALLATION_TELEMETRY='disable-installation-telemetry' | ||
| ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT='l' | ||
| ARG_LONG_INSTALLATION_LOGFILE_ENDPOINT='installation-logfile-endpoint' | ||
|
|
||
| readonly ARG_SHORT_TOKEN ARG_LONG_TOKEN ARG_SHORT_HELP ARG_LONG_HELP ARG_SHORT_API ARG_LONG_API | ||
| readonly ARG_SHORT_TAG ARG_LONG_TAG ARG_SHORT_VERSION ARG_LONG_VERSION ARG_SHORT_YES ARG_LONG_YES | ||
|
|
@@ -73,6 +77,8 @@ readonly ARG_SHORT_TIMEZONE ARG_LONG_TIMEZONE | |
| readonly ARG_SHORT_CLOBBER ARG_LONG_CLOBBER | ||
| readonly ARG_SHORT_PACKAGE_PATH ARG_LONG_PACKAGE_PATH | ||
| readonly DEPRECATED_ARG_LONG_TOKEN DEPRECATED_ENV_TOKEN DEPRECATED_ARG_LONG_SKIP_TOKEN | ||
| readonly ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY ARG_LONG_DISABLE_INSTALLATION_TELEMETRY | ||
| readonly ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT ARG_LONG_INSTALLATION_LOGFILE_ENDPOINT | ||
|
|
||
| ############################ Variables (see set_defaults function for default values) | ||
|
|
||
|
|
@@ -138,6 +144,11 @@ PACKAGECLOUD_MASTER_TOKEN="${PACKAGECLOUD_MASTER_TOKEN:-}" | |
| # built and not the latest package uploaded to S3. | ||
| DARWIN_PKG_URL="${DARWIN_PKG_URL:-}" | ||
|
|
||
|
|
||
| DISABLE_INSTALLATION_TELEMETRY=false | ||
| INSTALLATION_LOGFILE="${TMPDIR:=/tmp}/sumologic-otel-collector_installation.log" | ||
| INSTALLATION_LOGFILE_ENDPOINT='https://open-collectors.sumologic.com/api/v1/collector/installation/logs' | ||
|
|
||
| ############################ Functions | ||
|
|
||
| function usage() { | ||
|
|
@@ -173,6 +184,7 @@ Supported arguments: | |
| -${ARG_SHORT_CLOBBER}, --${ARG_LONG_CLOBBER} Overwrite existing installation without asking for confirmation. | ||
| -${ARG_SHORT_PACKAGE_PATH}, --${ARG_LONG_PACKAGE_PATH} <path> Install package from file path instead of fetching it. | ||
| -${ARG_SHORT_YES}, --${ARG_LONG_YES} Disable confirmation asks. | ||
| -${ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY}, --${ARG_LONG_DISABLE_INSTALLATION_TELEMETRY} Do not report installation logs to Sumologic. | ||
|
|
||
| -${ARG_SHORT_HELP}, --${ARG_LONG_HELP} Prints this help and usage. | ||
|
|
||
|
|
@@ -181,6 +193,19 @@ Supported env variables: | |
| EOF | ||
| } | ||
|
|
||
| # Don't warn about unreachable commands in this function | ||
| # ShellCheck may incorrectly believe that code is unreachable if it's invoked in | ||
| # a trap, like the reporter function. | ||
| # shellcheck disable=SC2317,SC2119,SC2120,SC2317,SC2329,SC1072,SC1073,SC1125 | ||
| function reporter { | ||
| if ! $DISABLE_INSTALLATION_TELEMETRY; then | ||
| echo "SUMOLOGIC_INSTALLATION_TOKEN=${SUMOLOGIC_INSTALLATION_TOKEN}" >> "$INSTALLATION_LOGFILE" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is writing the installation token to the log file to i saw in the repo i was looking at what mktemp is, and it creates a secure temporary file. would using something like that be better? |
||
| curl --silent --location -X POST --data-binary @"${INSTALLATION_LOGFILE}" "${INSTALLATION_LOGFILE_ENDPOINT}" | ||
| rm -f "${INSTALLATION_LOGFILE}" | ||
| fi | ||
| } | ||
| trap reporter EXIT | ||
|
|
||
| function set_defaults() { | ||
| DOWNLOAD_CACHE_DIR="/var/cache/otelcol-sumo" # this is in case we want to keep downloaded binaries | ||
| CONFIG_DIRECTORY="/etc/otelcol-sumo" | ||
|
|
@@ -269,7 +294,7 @@ function parse_options() { | |
| "--${ARG_LONG_TIMEOUT}") | ||
| set -- "$@" "-${ARG_SHORT_TIMEOUT}" | ||
| ;; | ||
| "-${ARG_SHORT_TOKEN}"|"-${ARG_SHORT_HELP}"|"-${ARG_SHORT_API}"|"-${ARG_SHORT_OPAMP_API}"|"-${ARG_SHORT_TAG}"|"-${ARG_SHORT_VERSION}"|"-${ARG_SHORT_FIPS}"|"-${ARG_SHORT_YES}"|"-${ARG_SHORT_UNINSTALL}"|"-${ARG_SHORT_UPGRADE}"|"-${ARG_SHORT_PURGE}"|"-${ARG_SHORT_SKIP_TOKEN}"|"-${ARG_SHORT_DOWNLOAD}"|"-${ARG_SHORT_CONFIG_BRANCH}"|"-${ARG_SHORT_BINARY_BRANCH}"|"-${ARG_SHORT_BRANCH}"|"-${ARG_SHORT_KEEP_DOWNLOADS}"|"-${ARG_SHORT_TIMEOUT}"|"-${ARG_SHORT_INSTALL_HOSTMETRICS}"|"-${ARG_SHORT_REMOTELY_MANAGED}"|"-${ARG_SHORT_EPHEMERAL}"|"-${ARG_SHORT_TIMEZONE}"|"-${ARG_SHORT_CLOBBER}"|"-${ARG_SHORT_PACKAGE_PATH}") | ||
| "-${ARG_SHORT_TOKEN}"|"-${ARG_SHORT_HELP}"|"-${ARG_SHORT_API}"|"-${ARG_SHORT_OPAMP_API}"|"-${ARG_SHORT_TAG}"|"-${ARG_SHORT_VERSION}"|"-${ARG_SHORT_FIPS}"|"-${ARG_SHORT_YES}"|"-${ARG_SHORT_UNINSTALL}"|"-${ARG_SHORT_UPGRADE}"|"-${ARG_SHORT_PURGE}"|"-${ARG_SHORT_SKIP_TOKEN}"|"-${ARG_SHORT_DOWNLOAD}"|"-${ARG_SHORT_CONFIG_BRANCH}"|"-${ARG_SHORT_BINARY_BRANCH}"|"-${ARG_SHORT_BRANCH}"|"-${ARG_SHORT_KEEP_DOWNLOADS}"|"-${ARG_SHORT_TIMEOUT}"|"-${ARG_SHORT_INSTALL_HOSTMETRICS}"|"-${ARG_SHORT_REMOTELY_MANAGED}"|"-${ARG_SHORT_EPHEMERAL}"|"-${ARG_SHORT_TIMEZONE}"|"-${ARG_SHORT_CLOBBER}"|"-${ARG_SHORT_PACKAGE_PATH}"|"-${ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY}"|"-${ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT}") | ||
| set -- "$@" "${arg}" | ||
| ;; | ||
| "--${ARG_LONG_INSTALL_HOSTMETRICS}") | ||
|
|
@@ -290,6 +315,12 @@ function parse_options() { | |
| "--${ARG_LONG_PACKAGE_PATH}") | ||
| set -- "$@" "-${ARG_SHORT_PACKAGE_PATH}" | ||
| ;; | ||
| "--${ARG_LONG_DISABLE_INSTALLATION_TELEMETRY}") | ||
| set -- "$@" "-${ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY}" | ||
| ;; | ||
| "--${ARG_LONG_INSTALLATION_LOGFILE_ENDPOINT}") | ||
| set -- "$@" "-${ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT}" | ||
| ;; | ||
| -*) | ||
| echo "Unknown option ${arg}"; usage; exit 2 ;; | ||
| *) | ||
|
|
@@ -302,7 +333,7 @@ function parse_options() { | |
|
|
||
| while true; do | ||
| set +e | ||
| getopts "${ARG_SHORT_HELP}${ARG_SHORT_TOKEN}:${ARG_SHORT_API}:${ARG_SHORT_OPAMP_API}:${ARG_SHORT_TAG}:${ARG_SHORT_VERSION}:${ARG_SHORT_FIPS}${ARG_SHORT_YES}${ARG_SHORT_UPGRADE}${ARG_SHORT_UNINSTALL}${ARG_SHORT_PURGE}${ARG_SHORT_SKIP_TOKEN}${ARG_SHORT_DOWNLOAD}${ARG_SHORT_KEEP_DOWNLOADS}${ARG_SHORT_CONFIG_BRANCH}:${ARG_SHORT_BINARY_BRANCH}:${ARG_SHORT_BRANCH}:${ARG_SHORT_EPHEMERAL}${ARG_SHORT_TIMEZONE}:${ARG_SHORT_CLOBBER}${ARG_SHORT_REMOTELY_MANAGED}${ARG_SHORT_INSTALL_HOSTMETRICS}${ARG_SHORT_TIMEOUT}:${ARG_SHORT_PACKAGE_PATH}:" opt | ||
| getopts "${ARG_SHORT_HELP}${ARG_SHORT_TOKEN}:${ARG_SHORT_API}:${ARG_SHORT_OPAMP_API}:${ARG_SHORT_TAG}:${ARG_SHORT_VERSION}:${ARG_SHORT_FIPS}${ARG_SHORT_YES}${ARG_SHORT_UPGRADE}${ARG_SHORT_UNINSTALL}${ARG_SHORT_PURGE}${ARG_SHORT_SKIP_TOKEN}${ARG_SHORT_DOWNLOAD}${ARG_SHORT_KEEP_DOWNLOADS}${ARG_SHORT_CONFIG_BRANCH}:${ARG_SHORT_BINARY_BRANCH}:${ARG_SHORT_BRANCH}:${ARG_SHORT_EPHEMERAL}${ARG_SHORT_TIMEZONE}:${ARG_SHORT_CLOBBER}${ARG_SHORT_REMOTELY_MANAGED}${ARG_SHORT_INSTALL_HOSTMETRICS}${ARG_SHORT_TIMEOUT}:${ARG_SHORT_PACKAGE_PATH}:${ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY}${ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT}:" opt | ||
| set -e | ||
|
|
||
| # Invalid argument catched, print and exit | ||
|
|
@@ -314,7 +345,7 @@ function parse_options() { | |
|
|
||
| # Validate opt and set arguments | ||
| case "$opt" in | ||
| "${ARG_SHORT_HELP}") usage; exit 0 ;; | ||
| "${ARG_SHORT_HELP}") usage; DISABLE_INSTALLATION_TELEMETRY=true exit 0 ;; | ||
| "${ARG_SHORT_TOKEN}") SUMOLOGIC_INSTALLATION_TOKEN="${OPTARG}" ;; | ||
| "${ARG_SHORT_API}") API_BASE_URL="${OPTARG}" ;; | ||
| "${ARG_SHORT_OPAMP_API}") OPAMP_API_URL="${OPTARG}" ;; | ||
|
|
@@ -339,6 +370,8 @@ function parse_options() { | |
| "${ARG_SHORT_EPHEMERAL}") EPHEMERAL=true ;; | ||
| "${ARG_SHORT_TIMEZONE}") TIMEZONE="${OPTARG}" ;; | ||
| "${ARG_SHORT_CLOBBER}") CLOBBER=true ;; | ||
| "${ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY}") DISABLE_INSTALLATION_TELEMETRY=true ;; | ||
| "${ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT}") INSTALLATION_LOGFILE_ENDPOINT="${OPTARG}" ;; | ||
| "${ARG_SHORT_KEEP_DOWNLOADS}") KEEP_DOWNLOADS=true ;; | ||
| "${ARG_SHORT_TIMEOUT}") CURL_MAX_TIME="${OPTARG}" ;; | ||
| "${ARG_SHORT_TAG}") FIELDS+=("${OPTARG}") ;; | ||
|
|
@@ -1134,6 +1167,10 @@ function get_user_token() { | |
|
|
||
| ############################ Main code | ||
|
|
||
| # Redirect a copy of stdout and stderr into $INSTALLATION_LOGFILE | ||
| exec > >(tee "${INSTALLATION_LOGFILE}") 2>&1 | ||
|
|
||
|
|
||
| if [ "${S3_BUCKET}" = "sumologic-osc-stable" ]; then | ||
| DOWNLOAD_URI="$CDN_URI" | ||
| else | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this continue to run even after the error message pops up? should the error message get thrown? i saw
write-erroron line 408: