Skip to content

Commit

Permalink
feat: Linux packages (#275)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BinaryFissionGames authored Mar 15, 2022
1 parent 551be03 commit 51f47f2
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ windows/config.yaml
opentelemetry-java-contrib-jmx-metrics.jar
plugins
release_deps
tmp
47 changes: 47 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
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"
Expand Down
116 changes: 116 additions & 0 deletions scripts/package/postinstall.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF > ${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
33 changes: 33 additions & 0 deletions scripts/package/preinstall.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 51f47f2

Please sign in to comment.