Skip to content

Commit

Permalink
[Fleet] Add latest package flag and remove force flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Apr 15, 2021
1 parent 7a070e8 commit a6d4beb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/common/constants/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@

export const PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE =
'fleet-preconfiguration-deletion-record';

export const PRECONFIGURATION_LATEST_KEYWORD = 'latest';
4 changes: 1 addition & 3 deletions x-pack/plugins/fleet/common/types/models/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ export interface PreconfiguredAgentPolicy extends Omit<NewAgentPolicy, 'namespac
>;
}

export interface PreconfiguredPackage extends Omit<PackagePolicyPackage, 'title'> {
force?: boolean;
}
export type PreconfiguredPackage = Omit<PackagePolicyPackage, 'title'>;
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ export {
ENROLLMENT_API_KEYS_INDEX,
AGENTS_INDEX,
PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE,
PRECONFIGURATION_LATEST_KEYWORD,
} from '../../common';
16 changes: 10 additions & 6 deletions x-pack/plugins/fleet/server/services/epm/packages/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,19 @@ export async function ensureInstalledDefaultPackages(
});
}

export async function isPackageVersionInstalled(options: {
async function isPackageVersionOrLaterInstalled(options: {
savedObjectsClient: SavedObjectsClientContract;
pkgName: string;
pkgVersion?: string;
}): Promise<Installation | false> {
const { savedObjectsClient, pkgName, pkgVersion } = options;
const installedPackage = await getInstallation({ savedObjectsClient, pkgName });
if (installedPackage && (!pkgVersion || installedPackage.version === pkgVersion)) {
if (
installedPackage &&
(!pkgVersion ||
installedPackage.version === pkgVersion ||
semverLt(pkgVersion, installedPackage.version))
) {
return installedPackage;
}
return false;
Expand All @@ -115,10 +120,9 @@ export async function ensureInstalledPackage(options: {
pkgName: string;
esClient: ElasticsearchClient;
pkgVersion?: string;
force?: boolean;
}): Promise<Installation> {
const { savedObjectsClient, pkgName, esClient, pkgVersion, force } = options;
const installedPackage = await isPackageVersionInstalled({
const { savedObjectsClient, pkgName, esClient, pkgVersion } = options;
const installedPackage = await isPackageVersionOrLaterInstalled({
savedObjectsClient,
pkgName,
pkgVersion,
Expand All @@ -137,7 +141,7 @@ export async function ensureInstalledPackage(options: {
savedObjectsClient,
pkgkey,
esClient,
force,
force: true, // Always force outdated packages to be installed if a later version isn't installed
});
} else {
await installLatestPackage({
Expand Down
16 changes: 9 additions & 7 deletions x-pack/plugins/fleet/server/services/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import type {
PreconfiguredAgentPolicy,
PreconfiguredPackage,
} from '../../common';
import { PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE } from '../constants';
import {
PRECONFIGURATION_DELETION_RECORD_SAVED_OBJECT_TYPE,
PRECONFIGURATION_LATEST_KEYWORD,
} from '../constants';

import { escapeSearchQueryPhrase } from './saved_object';

Expand Down Expand Up @@ -64,8 +67,8 @@ export async function ensurePreconfiguredPackagesAndPolicies(

// Preinstall packages specified in Kibana config
const preconfiguredPackages = await Promise.all(
packages.map(({ name, version, force }) =>
ensureInstalledPreconfiguredPackage(soClient, esClient, name, version, force)
packages.map(({ name, version }) =>
ensureInstalledPreconfiguredPackage(soClient, esClient, name, version)
)
);

Expand Down Expand Up @@ -202,15 +205,14 @@ async function ensureInstalledPreconfiguredPackage(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
pkgName: string,
pkgVersion: string,
force?: boolean
pkgVersion: string
) {
const isLatest = pkgVersion === PRECONFIGURATION_LATEST_KEYWORD;
return ensureInstalledPackage({
savedObjectsClient: soClient,
pkgName,
esClient,
pkgVersion,
force,
pkgVersion: isLatest ? undefined : pkgVersion,
});
}

Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugins/fleet/server/types/models/preconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { i18n } from '@kbn/i18n';
import { schema } from '@kbn/config-schema';
import semverValid from 'semver/functions/valid';

import { PRECONFIGURATION_LATEST_KEYWORD } from '../../constants';

import { AgentPolicyBaseSchema } from './agent_policy';
import { NamespaceSchema } from './package_policy';

Expand All @@ -26,14 +28,13 @@ export const PreconfiguredPackagesSchema = schema.arrayOf(
name: schema.string(),
version: schema.string({
validate: (value) => {
if (!semverValid(value)) {
if (value !== PRECONFIGURATION_LATEST_KEYWORD && !semverValid(value)) {
return i18n.translate('xpack.fleet.config.invalidPackageVersionError', {
defaultMessage: 'must be a valid semver',
defaultMessage: 'must be a valid semver, or the keyword `latest`',
});
}
},
}),
force: schema.maybe(schema.boolean()),
})
);

Expand Down

0 comments on commit a6d4beb

Please sign in to comment.