Skip to content
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

Add upload of installation script logs #1312

Closed
wants to merge 13 commits into from
66 changes: 60 additions & 6 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ param (
# Fips is used to download a fips binary installer.
[bool] $Fips,

# 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,

# Specifies wether or not remote management is enabled
[bool] $RemotelyManaged,

Expand Down Expand Up @@ -386,19 +394,48 @@ 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}"
}

Write-Error $errMsg -ErrorAction Stop
}
}

##
# Main code
##

try {
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
$InstallationLogFile = New-TemporaryFile

if ($InstallationLogFileEndpoint -eq "") {
$InstallationLogFileEndpoint = "https://stag-open-events.sumologic.net/api/v1/collector/installation/logs"
}

$osName = Get-OSName
$archName = Get-ArchName
Write-Host "Detected OS type:`t${osName}"
Write-Host "Detected architecture:`t${archName}"
Start-Transcript $InstallationLogFile | Out-Null

$handler = New-Object HttpClientHandler
$handler.AllowAutoRedirect = $true
Expand All @@ -410,6 +447,15 @@ try {
# 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
$archName = Get-ArchName
Write-Host "Detected OS type:`t${osName}"
Write-Host "Detected architecture:`t${archName}"

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"
Expand Down Expand Up @@ -516,4 +562,12 @@ try {
msiexec.exe /i "$msiPath" /passive $msiProperties
} catch [HttpRequestException] {
Write-Error $_.Exception.InnerException.Message
} finally {
Stop-Transcript | Out-Null

if ($DisableInstallationTelemetry -eq $false) {
Send-Installation-Logs -Endpoint $InstallationLogFileEndpoint -Path $InstallationLogFile -HttpClient $httpClient
}

Remove-Item $InstallationLogFile
}
38 changes: 35 additions & 3 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ ARG_SHORT_EPHEMERAL='E'
ARG_LONG_EPHEMERAL='ephemeral'
ARG_SHORT_TIMEOUT='m'
ARG_LONG_TIMEOUT='download-timeout'
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'

PACKAGE_GITHUB_ORG="SumoLogic"
PACKAGE_GITHUB_REPO="sumologic-otel-collector-packaging"
Expand All @@ -65,6 +69,8 @@ readonly ARG_SHORT_INSTALL_HOSTMETRICS ARG_LONG_INSTALL_HOSTMETRICS
readonly ARG_SHORT_REMOTELY_MANAGED ARG_LONG_REMOTELY_MANAGED
readonly ARG_SHORT_EPHEMERAL ARG_LONG_EPHEMERAL
readonly ARG_SHORT_TIMEOUT ARG_LONG_TIMEOUT
readonly ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY ARG_LONG_DISABLE_INSTALLATION_TELEMETRY
readonly ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT ARG_LONG_INSTALLATION_LOGFILE_ENDPOINT
readonly DEPRECATED_ARG_LONG_TOKEN DEPRECATED_ENV_TOKEN DEPRECATED_ARG_LONG_SKIP_TOKEN
readonly PACKAGE_GITHUB_ORG PACKAGE_GITHUB_REPO

Expand Down Expand Up @@ -130,6 +136,10 @@ SYSTEMD_DISABLED=false
# alternative commands
TAC="tac"

DISABLE_INSTALLATION_TELEMETRY=false
INSTALLATION_LOGFILE="/tmp/sumologic-otel-collector_installation.log"
INSTALLATION_LOGFILE_ENDPOINT='https://stag-open-events.sumologic.net/api/v1/collector/installation/logs'

############################ Functions

function usage() {
Expand Down Expand Up @@ -162,6 +172,7 @@ Supported arguments:
-${ARG_SHORT_EPHEMERAL}, --${ARG_LONG_EPHEMERAL} Delete the collector from Sumo Logic after 12 hours of inactivity.
-${ARG_SHORT_TIMEOUT}, --${ARG_LONG_TIMEOUT} <timeout> Timeout in seconds after which download will fail. Default is ${CURL_MAX_TIME}.
-${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.

Expand All @@ -170,6 +181,16 @@ Supported env variables:
EOF
}

function reporter {
if ! $DISABLE_INSTALLATION_TELEMETRY; then
HASHED_INSTALLATION_TOKEN=$(echo -n "${SUMOLOGIC_INSTALLATION_TOKEN}" | shasum -a 256 | cut -d ' ' -f1)
curl --silent --location -X POST \
--header "X-Sumo-Name:$HASHED_INSTALLATION_TOKEN" \
--data-binary @"${INSTALLATION_LOGFILE}" "${INSTALLATION_LOGFILE_ENDPOINT}"
fi
}
trap reporter EXIT

function set_defaults() {
HOME_DIRECTORY="/var/lib/otelcol-sumo"
FILE_STORAGE="${HOME_DIRECTORY}/file_storage"
Expand Down Expand Up @@ -264,7 +285,7 @@ function parse_options() {
"--${ARG_LONG_TIMEOUT}")
set -- "$@" "-${ARG_SHORT_TIMEOUT}"
;;
"-${ARG_SHORT_TOKEN}"|"-${ARG_SHORT_HELP}"|"-${ARG_SHORT_API}"|"-${ARG_SHORT_TAG}"|"-${ARG_SHORT_SKIP_CONFIG}"|"-${ARG_SHORT_VERSION}"|"-${ARG_SHORT_FIPS}"|"-${ARG_SHORT_YES}"|"-${ARG_SHORT_SKIP_SYSTEMD}"|"-${ARG_SHORT_UNINSTALL}"|"-${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_TOKEN}"|"-${ARG_SHORT_HELP}"|"-${ARG_SHORT_API}"|"-${ARG_SHORT_TAG}"|"-${ARG_SHORT_SKIP_CONFIG}"|"-${ARG_SHORT_VERSION}"|"-${ARG_SHORT_FIPS}"|"-${ARG_SHORT_YES}"|"-${ARG_SHORT_SKIP_SYSTEMD}"|"-${ARG_SHORT_UNINSTALL}"|"-${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_DISABLE_INSTALLATION_TELEMETRY}"|"-${ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT}")
set -- "$@" "${arg}"
;;
"--${ARG_LONG_INSTALL_HOSTMETRICS}")
Expand All @@ -276,6 +297,12 @@ function parse_options() {
"--${ARG_LONG_EPHEMERAL}")
set -- "$@" "-${ARG_SHORT_EPHEMERAL}"
;;
"--${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 1 ;;
*)
Expand All @@ -288,7 +315,7 @@ function parse_options() {

while true; do
set +e
getopts "${ARG_SHORT_HELP}${ARG_SHORT_TOKEN}:${ARG_SHORT_API}:${ARG_SHORT_TAG}:${ARG_SHORT_VERSION}:${ARG_SHORT_FIPS}${ARG_SHORT_YES}${ARG_SHORT_SKIP_SYSTEMD}${ARG_SHORT_UNINSTALL}${ARG_SHORT_PURGE}${ARG_SHORT_SKIP_TOKEN}${ARG_SHORT_SKIP_CONFIG}${ARG_SHORT_DOWNLOAD}${ARG_SHORT_KEEP_DOWNLOADS}${ARG_SHORT_CONFIG_BRANCH}:${ARG_SHORT_BINARY_BRANCH}:${ARG_SHORT_BRANCH}:${ARG_SHORT_EPHEMERAL}${ARG_SHORT_REMOTELY_MANAGED}${ARG_SHORT_INSTALL_HOSTMETRICS}${ARG_SHORT_TIMEOUT}:" opt
getopts "${ARG_SHORT_HELP}${ARG_SHORT_TOKEN}:${ARG_SHORT_API}:${ARG_SHORT_TAG}:${ARG_SHORT_VERSION}:${ARG_SHORT_FIPS}${ARG_SHORT_YES}${ARG_SHORT_SKIP_SYSTEMD}${ARG_SHORT_UNINSTALL}${ARG_SHORT_PURGE}${ARG_SHORT_SKIP_TOKEN}${ARG_SHORT_SKIP_CONFIG}${ARG_SHORT_DOWNLOAD}${ARG_SHORT_KEEP_DOWNLOADS}${ARG_SHORT_CONFIG_BRANCH}:${ARG_SHORT_BINARY_BRANCH}:${ARG_SHORT_BRANCH}:${ARG_SHORT_EPHEMERAL}${ARG_SHORT_REMOTELY_MANAGED}${ARG_SHORT_INSTALL_HOSTMETRICS}${ARG_SHORT_TIMEOUT}:${ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY}${ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT}:" opt
set -e

# Invalid argument catched, print and exit
Expand All @@ -300,7 +327,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_SKIP_CONFIG}") SKIP_CONFIG=true ;;
Expand All @@ -326,6 +353,8 @@ function parse_options() {
"${ARG_SHORT_EPHEMERAL}") EPHEMERAL=true ;;
"${ARG_SHORT_KEEP_DOWNLOADS}") KEEP_DOWNLOADS=true ;;
"${ARG_SHORT_TIMEOUT}") CURL_MAX_TIME="${OPTARG}" ;;
"${ARG_SHORT_DISABLE_INSTALLATION_TELEMETRY}") DISABLE_INSTALLATION_TELEMETRY=true ;;
"${ARG_SHORT_INSTALLATION_LOGFILE_ENDPOINT}") INSTALLATION_LOGFILE_ENDPOINT="${OPTARG}" ;;
"${ARG_SHORT_TAG}")
if [[ "${OPTARG}" != ?*"="* ]]; then
echo "Invalid tag: '${OPTARG}'. Should be in 'key=value' format"
Expand Down Expand Up @@ -1736,6 +1765,9 @@ function plutil_replace_key() {

############################ Main code

# Redirect a copy of stdout and stderr into $INSTALLATION_LOGFILE
exec > >(tee "${INSTALLATION_LOGFILE}") 2>&1

OS_TYPE="$(get_os_type)"
ARCH_TYPE="$(get_arch_type)"
readonly OS_TYPE ARCH_TYPE
Expand Down