From 51f47f2634f55b168e9094c1120dfef15eba2db6 Mon Sep 17 00:00:00 2001 From: Brandon Johnson Date: Tue, 15 Mar 2022 12:54:19 -0400 Subject: [PATCH] feat: Linux packages (#275) * Initial pass at rpm & deb packaging * plugins fixes + install tests * add service tests * Tighten up testing No more relative paths (depending on running scripts in certain dir) Makefile with easy-to-run targets (no guessing on how to run these things) Document requirements (vagrant, cinc, and docker) * ignore all tmp dirs * add license * PR feedback * print service info in postinstall script * Add useful information in the post-install script * remove version from package filename * Use more cross-platform user/group add * fix iris group add in preinstall * Group can only read config, not write * Remove conditionals in file name template --- .gitignore | 1 + .goreleaser.yml | 47 +++++++++++++ scripts/package/postinstall.sh | 116 +++++++++++++++++++++++++++++++++ scripts/package/preinstall.sh | 33 ++++++++++ 4 files changed, 197 insertions(+) create mode 100644 scripts/package/postinstall.sh create mode 100644 scripts/package/preinstall.sh diff --git a/.gitignore b/.gitignore index 2a9299585..103085b79 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ windows/config.yaml opentelemetry-java-contrib-jmx-metrics.jar plugins release_deps +tmp diff --git a/.goreleaser.yml b/.goreleaser.yml index 162c38634..181567ebb 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -50,6 +50,53 @@ archives: - goos: windows format: zip +nfpms: + - id: collector + file_name_template: '{{ .PackageName }}_{{ .Os }}_{{ .Arch }}' + package_name: observiq-collector + vendor: observIQ, Inc + maintainer: observIQ + description: observIQ's distribution of the OpenTelemetry collector + homepage: https://github.com/observIQ/observiq-collector + license: Apache 2.0 + formats: + - rpm + - deb + bindir: /opt/observiq-collector + contents: + - dst: /opt/observiq-collector + type: dir + file_info: + mode: 0755 + owner: observiq-collector + group: observiq-collector + - src: release_deps/config.yaml + dst: /opt/observiq-collector/config.yaml + file_info: + mode: 0640 + owner: observiq-collector + group: observiq-collector + type: config|noreplace + - src: release_deps/opentelemetry-java-contrib-jmx-metrics.jar + dst: /opt/opentelemetry-java-contrib-jmx-metrics.jar + file_info: + mode: 0755 + owner: observiq-collector + group: observiq-collector + - dst: /opt/observiq-collector/plugins + type: dir + file_info: + mode: 0750 # restrict plugins to owner / group only + owner: observiq-collector + group: observiq-collector + # Note: plugins owner/group/mode is set in the post-install script + # Attempting to set the permissions here results in the following error: + # nfpm failed: cannot write header of release_deps/plugins/amazon_eks.yaml to data.tar.gz: archive/tar: missed writing 1736 bytes + - src: release_deps/plugins/* + dst: /opt/observiq-collector/plugins + scripts: + preinstall: "./scripts/package/preinstall.sh" + postinstall: ./scripts/package/postinstall.sh # https://goreleaser.com/customization/checksum/ checksum: name_template: "{{ .ProjectName }}-v{{ .Version }}-SHA256SUMS" diff --git a/scripts/package/postinstall.sh b/scripts/package/postinstall.sh new file mode 100644 index 000000000..98ca28370 --- /dev/null +++ b/scripts/package/postinstall.sh @@ -0,0 +1,116 @@ +#!/bin/sh +# Copyright observIQ, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +set -e + +create_systemd_service() { + if [ -d "/usr/lib/systemd/system" ]; then + systemd_service_dir="/usr/lib/systemd/system" + elif [ -d "/lib/systemd/system" ]; then + systemd_service_dir="/lib/systemd/system" + elif [ -d "/etc/systemd/system" ]; then + systemd_service_dir="/etc/systemd/system" + else + echo "failed to detect systemd service file directory" + exit 1 + fi + + echo "detected service file directory: ${systemd_service_dir}" + + systemd_service_file="${systemd_service_dir}/observiq-collector.service" + + cat < ${systemd_service_file} +[Unit] +Description=observIQ's distribution of the OpenTelemetry collector +After=network.target +StartLimitIntervalSec=120 +StartLimitBurst=5 +[Service] +Type=simple +User=observiq-collector +Group=observiq-collector +Environment=PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin +WorkingDirectory=/opt/observiq-collector +ExecStart=/opt/observiq-collector/observiq-collector --config config.yaml +SuccessExitStatus=0 +TimeoutSec=120 +StandardOutput=journal +Restart=on-failure +RestartSec=5s +[Install] +WantedBy=multi-user.target +EOF + + chmod 0644 "${systemd_service_file}" + chown root:root "${systemd_service_file}" + + systemctl daemon-reload + + echo "configured systemd service" + + cat << EOF + +The "observiq-collector" service has been configured! + +The collector's config file can be found here: + /opt/observiq-collector/config.yaml + +For more information on configuring the collector, see the OpenTelemetry docs: + https://opentelemetry.io/docs/collector/configuration/ + +To stop the observiq-collector service, run: + sudo systemctl stop observiq-collector + +To start the observiq-collector service, run: + sudo systemctl start observiq-collector + +To restart the observiq-collector service, run: + sudo systemctl restart observiq-collector +EOF +} + +init_type() { + systemd_test="$(systemctl 2>/dev/null || : 2>&1)" + if command printf "$systemd_test" | grep -q '\-.mount'; then + command printf "systemd" + return + fi + + command printf "unknown" + return +} + +install_service() { + service_type="$(init_type)" + case "$service_type" in + systemd) + create_systemd_service + ;; + *) + echo "could not detect init system, skipping service configuration" + esac +} + +finish_permissions() { + # Goreleaser does not set plugin file permissions, so do them here + # We also change the owner of the binary to observiq-collector + chown -R observiq-collector:observiq-collector /opt/observiq-collector/observiq-collector /opt/observiq-collector/plugins/* + chmod 0640 /opt/observiq-collector/plugins/* +} + + +finish_permissions +install_service diff --git a/scripts/package/preinstall.sh b/scripts/package/preinstall.sh new file mode 100644 index 000000000..db260f837 --- /dev/null +++ b/scripts/package/preinstall.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# Copyright observIQ, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +set -e + +username="observiq-collector" + +if getent group "$username" > /dev/null 2>&1; then + echo "Group ${username} already exists." +else + groupadd "$username" +fi + +if id "$username" > /dev/null 2>&1; then + echo "User ${username} already exists" + exit 0 +else + useradd --shell /sbin/nologin --system "$username" -g "$username" +fi +