Skip to content

Commit

Permalink
Merge branch 'wz-init' into 66-automate-the-packages-generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tostti committed Jul 11, 2023
2 parents 5919f28 + f8eeb62 commit 4edbd38
Show file tree
Hide file tree
Showing 22 changed files with 1,972 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.aws-config.json
.signing-config.json
.ackrc
/dev-tools/build-packages/*/output
/.opensearch
/.chromium
.DS_Store
Expand Down
9 changes: 9 additions & 0 deletions config/node.options.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Node command line options
## See `node --help` and `node --v8-options` for available options
## Please note you should specify one option per line

## max size of old space in megabytes
#--max-old-space-size=4096

## Wazuh - do not terminate process on unhandled promise rejection
--unhandled-rejections=warn
70 changes: 70 additions & 0 deletions dev-tools/build-packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Package building
This folder contains tools used to create `rpm` and `deb` packages.

## Requirements
- A system with Docker.
- Internet connection (to download the docker images the first time).

## Builders

### Tarball

To system packages (deb and rpm), a tarball of Wazuh dashboard `.tar.gz` is required.
This tarball contains the [Wazuh plugin][wazuh-plugin], the [Wazuh Security plugin][wazuh-security-plugin],
a set of OpenSearch plugins and the default configuration for the app.

The `generate_base.sh` script generates a `.tar.gz` file using the following inputs:
- `-a` | `--app`: URL to the zipped Wazuh plugin.*
- `-b` | `--base`: URL to the Wazuh dashboard `.tar.gz`, as generated with `yarn build --skip-os-packages --release`.*
- `-s` | `--security`: URL to the zipped Wazuh Security plugin, as generated with `yarn build`.*
- `-v` | `--version`: the Wazuh version of the package.
- `-r` | `--revision`: [Optional] Set the revision of the build. By default, it is set to 1.
- `-o` | `--output` [Optional] Set the destination path of package. By default, an output folder will be created in the same directory as the script.

*Note:* use `file://<absolute_path>` to indicate a local file. Otherwise, the script will try to download the file from the given URL.

Example:
```bash
bash generate_base.sh \
--app https://packages-dev.wazuh.com/pre-release/ui/dashboard/wazuh-4.6.0-1.zip \
--base file:///home/user/wazuh-dashboard/target/opensearch-dashboards-2.4.1-linux-x64.tar.gz \
--security file:///home/user/wazuh-security-dashboards-plugin/build/security-dashboards-2.4.1.0.zip \
--version 4.6.0
```

### DEB

The `launcher.sh` script generates a `.deb` package based on the previously generated `.tar.gz`.
A Docker container is used to generate the package. It takes the following inputs:
- `-v` | `--version`: the Wazuh version of the package.
- `-p` | `--package`: the location of the `.tar.gz` file. It can be a URL or a PATH, with the format `file://<absolute_path>`
- `-r` | `--revision`: [Optional] Set the revision of the build. By default, it is set to 1.
- `-o` | `--output` [Optional] Set the destination path of package. By default, an output folder will be created in the same directory as the script.
- `--dont-build-docker`: [Optional] Locally built Docker image will be used instead of generating a new one.

Example:
```bash
bash launcher.sh \
--version 4.6.0 \
--package file:///home/user/wazuh-dashboard/dev-tools/build-packages/base/output/wazuh-dashboard-4.6.0-1-linux-x64.tar.gz
```

### RPM

The `launcher.sh` script generates a `.rpm` package based on the previously generated `.tar.gz`.
A Docker container is used to generate the package. It takes the following inputs:
- `-v` | `--version`: the Wazuh version of the package.
- `-p` | `--package`: the location of the `.tar.gz` file. It can be a URL or a PATH, with the format `file://<absolute_path>`
- `-r` | `--revision`: [Optional] Set the revision of the build. By default, it is set to 1.
- `-o` | `--output` [Optional] Set the destination path of package. By default, an output folder will be created in the same directory as the script.
- `--dont-build-docker`: [Optional] Locally built Docker image will be used instead of generating a new one.

Example:
```bash
bash launcher.sh \
--version 4.6.0 \
--package file:///home/user/wazuh-dashboard/dev-tools/build-packages/base/output/wazuh-dashboard-4.6.0-1-linux-x64.tar.gz
```

[wazuh-plugin]: https://github.com/wazuh/wazuh-kibana-app
[wazuh-security-plugin]: https://github.com/wazuh/wazuh-security-dashboards-plugin
217 changes: 217 additions & 0 deletions dev-tools/build-packages/base/generate_base.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#!/bin/bash

# Wazuh package generator
# Copyright (C) 2022, Wazuh Inc.
#
# This program is a free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License (version 2) as published by the FSF - Free Software
# Foundation.

set -e

# Inputs
app=""
base=""
revision="1"
security=""
version=""

# Paths
current_path="$( cd $(dirname $0) ; pwd -P )"
config_path=$(realpath $current_path/../../../config)

# Folders
out_dir="${current_path}/output"
tmp_dir="${current_path}/tmp"

trap ctrl_c INT

clean() {
exit_code=$1
echo
echo "Cleaning temporary files..."
echo
# Clean the files
rm -r $tmp_dir

if [ $exit_code != 0 ]; then
rm $out_dir/*.tar.gz
rmdir $out_dir
fi

exit ${exit_code}
}

ctrl_c() {
clean 1
}

# -----------------------------------------------------------------------------

build() {
# Validate and download files to build the package
valid_url='(https?|ftp|file)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]'
echo
echo "Downloading files..."
echo
mkdir -p $tmp_dir
cd $tmp_dir
if [[ $app =~ $valid_url ]]; then
if ! curl --output app.zip --silent --fail "${app}"; then
echo "The given URL or Path to the Wazuh App is not working: ${app}"
clean 1
fi
else
echo "The given URL or Path to the Wazuh App is not valid: ${app}"
clean 1
fi

if [[ $base =~ $valid_url ]]; then
if ! curl --output wazuh-dashboard.tar.gz --silent --fail "${base}"; then
echo "The given URL or Path to the Wazuh Dashboard base is not working: ${base}"
clean 1
fi
else
echo "The given URL or Path to the Wazuh Dashboard base is not valid: ${base}"
clean 1
fi

if [[ $security =~ $valid_url ]]; then
if ! curl --output security.zip --silent --fail "${security}"; then
echo "The given URL or Path to the Wazuh Security Plugin is not working: ${security}"
clean 1
fi
else
echo "The given URL or Path to the Wazuh Security Plugin is not valid: ${security}"
clean 1
fi

tar -zxf wazuh-dashboard.tar.gz
directory_name=$(tar tf wazuh-dashboard.tar.gz | head -1 | sed 's#/.*##' | sort -u)
working_dir="wazuh-dashboard-$version-$revision-linux-x64"
mv $directory_name $working_dir
cd $working_dir

echo
echo Building the package...
echo

# Install plugins
bin/opensearch-dashboards-plugin install alertingDashboards
bin/opensearch-dashboards-plugin install customImportMapDashboards
bin/opensearch-dashboards-plugin install ganttChartDashboards
bin/opensearch-dashboards-plugin install indexManagementDashboards
bin/opensearch-dashboards-plugin install notificationsDashboards
bin/opensearch-dashboards-plugin install reportsDashboards
bin/opensearch-dashboards-plugin install file:../security.zip
bin/opensearch-dashboards-plugin install file:../app.zip

# Enable the default configuration (renaming)
cp $config_path/opensearch_dashboards.prod.yml config/opensearch_dashboards.yml
cp $config_path/node.options.prod config/node.options

# TODO: investigate to remove this if possible
# Fix ambiguous shebangs (necessary for RPM building)
grep -rnwl './node_modules/' -e '#!/usr/bin/env python$' | xargs -I {} sed -i 's/#!\/usr\/bin\/env python/#!\/usr\/bin\/env python3/g' {}
grep -rnwl './node_modules/' -e '#!/usr/bin/python$' | xargs -I {} sed -i 's/#!\/usr\/bin\/python/#!\/usr\/bin\/python3/g' {}

# Compress
echo
echo Compressing the package...
echo
cd ..
mkdir -p $out_dir
tar -czvf $out_dir/$working_dir.tar.gz $working_dir

echo
echo DONE!
echo
clean 0
}

# -----------------------------------------------------------------------------

help() {
echo
echo "Usage: $0 [OPTIONS]"
echo " -a, --app <url/path> Set the location of the .zip file containing the Wazuh plugin."
echo " -b, --base <url/path> Set the location of the .tar.gz file containing the base wazuh-dashboard build."
echo " -s, --security <url/path> Set the location of the .zip file containing the wazuh-security-dashboards-plugin."
echo " -v, --version <version> Set the version of this build."
echo " -r, --revision <revision> [Optional] Set the revision of this build. By default, it is set to 1."
echo " -o, --output <path> [Optional] Set the destination path of package. By default, an output folder will be created."
echo " -h, --help Show this help."
echo
exit $1
}

# -----------------------------------------------------------------------------

main() {
while [ -n "${1}" ]; do
case "${1}" in
"-h" | "--help")
help 0
;;
"-a" | "--app")
if [ -n "$2" ]; then
app="$2"
shift 2
else
help 1
fi
;;
"-s" | "--security")
if [ -n "${2}" ]; then
security="${2}"
shift 2
else
help 0
fi
;;
"-b" | "--base")
if [ -n "${2}" ]; then
base="${2}"
shift 2
else
help 0
fi
;;
"-v" | "--version")
if [ -n "${2}" ]; then
version="${2}"
shift 2
else
help 0
fi
;;
"-r" | "--revision")
if [ -n "${2}" ]; then
revision="${2}"
shift 2
fi
;;
"-o" | "--output")
if [ -n "${2}" ]; then
output="${2}"
shift 2
fi
;;
*)

help 1
;;
esac
done

if [ -z "$app" ] | [ -z "$base" ] | [ -z "$security" ] | [ -z "$version" ]; then
help 1
fi

build || exit 1

exit 0
}

main "$@"
7 changes: 7 additions & 0 deletions dev-tools/build-packages/config/default
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
user="wazuh-dashboard"
group="wazuh-dashboard"
chroot="/"
chdir="/"
nice=""
KILL_ON_STOP_TIMEOUT=0

Loading

0 comments on commit 4edbd38

Please sign in to comment.