diff --git a/.gitignore b/.gitignore index 268fce1d..11530c4c 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ UpgradeLog*.htm wix/packages msi/wix/packages msi/SumoLogic.wixext/packages +install-script/test/sumologic_scripts_tests.test diff --git a/install-script/install.sh b/install-script/install.sh index 02eaff61..246cf236 100755 --- a/install-script/install.sh +++ b/install-script/install.sh @@ -11,6 +11,8 @@ ARG_SHORT_HELP='h' ARG_LONG_HELP='help' ARG_SHORT_API='a' ARG_LONG_API='api' +ARG_SHORT_OPAMP_API='o' +ARG_LONG_OPAMP_API='opamp-api' ARG_SHORT_TAG='t' ARG_LONG_TAG='tag' ARG_SHORT_VERSION='v' @@ -81,6 +83,7 @@ fi set -u API_BASE_URL="" +OPAMP_API_URL="" FIELDS="" VERSION="" FIPS=false @@ -107,6 +110,7 @@ LAUNCHD_ENV_KEY="" LAUNCHD_TOKEN_KEY="" USER_API_URL="" +USER_OPAMP_API_URL="" USER_TOKEN="" USER_FIELDS="" @@ -132,7 +136,7 @@ SYSTEMD_DISABLED=false function usage() { cat << EOF -Usage: bash install.sh [--${ARG_LONG_TOKEN} ] [--${ARG_LONG_TAG} = [ --${ARG_LONG_TAG} ...]] [--${ARG_LONG_API} ] [--${ARG_LONG_VERSION} ] \\ +Usage: bash install.sh [--${ARG_LONG_TOKEN} ] [--${ARG_LONG_TAG} = [ --${ARG_LONG_TAG} ...]] [--${ARG_LONG_API} ] [--${ARG_LONG_OPAMP_API} ] [--${ARG_LONG_VERSION} ] \\ [--${ARG_LONG_YES}] [--${ARG_LONG_VERSION} ] [--${ARG_LONG_HELP}] Supported arguments: @@ -148,7 +152,8 @@ Supported arguments: -${ARG_SHORT_PURGE}, --${ARG_LONG_PURGE} It has to be used with '--${ARG_LONG_UNINSTALL}'. It removes all Sumo Logic Distribution for OpenTelemetry Collector related configuration and data. - -${ARG_SHORT_API}, --${ARG_LONG_API} Api URL + -${ARG_SHORT_API}, --${ARG_LONG_API} API URL, forces the collector to use non-default API + -${ARG_SHORT_OPAMP_API}, --${ARG_LONG_OPAMP_API} OpAmp API URL, forces the collector to use non-default OpAmp API -${ARG_SHORT_SKIP_SYSTEMD}, --${ARG_LONG_SKIP_SYSTEMD} Do not install systemd unit. -${ARG_SHORT_SKIP_CONFIG}, --${ARG_LONG_SKIP_CONFIG} Do not create default configuration. -${ARG_SHORT_VERSION}, --${ARG_LONG_VERSION} Version of Sumo Logic Distribution for OpenTelemetry Collector to install, e.g. 0.57.2-sumo-1. @@ -209,6 +214,9 @@ function parse_options() { echo "--${DEPRECATED_ARG_LONG_TOKEN}" is deprecated. Please use "--${ARG_LONG_TOKEN}" instead. set -- "$@" "-${ARG_SHORT_TOKEN}" ;; + "--${ARG_LONG_OPAMP_API}") + set -- "$@" "-${ARG_SHORT_OPAMP_API}" + ;; "--${ARG_LONG_API}") set -- "$@" "-${ARG_SHORT_API}" ;; @@ -261,7 +269,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_OPAMP_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}") set -- "$@" "${arg}" ;; "--${ARG_LONG_INSTALL_HOSTMETRICS}") @@ -285,7 +293,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_OPAMP_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 set -e # Invalid argument catched, print and exit @@ -300,6 +308,7 @@ function parse_options() { "${ARG_SHORT_HELP}") usage; exit 0 ;; "${ARG_SHORT_TOKEN}") SUMOLOGIC_INSTALLATION_TOKEN="${OPTARG}" ;; "${ARG_SHORT_API}") API_BASE_URL="${OPTARG}" ;; + "${ARG_SHORT_OPAMP_API}") OPAMP_API_URL="${OPTARG}" ;; "${ARG_SHORT_SKIP_CONFIG}") SKIP_CONFIG=true ;; "${ARG_SHORT_VERSION}") VERSION="${OPTARG}" ;; "${ARG_SHORT_FIPS}") FIPS=true ;; @@ -681,7 +690,7 @@ function setup_config() { mkdir -p "${REMOTE_CONFIG_DIRECTORY}" write_sumologic_extension "${CONFIG_PATH}" "${INDENTATION}" - write_opamp_extension "${CONFIG_PATH}" "${REMOTE_CONFIG_DIRECTORY}" "${INDENTATION}" "${EXT_INDENTATION}" "${API_BASE_URL}" + write_opamp_extension "${CONFIG_PATH}" "${REMOTE_CONFIG_DIRECTORY}" "${INDENTATION}" "${EXT_INDENTATION}" "${OPAMP_API_URL}" if [[ -n "${SUMOLOGIC_INSTALLATION_TOKEN}" && "${SYSTEMD_DISABLED}" == "true" ]]; then write_installation_token "${SUMOLOGIC_INSTALLATION_TOKEN}" "${CONFIG_PATH}" "${EXT_INDENTATION}" @@ -695,6 +704,10 @@ function setup_config() { write_api_url "${API_BASE_URL}" "${CONFIG_PATH}" "${EXT_INDENTATION}" fi + if [[ -n "${OPAMP_API_URL}" ]]; then + write_opamp_endpoint "${OPAMP_API_URL}" "${CONFIG_PATH}" "${EXT_INDENTATION}" + fi + if [[ -n "${FIELDS}" ]]; then write_tags "${FIELDS}" "${CONFIG_PATH}" "${INDENTATION}" "${EXT_INDENTATION}" fi @@ -751,6 +764,11 @@ function setup_config() { write_api_url "${API_BASE_URL}" "${COMMON_CONFIG_PATH}" "${EXT_INDENTATION}" fi + # fill in opamp url + if [[ -n "${OPAMP_API_URL}" && -z "${USER_OPAMP_API_URL}" ]]; then + write_opamp_extension "${CONFIG_PATH}" "${REMOTE_CONFIG_DIRECTORY}" "${INDENTATION}" "${EXT_INDENTATION}" "${OPAMP_API_URL}" + fi + if [[ -n "${FIELDS}" && -z "${USER_FIELDS}" ]]; then write_tags "${FIELDS}" "${COMMON_CONFIG_PATH}" "${INDENTATION}" "${EXT_INDENTATION}" fi @@ -796,7 +814,7 @@ function setup_config_darwin() { echo -e "Creating remote configurations directory (${REMOTE_CONFIG_DIRECTORY})" mkdir -p "${REMOTE_CONFIG_DIRECTORY}" - write_opamp_extension "${config_path}" "${REMOTE_CONFIG_DIRECTORY}" "${INDENTATION}" "${EXT_INDENTATION}" "${API_BASE_URL}" + write_opamp_extension "${config_path}" "${REMOTE_CONFIG_DIRECTORY}" "${INDENTATION}" "${EXT_INDENTATION}" "${OPAMP_API_URL}" write_remote_config_launchd "${LAUNCHD_CONFIG}" @@ -1092,6 +1110,25 @@ function get_user_api_url() { || echo "" } +function get_user_opamp_endpoint() { + local file + readonly file="${1}" + + if [[ ! -f "${file}" ]]; then + return + fi + + # extract endpoint and strip quotes + grep -m 1 endpoint "${file}" \ + | sed 's/.*endpoint:[[:blank:]]*//' \ + | sed 's/[[:blank:]]*$//' \ + | sed 's/^"//' \ + | sed "s/^'//" \ + | sed 's/"$//' \ + | sed "s/'\$//" \ + || echo "" +} + function get_user_tags() { local file readonly file="${1}" @@ -1316,6 +1353,27 @@ function write_api_url() { fi } +# write opamp endpoint to user configuration file +function write_opamp_endpoint() { + local opamp_endpoint + readonly opamp_endpoint="${1}" + + local file + readonly file="${2}" + + local ext_indentation + readonly ext_indentation="${3}" + + # ToDo: ensure we override only sumologic `api_base_url` + if grep "endpoint" "${file}" > /dev/null; then + sed -i.bak -e "s/endpoint:.*$/endpoint: $(escape_sed "${opamp_endpoint}")/" "${file}" + else + # write endpoint on the top of sumologic: opamp: extension + sed -i.bak -e "1,/opamp:/ s/opamp:/opamp:\\ +\\${ext_indentation}endpoint: $(escape_sed "${opamp_endpoint}")/" "${file}" + fi +} + # write tags to user configuration file function write_tags() { local fields @@ -1380,18 +1438,11 @@ ${indentation}opamp:/" "${file}" # if a different base url is specified, configure the corresponding opamp endpoint if [[ -n "${api_url}" ]]; then - wss_url=${api_url/https:/"wss:"} - wss_url=${wss_url%/} - wss_url=${wss_url/open-events/"opamp-events"} - wss_url=${wss_url/open-collectors/"opamp-collectors"} - wss_url=${wss_url/\.net/".net/v1/opamp"} - wss_url=${wss_url/\.com/".com/v1/opamp"} - if grep "endpoint: wss:" "${file}" > /dev/null; then - sed -i.bak -e "s/endpoint: wss:.*$/endpoint: $(escape_sed "${wss_url}")/" "${file}" + sed -i.bak -e "s/endpoint: wss:.*$/endpoint: $(escape_sed "${api_url}")/" "${file}" else sed -i.bak -e "s/opamp:/opamp:\\ -\\${ext_indentation}endpoint: $(escape_sed "${wss_url}")/" "${file}" +\\${ext_indentation}endpoint: $(escape_sed "${api_url}")/" "${file}" fi fi @@ -1712,7 +1763,7 @@ set_tmpdir install_missing_dependencies check_dependencies -readonly SUMOLOGIC_INSTALLATION_TOKEN API_BASE_URL FIELDS CONTINUE FILE_STORAGE CONFIG_DIRECTORY SYSTEMD_CONFIG UNINSTALL +readonly SUMOLOGIC_INSTALLATION_TOKEN API_BASE_URL OPAMP_API_URL FIELDS CONTINUE FILE_STORAGE CONFIG_DIRECTORY SYSTEMD_CONFIG UNINSTALL readonly USER_CONFIG_DIRECTORY USER_ENV_DIRECTORY CONFIG_DIRECTORY CONFIG_PATH COMMON_CONFIG_PATH readonly ACL_LOG_FILE_PATHS readonly INSTALL_HOSTMETRICS @@ -1766,6 +1817,12 @@ if [[ -z "${DOWNLOAD_ONLY}" ]]; then exit 1 fi + USER_OPAMP_API_URL="$(get_user_opamp_endpoint "${COMMON_CONFIG_PATH}")" + if [[ -n "${USER_OPAMP_API_URL}" && -n "${OPAMP_API_URL}" && "${USER_OPAMP_API_URL}" != "${OPAMP_API_URL}" ]]; then + echo "You are trying to install with different opamp endpoint than in your configuration file!" + exit 1 + fi + USER_FIELDS="$(get_user_tags "${COMMON_CONFIG_PATH}" "${INDENTATION}" "${EXT_INDENTATION}")" FIELDS_TO_COMPARE="$(get_fields_to_compare "${FIELDS}")" diff --git a/install-script/test/check.go b/install-script/test/check.go index 84f7078c..d3eb96a6 100644 --- a/install-script/test/check.go +++ b/install-script/test/check.go @@ -106,6 +106,13 @@ func checkNoBakFilesPresent(c check) { } } +func checkOpAmpEndpointSet(c check) { + conf, err := getConfig(configPath) + require.NoError(c.test, err, "error while reading configuration") + + require.Equal(c.test, conf.Extensions.OpAmp.Endpoint, "wss://example.com") +} + func checkHostmetricsConfigCreated(c check) { require.FileExists(c.test, hostmetricsConfigPath, "hostmetrics configuration has not been created properly") } diff --git a/install-script/test/command_unix.go b/install-script/test/command_unix.go index 38a3537c..3cbfa8d8 100644 --- a/install-script/test/command_unix.go +++ b/install-script/test/command_unix.go @@ -32,6 +32,7 @@ type installOptions struct { remotelyManaged bool ephemeral bool timeout float64 + opampEndpoint string } func (io *installOptions) string() []string { @@ -105,6 +106,10 @@ func (io *installOptions) string() []string { opts = append(opts, "--download-timeout", fmt.Sprintf("%f", io.timeout)) } + if io.opampEndpoint != "" { + opts = append(opts, "--opamp-api", io.opampEndpoint) + } + return opts } diff --git a/install-script/test/config.go b/install-script/test/config.go index eb2bf82f..66abeb17 100644 --- a/install-script/test/config.go +++ b/install-script/test/config.go @@ -12,6 +12,7 @@ type config struct { type extensions struct { Sumologic sumologicExtension `yaml:"sumologic"` + OpAmp opampExtension `yaml:"opamp"` } type sumologicExtension struct { @@ -21,6 +22,10 @@ type sumologicExtension struct { Ephemeral bool `yaml:"ephemeral"` } +type opampExtension struct { + Endpoint string +} + func getConfig(path string) (config, error) { var conf config diff --git a/install-script/test/install_unix_test.go b/install-script/test/install_unix_test.go index 93b1ffc2..f4f6231d 100644 --- a/install-script/test/install_unix_test.go +++ b/install-script/test/install_unix_test.go @@ -179,6 +179,28 @@ func TestInstallScript(t *testing.T) { checkUserNotExists, }, }, + { + name: "installation token, remotely-managed, and opamp-api", + options: installOptions{ + skipSystemd: true, + installToken: installToken, + remotelyManaged: true, + opampEndpoint: "wss://example.com", + }, + preChecks: []checkFunc{checkBinaryNotCreated, checkConfigNotCreated, checkUserConfigNotCreated, checkUserNotExists}, + postChecks: []checkFunc{ + checkBinaryCreated, + checkBinaryIsRunning, + checkConfigCreated, + checkRemoteConfigDirectoryCreated, + checkConfigFilesOwnershipAndPermissions(rootUser, rootGroup), + checkTokenInSumoConfig, + checkEphemeralNotInConfig(configPath), + checkSystemdConfigNotCreated, + checkUserNotExists, + checkOpAmpEndpointSet, + }, + }, { name: "installation token only, binary not in PATH", options: installOptions{