Skip to content

Commit

Permalink
Change import approach to improve tree-shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Nov 10, 2020
1 parent a722532 commit 206b825
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
9 changes: 5 additions & 4 deletions x-pack/plugins/fleet/common/services/is_agent_upgradeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import semver from 'semver';
import semverCoerce from 'semver/functions/coerce';
import semverLt from 'semver/functions/lt';
import { Agent } from '../types';

export function isAgentUpgradeable(agent: Agent, kibanaVersion: string) {
Expand All @@ -17,9 +18,9 @@ export function isAgentUpgradeable(agent: Agent, kibanaVersion: string) {
if (!agent.local_metadata.elastic.agent.upgradeable) return false;

// make sure versions are only the number before comparison
const agentVersionNumber = semver.coerce(agentVersion);
const agentVersionNumber = semverCoerce(agentVersion);
if (!agentVersionNumber) throw new Error('agent version is invalid');
const kibanaVersionNumber = semver.coerce(kibanaVersion);
const kibanaVersionNumber = semverCoerce(kibanaVersion);
if (!kibanaVersionNumber) throw new Error('kibana version is invalid');
return semver.lt(agentVersionNumber, kibanaVersionNumber);
return semverLt(agentVersionNumber, kibanaVersionNumber);
}
6 changes: 3 additions & 3 deletions x-pack/plugins/fleet/server/routes/agent/upgrade_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { RequestHandler } from 'src/core/server';
import { TypeOf } from '@kbn/config-schema';
import semver from 'semver';
import semverCoerce from 'semver/functions/coerce';
import {
AgentSOAttributes,
PostAgentUpgradeResponse,
Expand Down Expand Up @@ -122,9 +122,9 @@ export const postBulkAgentsUpgradeHandler: RequestHandler<

export const checkVersionIsSame = (version: string, kibanaVersion: string) => {
// get version number only in case "-SNAPSHOT" is in it
const kibanaVersionNumber = semver.coerce(kibanaVersion)?.version;
const kibanaVersionNumber = semverCoerce(kibanaVersion)?.version;
if (!kibanaVersionNumber) throw new Error(`kibanaVersion ${kibanaVersionNumber} is not valid`);
const versionToUpgradeNumber = semver.coerce(version)?.version;
const versionToUpgradeNumber = semverCoerce(version)?.version;
if (!versionToUpgradeNumber)
throw new Error(`version to upgrade ${versionToUpgradeNumber} is not valid`);
// temporarily only allow upgrading to the same version as the installed kibana version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import semver from 'semver';
import semverParse from 'semver/functions/parse';
import semverLt from 'semver/functions/lt';

import { timer, from, Observable, TimeoutError, of, EMPTY } from 'rxjs';
import { omit } from 'lodash';
import {
Expand Down Expand Up @@ -132,10 +134,10 @@ export async function createAgentActionFromPolicyAction(
policyAction: AgentPolicyAction
) {
// Transform the policy action for agent version <= 7.9.x for BWC
const agentVersion = semver.parse((agent.local_metadata?.elastic as any)?.agent?.version);
const agentVersion = semverParse((agent.local_metadata?.elastic as any)?.agent?.version);
const agentPolicyAction: AgentPolicyAction | AgentPolicyActionV7_9 =
agentVersion &&
semver.lt(
semverLt(
agentVersion,
// A prerelease tag is added here so that agent versions with prerelease tags can be compared
// correctly using `semvar`
Expand Down
13 changes: 8 additions & 5 deletions x-pack/plugins/fleet/server/services/agents/enroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/

import Boom from '@hapi/boom';
import semver from 'semver';
import semverParse from 'semver/functions/parse';
import semverDiff from 'semver/functions/diff';
import semverLte from 'semver/functions/lte';

import { SavedObjectsClientContract } from 'src/core/server';
import { AgentType, Agent, AgentSOAttributes } from '../../types';
import { savedObjectToAgent } from './saved_objects';
Expand Down Expand Up @@ -94,17 +97,17 @@ export function validateAgentVersion(
agentVersion: string,
kibanaVersion = appContextService.getKibanaVersion()
) {
const agentVersionParsed = semver.parse(agentVersion);
const agentVersionParsed = semverParse(agentVersion);
if (!agentVersionParsed) {
throw Boom.badRequest('Agent version not provided');
}

const kibanaVersionParsed = semver.parse(kibanaVersion);
const kibanaVersionParsed = semverParse(kibanaVersion);
if (!kibanaVersionParsed) {
throw Boom.badRequest('Kibana version is not set or provided');
}

const diff = semver.diff(agentVersion, kibanaVersion);
const diff = semverDiff(agentVersion, kibanaVersion);
switch (diff) {
// section 1) very close versions, only patch release differences - all combos should work
// Agent a.b.1 < Kibana a.b.2
Expand All @@ -130,7 +133,7 @@ export function validateAgentVersion(
// Agent 7.10.x > Kibana 7.9.x
// Agent 8.0.x > Kibana 7.9.x
default:
if (semver.lte(agentVersionParsed, kibanaVersionParsed)) return;
if (semverLte(agentVersionParsed, kibanaVersionParsed)) return;
else
throw Boom.badRequest(
`Agent version ${agentVersion} is not compatible with Kibana version ${kibanaVersion}`
Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugins/fleet/server/services/epm/packages/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

import { SavedObject, SavedObjectsClientContract } from 'src/core/server';
import semver from 'semver';
import semverGt from 'semver/functions/gt';
import semverLt from 'semver/functions/lt';
import Boom from '@hapi/boom';
import { UnwrapPromise } from '@kbn/utility-types';
import { BulkInstallPackageInfo, InstallSource, defaultPackages } from '../../../../common';
Expand Down Expand Up @@ -179,7 +180,7 @@ export async function upgradePackage({
latestPkg,
pkgToUpgrade,
}: UpgradePackageParams): Promise<BulkInstallResponse> {
if (!installedPkg || semver.gt(latestPkg.version, installedPkg.attributes.version)) {
if (!installedPkg || semverGt(latestPkg.version, installedPkg.attributes.version)) {
const pkgkey = Registry.pkgToPkgKey({
name: latestPkg.name,
version: latestPkg.version,
Expand Down Expand Up @@ -249,7 +250,7 @@ async function installPackageFromRegistry({
// let the user install if using the force flag or needing to reinstall or install a previous version due to failed update
const installOutOfDateVersionOk =
installType === 'reinstall' || installType === 'reupdate' || installType === 'rollback';
if (semver.lt(pkgVersion, latestPackage.version) && !force && !installOutOfDateVersionOk) {
if (semverLt(pkgVersion, latestPackage.version) && !force && !installOutOfDateVersionOk) {
throw new PackageOutdatedError(`${pkgkey} is out-of-date and cannot be installed or updated`);
}

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/epm/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import mime from 'mime-types';
import semver from 'semver';
import semverValid from 'semver/functions/valid';
import { Response } from 'node-fetch';
import { URL } from 'url';
import {
Expand Down Expand Up @@ -51,7 +51,7 @@ export function splitPkgKey(pkgkey: string): { pkgName: string; pkgVersion: stri

// this will return the entire string if `indexOf` return -1
const pkgVersion = pkgkey.substr(pkgkey.indexOf('-') + 1);
if (!semver.valid(pkgVersion)) {
if (!semverValid(pkgVersion)) {
throw new Error('Package key parsing failed: package version was not a valid semver');
}
return { pkgName, pkgVersion };
Expand Down

0 comments on commit 206b825

Please sign in to comment.