-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic systemd service to deb and rpm (#1335)
- Install `otel-collector` systemd service and run as `otel:otel` user:group - Add simple installation test scripts using systemd-in-docker images
- Loading branch information
Showing
13 changed files
with
238 additions
and
26 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
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,45 @@ | ||
#!/bin/bash | ||
|
||
FPM_DIR="$( cd "$( dirname ${BASH_SOURCE[0]} )" && pwd )" | ||
|
||
PKG_NAME="otel-collector" | ||
PKG_VENDOR="OpenTelemetry Community" | ||
PKG_MAINTAINER="OpenTelemetry Community <[email protected]>" | ||
PKG_DESCRIPTION="OpenTelemetry Collector" | ||
PKG_LICENSE="Apache 2.0" | ||
PKG_URL="https://github.com/open-telemetry/opentelemetry-collector" | ||
PKG_USER="otel" | ||
PKG_GROUP="otel" | ||
|
||
SERVICE_NAME="otel-collector" | ||
PROCESS_NAME="otelcol" | ||
|
||
SERVICE_PATH="$FPM_DIR/$SERVICE_NAME.service" | ||
PREINSTALL_PATH="$FPM_DIR/preinstall.sh" | ||
POSTINSTALL_PATH="$FPM_DIR/postinstall.sh" | ||
PREUNINSTALL_PATH="$FPM_DIR/preuninstall.sh" | ||
|
||
install_pkg() { | ||
local pkg_path="$1" | ||
local pkg_base=$( basename "$pkg_path" ) | ||
|
||
echo "Installing $pkg_base ..." | ||
docker cp "$pkg_path" $image_name:/tmp/$pkg_base | ||
if [[ "${pkg_base##*.}" = "deb" ]]; then | ||
$docker_exec dpkg -i /tmp/$pkg_base | ||
else | ||
$docker_exec rpm -ivh /tmp/$pkg_base | ||
fi | ||
} | ||
|
||
uninstall_pkg() { | ||
local pkg_type="$1" | ||
local pkg_name="${2:-"$PKG_NAME"}" | ||
|
||
echo "Uninstalling $pkg_name ..." | ||
if [[ "$pkg_type" = "deb" ]]; then | ||
$docker_exec dpkg -r $pkg_name | ||
else | ||
$docker_exec rpm -e $pkg_name | ||
fi | ||
} |
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,22 @@ | ||
# A debian9 image with systemd enabled. Must be run with: | ||
# `-d --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro` flags | ||
FROM debian:9 | ||
|
||
RUN apt-get update &&\ | ||
apt-get install -yq ca-certificates procps systemd | ||
|
||
ENV container docker | ||
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i = \ | ||
"systemd-tmpfiles-setup.service" ] || rm -f $i; done); \ | ||
rm -f /lib/systemd/system/multi-user.target.wants/*;\ | ||
rm -f /lib/systemd/system/local-fs.target.wants/*; \ | ||
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \ | ||
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \ | ||
rm -f /lib/systemd/system/anaconda.target.wants/*; | ||
|
||
RUN systemctl set-default multi-user.target | ||
ENV init /lib/systemd/systemd | ||
|
||
VOLUME [ "/sys/fs/cgroup" ] | ||
|
||
ENTRYPOINT ["/lib/systemd/systemd"] |
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 |
---|---|---|
|
@@ -7,22 +7,30 @@ REPO_DIR="$( cd "$SCRIPT_DIR/../../../../../" && pwd )" | |
VERSION="${1:-}" | ||
ARCH="${2:-"amd64"}" | ||
OUTPUT_DIR="${3:-"$REPO_DIR/bin/"}" | ||
OTELCOL_PATH="$REPO_DIR/bin/otelcol_linux_amd64" | ||
OTELCOL_PATH="$REPO_DIR/bin/otelcol_linux_$ARCH" | ||
CONFIG_PATH="$REPO_DIR/examples/otel-local-config.yaml" | ||
|
||
. $SCRIPT_DIR/../common.sh | ||
|
||
if [[ -z "$VERSION" ]]; then | ||
latest_tag="$( git describe --abbrev=0 --match v[0-9]* )" | ||
VERSION="${latest_tag}-post" | ||
fi | ||
|
||
fpm -s dir -t deb -n otel-collector -v ${VERSION#v} -f -p "$OUTPUT_DIR" \ | ||
--vendor "OpenTelemetry Community" \ | ||
--maintainer "OpenTelemetry Community <[email protected]>" \ | ||
--description "OpenTelemetry Collector" \ | ||
--license "Apache 2.0" \ | ||
--url "https://github.com/open-telemetry/opentelemetry-collector" \ | ||
fpm -s dir -t deb -n $PKG_NAME -v ${VERSION#v} -f -p "$OUTPUT_DIR" \ | ||
--vendor "$PKG_VENDOR" \ | ||
--maintainer "$PKG_MAINTAINER" \ | ||
--description "$PKG_DESCRIPTION" \ | ||
--license "$PKG_LICENSE" \ | ||
--url "$PKG_URL" \ | ||
--architecture "$ARCH" \ | ||
--config-files /etc/otel-collector/config.yaml \ | ||
--deb-dist "stable" \ | ||
$OTELCOL_PATH=/usr/bin/otelcol \ | ||
--deb-user "$PKG_USER" \ | ||
--deb-group "$PKG_GROUP" \ | ||
--before-install "$PREINSTALL_PATH" \ | ||
--after-install "$POSTINSTALL_PATH" \ | ||
--pre-uninstall "$PREUNINSTALL_PATH" \ | ||
$OTELCOL_PATH=/usr/bin/$PROCESS_NAME \ | ||
$SERVICE_PATH=/lib/systemd/system/$SERVICE_NAME.service \ | ||
$CONFIG_PATH=/etc/otel-collector/config.yaml |
14 changes: 14 additions & 0 deletions
14
internal/buildscripts/packaging/fpm/otel-collector.service
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,14 @@ | ||
[Unit] | ||
Description=OpenTelemety Collector | ||
After=network.target | ||
|
||
[Service] | ||
ExecStart=/usr/bin/otelcol --config /etc/otel-collector/config.yaml | ||
KillMode=mixed | ||
Restart=on-failure | ||
Type=simple | ||
User=otel | ||
Group=otel | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
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,8 @@ | ||
#!/bin/sh | ||
|
||
if command -v systemctl >/dev/null 2>&1; then | ||
systemctl enable otel-collector.service | ||
if [ -f /etc/otel-collector/config.yaml ]; then | ||
systemctl start otel-collector.service | ||
fi | ||
fi |
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,3 @@ | ||
#!/bin/sh | ||
|
||
getent passwd otel >/dev/null || useradd --system --user-group --no-create-home --shell /sbin/nologin otel |
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,6 @@ | ||
#!/bin/sh | ||
|
||
if command -v systemctl >/dev/null 2>&1; then | ||
systemctl stop otel-collector.service | ||
systemctl disable otel-collector.service | ||
fi |
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,20 @@ | ||
# A centos8 image with systemd enabled. Must be run with: | ||
# `-d --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro` flags | ||
FROM centos:8 | ||
|
||
ENV container docker | ||
|
||
RUN dnf install -y initscripts | ||
|
||
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i = \ | ||
"systemd-tmpfiles-setup.service" ] || rm -f $i; done); \ | ||
rm -f /lib/systemd/system/multi-user.target.wants/*;\ | ||
rm -f /lib/systemd/system/local-fs.target.wants/*; \ | ||
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \ | ||
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \ | ||
rm -f /lib/systemd/system/basic.target.wants/*;\ | ||
rm -f /lib/systemd/system/anaconda.target.wants/*; | ||
|
||
VOLUME [ "/sys/fs/cgroup" ] | ||
|
||
CMD ["/usr/sbin/init"] |
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 |
---|---|---|
|
@@ -7,22 +7,30 @@ REPO_DIR="$( cd "$SCRIPT_DIR/../../../../../" && pwd )" | |
VERSION="${1:-}" | ||
ARCH="${2:-"amd64"}" | ||
OUTPUT_DIR="${3:-"$REPO_DIR/bin/"}" | ||
OTELCOL_PATH="$REPO_DIR/bin/otelcol_linux_amd64" | ||
OTELCOL_PATH="$REPO_DIR/bin/otelcol_linux_$ARCH" | ||
CONFIG_PATH="$REPO_DIR/examples/otel-local-config.yaml" | ||
|
||
. $SCRIPT_DIR/../common.sh | ||
|
||
if [[ -z "$VERSION" ]]; then | ||
latest_tag="$( git describe --abbrev=0 --match v[0-9]* )" | ||
VERSION="${latest_tag}~post" | ||
fi | ||
|
||
fpm -s dir -t rpm -n otel-collector -v ${VERSION#v} -f -p "$OUTPUT_DIR" \ | ||
--vendor "OpenTelemetry Community" \ | ||
--maintainer "OpenTelemetry Community <[email protected]>" \ | ||
--description "OpenTelemetry Collector" \ | ||
--license "Apache 2.0" \ | ||
--rpm-summary "OpenTelemetry Collector" \ | ||
--url "https://github.com/open-telemetry/opentelemetry-collector" \ | ||
fpm -s dir -t rpm -n $PKG_NAME -v ${VERSION#v} -f -p "$OUTPUT_DIR" \ | ||
--vendor "$PKG_VENDOR" \ | ||
--maintainer "$PKG_MAINTAINER" \ | ||
--description "$PKG_DESCRIPTION" \ | ||
--license "$PKG_LICENSE" \ | ||
--url "$PKG_URL" \ | ||
--architecture "$ARCH" \ | ||
--config-files /etc/otel-collector/config.yaml \ | ||
$OTELCOL_PATH=/usr/bin/otelcol \ | ||
--rpm-summary "$PKG_DESCRIPTION" \ | ||
--rpm-user "$PKG_USER" \ | ||
--rpm-group "$PKG_GROUP" \ | ||
--before-install "$PREINSTALL_PATH" \ | ||
--after-install "$POSTINSTALL_PATH" \ | ||
--pre-uninstall "$PREUNINSTALL_PATH" \ | ||
$OTELCOL_PATH=/usr/bin/$PROCESS_NAME \ | ||
$SERVICE_PATH=/lib/systemd/system/$SERVICE_NAME.service \ | ||
$CONFIG_PATH=/etc/otel-collector/config.yaml |
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,73 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
SCRIPT_DIR="$( cd "$( dirname ${BASH_SOURCE[0]} )" && pwd )" | ||
REPO_DIR="$( cd "$SCRIPT_DIR/../../../../" && pwd )" | ||
PKG_PATH=${1:-} | ||
|
||
. $SCRIPT_DIR/common.sh | ||
|
||
if [[ -z "$PKG_PATH" ]]; then | ||
echo "usage: ${BASH_SOURCE[0]} DEB_OR_RPM_PATH" >&2 | ||
exit 1 | ||
fi | ||
|
||
if [[ ! -f "$PKG_PATH" ]]; then | ||
echo "$PKG_PATH not found!" >&2 | ||
exit 1 | ||
fi | ||
|
||
pkg_name="$( basename "$PKG_PATH" )" | ||
pkg_type="${pkg_name##*.}" | ||
if [[ ! "$pkg_type" =~ ^(deb|rpm)$ ]]; then | ||
echo "$PKG_PATH not supported!" >&2 | ||
exit 1 | ||
fi | ||
image_name="otelcol-$pkg_type-test" | ||
container_name="$image_name" | ||
docker_run="docker run --name $container_name -d -v /sys/fs/cgroup:/sys/fs/cgroup:ro --privileged $image_name" | ||
docker_exec="docker exec $container_name" | ||
|
||
trap "docker rm -fv $container_name >/dev/null 2>&1 || true" EXIT | ||
|
||
docker build -t $image_name -f "$SCRIPT_DIR/$pkg_type/Dockerfile.test" "$SCRIPT_DIR" | ||
docker rm -fv $container_name >/dev/null 2>&1 || true | ||
|
||
# test install | ||
echo | ||
$docker_run | ||
install_pkg "$PKG_PATH" | ||
|
||
# ensure service has started and still running after 5 seconds | ||
sleep 5 | ||
echo "Checking $SERVICE_NAME service status ..." | ||
$docker_exec systemctl --no-pager status $SERVICE_NAME | ||
|
||
echo "Checking $PROCESS_NAME process ..." | ||
$docker_exec pgrep -a -u otel $PROCESS_NAME | ||
|
||
# test uninstall | ||
echo | ||
uninstall_pkg $pkg_type | ||
|
||
echo "Checking $SERVICE_NAME service status after uninstall ..." | ||
if $docker_exec systemctl --no-pager status $SERVICE_NAME; then | ||
echo "$SERVICE_NAME service still running after uninstall!" >&2 | ||
exit 1 | ||
fi | ||
echo "$SERVICE_NAME service successfully stopped after uninstall" | ||
|
||
echo "Checking $SERVICE_NAME service existence after uninstall ..." | ||
if $docker_exec systemctl list-unit-files --all | grep $SERVICE_NAME; then | ||
echo "$SERVICE_NAME service still exists after uninstall!" >&2 | ||
exit 1 | ||
fi | ||
echo "$SERVICE_NAME service successfully removed after uninstall" | ||
|
||
echo "Checking $PROCESS_NAME process after uninstall ..." | ||
if $docker_exec pgrep $PROCESS_NAME; then | ||
echo "$PROCESS_NAME process still running after uninstall!" >&2 | ||
exit 1 | ||
fi | ||
echo "$PROCESS_NAME process successfully killed after uninstall" |