Skip to content

Commit

Permalink
[8.x] [Observability Onboarding] Fix auto-detect flow fails with tar …
Browse files Browse the repository at this point in the history
…error (#194002) (#194335)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Observability Onboarding] Fix auto-detect flow fails with tar error
(#194002)](#194002)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Thom
Heymann","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-27T17:34:42Z","message":"[Observability
Onboarding] Fix auto-detect flow fails with tar error
(#194002)\n\nResolves
[#193284](https://github.com/elastic/kibana/issues/193284)\r\n\r\n-
Fixes an issue where auto-detect would fail on GNU tar.\r\n- Also
changes the behaviour of the integration installation step to\r\nonly
throw an error when all integrations fail to
install.","sha":"7d395ad895c561bb6c826f834c0e67bf1fbc862c","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","ci:project-deploy-observability"],"title":"[Observability
Onboarding] Fix auto-detect flow fails with tar
error","number":194002,"url":"https://github.com/elastic/kibana/pull/194002","mergeCommit":{"message":"[Observability
Onboarding] Fix auto-detect flow fails with tar error
(#194002)\n\nResolves
[#193284](https://github.com/elastic/kibana/issues/193284)\r\n\r\n-
Fixes an issue where auto-detect would fail on GNU tar.\r\n- Also
changes the behaviour of the integration installation step to\r\nonly
throw an error when all integrations fail to
install.","sha":"7d395ad895c561bb6c826f834c0e67bf1fbc862c"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/194002","number":194002,"mergeCommit":{"message":"[Observability
Onboarding] Fix auto-detect flow fails with tar error
(#194002)\n\nResolves
[#193284](https://github.com/elastic/kibana/issues/193284)\r\n\r\n-
Fixes an issue where auto-detect would fail on GNU tar.\r\n- Also
changes the behaviour of the integration installation step to\r\nonly
throw an error when all integrations fail to
install.","sha":"7d395ad895c561bb6c826f834c0e67bf1fbc862c"}}]}]
BACKPORT-->

Co-authored-by: Thom Heymann <[email protected]>
  • Loading branch information
kibanamachine and thomheymann authored Sep 27, 2024
1 parent 733cac0 commit e9d8a99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ apply_elastic_agent_config() {
local decoded_ingest_api_key=$(echo "$ingest_api_key_encoded" | base64 -d)

# Verify that the downloaded archive contains the expected `elastic-agent.yml` file
tar --list --file "$elastic_agent_tmp_config_path" --include 'elastic-agent.yml' >/dev/null &&
tar --list --file "$elastic_agent_tmp_config_path" | grep "$(basename "$elastic_agent_config_path")" >/dev/null &&
# Remove existing config file including `inputs.d` directory
rm -rf "$elastic_agent_config_path" "$(dirname "$elastic_agent_config_path")/inputs.d" &&
# Extract new config files from downloaded archive
tar --extract --file "$elastic_agent_tmp_config_path" --include 'elastic-agent.yml' --include 'inputs.d/*.yml' --directory "$(dirname "$elastic_agent_config_path")" &&
tar --extract --file "$elastic_agent_tmp_config_path" --directory "$(dirname "$elastic_agent_config_path")" &&
# Replace placeholder with the Ingest API key
sed -i '' "s/\${API_KEY}/$decoded_ingest_api_key/" "$elastic_agent_config_path"
sed -i='' "s/\${API_KEY}/$decoded_ingest_api_key/" "$elastic_agent_config_path"
if [ "$?" -eq 0 ]; then
printf "\e[1;32m✓\e[0m %s\n" "Config written to:"
tar --list --file "$elastic_agent_tmp_config_path" --include 'elastic-agent.yml' --include 'inputs.d/*.yml' | while read -r file; do
tar --list --file "$elastic_agent_tmp_config_path" | grep '\.yml$' | while read -r file; do
echo " - $(dirname "$elastic_agent_config_path")/$file"
done

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ const createFlowRoute = createObservabilityOnboardingServerRoute({
*
* The request format is TSV (tab-separated values) to simplify parsing in bash.
*
* The response format is either a YAML file or a tarball containing the Elastic Agent
* The response format is either a YAML file or a tar archive containing the Elastic Agent
* configuration, depending on the `Accept` header.
*
* Errors during installation are ignore unless all integrations fail to install. When that happens
* a 500 Internal Server Error is returned with the first error message.
*
* Example request:
*
* ```text
Expand Down Expand Up @@ -335,10 +338,21 @@ const integrationsInstallRoute = createObservabilityOnboardingServerRoute({

let installedIntegrations: InstalledIntegration[] = [];
try {
installedIntegrations = await ensureInstalledIntegrations(
const settledResults = await ensureInstalledIntegrations(
integrationsToInstall,
packageClient
);
installedIntegrations = settledResults.reduce<InstalledIntegration[]>((acc, result) => {
if (result.status === 'fulfilled') {
acc.push(result.value);
}
return acc;
}, []);
// Errors during installation are ignore unless all integrations fail to install. When that happens
// a 500 Internal Server Error is returned with the first error message.
if (!installedIntegrations.length) {
throw (settledResults[0] as PromiseRejectedResult).reason;
}
} catch (error) {
if (error instanceof FleetUnauthorizedError) {
return response.forbidden({
Expand Down Expand Up @@ -401,8 +415,8 @@ export type IntegrationToInstall = RegistryIntegrationToInstall | CustomIntegrat
async function ensureInstalledIntegrations(
integrationsToInstall: IntegrationToInstall[],
packageClient: PackageClient
): Promise<InstalledIntegration[]> {
return Promise.all(
): Promise<Array<PromiseSettledResult<InstalledIntegration>>> {
return Promise.allSettled(
integrationsToInstall.map(async (integration) => {
const { pkgName, installSource } = integration;

Expand Down

0 comments on commit e9d8a99

Please sign in to comment.