Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ingest Manager] Unify install* under installPackage #82916

Merged
merged 3 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import {
getPackageInfo,
handleInstallPackageFailure,
isBulkInstallError,
installPackageFromRegistry,
installPackageByUpload,
installPackage,
removeInstallation,
getLimitedPackages,
getInstallationObject,
Expand Down Expand Up @@ -149,7 +148,8 @@ export const installPackageFromRegistryHandler: RequestHandler<
const { pkgName, pkgVersion } = splitPkgKey(pkgkey);
const installedPkg = await getInstallationObject({ savedObjectsClient, pkgName });
try {
const res = await installPackageFromRegistry({
const res = await installPackage({
installSource: 'registry',
savedObjectsClient,
pkgkey,
callCluster,
Expand Down Expand Up @@ -224,7 +224,8 @@ export const installPackageByUploadHandler: RequestHandler<
const contentType = request.headers['content-type'] as string; // from types it could also be string[] or undefined but this is checked later
const archiveBuffer = Buffer.from(request.body);
try {
const res = await installPackageByUpload({
const res = await installPackage({
installSource: 'upload',
savedObjectsClient,
callCluster,
archiveBuffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ describe('_installPackage', () => {
const installationPromise = _installPackage({
savedObjectsClient: soClient,
callCluster,
pkgName: 'abc',
pkgVersion: '1.2.3',
paths: [],
removable: false,
internal: false,
packageInfo: {
name: 'xyz',
version: '4.5.6',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { installPipelines, deletePreviousPipelines } from '../elasticsearch/inge
import { installILMPolicy } from '../elasticsearch/ilm/install';
import { installKibanaAssets, getKibanaAssets } from '../kibana/assets/install';
import { updateCurrentWriteIndices } from '../elasticsearch/template/template';
import { isRequiredPackage } from './index';
import { deleteKibanaSavedObjectsAssets } from './remove';
import { installTransform } from '../elasticsearch/transform/install';
import { createInstallation, saveKibanaAssetsRefs, updateVersion } from './install';
Expand All @@ -32,28 +33,22 @@ import { createInstallation, saveKibanaAssetsRefs, updateVersion } from './insta
export async function _installPackage({
savedObjectsClient,
callCluster,
pkgName,
pkgVersion,
installedPkg,
paths,
removable,
internal,
packageInfo,
installType,
installSource,
}: {
savedObjectsClient: SavedObjectsClientContract;
callCluster: CallESAsCurrentUser;
pkgName: string;
pkgVersion: string;
installedPkg?: SavedObject<Installation>;
paths: string[];
removable: boolean;
internal: boolean;
packageInfo: InstallablePackage;
installType: InstallType;
installSource: InstallSource;
}): Promise<AssetReference[]> {
const { internal = false, name: pkgName, version: pkgVersion } = packageInfo;
const removable = !isRequiredPackage(pkgName);
const toSaveESIndexPatterns = generateESIndexPatterns(packageInfo.data_streams);
// add the package installation to the saved object.
// if some installation already exists, just update install info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export {
BulkInstallResponse,
IBulkInstallPackageError,
handleInstallPackageFailure,
installPackageFromRegistry,
installPackageByUpload,
installPackage,
ensureInstalledPackage,
} from './install';
export { removeInstallation } from './remove';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as Registry from '../registry';
import {
getInstallation,
getInstallationObject,
isRequiredPackage,
bulkInstallPackages,
isBulkInstallError,
} from './index';
Expand Down Expand Up @@ -52,7 +51,7 @@ export async function installLatestPackage(options: {
name: latestPackage.name,
version: latestPackage.version,
});
return installPackageFromRegistry({ savedObjectsClient, pkgkey, callCluster });
return installPackage({ installSource: 'registry', savedObjectsClient, pkgkey, callCluster });
} catch (err) {
throw err;
}
Expand Down Expand Up @@ -148,7 +147,8 @@ export async function handleInstallPackageFailure({
}
const prevVersion = `${pkgName}-${installedPkg.attributes.version}`;
logger.error(`rolling back to ${prevVersion} after error installing ${pkgkey}`);
await installPackageFromRegistry({
await installPackage({
installSource: 'registry',
savedObjectsClient,
pkgkey: prevVersion,
callCluster,
Expand Down Expand Up @@ -186,7 +186,12 @@ export async function upgradePackage({
});

try {
const assets = await installPackageFromRegistry({ savedObjectsClient, pkgkey, callCluster });
const assets = await installPackage({
installSource: 'registry',
savedObjectsClient,
pkgkey,
callCluster,
});
return {
name: pkgToUpgrade,
newVersion: latestPkg.version,
Expand Down Expand Up @@ -218,19 +223,19 @@ export async function upgradePackage({
}
}

interface InstallPackageParams {
interface InstallRegistryPackageParams {
savedObjectsClient: SavedObjectsClientContract;
pkgkey: string;
callCluster: CallESAsCurrentUser;
force?: boolean;
}

export async function installPackageFromRegistry({
async function installPackageFromRegistry({
savedObjectsClient,
pkgkey,
callCluster,
force = false,
}: InstallPackageParams): Promise<AssetReference[]> {
}: InstallRegistryPackageParams): Promise<AssetReference[]> {
// TODO: change epm API to /packageName/version so we don't need to do this
const { pkgName, pkgVersion } = Registry.splitPkgKey(pkgkey);
// TODO: calls to getInstallationObject, Registry.fetchInfo, and Registry.fetchFindLatestPackge
Expand All @@ -250,37 +255,36 @@ export async function installPackageFromRegistry({

const { paths, registryPackageInfo } = await Registry.getRegistryPackage(pkgName, pkgVersion);

const removable = !isRequiredPackage(pkgName);
const { internal = false } = registryPackageInfo;
const installSource = 'registry';

return _installPackage({
savedObjectsClient,
callCluster,
pkgName,
pkgVersion,
installedPkg,
paths,
removable,
internal,
packageInfo: registryPackageInfo,
installType,
installSource,
installSource: 'registry',
});
}

export async function installPackageByUpload({
savedObjectsClient,
callCluster,
archiveBuffer,
contentType,
}: {
interface InstallUploadedArchiveParams {
savedObjectsClient: SavedObjectsClientContract;
callCluster: CallESAsCurrentUser;
archiveBuffer: Buffer;
contentType: string;
}): Promise<AssetReference[]> {
}

export type InstallPackageParams =
| ({ installSource: Extract<InstallSource, 'registry'> } & InstallRegistryPackageParams)
| ({ installSource: Extract<InstallSource, 'upload'> } & InstallUploadedArchiveParams);

async function installPackageByUpload({
savedObjectsClient,
callCluster,
archiveBuffer,
contentType,
}: InstallUploadedArchiveParams): Promise<AssetReference[]> {
const { paths, archivePackageInfo } = await loadArchivePackage({ archiveBuffer, contentType });

const installedPkg = await getInstallationObject({
savedObjectsClient,
pkgName: archivePackageInfo.name,
Expand All @@ -292,25 +296,45 @@ export async function installPackageByUpload({
);
}

const removable = !isRequiredPackage(archivePackageInfo.name);
const { internal = false } = archivePackageInfo;
const installSource = 'upload';

return _installPackage({
savedObjectsClient,
callCluster,
pkgName: archivePackageInfo.name,
pkgVersion: archivePackageInfo.version,
installedPkg,
paths,
removable,
internal,
packageInfo: archivePackageInfo,
installType,
installSource,
installSource: 'upload',
});
}

export async function installPackage(args: InstallPackageParams) {
if (!('installSource' in args)) {
throw new Error('installSource is required');
}

if (args.installSource === 'registry') {
const { savedObjectsClient, pkgkey, callCluster, force } = args;

return installPackageFromRegistry({
savedObjectsClient,
pkgkey,
callCluster,
force,
});
} else if (args.installSource === 'upload') {
const { savedObjectsClient, callCluster, archiveBuffer, contentType } = args;

return installPackageByUpload({
savedObjectsClient,
callCluster,
archiveBuffer,
contentType,
});
}
// @ts-expect-error s/b impossibe b/c `never` by this point, but just in case
throw new Error(`Unknown installSource: ${args.installSource}`);
}

export const updateVersion = async (
savedObjectsClient: SavedObjectsClientContract,
pkgName: string,
Expand Down Expand Up @@ -421,7 +445,9 @@ export async function ensurePackagesCompletedInstall(
const pkgkey = `${pkg.attributes.name}-${pkg.attributes.install_version}`;
// reinstall package
if (elapsedTime > MAX_TIME_COMPLETE_INSTALL) {
acc.push(installPackageFromRegistry({ savedObjectsClient, pkgkey, callCluster }));
acc.push(
installPackage({ installSource: 'registry', savedObjectsClient, pkgkey, callCluster })
);
}
return acc;
}, []);
Expand Down