Skip to content
Open
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
35 changes: 24 additions & 11 deletions .github/workflows/dev-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,28 @@ on:
default: false
required: false

env:
MAIN_PLUGIN_PATH: 'wazuh/plugins/main'

jobs:
# Deploy the plugin in a development environment and run a command
# using a pre-built Docker image, hosted in Quay.io.
deploy_and_run_command:
name: Deploy and run command
runs-on: ubuntu-latest
strategy:
matrix:
plugins:
[
{
name: 'Main',
path: 'wazuh/plugins/main',
container_path: 'wazuh',
},
{
name: 'Wazuh Check Updates',
path: 'wazuh/plugins/wazuh-check-updates',
container_path: 'wazuh-check-updates',
},
]

steps:
- name: Step 01 - Download the plugin's source code
uses: actions/checkout@v3
Expand All @@ -65,13 +78,13 @@ jobs:
# Detect which platform to use from source code
platform=kbn;
echo "Detecting platform [kbn, osd]...";
find ${{ env.MAIN_PLUGIN_PATH }}/opensearch_dashboards.json && { platform=osd; };
find ${{ matrix.plugins.path }}/opensearch_dashboards.json && { platform=osd; };
echo "Platform is $platform";

# Read the platform version from the package.json file
echo "Reading the platform version from the package.json...";
# Support plugins whose version is defined under pluginPlatform or Kibana properties
platform_version=$(jq -r '.pluginPlatform.version, .kibana.version | select(. != null)' ${{ env.MAIN_PLUGIN_PATH }}/package.json);
platform_version=$(jq -r '.pluginPlatform.version, .kibana.version | select(. != null)' ${{ matrix.plugins.path }}/package.json);
echo "Plugin platform version: $platform_version";

# Set the environment variable to the correct platform
Expand All @@ -81,26 +94,26 @@ jobs:
# Up the environment and run the command
docker run -t --rm \
-e ${docker_env_plugin_platform}=${platform_version} \
-v `pwd`/${{ env.MAIN_PLUGIN_PATH }}:/home/node/kbn/plugins/wazuh \
-v `pwd`/${{ matrix.plugins.path }}:/home/node/kbn/plugins/${{ matrix.plugins.container_path }} \
${{ inputs.docker_run_extra_args }} \
quay.io/wazuh/${platform}-dev:${platform_version} \
bash -c '
yarn config set registry https://registry.yarnpkg.com;
cd /home/node/kbn/plugins/wazuh && yarn && ${{ inputs.command }};
cd /home/node/kbn/plugins/${{ matrix.plugins.container_path }} && yarn && ${{ inputs.command }};
'

- name: Step 04 - Upload artifact to GitHub
if: ${{ inputs.artifact_name && inputs.artifact_path }}
uses: actions/upload-artifact@v3
with:
name: ${{ inputs.artifact_name }}
path: ${{env.MAIN_PLUGIN_PATH}}/${{ inputs.artifact_path }}
path: ${{ matrix.plugins.path }}/${{ inputs.artifact_path }}
if-no-files-found: 'error'

- name: Step 05 - Upload coverage results to GitHub
if: ${{ inputs.notify_jest_coverage_summary && github.event_name == 'pull_request' }}
uses: AthleticNet/comment-test-coverage@1.2.2
uses: lucianogorza/comment-test-coverage@1.0.3
with:
token: ${{ secrets.GITHUB_TOKEN }}
path: ./${{ env.MAIN_PLUGIN_PATH }}/target/test-coverage/coverage-summary.json
title: "Code coverage (Jest)"
path: ./${{ matrix.plugins.path }}/target/test-coverage/coverage-summary.json
title: '${{ matrix.plugins.name }} plugin code coverage (Jest) test'
2 changes: 1 addition & 1 deletion plugins/wazuh-check-updates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
"@types/md5": "^2.3.2",
"@types/node-cron": "^3.0.8"
}
}
}
17 changes: 17 additions & 0 deletions plugins/wazuh-check-updates/scripts/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

/* eslint-disable @typescript-eslint/no-var-requires */

const fs = require('fs');

/**
* Reads the package.json file.
* @returns {Object} JSON object.
*/
function loadPackageJson() {
const packageJson = fs.readFileSync('./package.json');
return JSON.parse(packageJson);
}

module.exports = {
loadPackageJson
};
149 changes: 149 additions & 0 deletions plugins/wazuh-check-updates/scripts/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* eslint-disable array-element-newline */
/* eslint-disable @typescript-eslint/no-var-requires */

/**
Runs yarn commands using a Docker container.

Intended to test and build locally.

Uses development images. Must be executed from the root folder of the project.

See /docker/runner/docker-compose.yml for available environment variables.

# Usage:
# -------------
# - node scripts/runner <command> [<command_args>]
# - yarn test:jest:runner [<jest_args>]
# - yarn build:runner
*/

const childProcess = require('child_process');
const { loadPackageJson } = require('./manifest');

const COMPOSE_DIR = '../../docker/runner';

function getProjectInfo() {
const manifest = loadPackageJson();

return {
app: 'osd',
version: manifest['pluginPlatform']['version'],
repo: process.cwd(),
};
}

function getBuildArgs({ version }) {
return `--opensearch-dashboards-version=${version}`
}

/**
* Transforms the Jest CLI options from process.argv back to a string.
* If no options are provided, default ones are generated.
* @returns {String} Space separated string with all Jest CLI options provided.
*/
function getJestArgs() {
// Take args only after `test` word
const index = process.argv.indexOf('test');
const args = process.argv.slice(index + 1);
// Remove duplicates using set
return Array.from(new Set([ ...args, '--runInBand' ]))
.join(' ');
}

/**
* Generates the execution parameters if they are not set.
* @returns {Object} Default environment variables.
*/
const buildEnvVars = ({ app, version, repo, cmd, args }) => {
return {
APP: app,
VERSION: version,
REPO: repo,
CMD: cmd,
ARGS: args,
};
};

/**
* Captures the SIGINT signal (Ctrl + C) to stop the container and exit.
*/
function setupAbortController() {
process.on('SIGINT', () => {
childProcess.spawnSync('docker', [
'compose',
'--project-directory',
COMPOSE_DIR,
'stop',
]);
process.exit();
});
}

/**
* Start the container.
*/
function startRunner() {
const runner = childProcess.spawn('docker', [
'compose',
'--project-directory',
COMPOSE_DIR,
'up',
]);

runner.stdout.on('data', data => {
console.log(`${data}`);
});

runner.stderr.on('data', data => {
console.error(`${data}`);
});
}

/**
* Main function
*/
function main() {
if (process.argv.length < 2) {
process.stderr.write('Required parameters not provided');
process.exit(-1);
}

const projectInfo = getProjectInfo();
let envVars = {};

switch (process.argv[2]) {
case 'build':
envVars = buildEnvVars({
...projectInfo,
cmd: 'plugin-helpers build',
args: getBuildArgs({ ...projectInfo }),
});
break;

case 'test':
envVars = buildEnvVars({
...projectInfo,
cmd: 'test:jest',
args: getJestArgs(),
});
break;

default:
// usage();
console.error('Unsupported or invalid yarn command.');
process.exit(-1);
}

// Check the required environment variables are set
for (const [ key, value ] of Object.entries(envVars)) {
if (!process.env[key]) {
process.env[key] = value;
}
console.log(`${key}: ${process.env[key]}`);
}

setupAbortController();
startRunner();
}

main();
4 changes: 2 additions & 2 deletions plugins/wazuh-core/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "wazuh-core",
"version": "4.8.0",
"version": "1.0.0",
"revision": "01",
"pluginPlatform": {
"version": "2.10.0"
"version": "4.7.0"
},
"description": "Wazuh Core",
"private": true,
Expand Down