Skip to content

Commit 082e856

Browse files
committed
fix: build
1 parent 718d382 commit 082e856

6 files changed

Lines changed: 219 additions & 141 deletions

File tree

.devcontainer/build.sh

Lines changed: 184 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,187 @@
11
#!/bin/bash
2-
set -e
3-
# SPDX-FileCopyrightText: © 2022 ELABIT GmbH <mail@elabit.de>
4-
# SPDX-License-Identifier: GPL-3.0-or-later
5-
# This file is part of the Robotmk project (https://www.robotmk.org)
6-
7-
# This file creates a CMK MKP file.
8-
# It leverages the "mkp" command from CMK, which reads a package description file (JSON)
9-
10-
# After the MKP has been built, the script checks if it runs within a Github
11-
# Workflow. If so, it sets the artifact name as output variable.
12-
13-
if [ -z "$WORKSPACE" ]; then
14-
if [ -n "$GITHUB_WORKSPACE" ]; then
15-
WORKSPACE="$GITHUB_WORKSPACE"
16-
else
17-
echo "ERROR: WORKSPACE environment variable not set and GITHUB_WORKSPACE not available. Exiting."
18-
exit 1
19-
fi
20-
fi
21-
22-
# print the current workspace
23-
echo "Workspace folder: $WORKSPACE"
24-
ls -la "$WORKSPACE"
25-
26-
if [ -f /omd/sites/cmk/.profile ]; then
27-
echo "Loading .profile..."
2+
3+
#set -Eeuo pipefail
4+
5+
main() {
6+
if [[ $# -ne 1 ]]; then
7+
usage
8+
exit 1
9+
fi
10+
11+
local pkg_version=$1
12+
13+
ensure_workspace
14+
load_site_profile
15+
load_project_name
16+
load_cmk_version_util
17+
18+
OMD_VER=$(omd version | awk '{print $NF}')
19+
export OMD_VER
20+
21+
prepare_git
22+
generate_package_file "$pkg_version" "$CMK_VERSION_MM"
23+
build_mkp "$pkg_version" "$CMK_VERSION_MM"
24+
set_github_outputs
25+
log "END OF build.sh"
26+
}
27+
28+
err() {
29+
printf 'ERROR: %s\n' "$*" >&2
30+
}
31+
32+
log() {
33+
printf '%s\n' "$*"
34+
}
35+
36+
usage() {
37+
err "Usage: $0 <PKG_VERSION>"
38+
}
39+
40+
trap 'err "Unexpected error at line $LINENO"; exit 1' ERR
41+
42+
PROJECT_NAME=""
43+
PACKAGE_FILE=""
44+
PKG_PATH=""
45+
OMD_VER=""
46+
47+
ensure_workspace() {
48+
if [[ -n "${WORKSPACE:-}" ]]; then
49+
return
50+
fi
51+
52+
if [[ -n "${GITHUB_WORKSPACE:-}" ]]; then
53+
WORKSPACE="$GITHUB_WORKSPACE"
54+
export WORKSPACE
55+
return
56+
fi
57+
58+
err "WORKSPACE environment variable not set and GITHUB_WORKSPACE not available."
59+
exit 1
60+
}
61+
62+
report_workspace() {
63+
log "Workspace folder: $WORKSPACE"
64+
ls -la "$WORKSPACE"
65+
}
66+
67+
load_site_profile() {
68+
local profile_path="/omd/sites/cmk/.profile"
69+
if [[ ! -f "$profile_path" ]]; then
70+
err "Site profile not found at $profile_path."
71+
exit 1
72+
fi
73+
74+
log "Loading site profile $profile_path"
75+
set -a
76+
# shellcheck disable=SC1090
77+
source "$profile_path" || true
78+
set +a
79+
80+
if [[ -z "${OMD_SITE:-}" ]]; then
81+
err "OMD_SITE variable not set after sourcing profile."
82+
exit 1
83+
fi
84+
}
85+
86+
load_cmk_version_util() {
87+
# shellcheck disable=SC1090
88+
source "${WORKSPACE}/.devcontainer/cmk_version.sh"
89+
if [[ -z "${CMK_VERSION_MM:-}" ]]; then
90+
err "CMK_VERSION_MM not exported by cmk_version.sh"
91+
exit 1
92+
fi
93+
echo " - CMK_VERSION_MM=${CMK_VERSION_MM}"
94+
}
95+
96+
97+
load_project_name() {
98+
# project.env contains some generic useful variables
2899
set -a
29-
. /omd/sites/cmk/.profile
100+
source $WORKSPACE/project.env
30101
set +a
31-
else
32-
echo "ERROR: .profile not found in /omd/sites/cmk. Exiting."
33-
exit 1
34-
fi
35-
36-
if [ -z $OMD_SITE ]; then
37-
echo "ERROR: You do not seem to be on a OMD site (variable OMD_SITE not set). Exiting."
38-
exit 1
39-
fi
40-
41-
# Source CMK version detection utilities
42-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
43-
source "${SCRIPT_DIR}/cmk_version.sh"
44-
45-
set -u
46-
# Use the CMK_VERSION_MM from the shared utility
47-
CMK_MM="$CMK_VERSION_MM"
48-
# Get full OMD version for packaging
49-
OMD_VER=$(omd version | awk '{print $NF}')
50-
NAME="robotmk"
51-
PACKAGEFILE=$OMD_ROOT/var/check_mk/packages/$NAME
52-
PKGDIR=$OMD_ROOT/var/check_mk/packages_local
53-
PKG_DEST_DIR=$WORKSPACE/build
54-
55-
56-
# Ownership can look dubious for git, fix this.
57-
git config --global --add safe.directory $WORKSPACE
58-
59-
# If there is "## Unreleased" then assume a manual build, prompt the user to enter the version.
60-
# otherwise, take the version form the first H2 header in the CHANGELOG.md, format is:
61-
# ## 1.4.4 - 2024-03-26
62-
# or
63-
# ## 1.4.4
64-
# and extract the version number.
65-
66-
if [ -n "$(awk '/^## Unreleased/ {print $2; exit}' "$WORKSPACE/CHANGELOG.md")" ]; then
67-
echo "ERROR: Changelog contains Unreleased version. Please enter the version number manually."
68-
read -p "Enter the version number: " PKG_VERSION
69-
else
70-
echo "Reading the first version from the CHANGELOG.md..."
71-
export PKG_VERSION=${PKG_VERSION:-$(awk '/^## [0-9]+\.[0-9]+/ {print $2; exit}' "$WORKSPACE/CHANGELOG.md")}
72-
fi
73-
74-
echo "PKG_VERSION: $PKG_VERSION"
75-
76-
77-
78-
echo "---------------------------------------------"
79-
PACKAGEFILE_TEMPLATE=$WORKSPACE/pkginfo/cmk$CMK_MM.json
80-
echo "▹ Generating package infofile using the $PACKAGEFILE template"
81-
82-
83-
jq --arg version "$PKG_VERSION" \
84-
--arg version_packaged "$OMD_VER" \
85-
--arg version_min_required "${CMK_MM}.0p1" \
86-
--arg version_usable_until "${CMK_MM}.200" \
87-
'
88-
.version = $version
89-
| .["version.packaged"] = $version_packaged
90-
| .["version.min_required"] = $version_min_required
91-
| .["version.usable_until"] = $version_usable_until
92-
' \
93-
"$PACKAGEFILE_TEMPLATE" > "$PACKAGEFILE"
94-
95-
echo "---------------------------------------------"
96-
echo "$PACKAGEFILE:"
97-
cat $PACKAGEFILE
98-
echo "---------------------------------------------"
99-
100-
echo "▹ Building the MKP '$NAME' v$PKG_VERSION for CMK $CMK_MM ..."
101-
# set -x
102-
mkp -v package $PACKAGEFILE
103-
104-
105-
FILE=$(ls -rt1 $PKGDIR/*.mkp | tail -1)
106-
# robotmk-<ver>.mkp => rename to include cmk major.minor
107-
NEWFILENAME=$NAME.$PKG_VERSION-cmk$CMK_MM.mkp
108-
mkdir -p $PKG_DEST_DIR
109-
mv $FILE $PKG_DEST_DIR/$NEWFILENAME
110-
PKG_PATH=$PKG_DEST_DIR/$NEWFILENAME
111-
echo "📦 $PKG_PATH"
112-
echo "---------------------------------------------"
113-
114-
echo ""
115-
echo "Checking for Github Workflow..."
116-
if [ -n "${GITHUB_WORKSPACE-}" ]; then
117-
echo "🐙 ...Github Workflow exists."
118-
echo "▹ Set Outputs for GitHub Workflow steps"
119-
echo "pkgfile=$PKG_PATH" >> $GITHUB_OUTPUT
120-
echo "artifactname=$NEWFILENAME" >> $GITHUB_OUTPUT
121-
else
122-
echo "...no GitHub Workflow detected (local execution)."
123-
fi
124-
echo "END OF build.sh"
125-
echo "---------------------------------------------"
102+
echo " - PROJECT_NAME=${PROJECT_NAME}"
103+
}
104+
105+
prepare_git() {
106+
git config --global --add safe.directory "$WORKSPACE"
107+
}
108+
109+
generate_package_file() {
110+
local pkg_version=$1
111+
local cmk_mm=$2
112+
local template
113+
114+
template="$WORKSPACE/pkginfo/cmk${cmk_mm}.json"
115+
PACKAGE_FILE="$OMD_ROOT/var/check_mk/packages/${PROJECT_NAME}"
116+
117+
if [[ ! -f "$template" ]]; then
118+
err "Template $template not found."
119+
exit 1
120+
fi
121+
122+
log " - Package template: $template"
123+
jq \
124+
--arg version "$pkg_version" \
125+
--arg version_packaged "$OMD_VER" \
126+
--arg version_min_required "${cmk_mm}.0p1" \
127+
--arg version_usable_until "${cmk_mm}.200" \
128+
'
129+
.version = $version
130+
| .["version.packaged"] = $version_packaged
131+
| .["version.min_required"] = $version_min_required
132+
| .["version.usable_until"] = $version_usable_until
133+
' \
134+
"$template" >"$PACKAGE_FILE"
135+
136+
log " - Package file: $PACKAGE_FILE"
137+
}
138+
139+
latest_mkp() {
140+
local pkgdir=$1
141+
local entries
142+
143+
mapfile -t entries < <(find "$pkgdir" -maxdepth 1 -type f -name '*.mkp' -printf '%T@ %p\n' | sort -n)
144+
if [[ ${#entries[@]} -eq 0 ]]; then
145+
err "No MKP files found in $pkgdir"
146+
exit 1
147+
fi
148+
149+
printf '%s' "${entries[-1]##* }"
150+
}
151+
152+
build_mkp() {
153+
local pkg_version=$1
154+
local cmk_mm=$2
155+
local pkgdir pkg_dest latest
156+
157+
pkgdir="$OMD_ROOT/var/check_mk/packages_local"
158+
pkg_dest="$WORKSPACE/build"
159+
160+
log "Building MKP ${PROJECT_NAME} v${pkg_version} for CMK ${cmk_mm}"
161+
mkp -v package "$PACKAGE_FILE"
162+
163+
latest=$(latest_mkp "$pkgdir")
164+
mkdir -p "$pkg_dest"
165+
PKG_PATH="$pkg_dest/${PROJECT_NAME}.${pkg_version}-cmk${cmk_mm}.mkp"
166+
mv "$latest" "$PKG_PATH"
167+
log " - PKG_PATH: $PKG_PATH"
168+
}
169+
170+
set_github_outputs() {
171+
if [[ -n "${GITHUB_WORKSPACE:-}" && -n "${GITHUB_OUTPUT:-}" ]]; then
172+
log "Publishing GitHub Actions outputs"
173+
printf 'pkgfile=%s\n' "$PKG_PATH" >>"$GITHUB_OUTPUT"
174+
printf 'artifactname=%s\n' "$(basename "$PKG_PATH")" >>"$GITHUB_OUTPUT"
175+
else
176+
log "No GitHub Actions environment detected"
177+
fi
178+
}
179+
180+
folder_of() {
181+
DIR="${1%/*}"
182+
(cd "$DIR" && echo "$(pwd -P)")
183+
}
184+
185+
186+
main "$@"
187+

.devcontainer/linkfiles.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function symlink_project_files {
9292

9393
# stable paths across 2.2-2.4
9494
# Agent plugins
95-
create_symlink agents_plugins $CMK_DIR_AGENT_PLUGINS
95+
create_symlink agent_plugins $CMK_DIR_AGENT_PLUGINS
9696

9797
# BAKERY
9898
create_symlink bakery $CMK_DIR_BAKERY
@@ -134,7 +134,7 @@ function symlink_common_files {
134134

135135

136136
function rmpath {
137-
echo "clearing $1"
137+
# echo "clearing $1"
138138
rm -rf $1
139139
}
140140

@@ -145,8 +145,8 @@ function linkpath {
145145
echo "linking $TARGET -> $LINKNAME"
146146
# check if target file or dir exists
147147
if [ ! -e $TARGET ]; then
148-
echo "ERROR: $TARGET does not exist!"
149-
exit 1
148+
echo "NOTE: $TARGET won't be linked - does not exist!"
149+
return
150150
fi
151151

152152
# make sure that the link's parent dir exists
@@ -179,7 +179,7 @@ function create_symlink {
179179

180180
rmpath $LINKNAME
181181
linkpath $TARGET $LINKNAME
182-
echo "clearing $LINKNAME/__pycache__"
182+
# echo "clearing $LINKNAME/__pycache__"
183183
rm -rf $LINKNAME/__pycache__ || true
184184
}
185185

.github/workflows/build-mkp.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ jobs:
1414
image: checkmk/check-mk-raw:2.4.0-daily
1515
steps:
1616
- name: Install tools
17-
run: apt update && apt-get -y install sudo jq tree hub
17+
# TODO: remove vim
18+
run: apt update && apt-get -y install sudo jq tree hub vim
1819
- name: Create Checkmk Site
1920
run: /docker-entrypoint.sh /bin/true
2021
- name: Repo Checkout

pkginfo/cmk2.4 copy.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "robotmk-bridge-plugin",
3+
"author": "Simon Meggle, ELABIT GmbH",
4+
"description": "Robot-Bridge acts as a universal adapter that converts results from any test tool into Robot Framework XML for seamless integration with Robotmk and Checkmk.",
5+
"download_url": "https://github.com/elabit/robotmk-bridge-plugin/releases",
6+
"title": "Robotmk Bridge Plugin",
7+
"files": {
8+
"agents": [
9+
"plugins/robotmk_bridge_plugin.py"
10+
],
11+
"cmk_addons_plugins": [
12+
"robotmk/agent_based/robotmk_bridge_plugin.py",
13+
"robotmk/graphing/graphing_robotmk_bridge_plugin.py"
14+
],
15+
"lib": [
16+
"check_mk/base/cee/plugins/bakery/robotmk_bridge_plugin.py"
17+
],
18+
"web": [
19+
"plugins/wato/robotmk_wato_params_bakery.py",
20+
"plugins/wato/robotmk_wato_params_check.py",
21+
"plugins/wato/robotmk_wato_params_discovery.py"
22+
]
23+
},
24+
"version.packaged": "2.4.0",
25+
"version.min_required": "2.3.0p1",
26+
"version.usable_until": "2.4.20"
27+
}

0 commit comments

Comments
 (0)