-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1311 from TheHive-Project/utils-improvements
utils improvements
- Loading branch information
Showing
6 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env bash | ||
|
||
### | ||
# This program assumes your analyzers and responders folder looks like: | ||
# | ||
# Custom-Analyzers | ||
# ├── analyzers/ | ||
# │ └── My_custom_analyzer/ | ||
# └── responders/ | ||
# └── My_custom_responder/ | ||
# ├── customresponderflavor.json | ||
# ├── Dockerfile | ||
# ├── program.py* | ||
# ├── README.md | ||
# └── requirements.txt | ||
# | ||
# Usage: | ||
# Update DOCKER_REPOSITORY variable | ||
# cd ./Custom-Analyzers | ||
# bash /path/to/build.sh | ||
### | ||
|
||
# Set your docker repository name | ||
DOCKER_REPOSITORY=ilovestrangebee | ||
|
||
build_image() { | ||
JSON=$1 | ||
cat << EOF > /tmp/default_dockerfile | ||
FROM python:3 | ||
WORKDIR /worker | ||
ARG workername | ||
ARG command | ||
COPY . \$workername | ||
RUN test ! -e \$workername/requirements.txt || pip install --no-cache-dir -r \$workername/requirements.txt | ||
ENTRYPOINT \$command | ||
EOF | ||
|
||
DEFAULT_DOCKERFILE=/tmp/default_dockerfile | ||
TAG=`cat ${JSON} | jq -r '( "'"$DOCKER_REPOSITORY"'" + "/" + (.name | ascii_downcase) + ":" + (.version))'` | ||
WORKER_NAME=`cat ${JSON} | jq -r '(.version)'` | ||
COMMAND=`cat ${JSON} | jq -r '(.command)'` | ||
DIRNAME=`dirname ${JSON}` | ||
WORKER_NAME=`basename ${DIRNAME}` | ||
if test -f ${DIRNAME}/Dockerfile | ||
then | ||
docker build -t ${TAG} `dirname ${JSON}` | ||
else | ||
docker build --build-arg workername=${WORKER_NAME} --build-arg command=${COMMAND} -f ${DEFAULT_DOCKERFILE} -t ${TAG} `dirname ${JSON}` | ||
fi | ||
} | ||
|
||
build_catalog() { | ||
DIR=$1 | ||
echo '[' > ${DIR}/${DIR}.json | ||
|
||
|
||
first=1 | ||
for JSON in ${DIR}/*/*.json | ||
do | ||
build_image ${JSON} | ||
if test -z "${first}" | ||
then | ||
echo ',' >> ${DIR}/${DIR}.json | ||
else | ||
first= | ||
fi | ||
jq 'del(.command) + { dockerImage: ("'"$DOCKER_REPOSITORY"'" + "/" + (.name | ascii_downcase) + ":" + (.version)) }' ${JSON} >> ${DIR}/${DIR}.json | ||
done | ||
|
||
echo ']' >> ${DIR}/${DIR}.json | ||
} | ||
|
||
build_catalog analyzers | ||
build_catalog responders |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
jsonschema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Cortex-Neurons local documentation preview | ||
|
||
This script renders the Cortex-Neurons documentation locally. | ||
|
||
## Usage | ||
|
||
1. Run the script from root directory Cortex-Analyzers | ||
```bash | ||
bash utils/test_doc/testdoc-venv.sh | ||
``` | ||
|
||
2. View the documentation at http://0.0.0.0:8889 | ||
|
||
## Script Overview | ||
The script performs the following actions: | ||
|
||
- Creates a test environment for documentation in a temporary folder. | ||
- Copies necessary files and directories into the temporary folder. | ||
- Sets up a Python virtual environment and installs dependencies from requirements.txt. | ||
- Clones the doc-builder repository from GitHub if not already cloned. | ||
- Runs the documentation generation script. | ||
- Serves the generated documentation using MkDocs. | ||
- Cleans up temporary files upon completion. | ||
|
||
## Notes | ||
Ensure the script is ran from the root directory of the Cortex-Analyzers repository. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/bin/bash | ||
|
||
# This program is for test purposes only, to ensure the documentation is generated as expected. | ||
# This program should be run from the Cortex-Analyzers/ path only. | ||
|
||
ROOT_PATH="${PWD}" | ||
TEST_PATH="./test_doc" | ||
VENV_PATH="${ROOT_PATH}/venv" | ||
REQUIREMENTS_FILE="${ROOT_PATH}/utils/test_doc/requirements.txt" | ||
|
||
# Cleanup function | ||
cleanup() { | ||
echo "Cleaning up temporary files..." | ||
deactivate 2>/dev/null | ||
cd "${ROOT_PATH}" || exit | ||
rm -rf "${TEST_PATH}" | ||
} | ||
trap 'cleanup' EXIT | ||
|
||
# Create the test documentation path | ||
mkdir -p "${TEST_PATH}" | ||
|
||
# Copy necessary directories and files | ||
for I in analyzers responders assets images AUTHORS docs *.md; do | ||
cp -rv "$I" "${TEST_PATH}" | ||
done | ||
|
||
cd "${TEST_PATH}" || exit | ||
|
||
# Create a Python virtual environment if not already created | ||
if [ ! -d "$VENV_PATH" ]; then | ||
echo "Creating virtual environment..." | ||
python3 -m venv "$VENV_PATH" | ||
fi | ||
|
||
# Activate the virtual environment | ||
source "${VENV_PATH}/bin/activate" | ||
|
||
# Ensure pip is updated | ||
pip install --upgrade pip | ||
|
||
# Check and install dependencies from requirements.txt | ||
if [ -f "$REQUIREMENTS_FILE" ]; then | ||
echo "Installing dependencies from $REQUIREMENTS_FILE..." | ||
pip install -r "$REQUIREMENTS_FILE" | ||
else | ||
echo "Error: $REQUIREMENTS_FILE not found!" | ||
deactivate | ||
exit 1 | ||
fi | ||
|
||
# Clone the repository if not already cloned | ||
if [ ! -d "doc-builder" ]; then | ||
gh repo clone TheHive-Project/doc-builder | ||
fi | ||
|
||
# Execute the generate script | ||
python doc-builder/build/Cortex-Neurons/generate.py | ||
|
||
# Copy specific files to the docs folder | ||
cp -v CHANGELOG.md docs/. | ||
cp -v code_of_conduct.md docs/. | ||
cp -v README.md docs/ | ||
cp -v SECURITY.md docs/ | ||
cp -v AUTHORS docs/AUTHORS.md | ||
|
||
# Serve documentation with mkdocs | ||
mkdocs serve -a 0.0.0.0:8889 | ||
|
||
echo "Script execution completed!" |