From 888d29c2973dfa68f0a15abd28f7428110def801 Mon Sep 17 00:00:00 2001 From: Nicolas Agustin Guevara Pihen <42900763+Tostti@users.noreply.github.com> Date: Mon, 28 Oct 2024 06:45:30 -0300 Subject: [PATCH] Support arm packages generation (#376) --- .github/workflows/build_base.yml | 26 +- .../build_wazuh_dashboard_with_plugins.yml | 27 +- .gitignore | 5 +- dev-tools/build-packages/README.md | 90 +++--- .../build-packages/base/Docker/Dockerfile | 35 +++ dev-tools/build-packages/base/base-builder.sh | 162 ++++++++++ .../build-packages/base/generate_base.sh | 292 ------------------ dev-tools/build-packages/build-packages.sh | 165 ++++++++-- .../deb/{docker/amd64 => Docker}/Dockerfile | 9 +- .../deb/{builder.sh => deb-builder.sh} | 62 +++- dev-tools/build-packages/deb/debian/rules | 2 +- dev-tools/build-packages/deb/launcher.sh | 204 ------------ .../rpm/{docker/x86_64 => Docker}/Dockerfile | 7 +- dev-tools/build-packages/rpm/builder.sh | 57 ---- dev-tools/build-packages/rpm/launcher.sh | 207 ------------- dev-tools/build-packages/rpm/rpm-builder.sh | 112 +++++++ .../build-packages/rpm/wazuh-dashboard.spec | 2 +- 17 files changed, 578 insertions(+), 886 deletions(-) create mode 100644 dev-tools/build-packages/base/Docker/Dockerfile create mode 100755 dev-tools/build-packages/base/base-builder.sh delete mode 100755 dev-tools/build-packages/base/generate_base.sh rename dev-tools/build-packages/deb/{docker/amd64 => Docker}/Dockerfile (63%) rename dev-tools/build-packages/deb/{builder.sh => deb-builder.sh} (53%) delete mode 100755 dev-tools/build-packages/deb/launcher.sh rename dev-tools/build-packages/rpm/{docker/x86_64 => Docker}/Dockerfile (80%) delete mode 100755 dev-tools/build-packages/rpm/builder.sh delete mode 100755 dev-tools/build-packages/rpm/launcher.sh create mode 100644 dev-tools/build-packages/rpm/rpm-builder.sh diff --git a/.github/workflows/build_base.yml b/.github/workflows/build_base.yml index 0fac06eaa850..665a7e4f69f8 100644 --- a/.github/workflows/build_base.yml +++ b/.github/workflows/build_base.yml @@ -10,6 +10,11 @@ on: required: true default: '' type: string + ARCHITECTURE: + description: 'The architecture to build the package for' + required: true + default: 'amd64' + type: string workflow_dispatch: inputs: @@ -17,10 +22,15 @@ on: description: 'The branch/tag/commit to checkout to' required: true default: '' + ARCHITECTURE: + description: 'The architecture to build the package for' + required: true + default: 'amd64' + type: string jobs: build: - runs-on: ubuntu-latest + runs-on: ${{ (inputs.ARCHITECTURE == 'x86_64' || inputs.ARCHITECTURE == 'amd64') && 'ubuntu-latest' || 'wz-linux-arm64' }} name: Build defaults: run: @@ -28,7 +38,6 @@ jobs: strategy: matrix: DISTRIBUTION: [tar.gz] - ARCHITECTURE: [x64] steps: - name: Checkout code @@ -69,21 +78,16 @@ jobs: - name: Get artifact build name run: | - echo "ARTIFACT_BUILD_NAME=wazuh-dashboard_${{ env.WZD_VERSION }}-${{ env.WZD_REVISION }}_${{ matrix.ARCHITECTURE }}.${{ matrix.DISTRIBUTION }}" >> $GITHUB_ENV + echo "ARTIFACT_BUILD_NAME=wazuh-dashboard_${{ env.WZD_VERSION }}-${{ env.WZD_REVISION }}_${{ (inputs.ARCHITECTURE == 'x86_64' || inputs.ARCHITECTURE == 'amd64') && 'x64' || 'arm64' }}.${{ matrix.DISTRIBUTION }}" >> $GITHUB_ENV - name: Run bootstrap run: yarn osd bootstrap - - name: Build linux-x64 - if: matrix.ARCHITECTURE == 'x64' - run: yarn build-platform --linux --skip-os-packages --release - - - name: Build linux-arm64 - if: matrix.ARCHITECTURE == 'arm64' - run: yarn build-platform --linux-arm --skip-os-packages --release + - name: Build + run: yarn build-platform --${{(inputs.ARCHITECTURE == 'x86_64' || inputs.ARCHITECTURE == 'amd64') && 'linux' || 'linux-arm'}} --skip-os-packages --release - name: Rename artifact - run: mv /home/runner/work/wazuh-dashboard/wazuh-dashboard/artifacts/target/opensearch-dashboards-${{ env.VERSION }}-linux-${{ matrix.ARCHITECTURE }}.${{ matrix.DISTRIBUTION }} /home/runner/work/wazuh-dashboard/wazuh-dashboard/artifacts/target/${{ env.ARTIFACT_BUILD_NAME }} + run: mv /home/runner/work/wazuh-dashboard/wazuh-dashboard/artifacts/target/opensearch-dashboards-${{ env.VERSION }}-linux-${{ (inputs.ARCHITECTURE == 'x86_64' || inputs.ARCHITECTURE == 'amd64') && 'x64' || 'arm64' }}.${{ matrix.DISTRIBUTION }} /home/runner/work/wazuh-dashboard/wazuh-dashboard/artifacts/target/${{ env.ARTIFACT_BUILD_NAME }} - uses: actions/upload-artifact@v3 if: success() diff --git a/.github/workflows/build_wazuh_dashboard_with_plugins.yml b/.github/workflows/build_wazuh_dashboard_with_plugins.yml index dec43ac031f6..f55a16767374 100644 --- a/.github/workflows/build_wazuh_dashboard_with_plugins.yml +++ b/.github/workflows/build_wazuh_dashboard_with_plugins.yml @@ -19,6 +19,8 @@ on: options: - amd64 - x86_64 + - aarch64 + - arm64 default: amd64 revision: type: string @@ -91,11 +93,11 @@ jobs: steps: - name: Validate inputs run: | - if [ "${{ inputs.architecture }}" = "amd64" ] && [ "${{ inputs.system }}" = "rpm" ]; then + if [[ "${{ inputs.architecture }}" == "amd64" || "${{ inputs.architecture }}" == "arm64" ]] && [[ "${{ inputs.system }}" == "rpm" ]]; then echo "Invalid combination of architecture and system" exit 1 fi - if [ "${{ inputs.architecture }}" = "x86_64" ] && [ "${{ inputs.system }}" = "deb" ]; then + if [[ "${{ inputs.architecture }}" == "x86_64" || "${{ inputs.architecture }}" == "aarch64" ]] && [[ "${{ inputs.system }}" == "deb" ]]; then echo "Invalid combination of architecture and system" exit 1 fi @@ -106,6 +108,7 @@ jobs: uses: wazuh/wazuh-dashboard/.github/workflows/build_base.yml@4.10.2 with: CHECKOUT_TO: ${{ github.head_ref || github.ref_name }} + ARCHITECTURE: ${{ inputs.architecture }} build-main-plugins: needs: [validate-inputs] @@ -123,7 +126,7 @@ jobs: build-and-test-package: needs: [build-main-plugins, build-base, build-security-plugin] - runs-on: ubuntu-latest + runs-on: ${{ (inputs.architecture == 'arm64' || inputs.architecture == 'aarch64') && 'wz-linux-arm64' || 'ubuntu-22.04' }} name: Generate packages steps: - name: Checkout code @@ -147,9 +150,14 @@ jobs: - name: Setup packages names run: | - echo "WAZUH_DASHBOARD_SLIM=wazuh-dashboard_${{ env.VERSION }}-${{ env.REVISION }}_x64.tar.gz" >> $GITHUB_ENV + echo "WAZUH_DASHBOARD_SLIM=wazuh-dashboard_${{ env.VERSION }}-${{ env.REVISION }}_${{(inputs.ARCHITECTURE == 'x86_64' || inputs.ARCHITECTURE == 'amd64') && 'x64' || 'arm64'}}.tar.gz" >> $GITHUB_ENV echo "WAZUH_SECURITY_PLUGIN=wazuh-security-dashboards-plugin_${{ env.VERSION }}-${{ env.REVISION }}_${{ inputs.reference_security_plugins }}.zip" >> $GITHUB_ENV echo "WAZUH_PLUGINS=wazuh-dashboard-plugins_${{ env.VERSION }}-${{ env.REVISION }}_${{ inputs.reference_wazuh_plugins }}.zip" >> $GITHUB_ENV + if [[ "${{ inputs.architecture }}" == "x86_64" || "${{ inputs.architecture }}" == "amd64" ]]; then + echo "ARCHITECTURE_FLAG=" >> $GITHUB_ENV + else + echo "ARCHITECTURE_FLAG=--arm" >> $GITHUB_ENV + fi if [ "${{ inputs.system }}" = "deb" ]; then if [ "${{ inputs.is_stage }}" = "true" ]; then echo "PACKAGE_NAME=wazuh-dashboard_${{ env.VERSION }}-${{ inputs.revision }}_${{ inputs.architecture }}.deb" >> $GITHUB_ENV @@ -193,17 +201,16 @@ jobs: cd ${{ env.CURRENT_DIR }}/dev-tools/build-packages bash ./build-packages.sh \ -v ${{ env.VERSION }} \ - -r ${{ inputs.revision }} \ + -r ${{ inputs.revision }} ${{env.ARCHITECTURE_FLAG}}\ -a file://${{env.CURRENT_DIR}}/artifacts/wazuh-package.zip \ -s file://${{env.CURRENT_DIR}}/artifacts/security-package.zip \ -b file://${{env.CURRENT_DIR}}/artifacts/dashboard-package.zip \ - --${{ inputs.system }} ${{ env.PRODUCTION }} + --${{ inputs.system }} --debug ${{ env.PRODUCTION }} - name: Test package run: | cd ${{ env.CURRENT_DIR }}/dev-tools/test-packages - ls -la ${{ env.CURRENT_DIR }}/dev-tools/build-packages/output/${{ inputs.system }} - cp ${{ env.CURRENT_DIR }}/dev-tools/build-packages/output/${{ inputs.system }}/${{env.PACKAGE_NAME}} ${{ env.CURRENT_DIR }}/dev-tools/test-packages/${{ inputs.system }} + cp ${{ env.CURRENT_DIR }}/dev-tools/build-packages/output/${{env.PACKAGE_NAME}} ${{ env.CURRENT_DIR }}/dev-tools/test-packages/${{ inputs.system }} bash ./test-packages.sh \ -p ${{env.PACKAGE_NAME}} @@ -217,7 +224,7 @@ jobs: - name: Upload package run: | echo "Uploading package" - aws s3 cp ${{ env.CURRENT_DIR }}/dev-tools/build-packages/output/${{ inputs.system }}/${{env.PACKAGE_NAME}} s3://packages-dev.internal.wazuh.com/development/wazuh/4.x/main/packages/ + aws s3 cp ${{ env.CURRENT_DIR }}/dev-tools/build-packages/output/${{env.PACKAGE_NAME}} s3://packages-dev.internal.wazuh.com/development/wazuh/4.x/main/packages/ s3uri="s3://packages-dev.internal.wazuh.com/development/wazuh/4.x/main/packages/${{env.PACKAGE_NAME}}" echo "S3 URI: ${s3uri}" @@ -225,6 +232,6 @@ jobs: if: ${{ inputs.checksum }} run: | echo "Uploading checksum" - aws s3 cp ${{ env.CURRENT_DIR }}/dev-tools/build-packages/output/${{ inputs.system }}/${{env.PACKAGE_NAME}}.sha512 s3://packages-dev.internal.wazuh.com/development/wazuh/4.x/main/packages/ + aws s3 cp ${{ env.CURRENT_DIR }}/dev-tools/build-packages/output/${{env.PACKAGE_NAME}}.sha512 s3://packages-dev.internal.wazuh.com/development/wazuh/4.x/main/packages/ s3uri="s3://packages-dev.internal.wazuh.com/development/wazuh/4.x/main/packages/${{env.PACKAGE_NAME}}.sha512" echo "S3 sha512 URI: ${s3uri}" diff --git a/.gitignore b/.gitignore index cf91a061d892..b8559c25273c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ .aws-config.json .signing-config.json .ackrc -/dev-tools/build-packages/*/output +/dev-tools/build-packages/output +/dev-tools/build-packages/tmp /.opensearch /.chromium /package.json.bak @@ -71,4 +72,4 @@ snapshots.js .yarn-local-mirror # Ignore the generated antlr files -/src/plugins/data/public/antlr/opensearch_sql/grammar/.antlr \ No newline at end of file +/src/plugins/data/public/antlr/opensearch_sql/grammar/.antlr diff --git a/dev-tools/build-packages/README.md b/dev-tools/build-packages/README.md index 16f085dde78f..31bb1618f3de 100644 --- a/dev-tools/build-packages/README.md +++ b/dev-tools/build-packages/README.md @@ -1,70 +1,56 @@ # Package building -This folder contains tools used to create `rpm` and `deb` packages. + +This folder contains tools used to create `tar`, `rpm` and `deb` packages. ## Requirements - - A system with Docker. - - Internet connection (to download the docker images the first time). -## Builders +- A system with Docker. +- Internet connection (to download the docker images the first time). -### Tarball +## How to build packages -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 script `build-packages.sh` is in charge of coordinating the different steps to build each type of packages. -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. +### Pre-requisites -*Note:* use `file://` to indicate a local file. Otherwise, the script will try to download the file from the given URL. +The script needs 3 different zip files, containing the following respectively: -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 -``` +- The base of Wazuh Dashboard, generated by running `yarn build --linux --skip-os-packages --release` or `yarn build --linux-arm --skip-os-packages --release` for arm packages +- The build of each plugin in `wazuh-dashboard-plugins` repo +- The build of the `wazuh-security-dashboards-plugin` repo -### DEB +### Building packages -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://` -- `-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. +The script can build a `.tar.gz` (former base), and `rpm` and `deb` packages. This can be for x64 and arm architectures (it is not cross-architecture building. You need to run the script in a machine of the same architecture that you are building). -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 -``` +The inputs are the following: + +- `-a`, `--app`: URL or path to the zip that contains the `wazuh-dashboard-plugins` plugins build. +- `-b`, `--base`: URL or path to the zip that contains the `wazuh-dashboard build`. +- `-s`, `--security`: URL or path to the zip that contains the `wazuh-security-dashboards-plugin` build. +- `-v`, `--version`: Set the version of this build. +- `-r`, `--revision`: [Optional] Set the revision of this build. By default, it is set to 1. +- `--all-platforms`: Build all platforms. +- `--deb`: Build deb. +- `--rpm`: Build rpm. +- `--tar`: Build tar.gz. +- `--production`:[Optional] The naming of the package will be ready for production. Otherwise, it will include the hash of the current commit. +- `--arm`: [Optional] Build for arm64 instead of x64. +- `--debug`: [Optional] Enables debug mode, which will show detailed information during the script run. +- `--silent`: [Optional] Enables silent mode, which will show the minimum possible information during the script run. `--debug` has priority over this. -### RPM +> [!IMPORTANT] +> In the inputs where a local path is available, use `file://` to indicate it. -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://` -- `-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. +> [!WARNING] +> To build `arm` packages, you need to run the script in an arm machine, and use an arm build of the wazuh-dashboard base with `-b` 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 +bash build-packages.sh \ + --app file:///home/user/packages/wazuh-package.zip \ + --base file:///home/user/packages/dashboard-package.zip \ + --security file:///home/user/packages/security-package.zip \ + --version 4.10.0 --revision 2 --deb --silent ``` - -[wazuh-plugin]: https://github.com/wazuh/wazuh-kibana-app -[wazuh-security-plugin]: https://github.com/wazuh/wazuh-security-dashboards-plugin \ No newline at end of file diff --git a/dev-tools/build-packages/base/Docker/Dockerfile b/dev-tools/build-packages/base/Docker/Dockerfile new file mode 100644 index 000000000000..6614bda6ce49 --- /dev/null +++ b/dev-tools/build-packages/base/Docker/Dockerfile @@ -0,0 +1,35 @@ +FROM rockylinux:8.5 + +# Install all the necessary tools to build the packages +RUN yum clean all && yum update -y +RUN yum install -y \ + curl \ + tar \ + findutils \ + git \ + xz \ + gcc \ + make \ + bc \ + sed \ + gzip \ + autoconf \ + automake \ + libtool \ + python3-devel \ + python3-pip \ + jq \ + unzip + +RUN git clone https://github.com/google/brotli.git -b v1.0.9 + +RUN cd brotli && chmod +x ./bootstrap && ./bootstrap && ./configure --prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/lib64/brotli --libdir=/usr/lib64/brotli --datarootdir=/usr/share --mandir=/usr/share/man/man1 --docdir=/usr/share/doc \ + && make && make install + +# Add the scripts to build the RPM package +ADD base-builder.sh /usr/local/bin/builder +ADD plugins /usr/local/bin/plugins +RUN chmod +x /usr/local/bin/builder + +# Set the entrypoint +ENTRYPOINT ["/usr/local/bin/builder"] diff --git a/dev-tools/build-packages/base/base-builder.sh b/dev-tools/build-packages/base/base-builder.sh new file mode 100755 index 000000000000..89ae12037e15 --- /dev/null +++ b/dev-tools/build-packages/base/base-builder.sh @@ -0,0 +1,162 @@ +#!/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 +version="$1" +revision="$2" +architecture="$3" +verbose="$4" + +if [ "$verbose" = "debug" ]; then + set -x +fi + +trap clean INT +trap clean EXIT + +log() { + if [ "$verbose" = "info" ] || [ "$verbose" = "debug" ]; then + echo "$@" + fi +} + +clean() { + exit_code=$? + # Clean the files + rm -rf ${tmp_dir}/* + trap '' EXIT + exit ${exit_code} +} + +# Paths +current_path="$( cd $(dirname $0) ; pwd -P )" + +# Folders +tmp_dir="/tmp" +out_dir="/output" +config_path=$tmp_dir/config + + +# ----------------------------------------------------------------------------- +cd $tmp_dir + +log +log "Extracting packages" +log + +mkdir -p applications +mkdir -p base +packages_list=(app base security) +packages_names=("Wazuh plugins" "Wazuh Dashboard" "Security plugin") + + +for i in "${!packages_list[@]}"; do + package_var="${packages_list[$i]}" + package_name="${packages_names[$i]}" + if [[ "$package_var" == "base" ]]; then + wzd_package_name=$(unzip -l "packages/${package_var}.zip" | awk 'NR==4 {print $4}') + unzip -o -q "packages/${package_var}.zip" -d base + else + unzip -o -q "packages/${package_var}.zip" -d applications + fi +done + +cd base + +log +log "Installing plugins" +log + +tar -zxf $wzd_package_name +directory_name=$(ls -td */ | head -1) +cd $directory_name +plugins=$(ls $tmp_dir/applications)' '$(cat $current_path/plugins) + for plugin in $plugins; do + if [[ $plugin =~ .*\.zip ]]; then + install="file://${tmp_dir}/applications/${plugin}" + else + install=$plugin + fi + log "Installing ${plugin} plugin" + if ! bin/opensearch-dashboards-plugin install $install 2>&1 --allow-root> /dev/null; then + echo "Plugin ${plugin} installation failed" + exit 1 + fi + log "Plugin ${plugin} installed successfully" + log + done + +log +log "Replacing application categories" +log + +category_explore='{id:"explore",label:"Explore",order:100,euiIconType:"search"}' +category_dashboard_management='{id:"management",label:"Indexer management",order:6e3,euiIconType:"managementApp"}' + +# Replace app category to Reporting app +sed -i -e "s|category:{id:\"opensearch\",label:_i18n.i18n.translate(\"opensearch.reports.categoryName\",{defaultMessage:\"OpenSearch Plugins\"}),order:2e3}|category:${category_explore}|" ./plugins/reportsDashboards/target/public/reportsDashboards.plugin.js + +# Replace app category to Alerting app +sed -i -e "s|category:{id:\"opensearch\",label:\"OpenSearch Plugins\",order:2e3}|category:${category_explore}|" ./plugins/alertingDashboards/target/public/alertingDashboards.plugin.js + +# Replace app category to Maps app +sed -i -e "s|category:{id:\"opensearch\",label:\"OpenSearch Plugins\",order:2e3}|category:${category_explore}|" ./plugins/customImportMapDashboards/target/public/customImportMapDashboards.plugin.js + +# Replace app category to Notifications app +sed -i -e "s|category:DEFAULT_APP_CATEGORIES.management|category:${category_explore}|" ./plugins/notificationsDashboards/target/public/notificationsDashboards.plugin.js + +# Replace app category to Index Management app +sed -i -e "s|category:DEFAULT_APP_CATEGORIES.management|category:${category_dashboard_management}|g" ./plugins/indexManagementDashboards/target/public/indexManagementDashboards.plugin.js + + +log +log "Recreating plugin files" +log + +# Generate compressed files +files_to_recreate=( + ./plugins/reportsDashboards/target/public/reportsDashboards.plugin.js + ./plugins/alertingDashboards/target/public/alertingDashboards.plugin.js + ./plugins/customImportMapDashboards/target/public/customImportMapDashboards.plugin.js + ./plugins/notificationsDashboards/target/public/notificationsDashboards.plugin.js + ./plugins/indexManagementDashboards/target/public/indexManagementDashboards.plugin.js +) + +for value in "${files_to_recreate[@]}" +do + gzip -c "$value" > "$value.gz" + brotli -c "$value" > "$value.br" +done + +log +log "Adding configuration files" +log + +cp -f $config_path/opensearch_dashboards.prod.yml config/opensearch_dashboards.yml +cp -f $config_path/node.options.prod config/node.options + +log +log "Fixing shebangs" +log +# 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' {} + +log +log "Compressing final package" +log + +mkdir -p $out_dir +tar -czf $out_dir/wazuh-dashboard-$version-$revision-linux-$architecture.tar.gz * + +log Done! diff --git a/dev-tools/build-packages/base/generate_base.sh b/dev-tools/build-packages/base/generate_base.sh deleted file mode 100755 index 6b45e7d35cff..000000000000 --- a/dev-tools/build-packages/base/generate_base.sh +++ /dev/null @@ -1,292 +0,0 @@ -#!/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 plugins..." - echo - mkdir -p $tmp_dir - cd $tmp_dir - mkdir -p applications - mkdir -p dashboards - if [[ $app =~ $valid_url ]]; then - if ! curl --output applications/app.zip --silent --fail "${app}"; then - echo "The given URL or Path to the Wazuh Apps is not working: ${app}" - clean 1 - else - echo "Extracting applications from app.zip" - unzip -q applications/app.zip -d applications - rm applications/app.zip - fi - else - echo "The given URL or Path to the Wazuh App is not valid: ${app}" - clean 1 - fi - - echo - echo "Downloading dashboards..." - echo - if [[ $base =~ $valid_url ]]; then - if [[ $base =~ .*\.zip ]]; then - if ! curl --output wazuh-dashboard.zip --silent --fail "${base}"; then - echo "The given URL or Path to the Wazuh Dashboard base is not working: ${base}" - clean 1 - else - echo "Extracting Wazuh Dashboard base" - unzip -q wazuh-dashboard.zip -d ./dashboards/ - rm wazuh-dashboard.zip - mv ./dashboards/$(ls ./dashboards) wazuh-dashboard.tar.gz - fi - else - 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 - fi - else - echo "The given URL or Path to the Wazuh Dashboard base is not valid: ${base}" - clean 1 - fi - - echo - echo "Downloading security plugin..." - echo - - if [[ $security =~ $valid_url ]]; then - if ! curl --output applications/security.zip --silent --fail "${security}"; then - echo "The given URL or Path to the Wazuh Security Plugin is not working: ${security}" - clean 1 - else - echo "Extracting Security application" - unzip -q applications/security.zip -d applications - rm applications/security.zip - 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=$(ls -td */ | head -1) - working_dir="wazuh-dashboard-$version-$revision-linux-x64" - mv $directory_name $working_dir - cd $working_dir - - echo - echo Building the package... - echo - - # Install Wazuh apps and Security app - - plugins=$(ls $tmp_dir/applications)' '$(cat $current_path/plugins) - for plugin in $plugins; do - if [[ $plugin =~ .*\.zip ]]; then - install='file:../applications/'$plugin - else - install=$plugin - fi - echo "Installing ${plugin%.*} plugin" - if ! bin/opensearch-dashboards-plugin install $install 2>&1 > /dev/null; then - echo "Plugin installation failed" - clean 1 - fi - done - - echo - echo Finished installing plugins - echo - - # Move installed plugins from categories after generating the package - category_explore='{id:"explore",label:"Explore",order:100,euiIconType:"search"}' - category_dashboard_management='{id:"management",label:"Indexer management",order:5e3,euiIconType:"managementApp"}' - - # Replace app category to Reporting app - sed -i -e "s|category:{id:\"opensearch\",label:_i18n.i18n.translate(\"opensearch.reports.categoryName\",{defaultMessage:\"OpenSearch Plugins\"}),order:2e3}|category:${category_explore}|" ./plugins/reportsDashboards/target/public/reportsDashboards.plugin.js - - # Replace app category to Alerting app - sed -i -e "s|category:{id:\"opensearch\",label:\"OpenSearch Plugins\",order:2e3}|category:${category_explore}|" ./plugins/alertingDashboards/target/public/alertingDashboards.plugin.js - - # Replace app category to Maps app - sed -i -e "s|category:{id:\"opensearch\",label:\"OpenSearch Plugins\",order:2e3}|category:${category_explore}|" ./plugins/customImportMapDashboards/target/public/customImportMapDashboards.plugin.js - - # Replace app category to Notifications app - sed -i -e "s|category:DEFAULT_APP_CATEGORIES.management|category:${category_explore}|" ./plugins/notificationsDashboards/target/public/notificationsDashboards.plugin.js - - # Replace app category to Indexer Management app - sed -i -e "s|category:DEFAULT_APP_CATEGORIES.management|category:${category_dashboard_management}|g" ./plugins/indexManagementDashboards/target/public/indexManagementDashboards.plugin.js - - # Generate compressed files - files_to_recreate=( - ./plugins/reportsDashboards/target/public/reportsDashboards.plugin.js - ./plugins/alertingDashboards/target/public/alertingDashboards.plugin.js - ./plugins/customImportMapDashboards/target/public/customImportMapDashboards.plugin.js - ./plugins/notificationsDashboards/target/public/notificationsDashboards.plugin.js - ./plugins/indexManagementDashboards/target/public/indexManagementDashboards.plugin.js - ) - - for value in "${files_to_recreate[@]}" - do - gzip -c "$value" > "$value.gz" - brotli -c "$value" > "$value.br" - done - - # 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 .. - if [ ! -d "$out_dir" ]; then - mkdir -p $out_dir - fi - tar -czf $out_dir/$working_dir.tar.gz $working_dir - - echo - echo DONE! - echo - clean 0 -} - -# ----------------------------------------------------------------------------- - -help() { - echo - echo "Usage: $0 [OPTIONS]" - echo " -a, --app Set the location of the .zip file containing the Wazuh plugin." - echo " -b, --base Set the location of the .tar.gz file containing the base wazuh-dashboard build." - echo " -s, --security Set the location of the .zip file containing the wazuh-security-dashboards-plugin." - echo " -v, --version Set the version of this build." - echo " -r, --revision [Optional] Set the revision of this build. By default, it is set to 1." - echo " -o, --output [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 "$@" diff --git a/dev-tools/build-packages/build-packages.sh b/dev-tools/build-packages/build-packages.sh index bf2d93f22dc3..71100a6429af 100755 --- a/dev-tools/build-packages/build-packages.sh +++ b/dev-tools/build-packages/build-packages.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -e app="" base="" @@ -9,59 +10,136 @@ all_platforms="no" deb="no" rpm="no" tar="no" -output="$( cd $(dirname $0) ; pwd -P )/output" -production="" +architecture="x64" +production="no" +commit_sha=$(git rev-parse --short HEAD) current_path="$( cd $(dirname $0) ; pwd -P )" +output_dir="${current_path}/output" +tmp_dir="${current_path}/tmp" +config_dir="${current_path}/../../config" +package_config_dir="${current_path}/config" +verbose="info" -build_tar() { - echo "Building tar package..." - cd ./base - bash ./generate_base.sh -a $app -b $base -s $security -v $version -r $revision +trap clean INT +trap clean EXIT - name_package_tar=$(ls ./output) +log() { + if [ "$verbose" = "info" ] || [ "$verbose" = "debug" ]; then + echo "$@" + fi +} - echo "Moving tar package to $output" - mv $current_path/base/output/$name_package_tar $output/$name_package_tar - cd ../ +clean() { + exit_code=$? + echo + echo "Cleaning temporary files..." + echo + # Clean the files + rm -rf ${tmp_dir} + rm -f ${current_path}/base/Docker/base-builder.sh + rm -f ${current_path}/base/Docker/plugins + rm -f ${current_path}/rpm/Docker/rpm-builder.sh + rm -f ${current_path}/rpm/Docker/wazuh-dashboard.spec + rm -f ${current_path}/deb/Docker/deb-builder.sh + rm -rf ${current_path}/deb/Docker/debian + trap '' EXIT + exit ${exit_code} } -build_deb() { - echo "Building deb package..." - name_package_tar=$(find $output -name "*.tar.gz") - cd ./deb - bash ./launcher.sh -v $version -r $revision -p file://$name_package_tar $production - name_package_tar=$(ls ./output) - echo "Moving deb package to $output/deb" - mv $current_path/deb/output $output/deb - cd ../ +ctrl_c() { + clean 1 +} + +get_packages(){ + packages_list=(app base security) + packages_names=("Wazuh plugins" "Wazuh Dashboard" "Security plugin") + valid_url='(https?|ftp|file)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]' + mkdir -p ${tmp_dir} + cd ${tmp_dir} + mkdir -p packages + for i in "${!packages_list[@]}"; do + package_var="${packages_list[$i]}" + package_name="${packages_names[$i]}" + package_url="${!package_var}" + + log + log "Downloading ${package_name}" + + if [[ $package_url =~ $valid_url ]]; then + if ! curl --output "packages/${package_var}.zip" --silent --fail "${package_url}"; then + echo "The given URL or Path to the ${package_name} is not working: ${package_url}" + clean 1 + fi + else + echo "The given URL or Path to the ${package_name} is not valid: ${package_url}" + clean 1 + fi + log "Done!" + log + done + cd .. +} + +build_tar() { + log + log "Building base package..." + log + mkdir -p ${output_dir} + cp -r ${config_dir} ${tmp_dir} + cd ./base + dockerfile_path="${current_path}/base/Docker" + container_name="dashboard-base-builder" + cp ./base-builder.sh ${dockerfile_path} + cp ./plugins ${dockerfile_path} + docker build -t ${container_name} ${dockerfile_path} || return 1 + docker run -t --rm -v ${tmp_dir}/:/tmp:Z -v ${output_dir}/:/output:Z\ + ${container_name} ${version} ${revision} ${architecture} ${verbose}|| return 1 + cd .. } build_rpm() { - echo "Building rpm package..." - name_package_tar=$(find $output -name "*.tar.gz") + log "Building rpm package..." cd ./rpm - bash ./launcher.sh -v $version -r $revision -p file://$name_package_tar $production - echo "Moving rpm package to $output/rpm" - mv $current_path/rpm/output $output/rpm + dockerfile_path="${current_path}/rpm/Docker" + container_name="dashboard-rpm-builder" + cp -r ${package_config_dir} ${tmp_dir} + cp ./rpm-builder.sh ${dockerfile_path} + cp ./wazuh-dashboard.spec ${dockerfile_path} + docker build -t ${container_name} ${dockerfile_path} || return 1 + docker run -t --rm -v ${tmp_dir}/:/tmp:Z -v ${output_dir}/:/output:Z\ + ${container_name} ${version} ${revision} ${architecture}\ + ${commit_sha} ${production} ${verbose}|| return 1 cd ../ } -build() { - name_package_tar="wazuh-dashboard-$version-$revision-linux-x64.tar.gz" +build_deb() { + log "Building deb package..." + cd ./deb + dockerfile_path="${current_path}/deb/Docker" + container_name="dashboard-deb-builder" + cp -r ${package_config_dir} ${tmp_dir} + cp ./deb-builder.sh ${dockerfile_path} + cp -r ./debian ${dockerfile_path} + docker build -t ${container_name} ${dockerfile_path} || return 1 + docker run -t --rm -v ${tmp_dir}/:/tmp:Z -v ${output_dir}/:/output:Z \ + ${container_name} ${version} ${revision} ${architecture}\ + ${commit_sha} ${production} ${verbose}|| return 1 + cd .. +} - if [ ! -d "$output" ]; then - mkdir $output - fi + + +build(){ + log "Building package..." if [ "$all_platforms" == "yes" ]; then deb="yes" rpm="yes" tar="yes" fi - + get_packages build_tar - cd $current_path if [ $deb == "yes" ]; then echo "Building deb package..." @@ -75,7 +153,7 @@ build() { if [ "$tar" == "no" ]; then echo "Removing tar package..." - rm -r $(find $output -name "*.tar.gz") + rm -r $(find $output_dir -name "*.tar.gz") fi } @@ -91,8 +169,10 @@ help() { echo " --rpm Build for rpm." echo " --tar Build for tar." echo " --production [Optional] The naming of the package will be ready for production." + echo " --arm [Optional] Build for arm64 instead of x64." + echo " --debug [Optional] Debug mode." + echo " --silent [Optional] Silent mode. Will not work if --debug is set." echo " -r, --revision [Optional] Set the revision of this build. By default, it is set to 1." - echo " -o, --output [Optional] Set the destination path of package. By default, an output folder will be created." echo " -h, --help Show this help." echo exit $1 @@ -145,7 +225,7 @@ main() { fi ;; "--production") - production="--production" + production="yes" shift 1 ;; "--all-platforms") @@ -164,6 +244,19 @@ main() { tar="yes" shift 1 ;; + "--arm") + architecture="arm64" + shift 1 + ;; + "--silent") + verbose="silent" + shift 1 + ;; + "--debug") + verbose="debug" + shift 1 + ;; + "-o" | "--output") if [ -n "${2}" ]; then output="${2}" @@ -188,6 +281,10 @@ main() { help 1 fi + if [ "$verbose" = "debug" ]; then + set -x + fi + build || exit 1 exit 0 diff --git a/dev-tools/build-packages/deb/docker/amd64/Dockerfile b/dev-tools/build-packages/deb/Docker/Dockerfile similarity index 63% rename from dev-tools/build-packages/deb/docker/amd64/Dockerfile rename to dev-tools/build-packages/deb/Docker/Dockerfile index f3ca3b981113..6289cd35d3ad 100644 --- a/dev-tools/build-packages/deb/docker/amd64/Dockerfile +++ b/dev-tools/build-packages/deb/Docker/Dockerfile @@ -1,16 +1,17 @@ FROM debian:10 -ENV DEBIAN_FRONTEND noninteractive +ENV DEBIAN_FRONTEND=noninteractive # Installing necessary packages RUN apt-get update && apt-get install -y --allow-change-held-packages apt-utils && \ apt-get install -y --allow-change-held-packages \ curl sudo wget expect gnupg build-essential \ - devscripts equivs selinux-basics procps gawk + devscripts equivs selinux-basics procps gawk jq # Add the script to build the Debian package -ADD builder.sh /usr/local/bin/builder +ADD deb-builder.sh /usr/local/bin/builder +ADD debian /usr/local/src/debian RUN chmod +x /usr/local/bin/builder # Set the entrypoint -ENTRYPOINT ["/usr/local/bin/builder"] \ No newline at end of file +ENTRYPOINT ["/usr/local/bin/builder"] diff --git a/dev-tools/build-packages/deb/builder.sh b/dev-tools/build-packages/deb/deb-builder.sh similarity index 53% rename from dev-tools/build-packages/deb/builder.sh rename to dev-tools/build-packages/deb/deb-builder.sh index 925997dd4b12..3e0e993d8c7a 100755 --- a/dev-tools/build-packages/deb/builder.sh +++ b/dev-tools/build-packages/deb/deb-builder.sh @@ -12,13 +12,58 @@ set -e # Script parameters to build the package target="wazuh-dashboard" -architecture=$1 +version=$1 revision=$2 -version=$3 +architecture=$3 commit_sha=$4 is_production=$5 +verbose=$6 + directory_base="/usr/share/wazuh-dashboard" +# Paths +current_path="$( cd $(dirname $0) ; pwd -P )" + +# Folders +tmp_dir="/tmp" +out_dir="/output" +config_path=$tmp_dir/config + +if [ "$verbose" = "debug" ]; then + set -x +fi +trap clean INT +trap clean EXIT + +log() { + if [ "$verbose" = "info" ] || [ "$verbose" = "debug" ]; then + echo "$@" + fi +} + +clean() { + exit_code=$? + # Clean the files + rm -rf ${tmp_dir}/* + trap '' EXIT + exit ${exit_code} +} + +mkdir -p ${tmp_dir}/wazuh-dashboard-base +cd ${tmp_dir}/wazuh-dashboard-base +log "Extracting base tar.gz..." +tar -zxf ${out_dir}/wazuh-dashboard-$version-$revision-linux-$architecture.tar.gz +log "Preparing the package..." +jq '.wazuh.revision="'${revision}'"' package.json > pkgtmp.json && mv pkgtmp.json package.json +cp $config_path/* . +echo ${version} > VERSION +cd .. +tar -czf wazuh-dashboard.tar.gz wazuh-dashboard-base + +log "Setting up parameters" +if [ "${architecture}" = "x64" ]; then + architecture="amd64" +fi # Build directories build_dir=/build pkg_name="${target}-${version}" @@ -29,8 +74,8 @@ final_name="${target}_${version}-${revision}_${architecture}_${commit_sha}.deb" mkdir -p ${source_dir}/debian -# Including spec file -cp -r /root/build-packages/deb/debian/* ${source_dir}/debian/ +# Including spec files +cp -r /usr/local/src/debian/* ${source_dir}/debian/ # Generating directory structure to build the .deb package cd ${build_dir}/${target} && tar -czf ${pkg_name}.orig.tar.gz "${pkg_name}" @@ -44,17 +89,18 @@ sed -i "s:export INSTALLATION_DIR=.*:export INSTALLATION_DIR=${directory_base}:g cd ${source_dir} mk-build-deps -ir -t "apt-get -o Debug::pkgProblemResolver=yes -y" +log "Building the package..." # Build package debuild --no-lintian -b -uc -us \ -eINSTALLATION_DIR="${directory_base}" \ -eVERSION="${version}" \ -eREVISION="${revision}" -cd ${pkg_path} && sha512sum ${deb_file} >/tmp/${deb_file}.sha512 +cd ${pkg_path} && sha512sum ${deb_file} >/${out_dir}/${deb_file}.sha512 if [ "${is_production}" = "no" ]; then - mv ${pkg_path}/${deb_file} /tmp/${final_name} - mv /tmp/${deb_file}.sha512 /tmp/${final_name}.sha512 + mv ${pkg_path}/${deb_file} /${out_dir}/${final_name} + mv /${out_dir}/${deb_file}.sha512 /${out_dir}/${final_name}.sha512 else - mv ${pkg_path}/${deb_file} /tmp/ + mv ${pkg_path}/${deb_file} /${out_dir}/ fi diff --git a/dev-tools/build-packages/deb/debian/rules b/dev-tools/build-packages/deb/debian/rules index 7fe98ddcc597..ac2087b35ef0 100644 --- a/dev-tools/build-packages/deb/debian/rules +++ b/dev-tools/build-packages/deb/debian/rules @@ -52,7 +52,7 @@ override_dh_auto_install: # ----------------------------------------------------------------------------- override_dh_install: - cp /opt/$(DASHBOARD_FILE) ./ + cp /tmp/$(DASHBOARD_FILE) ./ groupadd $(GROUP) useradd -g $(GROUP) $(USER) diff --git a/dev-tools/build-packages/deb/launcher.sh b/dev-tools/build-packages/deb/launcher.sh deleted file mode 100755 index dcab4b3653f0..000000000000 --- a/dev-tools/build-packages/deb/launcher.sh +++ /dev/null @@ -1,204 +0,0 @@ -#!/bin/bash - -# Wazuh package generator -# Copyright (C) 2021, 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. - -# Inputs -package="" -version="" -revision="0" -architecture="amd64" -build_base="yes" -build_docker="yes" -is_production="no" - -# Constants -deb_amd64_builder="deb_dashboard_builder_amd64" -deb_builder_dockerfile="${current_path}/docker" -commit_sha=$(git rev-parse --short HEAD) - -# 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 - rm $current_path/docker/amd64/*.sh - if [ $exit_code != 0 ]; then - rm $out_dir/* - rmdir $out_dir - fi - - exit ${exit_code} -} - -ctrl_c() { - clean 1 -} - -build_deb() { - container_name="$1" - dockerfile_path="$2" - - # 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 [[ $package =~ $valid_url ]]; then - if ! curl --output wazuh-dashboard.tar.gz --silent --fail "${package}"; then - echo "The given URL or Path to the Wazuh Dashboard package is not working: ${package}" - clean 1 - fi - else - echo "The given URL or Path to the Wazuh Dashboard package is not valid: ${package}" - clean 1 - fi - - echo - echo Building the package... - echo - - # Prepare the package - tar -zxf wazuh-dashboard.tar.gz - directory_name=$(ls -td */ | head -1) - rm wazuh-dashboard.tar.gz - mv $directory_name wazuh-dashboard-base - jq '.wazuh.revision="'${revision}'"' wazuh-dashboard-base/package.json > pkgtmp.json && mv pkgtmp.json wazuh-dashboard-base/package.json - cp $config_path/* wazuh-dashboard-base - echo ${version} >wazuh-dashboard-base/VERSION - tar -czf ./wazuh-dashboard.tar.gz wazuh-dashboard-base - - # Copy the necessary files - cp ${current_path}/builder.sh ${dockerfile_path} - - # Build the Docker image - if [[ ${build_docker} == "yes" ]]; then - docker build -t ${container_name} ${dockerfile_path} || return 1 - fi - # Build the Debian package with a Docker container - if [ ! -d "$out_dir" ]; then - mkdir -p $out_dir - fi - - volumes="-v ${out_dir}/:/tmp:Z -v ${tmp_dir}/wazuh-dashboard.tar.gz:/opt/wazuh-dashboard.tar.gz" - docker run -t --rm ${volumes} \ - -v ${current_path}/../..:/root:Z \ - ${container_name} ${architecture} \ - ${revision} ${version} ${commit_sha} ${is_production}\ - || return 1 - - echo "Package $(ls -Art ${out_dir} | tail -n 1) added to ${out_dir}." - - echo - echo DONE! - echo - - return 0 -} - -build() { - build_name="${deb_amd64_builder}" - file_path="../${deb_builder_dockerfile}/${architecture}" - build_deb ${build_name} ${file_path} ${commit_sha} ${is_production}|| return 1 - return 0 -} - -help() { - echo - echo "Usage: $0 [OPTIONS]" - echo - echo " -v, --version Wazuh version" - echo " -p, --package Set the location of the .tar.gz file containing the Wazuh Dashboard package." - echo " -r, --revision [Optional] Package revision. By default: 1." - echo " -o, --output [Optional] Set the destination path of package. By default, an output folder will be created." - echo " --dont-build-docker [Optional] Locally built Docker image will be used instead of generating a new one." - echo " --production [Optional] The naming of the package will be ready for production." - echo " -h, --help Show this help." - echo - exit $1 -} - -main() { - while [ -n "${1}" ]; do - case "${1}" in - "-h" | "--help") - help 0 - ;; - "-p" | "--package") - if [ -n "${2}" ]; then - package="${2}" - shift 2 - else - help 1 - fi - ;; - "-v" | "--version") - if [ -n "${2}" ]; then - version="${2}" - shift 2 - else - help 1 - fi - ;; - "-r" | "--revision") - if [ -n "${2}" ]; then - revision="${2}" - shift 2 - else - help 1 - fi - ;; - "--dont-build-docker") - build_docker="no" - shift 1 - ;; - "--production") - is_production="yes" - shift 1 - ;; - "-o" | "--output") - if [ -n "${2}" ]; then - out_dir="${2}" - shift 2 - else - help 1 - fi - ;; - *) - help 1 - ;; - esac - done - - if [ -z "$package" ] | [ -z "$version" ]; then - help 1 - fi - - build || clean 1 - - clean 0 -} - -main "$@" diff --git a/dev-tools/build-packages/rpm/docker/x86_64/Dockerfile b/dev-tools/build-packages/rpm/Docker/Dockerfile similarity index 80% rename from dev-tools/build-packages/rpm/docker/x86_64/Dockerfile rename to dev-tools/build-packages/rpm/Docker/Dockerfile index eb6493d15878..c62abf3820c1 100644 --- a/dev-tools/build-packages/rpm/docker/x86_64/Dockerfile +++ b/dev-tools/build-packages/rpm/Docker/Dockerfile @@ -5,16 +5,17 @@ RUN yum clean all && yum update -y RUN yum install -y openssh-clients sudo gnupg \ yum-utils epel-release redhat-rpm-config rpm-devel \ zlib zlib-devel rpm-build autoconf automake \ - glibc-devel libtool perl + glibc-devel libtool perl jq -#RUN yum install -y --nobest https://repo.ius.io/ius-release-el7.rpm +#RUN yum install -y --nobest https://repo.ius.io/ius-release-el7.rpm RUN yum update -y && yum install -y python3 RUN ln -fs /usr/bin/python3.6 /usr/bin/python # Add the scripts to build the RPM package -ADD builder.sh /usr/local/bin/builder +ADD rpm-builder.sh /usr/local/bin/builder +ADD wazuh-dashboard.spec /usr/local/src/wazuh-dashboard.spec RUN chmod +x /usr/local/bin/builder # Set the entrypoint diff --git a/dev-tools/build-packages/rpm/builder.sh b/dev-tools/build-packages/rpm/builder.sh deleted file mode 100755 index 63f5043a77b8..000000000000 --- a/dev-tools/build-packages/rpm/builder.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - -# Wazuh package builder -# Copyright (C) 2021, 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 - -# Script parameters to build the package -target="wazuh-dashboard" -architecture=$1 -revision=$2 -version=$3 -commit_sha=$4 -is_production=$5 -directory_base="/usr/share/wazuh-dashboard" - -# Build directories -build_dir=/build -rpm_build_dir=${build_dir}/rpmbuild -pkg_name=${target}-${version} -pkg_path="${rpm_build_dir}/RPMS/${architecture}" -file_name="${target}-${version}-${revision}" -rpm_file="${file_name}.${architecture}.rpm" -final_name="${target}_${version}-${revision}_${architecture}_${commit_sha}.rpm" - -mkdir -p ${rpm_build_dir}/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} - -# Prepare the sources directory to build the source tar.gz -mkdir ${build_dir}/${pkg_name} - -# Including spec file -cp /root/build-packages/rpm/${target}.spec ${rpm_build_dir}/SPECS/${pkg_name}.spec - -# Generating source tar.gz -cd ${build_dir} && tar czf "${rpm_build_dir}/SOURCES/${pkg_name}.tar.gz" "${pkg_name}" - -# Building RPM -/usr/bin/rpmbuild -v \ - --define "_topdir ${rpm_build_dir}" \ - --define "_version ${version}" \ - --define "_release ${revision}" \ - --define "_localstatedir ${directory_base}" \ - --target ${architecture} \ - -ba ${rpm_build_dir}/SPECS/${pkg_name}.spec - -cd ${pkg_path} && sha512sum ${rpm_file} >/tmp/${rpm_file}.sha512 - -find ${pkg_path}/ -maxdepth 3 -type f -name "${file_name}*" -exec mv {} /tmp/ \; -if [ "${is_production}" = "no" ]; then - mv /tmp/${rpm_file} /tmp/${final_name} - mv /tmp/${rpm_file}.sha512 /tmp/${final_name}.sha512 -fi diff --git a/dev-tools/build-packages/rpm/launcher.sh b/dev-tools/build-packages/rpm/launcher.sh deleted file mode 100755 index 9695f9e8c4df..000000000000 --- a/dev-tools/build-packages/rpm/launcher.sh +++ /dev/null @@ -1,207 +0,0 @@ -#!/bin/bash - -# Wazuh package generator -# Copyright (C) 2021, 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. - -# Inputs -package="" -version="" -revision="0" -architecture="x86_64" -build_base="yes" -build_docker="yes" -is_production="no" - -# Constants -rpm_x86_builder="rpm_dashboard_builder_x86" -rpm_builder_dockerfile="${current_path}/docker" -commit_sha=$(git rev-parse --short HEAD) - -# 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 - rm $current_path/docker/x86_64/*.sh - - if [ $exit_code != 0 ]; then - rm $out_dir/* - rmdir $out_dir - fi - - exit ${exit_code} -} - -ctrl_c() { - clean 1 -} - -build_rpm() { - container_name="$1" - dockerfile_path="$2" - - # 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 [[ $package =~ $valid_url ]]; then - if ! curl --output wazuh-dashboard.tar.gz --silent --fail "${package}"; then - echo "The given URL or Path to the Wazuh Dashboard package is not working: ${package}" - clean 1 - fi - else - echo "The given URL or Path to the Wazuh Dashboard package is not valid: ${package}" - clean 1 - fi - - echo - echo Building the package... - echo - - # Prepare the package - tar -zxf wazuh-dashboard.tar.gz - directory_name=$(ls -td */ | head -1) - rm wazuh-dashboard.tar.gz - mv $directory_name wazuh-dashboard-base - jq '.wazuh.revision="'${revision}'"' wazuh-dashboard-base/package.json > pkgtmp.json && mv pkgtmp.json wazuh-dashboard-base/package.json - mkdir -p wazuh-dashboard-base/etc/services - cp $config_path/* wazuh-dashboard-base/etc/services - echo ${version} >wazuh-dashboard-base/VERSION - tar -czf ./wazuh-dashboard.tar.gz wazuh-dashboard-base - - # Copy the necessary files - cp ${current_path}/builder.sh ${dockerfile_path} - - # Build the Docker image - if [[ ${build_docker} == "yes" ]]; then - docker build -t ${container_name} ${dockerfile_path} || return 1 - fi - - # Build the RPM package with a Docker container - - if [ ! -d "$out_dir" ]; then - mkdir -p $out_dir - fi - volumes="-v ${out_dir}/:/tmp:Z -v ${tmp_dir}/wazuh-dashboard.tar.gz:/opt/wazuh-dashboard.tar.gz" - docker run -t --rm ${volumes} \ - -v ${current_path}/../..:/root:Z \ - ${container_name} ${architecture} \ - ${revision} ${version} ${commit_sha} ${is_production}\ - || return 1 - - echo "Package $(ls -Art ${out_dir} | tail -n 1) added to ${out_dir}." - - echo - echo DONE! - echo - - return 0 -} - -build() { - build_name="${rpm_x86_builder}" - file_path="../${rpm_builder_dockerfile}/${architecture}" - build_rpm ${build_name} ${file_path} ${commit_sha} ${is_production}|| return 1 - return 0 -} - -help() { - echo - echo "Usage: $0 [OPTIONS]" - echo - echo " -v, --version Wazuh version" - echo " -p, --package Set the location of the .tar.gz file containing the Wazuh Dashboard package." - echo " -r, --revision [Optional] Package revision. By default: 1." - echo " -o, --output [Optional] Set the destination path of package. By default, an output folder will be created." - echo " --dont-build-docker [Optional] Locally built Docker image will be used instead of generating a new one." - echo " --production [Optional] The naming of the package will be ready for production." - echo " -h, --help Show this help." - echo - exit $1 -} - -main() { - while [ -n "${1}" ]; do - case "${1}" in - "-h" | "--help") - help 0 - ;; - "-p" | "--package") - if [ -n "${2}" ]; then - package="${2}" - shift 2 - else - help 1 - fi - ;; - "-v" | "--version") - if [ -n "${2}" ]; then - version="${2}" - shift 2 - else - help 1 - fi - ;; - "-r" | "--revision") - if [ -n "${2}" ]; then - revision="${2}" - shift 2 - else - help 1 - fi - ;; - "--dont-build-docker") - build_docker="no" - shift 1 - ;; - "--production") - is_production="yes" - shift 1 - ;; - "-o" | "--output") - if [ -n "${2}" ]; then - outdir="${2}" - shift 2 - else - help 1 - fi - ;; - *) - help 1 - ;; - esac - done - - if [ -z "$package" ] | [ -z "$version" ]; then - help 1 - fi - - build || clean 1 - - clean 0 -} - -main "$@" diff --git a/dev-tools/build-packages/rpm/rpm-builder.sh b/dev-tools/build-packages/rpm/rpm-builder.sh new file mode 100644 index 000000000000..180139b121ff --- /dev/null +++ b/dev-tools/build-packages/rpm/rpm-builder.sh @@ -0,0 +1,112 @@ +#!/bin/bash + +# Wazuh package builder +# Copyright (C) 2021, 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 + +# Script parameters to build the package +target="wazuh-dashboard" +version=$1 +revision=$2 +architecture=$3 +commit_sha=$4 +is_production=$5 +verbose=$6 + +# Paths +current_path="$( cd $(dirname $0) ; pwd -P )" + +# Folders +tmp_dir="/tmp" +out_dir="/output" +config_path=$tmp_dir/config + +if [ "$verbose" = "debug" ]; then + set -x +fi + +trap clean INT +trap clean EXIT + +log() { + if [ "$verbose" = "info" ] || [ "$verbose" = "debug" ]; then + echo "$@" + fi +} + +clean() { + exit_code=$? + # Clean the files + rm -rf ${tmp_dir}/* + trap '' EXIT + exit ${exit_code} +} + +mkdir -p ${tmp_dir}/wazuh-dashboard-base +cd ${tmp_dir}/wazuh-dashboard-base +log "Extracting base tar.gz..." +tar -zxf ${out_dir}/wazuh-dashboard-$version-$revision-linux-$architecture.tar.gz +log "Preparing the package..." +jq '.wazuh.revision="'${revision}'"' package.json > pkgtmp.json && mv pkgtmp.json package.json +mkdir -p etc/services +cp $config_path/* etc/services +echo ${version} > VERSION +cd .. +tar -czf wazuh-dashboard.tar.gz wazuh-dashboard-base + +log "Setting up parameters" + +if [ "${architecture}" = "x64" ]; then + architecture="x86_64" +else + architecture="aarch64" +fi + +build_dir=/build +rpm_build_dir=${build_dir}/rpmbuild +directory_base="/usr/share/wazuh-dashboard" + + +pkg_name=${target}-${version} +pkg_path="${rpm_build_dir}/RPMS/${architecture}" +file_name="${target}-${version}-${revision}" +rpm_file="${file_name}.${architecture}.rpm" + +if [ "$is_production" = "no" ]; then + final_name="${target}_${version}-${revision}_${architecture}_${commit_sha}.rpm" +else + final_name="${target}_${version}-${revision}_${architecture}.rpm" +fi + +mkdir -p ${rpm_build_dir}/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} + +mkdir ${build_dir}/${pkg_name} + +# Including spec file +cp /usr/local/src/${target}.spec ${rpm_build_dir}/SPECS/${pkg_name}.spec +cd ${build_dir} && tar czf "${rpm_build_dir}/SOURCES/${pkg_name}.tar.gz" "${pkg_name}" + +log "Building RPM..." +/usr/bin/rpmbuild -v \ + --define "_topdir ${rpm_build_dir}" \ + --define "_version ${version}" \ + --define "_release ${revision}" \ + --define "_localstatedir ${directory_base}" \ + --target ${architecture} \ + -ba ${rpm_build_dir}/SPECS/${pkg_name}.spec + +cd ${pkg_path} && sha512sum ${rpm_file} >/${out_dir}/${rpm_file}.sha512 + +find ${pkg_path}/ -maxdepth 3 -type f -name "${file_name}*" -exec mv {} /${out_dir}/ \; +if [ "${is_production}" = "no" ]; then + mv /${out_dir}/${rpm_file} /${out_dir}/${final_name} + mv /${out_dir}/${rpm_file}.sha512 /${out_dir}/${final_name}.sha512 +fi + + diff --git a/dev-tools/build-packages/rpm/wazuh-dashboard.spec b/dev-tools/build-packages/rpm/wazuh-dashboard.spec index 758e6ef7aa62..3addff105bde 100644 --- a/dev-tools/build-packages/rpm/wazuh-dashboard.spec +++ b/dev-tools/build-packages/rpm/wazuh-dashboard.spec @@ -44,7 +44,7 @@ Wazuh dashboard is a user interface and visualization tool for security-related %prep -cp /opt/%{DASHBOARD_FILE} ./ +cp /tmp/%{DASHBOARD_FILE} ./ groupadd %{GROUP} useradd -g %{GROUP} %{USER}