From 45d4f522bde162000a8dee42e3d2d62f63275f8d Mon Sep 17 00:00:00 2001 From: Marius Rieder Date: Wed, 3 May 2023 12:08:07 +0200 Subject: [PATCH] Initial commit --- .devcontainer/Dockerfile | 18 ++++++++ .devcontainer/build.sh | 15 +++++++ .devcontainer/devcontainer.json | 38 ++++++++++++++++ .devcontainer/requirements.txt | 4 ++ .devcontainer/symlink.sh | 12 +++++ .devcontainer/template-sync-includes.conf | 10 +++++ .devcontainer/template-sync.conf | 4 ++ .devcontainer/template-update.sh | 23 ++++++++++ .flake8 | 12 +++++ .github/workflows/build-release.yml | 53 +++++++++++++++++++++++ .github/workflows/build.yml | 39 +++++++++++++++++ .github/workflows/lint.yml | 26 +++++++++++ .github/workflows/pytest.yml | 30 +++++++++++++ .gitignore | 4 ++ .vscode/settings.json | 11 +++++ .vscode/tasks.json | 35 +++++++++++++++ README.md | 32 ++++++++++++++ package | 21 +++++++++ 18 files changed, 387 insertions(+) create mode 100644 .devcontainer/Dockerfile create mode 100755 .devcontainer/build.sh create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/requirements.txt create mode 100755 .devcontainer/symlink.sh create mode 100644 .devcontainer/template-sync-includes.conf create mode 100644 .devcontainer/template-sync.conf create mode 100755 .devcontainer/template-update.sh create mode 100644 .flake8 create mode 100644 .github/workflows/build-release.yml create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/pytest.yml create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 README.md create mode 100644 package diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..a675792 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,18 @@ +# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.155.1/containers/ubuntu/.devcontainer/base.Dockerfile + +# [Choice] CheckMK version: 1.6.0-latest, 2.0.0-latest, 2.1.0-latest +ARG VARIANT="2.1.0-latest" +FROM checkmk/check-mk-raw:${VARIANT} + +RUN /docker-entrypoint.sh /bin/true + +# [Optional] Uncomment this section to install additional OS packages. +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + && apt-get -y install --no-install-recommends nodejs gcc + +ADD requirements.txt /tmp/requirements.txt +RUN PATH="/omd/sites/cmk/bin:${PATH}" \ + OMD_ROOT="/omd/sites/cmk" \ + /omd/sites/cmk/bin/pip3 install -r /tmp/requirements.txt + +ENTRYPOINT ["/bin/bash"] \ No newline at end of file diff --git a/.devcontainer/build.sh b/.devcontainer/build.sh new file mode 100755 index 0000000..2701331 --- /dev/null +++ b/.devcontainer/build.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +NAME=$(python -c 'print(eval(open("package").read())["name"])') +rm /omd/sites/cmk/var/check_mk/packages/* ||: +ln -s $WORKSPACE/package /omd/sites/cmk/var/check_mk/packages/$NAME + +mkp -v pack $NAME + +# Set Outputs for GitHub Workflow steps +if [ -n "$GITHUB_WORKSPACE" ]; then + echo "::set-output name=pkgfile::$(ls *.mkp)" + echo "::set-output name=pkgname::${NAME}" + VERSION=$(python -c 'print(eval(open("package").read())["version"])') + echo "::set-output name=pkgversion::$VERSION" +fi \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..75a4e11 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,38 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: +// https://github.com/microsoft/vscode-dev-containers/tree/v0.155.1/containers/ubuntu +{ + "name": "Checkmk", + "build": { + "dockerfile": "Dockerfile", + // Update 'VARIANT' to pick an Ubuntu version: focal, bionic + "args": { "VARIANT": "2.1.0-latest" } + }, + + // Set *default* container specific settings.json values on container create. + "settings": { + "terminal.integrated.shell.linux": "/bin/bash" + }, + + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "ms-python.python", + "littlefoxteam.vscode-python-test-adapter" + ], + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": ".devcontainer/symlink.sh", + + // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "cmk", + + "remoteEnv": { + "PATH": "${containerEnv:PATH}:/omd/sites/cmk/bin:/omd/sites/cmk/local/lib/python3/bin/", + "OMD_ROOT": "/omd/sites/cmk", + "OMD_SITE": "cmk", + "CMK_SITE_ID": "cmk", + "WORKSPACE": "${containerWorkspaceFolder}" + } +} \ No newline at end of file diff --git a/.devcontainer/requirements.txt b/.devcontainer/requirements.txt new file mode 100644 index 0000000..c248ac1 --- /dev/null +++ b/.devcontainer/requirements.txt @@ -0,0 +1,4 @@ +flake8 +pytest +pytest-cov +requests-mock \ No newline at end of file diff --git a/.devcontainer/symlink.sh b/.devcontainer/symlink.sh new file mode 100755 index 0000000..57727b0 --- /dev/null +++ b/.devcontainer/symlink.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +for DIR in 'agents' 'checkman' 'checks' 'doc' 'inventory' 'notifications' 'pnp-templates' 'web'; do + rm -rfv $OMD_ROOT/local/share/check_mk/$DIR + ln -sv $WORKSPACE/$DIR $OMD_ROOT/local/share/check_mk/$DIR +done; + +rm -rfv $OMD_ROOT/local/lib/check_mk/base/plugins/agent_based +ln -sv $WORKSPACE/agent_based $OMD_ROOT/local/lib/check_mk/base/plugins/agent_based + +rm -rfv $OMD_ROOT/local/lib/nagios/plugins +ln -sv $WORKSPACE/nagios_plugins $OMD_ROOT/local/lib/nagios/plugins \ No newline at end of file diff --git a/.devcontainer/template-sync-includes.conf b/.devcontainer/template-sync-includes.conf new file mode 100644 index 0000000..8029625 --- /dev/null +++ b/.devcontainer/template-sync-includes.conf @@ -0,0 +1,10 @@ +# DO NOT EDIT - Change template-sync.conf +include .devcontainer/ +include .devcontainer/** +exclude .github/template-sync.conf +include .github/ +include .github/** +include .vscode/ +include .vscode/** +include .flake8 +include .gitignore \ No newline at end of file diff --git a/.devcontainer/template-sync.conf b/.devcontainer/template-sync.conf new file mode 100644 index 0000000..eda0b69 --- /dev/null +++ b/.devcontainer/template-sync.conf @@ -0,0 +1,4 @@ +# Add additional sync excludes for this repo +#exclude .github/do-not-sync +#exclude .flake8 +#exclude .gitignore \ No newline at end of file diff --git a/.devcontainer/template-update.sh b/.devcontainer/template-update.sh new file mode 100755 index 0000000..7439734 --- /dev/null +++ b/.devcontainer/template-update.sh @@ -0,0 +1,23 @@ +#!/usr/bin/bash + +TEMPDIR=$(mktemp -d) + +cleanup() { + echo "Removing $TEMPDIR" + rm -rf $TEMPDIR +} +trap cleanup EXIT + +git -C $TEMPDIR clone https://github.com/jiuka/checkmk_template.git + +CMD="rsync --archive --cvs-exclude --no-owner --no-group --no-times --verbose" +if [ -e ".devcontainer/template-sync.conf" ]; then + CMD="${CMD} --filter='merge .devcontainer/template-sync.conf'" +fi +if [ -e "${TEMPDIR}/checkmk_template/.devcontainer/template-sync-includes.conf" ]; then + CMD="${CMD} --filter='merge ${TEMPDIR}/checkmk_template/.devcontainer/template-sync-includes.conf'" +fi +CMD="${CMD} --filter='exclude *' ${TEMPDIR}/checkmk_template/ $(pwd)/" +bash -c "$CMD" + +echo $CMD \ No newline at end of file diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..1a36636 --- /dev/null +++ b/.flake8 @@ -0,0 +1,12 @@ +[flake8] +# In .style.yapf and .pylintrc we use 100, but in some cases this isn't enforced. +max-line-length=550 +ignore= + ################################################################################################ + # Miscellaneous stuff + ################################################################################################ + ################################################################################################ + # Incompatible with YAPF + ################################################################################################ + # unexpected spaces around keyword / parameter equals + E251, \ No newline at end of file diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml new file mode 100644 index 0000000..09e060c --- /dev/null +++ b/.github/workflows/build-release.yml @@ -0,0 +1,53 @@ +name: build-release + +on: + push: + tags: + - 'v*' + - '!v*[a-z]' + +jobs: + build-release: + name: Build Release Package + runs-on: ubuntu-latest + container: + image: checkmk/check-mk-raw:2.1.0-latest + + env: + OMD_ROOT: /omd/sites/cmk + OMD_SITE: cmk + CMK_SITE_ID: cmk + WORKSPACE: ${{ github.workspace }} + + steps: + - name: Initialize Checkmk Site + run: /docker-entrypoint.sh /bin/true + - uses: actions/checkout@v2 + - name: Setup links + run: .devcontainer/symlink.sh + - name: Update GITHUB_PATH + run: echo "/omd/sites/cmk/bin" >> $GITHUB_PATH + - name: Build Extension + run: .devcontainer/build.sh + id: cmkpkg + + - name: Create Release + id: create_release + uses: actions/create-release@v1.0.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + draft: false + prerelease: false + + - name: Upload Release Asset + uses: actions/upload-release-asset@v1.0.1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./${{ steps.cmkpkg.outputs.pkgfile }} + asset_name: ${{ steps.cmkpkg.outputs.pkgfile }} + asset_content_type: application/octet-stream \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..e5844d7 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,39 @@ +name: build + +on: + push: + branches: + - '**' + tags-ignore: + - 'v*' + pull_request: + +jobs: + build: + name: Build Checkmk package + runs-on: ubuntu-latest + container: + image: checkmk/check-mk-raw:2.1.0-latest + + env: + OMD_ROOT: /omd/sites/cmk + OMD_SITE: cmk + CMK_SITE_ID: cmk + WORKSPACE: ${{ github.workspace }} + + steps: + - name: Initialize Checkmk Site + run: /docker-entrypoint.sh /bin/true + - uses: actions/checkout@v2 + - name: Setup links + run: .devcontainer/symlink.sh + - name: Update GITHUB_PATH + run: echo "/omd/sites/cmk/bin" >> $GITHUB_PATH + - name: Build Extension + run: .devcontainer/build.sh + id: cmkpkg + - name: Upload artifact + uses: actions/upload-artifact@v2 + with: + name: ${{ steps.cmkpkg.outputs.pkgfile }} + path: ${{ steps.cmkpkg.outputs.pkgfile }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..09d5a6a --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,26 @@ +name: Lint + +on: + push: + paths: + - '**.py' + +jobs: + flake8_py3: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.8 + uses: actions/setup-python@v1 + with: + python-version: 3.8 + - name: Install flake8 + run: pip install flake8 + - name: Run flake8 + uses: suo/flake8-github-action@releases/v1 + with: + checkName: 'flake8_py3' # NOTE: this needs to be the same as the job name + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..8728de1 --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,30 @@ +name: pytest + +on: + push: [] + +jobs: + pytest: + + runs-on: ubuntu-latest + container: + image: checkmk/check-mk-raw:2.1.0-latest + + env: + OMD_ROOT: /omd/sites/cmk + OMD_SITE: cmk + CMK_SITE_ID: cmk + WORKSPACE: ${{ github.workspace }} + + steps: + - name: Initialize Checkmk Site + run: /docker-entrypoint.sh /bin/true + - uses: actions/checkout@v2 + - name: Setup links + run: ./.devcontainer/symlink.sh + - name: Install pytest + run: su -l -c "REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt pip3 install -r $GITHUB_WORKSPACE/.devcontainer/requirements.txt" cmk + - name: Update GITHUB_PATH + run: echo "/omd/sites/cmk/bin" >> $GITHUB_PATH + - name: Run pytest + run: python3 -m pytest \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..105bf3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.mkp +.coverage +__pycache__ +debug.log \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2b75935 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.pythonPath": "/omd/sites/cmk/bin/python3", + "python.linting.flake8Enabled": true, + "python.linting.enabled": true, + "python.testing.pytestArgs": [ + "." + ], + "python.testing.unittestEnabled": false, + "python.testing.nosetestsEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..f6240f4 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,35 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Install", + "type": "shell", + "command": ".devcontainer/build.sh", + "problemMatcher": [], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "Start Site", + "type": "shell", + "command": "omd start", + "problemMatcher": [] + }, + { + "label": "Re-Start Site", + "type": "shell", + "command": "omd restart", + "problemMatcher": [] + }, + { + "label": "Stop Site", + "type": "shell", + "command": "omd stop", + "problemMatcher": [] + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..fbd12a0 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# Checkmk extension for ... + +![build](https://github.com/jiuka/checkmk_phion/workflows/build/badge.svg) +![flake8](https://github.com/jiuka/checkmk_phion/workflows/Lint/badge.svg) +![pytest](https://github.com/jiuka/checkmk_phion/workflows/pytest/badge.svg) + +## Description + +This is a template to develop Checkmk Extensions + +## Development + +For the best development experience use [VSCode](https://code.visualstudio.com/) with the [Remote Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension. This maps your workspace into a checkmk docker container giving you access to the python environment and libraries the installed extension has. + +## Directories + +The following directories in this repo are getting mapped into the Checkmk site. + +* `agents`, `checkman`, `checks`, `doc`, `inventory`, `notifications`, `pnp-templates`, `web` are mapped into `local/share/check_mk/` +* `agent_based` is mapped to `local/lib/check_mk/base/plugins/agent_based` +* `nagios_plugins` is mapped to `local/lib/nagios/plugins` + +## Continuous integration +### Local + +To build the package hit `Crtl`+`Shift`+`B` to execute the build task in VSCode. + +`pytest` can be executed from the terminal or the test ui. + +### Github Workflow + +The provided Github Workflows run `pytest` and `flake8` in the same checkmk docker conatiner as vscode. diff --git a/package b/package new file mode 100644 index 0000000..e22a190 --- /dev/null +++ b/package @@ -0,0 +1,21 @@ +{ + 'author': u'', + 'description': u'', + 'download_url': '', + 'files': { + 'agent_based': [], + 'agents': [], + 'checkman': [], + 'checks': [], + 'doc': [], + 'inventory': [], + 'notifications': [], + 'pnp-templates': [], + 'web': [] + }, + 'name': '', + 'title': u'', + 'version': '0.1', + 'version.min_required': '2.0.0', + 'version.packaged': '2.0.0' +} \ No newline at end of file