Skip to content

Commit

Permalink
chore(scripts/install.sh): add support for systemd (#733)
Browse files Browse the repository at this point in the history
* chore(scripts/install.sh): do not abort script if configuration already exists

Signed-off-by: Dominik Rosiek <[email protected]>

* chore(examples): add systemd service example

Signed-off-by: Dominik Rosiek <[email protected]>

* chore(scripts/install.sh): fix tags generations

Signed-off-by: Dominik Rosiek <[email protected]>

* chore(scripts/install.sh): fix template generation

Signed-off-by: Dominik Rosiek <[email protected]>

* chore(scripts/install.sh): increase memory limit to 2GB

Signed-off-by: Dominik Rosiek <[email protected]>

* chore(scripts/install.sh): add support for systemd

Signed-off-by: Dominik Rosiek <[email protected]>

* chore(scripts/install.sh): fix syntax

Signed-off-by: Dominik Rosiek <[email protected]>

* Update scripts/install.sh

Signed-off-by: Dominik Rosiek <[email protected]>
  • Loading branch information
sumo-drosiek authored Sep 22, 2022
1 parent 91245f4 commit 38ad243
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 45 deletions.
13 changes: 13 additions & 0 deletions examples/systemd/otelcol-sumo.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Sumo Logic Distribution for OpenTelemetry Collector

[Service]
ExecStart=/usr/local/bin/otelcol-sumo --config /etc/otelcol-sumo/config.yaml
User=opentelemetry
Group=opentelemetry
MemoryHigh=2000M
MemoryMax=3000M
TimeoutStopSec=20

[Install]
WantedBy=multi-user.target
145 changes: 100 additions & 45 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ ARG_SHORT_STORAGE='s'
ARG_LONG_STORAGE='storage'
ARG_SHORT_COLLECTOR='n'
ARG_LONG_COLLECTOR='collector-name'
ARG_SHORT_SYSTEMD='d'
ARG_LONG_SYSTEMD='disable-systemd-installation'

############################ Variables (see set_defaults function for default values)

Expand All @@ -39,6 +41,8 @@ VERSION=""
CONTINUE=false
FILE_STORAGE=""
CONFIG_DIRECTORY=""
SYSTEMD_DISABLED=""
SYSTEMD_CONFIG=""

############################ Functions

Expand All @@ -52,6 +56,7 @@ Usage: bash install.sh [--${ARG_LONG_TOKEN} <token>] [--${ARG_LONG_COLLECTOR} na
-${ARG_SHORT_API}, --${ARG_LONG_API} <url> Api URL
-${ARG_SHORT_CONFIG}, --${ARG_LONG_CONFIG} <config dir path> Path to the configuration directory (default is '/etc/otelcol-sumo')
-${ARG_SHORT_SYSTEMD}, --${ARG_LONG_SYSTEMD} Do not set up systemd service
-${ARG_SHORT_STORAGE}, --${ARG_LONG_STORAGE} <storage dir path> Path to the storage directory (default is '/var/lib/sumologic/file_storage')
-${ARG_SHORT_VERSION}, --${ARG_LONG_VERSION} <version> Manually specified version, e.g. 0.55.0-sumo-0
-${ARG_SHORT_YES}, --${ARG_LONG_YES} Do not ask for confirmation
Expand All @@ -67,6 +72,8 @@ function set_defaults() {
COLLECTOR_NAME="$(hostname)"
FILE_STORAGE="/var/lib/sumologic/file_storage"
CONFIG_DIRECTORY="/etc/otelcol-sumo"
SYSTEMD_DISABLED=false
SYSTEMD_CONFIG="/etc/systemd/system/otelcol-sumo.service"
}

function parse_options() {
Expand Down Expand Up @@ -102,7 +109,10 @@ function parse_options() {
"--${ARG_LONG_COLLECTOR}")
set -- "$@" "-${ARG_SHORT_COLLECTOR}"
;;
"-${ARG_SHORT_TOKEN}"|"-${ARG_SHORT_HELP}"|"-${ARG_SHORT_API}"|"-${ARG_SHORT_TAG}"|"-${ARG_SHORT_VERSION}"|"-${ARG_SHORT_YES}"|"-${ARG_SHORT_CONFIG}"|"-${ARG_SHORT_STORAGE}"|"-${ARG_SHORT_COLLECTOR}")
"--${ARG_LONG_SYSTEMD}")
set -- "$@" "-${ARG_SHORT_SYSTEMD}"
;;
"-${ARG_SHORT_TOKEN}"|"-${ARG_SHORT_HELP}"|"-${ARG_SHORT_API}"|"-${ARG_SHORT_TAG}"|"-${ARG_SHORT_VERSION}"|"-${ARG_SHORT_YES}"|"-${ARG_SHORT_CONFIG}"|"-${ARG_SHORT_STORAGE}"|"-${ARG_SHORT_COLLECTOR}"|"-${ARG_SHORT_SYSTEMD}")
set -- "$@" "${arg}" ;;
-*)
echo "Unknown option ${arg}"; usage; exit 1 ;;
Expand All @@ -116,7 +126,7 @@ function parse_options() {

while true; do
set +e
getopts "${ARG_SHORT_HELP}${ARG_SHORT_TOKEN}:${ARG_SHORT_API}:${ARG_SHORT_TAG}:${ARG_SHORT_VERSION}:${ARG_SHORT_YES}${ARG_SHORT_CONFIG}:${ARG_SHORT_STORAGE}:${ARG_SHORT_COLLECTOR}:" opt
getopts "${ARG_SHORT_HELP}${ARG_SHORT_TOKEN}:${ARG_SHORT_API}:${ARG_SHORT_TAG}:${ARG_SHORT_VERSION}:${ARG_SHORT_YES}${ARG_SHORT_CONFIG}:${ARG_SHORT_STORAGE}:${ARG_SHORT_COLLECTOR}:${ARG_SHORT_SYSTEMD}" opt
set -e

# Invalid argument catched, print and exit
Expand All @@ -136,6 +146,7 @@ function parse_options() {
"${ARG_SHORT_VERSION}") VERSION="${OPTARG}" ;;
"${ARG_SHORT_COLLECTOR}") COLLECTOR_NAME="${OPTARG}" ;;
"${ARG_SHORT_YES}") CONTINUE=true ;;
"${ARG_SHORT_SYSTEMD}") SYSTEMD_DISABLED=true ;;
"${ARG_SHORT_TAG}")
if [[ "${OPTARG}" != ?*"="* ]]; then
echo "Invalid tag: '${OPTARG}'. Should be in 'key=value' format"
Expand Down Expand Up @@ -172,6 +183,10 @@ function check_dependencies() {
fi
done

if ! command -v systemctl &> /dev/null; then
SYSTEMD_DISABLED=true
fi

if [[ "${error}" == "1" ]] ; then
exit 1
fi
Expand Down Expand Up @@ -345,7 +360,7 @@ check_dependencies
set_defaults
parse_options "$@"

readonly INSTALL_TOKEN API_BASE_URL FIELDS COLLECTOR_NAME CONTINUE FILE_STORAGE CONFIG_DIRECTORY
readonly INSTALL_TOKEN API_BASE_URL FIELDS COLLECTOR_NAME CONTINUE FILE_STORAGE CONFIG_DIRECTORY SYSTEMD_CONFIG SYSTEMD_DISABLED

OS_TYPE="$(get_os_type)"
ARCH_TYPE="$(get_arch_type)"
Expand Down Expand Up @@ -427,56 +442,96 @@ ask_to_continue
readonly CONFIG_PATH="${CONFIG_DIRECTORY}/config.yaml"

if [[ -f "${CONFIG_PATH}" ]]; then
echo "Configuration (${CONFIG_PATH}) already exist). Aborting"
echo "Configuration (${CONFIG_PATH}) already exist)"
else

echo -e "Creating file_storage directory (${FILE_STORAGE})"
sudo mkdir -p "${FILE_STORAGE}"

echo -e "Creating configuration directory (${CONFIG_DIRECTORY})"
sudo mkdir -p "${CONFIG_DIRECTORY}"


echo "Generating configuration and saving as ${CONFIG_PATH}"

CONFIG_URL="https://raw.githubusercontent.com/SumoLogic/sumologic-otel-collector/v${VERSION}/examples/default.yaml"

# ToDo: remove this line after release
CONFIG_URL="https://raw.githubusercontent.com/SumoLogic/sumologic-otel-collector/5147e309c37ba543d76b4544fdedfbd4c77cc820/examples/default.yaml"

# Generate template
export FILE_STORAGE
export COLLECTOR_NAME
export INSTALL_TOKEN
export API_BASE_URL

curl -s "${CONFIG_URL}" | \
if [[ -n "${API_BASE_URL}" ]]; then
# add api_base_url after install_token
sed "/^ install_token/a\\
api_base_url: \${API_BASE_URL}
"
else
cat -
fi | \
if [[ -n "${FIELDS}" ]]; then
# add collector_fields after install_token
sed "/^ install_token/a\\
collector_fields:${FIELDS}
"
else
cat -
fi | \
if [[ "${OS_TYPE}" == "darwin" ]]; then
# adjust default configuration for macos
sed '/^ process:/d'
else
cat -
fi | \
envsubst | sudo tee "${CONFIG_PATH}"

echo 'Changing permissions for config file and storage'
sudo chmod 640 "${CONFIG_PATH}" "${FILE_STORAGE}"
fi

if [[ "${SYSTEMD_DISABLED}" == "true" ]]; then
echo "Use 'sudo otelcol-sumo --config=${CONFIG_PATH}' to run Sumo Logic Distribution for OpenTelemetry Collector"
exit 0
fi

echo -e "Creating file_storage directory (${FILE_STORAGE})"
sudo mkdir -p "${FILE_STORAGE}"
echo 'We are going to set up systemd service'
ask_to_continue

echo -e "Creating configuration directory (${CONFIG_DIRECTORY})"
sudo mkdir -p "${CONFIG_DIRECTORY}"
if [[ -f "${SYSTEMD_CONFIG}" ]]; then
echo "Configuration for systemd service (${SYSTEMD_CONFIG}) already exist. Aborting"
exit 0
fi

echo 'Creating user and group'
if getent passwd opentelemetry > /dev/null; then
echo 'User and group already created'
else
sudo useradd -mrUs /bin/false opentelemetry
fi

echo "Generating configuration and saving as ${CONFIG_PATH}"
echo 'Changing ownership for config and storage'
sudo chown -R opentelemetry:opentelemetry "${CONFIG_PATH}" "${FILE_STORAGE}"

CONFIG_URL="https://raw.githubusercontent.com/SumoLogic/sumologic-otel-collector/v${VERSION}/examples/default.yaml"
CONFIG_URL="https://raw.githubusercontent.com/SumoLogic/sumologic-otel-collector/v${VERSION}/examples/systemd/otelcol-sumo.service"

# ToDo: remove this line after release
CONFIG_URL="https://raw.githubusercontent.com/SumoLogic/sumologic-otel-collector/5147e309c37ba543d76b4544fdedfbd4c77cc820/examples/default.yaml"

# Generate template
export FILE_STORAGE
export COLLECTOR_NAME
export INSTALL_TOKEN
export API_BASE_URL

curl -s "${CONFIG_URL}" | \
if [[ -n "${API_BASE_URL}" ]]; then
# add api_base_url after install_token
sed "/^ install_token/a\\
api_base_url: \${API_BASE_URL}
"
else
cat -
fi | \
if [[ -n "${FIELDS}" ]]; then
# add collector_fields after install_token
sed "/^ install_token/a\\
collector_fields:${FIELDS}
"
else
cat -
fi | \
if [[ "${OS_TYPE}" == "darwin" ]]; then
# adjust default configuration for macos
sed '/^ process:/d'
else
cat -
fi | \
envsubst | sudo tee "${CONFIG_PATH}"
SYSTEMD_CONFIG="https://raw.githubusercontent.com/SumoLogic/sumologic-otel-collector/1e677911ea866769b33fa259c68f4507369fc141/examples/systemd/otelcol-sumo.service"

echo 'Getting service configuration'
curl -fL "${SYSTEMD_CONFIG}" --output otelcol-sumo.service --progress-bar
sudo mv otelcol-sumo.service "${SYSTEMD_CONFIG}"

echo 'Enable otelcol-sumo service'
sudo systemctl enable otelcol-sumo

echo 'Changing permissions to config file'
sudo chmod 640 "${CONFIG_PATH}"
echo 'Starting otelcol-sumo service'
sudo systemctl start otelcol-sumo

echo "Use 'sudo otelcol-sumo --config=${CONFIG_PATH}' to run Sumo Logic Distribution for OpenTelemetry Collector"
echo 'Waiting 10s before checking status'
sleep 10
sudo systemctl status otelcol-sumo

0 comments on commit 38ad243

Please sign in to comment.