Skip to content

Commit

Permalink
Merge branch 'feature-ingest' of github.com:elastic/kibana into featu…
Browse files Browse the repository at this point in the history
…re-ingest
  • Loading branch information
John Schulz committed Mar 11, 2020
2 parents 111a170 + fe96065 commit ab43d14
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { AgentConfigStatus } from '../types';
import { AgentConfigStatus, DefaultPackages } from '../types';

export const AGENT_CONFIG_SAVED_OBJECT_TYPE = 'agent_configs';

Expand All @@ -15,3 +15,5 @@ export const DEFAULT_AGENT_CONFIG = {
datasources: [],
is_default: true,
};

export const DEFAULT_AGENT_CONFIGS_PACKAGES = [DefaultPackages.system];
9 changes: 7 additions & 2 deletions x-pack/plugins/ingest_manager/server/services/agent_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AgentConfigService {
}

public async ensureDefaultAgentConfig(soClient: SavedObjectsClientContract) {
const configs = await soClient.find({
const configs = await soClient.find<AgentConfig>({
type: AGENT_CONFIG_SAVED_OBJECT_TYPE,
filter: 'agent_configs.attributes.is_default:true',
});
Expand All @@ -58,8 +58,13 @@ class AgentConfigService {
...DEFAULT_AGENT_CONFIG,
};

await this.create(soClient, newDefaultAgentConfig);
return this.create(soClient, newDefaultAgentConfig);
}

return {
id: configs.saved_objects[0].id,
...configs.saved_objects[0].attributes,
};
}

public async create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,41 @@ export async function installLatestPackage(options: {
export async function ensureInstalledDefaultPackages(
savedObjectsClient: SavedObjectsClientContract,
callCluster: CallESAsCurrentUser
): Promise<void> {
): Promise<Installation[]> {
const installations = [];
for (const pkgName in DefaultPackages) {
if (!DefaultPackages.hasOwnProperty(pkgName)) continue;
await ensureInstalledPackage({
const installation = await ensureInstalledPackage({
savedObjectsClient,
pkgName,
callCluster,
});
if (installation) installations.push(installation);
}

return installations;
}

export async function ensureInstalledPackage(options: {
savedObjectsClient: SavedObjectsClientContract;
pkgName: string;
callCluster: CallESAsCurrentUser;
}): Promise<void> {
}): Promise<Installation | undefined> {
const { savedObjectsClient, pkgName, callCluster } = options;
const installedPackage = await findInstalledPackageByName({ savedObjectsClient, pkgName });
if (installedPackage) {
return installedPackage;
}
// if the requested packaged was not found to be installed, try installing
if (!installedPackage) {
try {
await installLatestPackage({
savedObjectsClient,
pkgName,
callCluster,
});
} catch (err) {
throw new Error(err.message);
}
try {
await installLatestPackage({
savedObjectsClient,
pkgName,
callCluster,
});
return await findInstalledPackageByName({ savedObjectsClient, pkgName });
} catch (err) {
throw new Error(err.message);
}
}

Expand Down
9 changes: 7 additions & 2 deletions x-pack/plugins/ingest_manager/server/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const SAVED_OBJECT_TYPE = OUTPUT_SAVED_OBJECT_TYPE;

class OutputService {
public async ensureDefaultOutput(soClient: SavedObjectsClientContract) {
const outputs = await soClient.find({
const outputs = await soClient.find<Output>({
type: OUTPUT_SAVED_OBJECT_TYPE,
filter: 'outputs.attributes.is_default:true',
});
Expand All @@ -24,8 +24,13 @@ class OutputService {
ca_sha256: appContextService.getConfig()!.fleet.elasticsearch.ca_sha256,
} as NewOutput;

await this.create(soClient, newDefaultOutput);
return await this.create(soClient, newDefaultOutput);
}

return {
id: outputs.saved_objects[0].id,
...outputs.saved_objects[0].attributes,
};
}

public async updateOutput(
Expand Down
71 changes: 67 additions & 4 deletions x-pack/plugins/ingest_manager/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,79 @@ import { CallESAsCurrentUser } from '../types';
import { agentConfigService } from './agent_config';
import { outputService } from './output';
import { ensureInstalledDefaultPackages } from './epm/packages/install';
import {
packageToConfigDatasourceInputs,
Datasource,
AgentConfig,
Installation,
Output,
DEFAULT_AGENT_CONFIGS_PACKAGES,
} from '../../common';
import { getPackageInfo } from './epm/packages';
import { datasourceService } from './datasource';

export async function setup(
soClient: SavedObjectsClientContract,
callCluster: CallESAsCurrentUser
) {
await Promise.all([
outputService.ensureDefaultOutput(soClient),
agentConfigService.ensureDefaultAgentConfig(soClient),

const [installedPackages, defaultOutput, config] = await Promise.all([
// packages installed by default
ensureInstalledDefaultPackages(soClient, callCluster),
outputService.ensureDefaultOutput(soClient),
agentConfigService.ensureDefaultAgentConfig(soClient),
]);

// ensure default packages are added to the default conifg
const configWithDatasource = await agentConfigService.get(soClient, config.id, true);
if (!configWithDatasource) {
throw new Error('Config not found');
}
if (
configWithDatasource.datasources.length &&
typeof configWithDatasource.datasources[0] === 'string'
) {
throw new Error('Config not found');
}
for (const installedPackage of installedPackages) {
const packageShouldBeInstalled = DEFAULT_AGENT_CONFIGS_PACKAGES.some(
packageName => installedPackage.name === packageName
);
if (!packageShouldBeInstalled) {
continue;
}

const isInstalled = configWithDatasource.datasources.some((d: Datasource | string) => {
return typeof d !== 'string' && d.package?.name === installedPackage.name;
});

if (!isInstalled) {
await addPackageToConfig(soClient, installedPackage, configWithDatasource, defaultOutput);
}
}
}

async function addPackageToConfig(
soClient: SavedObjectsClientContract,
packageToInstall: Installation,
config: AgentConfig,
defaultOutput: Output
) {
const packageInfo = await getPackageInfo({
savedObjectsClient: soClient,
pkgkey: `${packageToInstall.name}-${packageToInstall.version}`,
});
const datasource = await datasourceService.create(soClient, {
name: `${packageInfo.name}-1`,
enabled: true,
package: {
name: packageInfo.name,
title: packageInfo.title,
version: packageInfo.version,
},
inputs: packageToConfigDatasourceInputs(packageInfo),
config_id: config.id,
output_id: defaultOutput.id,
});
// Assign it to the given agent config
await agentConfigService.assignDatasources(soClient, datasource.config_id, [datasource.id]);
}

0 comments on commit ab43d14

Please sign in to comment.