Skip to content

Commit

Permalink
[Serverless] Use latest published version for initial agent download (e…
Browse files Browse the repository at this point in the history
…lastic#166150)

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
joshdover and kibanamachine authored Sep 11, 2023
1 parent ee92d7d commit 32d743a
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import React, { useState } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import { EuiText, EuiLink, EuiSteps, EuiSpacer } from '@elastic/eui';
import { EuiText, EuiLink, EuiSteps, EuiSpacer, EuiLoadingSpinner } from '@elastic/eui';

import { Error } from '../../../../../../../components';
import { useKibanaVersion, useStartServices } from '../../../../../../../../../hooks';
import { useStartServices, useAgentVersion } from '../../../../../../../../../hooks';

import { CreatePackagePolicyBottomBar, NotObscuredByBottomBar } from '../..';
import {
Expand Down Expand Up @@ -40,7 +40,7 @@ export const InstallElasticAgentManagedPageStep: React.FC<InstallAgentPageProps>
const { docLinks } = core;
const link = docLinks.links.fleet.troubleshooting;

const kibanaVersion = useKibanaVersion();
const agentVersion = useAgentVersion();

const [commandCopied, setCommandCopied] = useState(false);
const [applyCommandCopied, setApplyCommandCopied] = useState(false);
Expand All @@ -66,7 +66,7 @@ export const InstallElasticAgentManagedPageStep: React.FC<InstallAgentPageProps>
apiKey: enrollmentAPIKey.api_key,
fleetProxy,
fleetServerHosts,
kibanaVersion,
agentVersion: agentVersion || '',
});

const steps = [
Expand Down Expand Up @@ -103,6 +103,10 @@ export const InstallElasticAgentManagedPageStep: React.FC<InstallAgentPageProps>
})
);

if (!agentVersion) {
return <EuiLoadingSpinner />;
}

return (
<>
<EuiText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
EuiButton,
EuiButtonEmpty,
EuiCallOut,
EuiLoadingSpinner,
EuiModal,
EuiModalBody,
EuiModalFooter,
Expand All @@ -25,7 +26,7 @@ import {
sendGetEnrollmentAPIKeys,
useCreateCloudShellUrl,
useFleetServerHostsForPolicy,
useKibanaVersion,
useAgentVersion,
} from '../../../../../hooks';
import { GoogleCloudShellGuide } from '../../../../../components';
import { ManualInstructions } from '../../../../../../../components/enrollment_instructions';
Expand All @@ -44,18 +45,22 @@ export const PostInstallGoogleCloudShellModal: React.FunctionComponent<{
})
);
const { fleetServerHosts, fleetProxy } = useFleetServerHostsForPolicy(agentPolicy);
const kibanaVersion = useKibanaVersion();
const agentVersion = useAgentVersion();

const { cloudShellUrl, error, isError, isLoading } = useCreateCloudShellUrl({
enrollmentAPIKey: apyKeysData?.data?.items[0]?.api_key,
packagePolicy,
});

if (!agentVersion) {
return <EuiLoadingSpinner />;
}

const installManagedCommands = ManualInstructions({
apiKey: apyKeysData?.data?.items[0]?.api_key || 'no_key',
fleetServerHosts,
fleetProxy,
kibanaVersion,
});

const { cloudShellUrl, error, isError, isLoading } = useCreateCloudShellUrl({
enrollmentAPIKey: apyKeysData?.data?.items[0]?.api_key,
packagePolicy,
agentVersion,
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock('../../hooks', () => {
...jest.requireActual('../../hooks'),
useFleetServerStandalone: jest.fn(),
useAgentEnrollmentFlyoutData: jest.fn(),
useAgentVersion: jest.fn().mockReturnValue('8.1.0'),
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React, { useState, useMemo, useEffect } from 'react';

import { EuiSteps } from '@elastic/eui';
import { EuiSteps, EuiLoadingSpinner } from '@elastic/eui';
import { safeDump } from 'js-yaml';

import type { EuiContainedStepProps } from '@elastic/eui/src/components/steps/steps';
Expand All @@ -21,8 +21,8 @@ import { StandaloneInstructions, ManualInstructions } from '../../enrollment_ins
import {
useGetOneEnrollmentAPIKey,
useStartServices,
useKibanaVersion,
sendGetOneAgentPolicyFull,
useAgentVersion,
} from '../../../hooks';

import type { InstructionProps } from '../types';
Expand Down Expand Up @@ -59,7 +59,6 @@ export const StandaloneSteps: React.FunctionComponent<InstructionProps> = ({
const { notifications } = core;
const [fullAgentPolicy, setFullAgentPolicy] = useState<FullAgentPolicy | undefined>();
const [yaml, setYaml] = useState<any | undefined>('');
const kibanaVersion = useKibanaVersion();

let downloadLink = '';

Expand Down Expand Up @@ -123,8 +122,10 @@ export const StandaloneSteps: React.FunctionComponent<InstructionProps> = ({
}
}, [fullAgentPolicy, isK8s]);

const agentVersion = useAgentVersion();

const instructionsSteps = useMemo(() => {
const standaloneInstallCommands = StandaloneInstructions(kibanaVersion);
const standaloneInstallCommands = StandaloneInstructions(agentVersion || '');

const steps: EuiContainedStepProps[] = !agentPolicy
? [
Expand Down Expand Up @@ -164,7 +165,7 @@ export const StandaloneSteps: React.FunctionComponent<InstructionProps> = ({

return steps;
}, [
kibanaVersion,
agentVersion,
isK8s,
cloudSecurityIntegration,
agentPolicy,
Expand All @@ -181,6 +182,10 @@ export const StandaloneSteps: React.FunctionComponent<InstructionProps> = ({
setMode,
]);

if (!agentVersion) {
return <EuiLoadingSpinner />;
}

return <EuiSteps steps={instructionsSteps} />;
};

Expand All @@ -202,7 +207,6 @@ export const ManagedSteps: React.FunctionComponent<InstructionProps> = ({
cloudSecurityIntegration,
installedPackagePolicy,
}) => {
const kibanaVersion = useKibanaVersion();
const core = useStartServices();
const { docLinks } = core;
const link = docLinks.links.fleet.troubleshooting;
Expand All @@ -214,11 +218,13 @@ export const ManagedSteps: React.FunctionComponent<InstructionProps> = ({

const enrolledAgentIds = usePollingAgentCount(selectedPolicy?.id || '');

const agentVersion = useAgentVersion();

const installManagedCommands = ManualInstructions({
apiKey: enrollToken,
fleetServerHosts,
fleetProxy,
kibanaVersion,
agentVersion: agentVersion || '',
});

const instructionsSteps = useMemo(() => {
Expand Down Expand Up @@ -326,5 +332,9 @@ export const ManagedSteps: React.FunctionComponent<InstructionProps> = ({
installedPackagePolicy,
]);

if (!agentVersion) {
return <EuiLoadingSpinner />;
}

return <EuiSteps steps={instructionsSteps} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,44 @@ export const ManualInstructions = ({
apiKey,
fleetServerHosts,
fleetProxy,
kibanaVersion,
agentVersion: agentVersion,
}: {
apiKey: string;
fleetServerHosts: string[];
fleetProxy?: FleetProxy;
kibanaVersion: string;
agentVersion: string;
}) => {
const enrollArgs = getfleetServerHostsEnrollArgs(apiKey, fleetServerHosts, fleetProxy);
const fleetServerUrl = enrollArgs?.split('--url=')?.pop()?.split('--enrollment')[0];
const enrollmentToken = enrollArgs?.split('--enrollment-token=')[1];

const k8sCommand = 'kubectl apply -f elastic-agent-managed-kubernetes.yml';

const linuxCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz
tar xzvf elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz
cd elastic-agent-${kibanaVersion}-linux-x86_64
const linuxCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-linux-x86_64.tar.gz
tar xzvf elastic-agent-${agentVersion}-linux-x86_64.tar.gz
cd elastic-agent-${agentVersion}-linux-x86_64
sudo ./elastic-agent install ${enrollArgs}`;

const macCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-darwin-x86_64.tar.gz
tar xzvf elastic-agent-${kibanaVersion}-darwin-x86_64.tar.gz
cd elastic-agent-${kibanaVersion}-darwin-x86_64
const macCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-darwin-x86_64.tar.gz
tar xzvf elastic-agent-${agentVersion}-darwin-x86_64.tar.gz
cd elastic-agent-${agentVersion}-darwin-x86_64
sudo ./elastic-agent install ${enrollArgs}`;

const windowsCommand = `$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-windows-x86_64.zip -OutFile elastic-agent-${kibanaVersion}-windows-x86_64.zip
Expand-Archive .\\elastic-agent-${kibanaVersion}-windows-x86_64.zip -DestinationPath .
cd elastic-agent-${kibanaVersion}-windows-x86_64
Invoke-WebRequest -Uri https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-windows-x86_64.zip -OutFile elastic-agent-${agentVersion}-windows-x86_64.zip
Expand-Archive .\\elastic-agent-${agentVersion}-windows-x86_64.zip -DestinationPath .
cd elastic-agent-${agentVersion}-windows-x86_64
.\\elastic-agent.exe install ${enrollArgs}`;

const linuxDebCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-amd64.deb
sudo dpkg -i elastic-agent-${kibanaVersion}-amd64.deb
const linuxDebCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-amd64.deb
sudo dpkg -i elastic-agent-${agentVersion}-amd64.deb
sudo elastic-agent enroll ${enrollArgs} \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`;

const linuxRpmCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-x86_64.rpm
sudo rpm -vi elastic-agent-${kibanaVersion}-x86_64.rpm
const linuxRpmCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-x86_64.rpm
sudo rpm -vi elastic-agent-${agentVersion}-x86_64.rpm
sudo elastic-agent enroll ${enrollArgs} \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`;

const googleCloudShellCommand = `gcloud config set project <PROJECT_ID> && \nFLEET_URL=${fleetServerUrl} ENROLLMENT_TOKEN=${enrollmentToken} STACK_VERSION=${kibanaVersion} ./deploy.sh`;
const googleCloudShellCommand = `gcloud config set project <PROJECT_ID> && \nFLEET_URL=${fleetServerUrl} ENROLLMENT_TOKEN=${enrollmentToken} STACK_VERSION=${agentVersion} ./deploy.sh`;

return {
linux: linuxCommand,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
*/
import type { CommandsByPlatform } from '../../../applications/fleet/components/fleet_server_instructions/utils/install_command_utils';

export const StandaloneInstructions = (kibanaVersion: string): CommandsByPlatform => {
const linuxDebCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-amd64.deb
sudo dpkg -i elastic-agent-${kibanaVersion}-amd64.deb \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`;
export const StandaloneInstructions = (agentVersion: string): CommandsByPlatform => {
const linuxDebCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-amd64.deb
sudo dpkg -i elastic-agent-${agentVersion}-amd64.deb \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`;

const linuxRpmCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-x86_64.rpm
sudo rpm -vi elastic-agent-${kibanaVersion}-x86_64.rpm \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`;
const linuxRpmCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-x86_64.rpm
sudo rpm -vi elastic-agent-${agentVersion}-x86_64.rpm \nsudo systemctl enable elastic-agent \nsudo systemctl start elastic-agent`;

const linuxCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz
tar xzvf elastic-agent-${kibanaVersion}-linux-x86_64.tar.gz
cd elastic-agent-${kibanaVersion}-linux-x86_64
const linuxCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-linux-x86_64.tar.gz
tar xzvf elastic-agent-${agentVersion}-linux-x86_64.tar.gz
cd elastic-agent-${agentVersion}-linux-x86_64
sudo ./elastic-agent install`;

const macCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-darwin-x86_64.tar.gz
tar xzvf elastic-agent-${kibanaVersion}-darwin-x86_64.tar.gz
cd elastic-agent-${kibanaVersion}-darwin-x86_64
const macCommand = `curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-darwin-x86_64.tar.gz
tar xzvf elastic-agent-${agentVersion}-darwin-x86_64.tar.gz
cd elastic-agent-${agentVersion}-darwin-x86_64
sudo ./elastic-agent install`;

const windowsCommand = `$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${kibanaVersion}-windows-x86_64.zip -OutFile elastic-agent-${kibanaVersion}-windows-x86_64.zip
Expand-Archive .\elastic-agent-${kibanaVersion}-windows-x86_64.zip -DestinationPath .
cd elastic-agent-${kibanaVersion}-windows-x86_64
Invoke-WebRequest -Uri https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-${agentVersion}-windows-x86_64.zip -OutFile elastic-agent-${agentVersion}-windows-x86_64.zip
Expand-Archive .\elastic-agent-${agentVersion}-windows-x86_64.zip -DestinationPath .
cd elastic-agent-${agentVersion}-windows-x86_64
.\\elastic-agent.exe install`;

const k8sCommand = 'kubectl apply -f elastic-agent-standalone-kubernetes.yml';
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/public/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export * from './use_fleet_server_standalone';
export * from './use_locator';
export * from './use_create_cloud_formation_url';
export * from './use_create_cloud_shell_url';
export * from './use_agent_version';
37 changes: 37 additions & 0 deletions x-pack/plugins/fleet/public/hooks/use_agent_version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useEffect, useState } from 'react';

import { useKibanaVersion } from './use_kibana_version';
import { sendGetAgentsAvailableVersions } from './use_request';

/**
* @returns The most recent agent version available to install or upgrade to.
*/
export const useAgentVersion = (): string | undefined => {
const kibanaVersion = useKibanaVersion();
const [agentVersion, setAgentVersion] = useState<string | undefined>(undefined);

useEffect(() => {
const getVersions = async () => {
try {
const res = await sendGetAgentsAvailableVersions();
// if the endpoint returns an error, use the fallback versions
const versionsList = res?.data?.items ? res.data.items : [kibanaVersion];

setAgentVersion(versionsList[0]);
} catch (err) {
return;
}
};

getVersions();
}, [kibanaVersion]);

return agentVersion;
};
Loading

0 comments on commit 32d743a

Please sign in to comment.