Skip to content

Commit

Permalink
[Ingest Manager] Remove package cache on package uninstall (#76873) (#…
Browse files Browse the repository at this point in the history
…76921)

* On uninstall, remove package from cache.

* Really remove package archive and contents from cache.

* Refactor

* move deletePackageCache to registry/index.ts
* clean up imports
  • Loading branch information
skh authored Sep 8, 2020
1 parent 9f737ef commit d47d438
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getInstallation, savedObjectTypes } from './index';
import { deletePipeline } from '../elasticsearch/ingest_pipeline/';
import { installIndexPatterns } from '../kibana/index_pattern/install';
import { packagePolicyService, appContextService } from '../..';
import { splitPkgKey } from '../registry';
import { splitPkgKey, deletePackageCache, getArchiveInfo } from '../registry';

export async function removeInstallation(options: {
savedObjectsClient: SavedObjectsClientContract;
Expand All @@ -22,7 +22,7 @@ export async function removeInstallation(options: {
}): Promise<AssetReference[]> {
const { savedObjectsClient, pkgkey, callCluster } = options;
// TODO: the epm api should change to /name/version so we don't need to do this
const { pkgName } = splitPkgKey(pkgkey);
const { pkgName, pkgVersion } = splitPkgKey(pkgkey);
const installation = await getInstallation({ savedObjectsClient, pkgName });
if (!installation) throw Boom.badRequest(`${pkgName} is not installed`);
if (installation.removable === false)
Expand Down Expand Up @@ -50,6 +50,11 @@ export async function removeInstallation(options: {
// could also update with [] or some other state
await savedObjectsClient.delete(PACKAGES_SAVED_OBJECT_TYPE, pkgName);

// remove the package archive and its contents from the cache so that a reinstall fetches
// a fresh copy from the registry
const paths = await getArchiveInfo(pkgName, pkgVersion);
deletePackageCache(pkgName, pkgVersion, paths);

// successful delete's in SO client return {}. return something more useful
return installedAssets;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ export const getArchiveLocation = (name: string, version: string) =>

export const setArchiveLocation = (name: string, version: string, location: string) =>
archiveLocationCache.set(pkgToPkgKey({ name, version }), location);

export const deleteArchiveLocation = (name: string, version: string) =>
archiveLocationCache.delete(pkgToPkgKey({ name, version }));
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ import {
RegistrySearchResults,
RegistrySearchResult,
} from '../../../types';
import { cacheGet, cacheSet, cacheHas, getArchiveLocation, setArchiveLocation } from './cache';
import {
cacheGet,
cacheSet,
cacheDelete,
cacheHas,
getArchiveLocation,
setArchiveLocation,
deleteArchiveLocation,
} from './cache';
import { ArchiveEntry, untarBuffer, unzipBuffer } from './extract';
import { fetchUrl, getResponse, getResponseStream } from './requests';
import { streamToBuffer } from './streams';
Expand Down Expand Up @@ -241,3 +249,17 @@ export function groupPathsByService(paths: string[]): AssetsGroupedByServiceByTy
// elasticsearch: assets.elasticsearch,
};
}

export const deletePackageCache = (name: string, version: string, paths: string[]) => {
const archiveLocation = getArchiveLocation(name, version);
if (archiveLocation) {
// delete cached archive
cacheDelete(archiveLocation);

// delete cached archive location
deleteArchiveLocation(name, version);
}
// delete cached archive contents
// this has been populated in Registry.getArchiveInfo()
paths.forEach((path) => cacheDelete(path));
};

0 comments on commit d47d438

Please sign in to comment.