Skip to content

Commit

Permalink
Simplify original commit to minimal viable
Browse files Browse the repository at this point in the history
  • Loading branch information
torcolvin committed Oct 10, 2024
1 parent 92e4048 commit d393e9d
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 29 deletions.
5 changes: 3 additions & 2 deletions integration-test/service-install-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
# software will be governed by the Apache License, Version 2.0, included in
# the file licenses/APL2.txt.

# This file is used by github CI or locally and runs a subset of test scripts to validate the service installation done by the package managers. This is intended to be run from Linux or Mac.

set -eux -o pipefail

IMAGES=(
#"almalinux:9"
"almalinux:9"
"amazonlinux:2"
"amazonlinux:2023"
"debian:10"
"debian:11"
"debian:12"
"redhat/ubi8"
"redhat/ubi9"
#"rockylinux:9"
"rockylinux:9"
"ubuntu:20.04"
"ubuntu:22.04"
"ubuntu:24.04"
Expand Down
10 changes: 9 additions & 1 deletion integration-test/service-test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#/bin/sh

# Copyright 2024-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included
Expand All @@ -6,7 +8,7 @@
# software will be governed by the Apache License, Version 2.0, included in
# the file licenses/APL2.txt.

#/bin/sh
# This code is intneded to be run from within a docker container of a specific platform and runs a subset of the service scripts. The full service can not be validated since systemd does not work in docker contains. This is intended to run in /bin/sh to test dash environments on debian systems.

set -eux -o pipefail

Expand Down Expand Up @@ -55,8 +57,14 @@ EOF

export PATH=/tmp/systemctl_wrapper:$PATH
fi

./sync_gateway_service_install.sh
./sync_gateway_service_upgrade.sh
./sync_gateway_service_uninstall.sh

# test again with runas option
./sync_gateway_service_install.sh --runas=root
./sync_gateway_service_upgrade.sh
./sync_gateway_service_uninstall.sh

echo "Successful service test"
119 changes: 108 additions & 11 deletions service/sync_gateway_service_install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/sh

set -x
# Copyright 2014-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included in
Expand All @@ -10,6 +9,8 @@ set -x
# licenses/APL2.txt.

# Set default values
OS=""
VER=""
SERVICE_NAME="sync_gateway"
# Determine the absolute path of the installation directory
# $( dirname "$0" ) get the directory containing this script
Expand All @@ -29,10 +30,9 @@ GATEWAY_TEMPLATE_VAR=${INSTALL_DIR}/bin/sync_gateway
CONFIG_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/sync_gateway.json
LOGS_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/logs
SERVICE_CMD_ONLY=false
PLATFORM="$(uname)"

usage() {
echo "This script creates a systemd or launchctl service to run a sync_gateway instance."
echo "This script creates a service to run a sync_gateway instance."
echo "If you want to install more than one service instance"
echo "create additional services with different names."
echo ""
Expand All @@ -46,7 +46,33 @@ usage() {
echo ""
}

PLATFORM=$(uname)
ostype() {
if [ -x "$(command -v lsb_release)" ]; then
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/os-release ]; then
. /etc/os-release
OS=$(echo "${ID}")
if [ "${OS}" = "debian" ]; then
VER=$(cat /etc/debian_version)
else
VER=$VERSION_ID
fi
elif [ -f /etc/redhat-release ]; then
OS=rhel
VER=$(cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//)
elif [ -f /etc/system-release ]; then
OS=rhel
VER=5.0
else
OS=$(uname -s)
VER=$(uname -r)
fi

OS=$(echo "${OS}" | tr "[:upper:]" "[:lower:]")
OS_MAJOR_VERSION=$(echo $VER | sed 's/\..*$//')
OS_MINOR_VERSION=$(echo $VER | sed s/[0-9]*\.//)
}

# expand template variables + preserve formatting
render_template() {
Expand All @@ -64,7 +90,7 @@ setup_output_dirs() {
# Run pre installation actions
pre_install_actions() {
# Check that runtime user account exists
if [ "$PLATFORM" != "Darwin" ] && [ -z $(id -u $RUNAS_TEMPLATE_VAR 2>/dev/null) ]; then
if [ "$OS" != "darwin" ] && [ -z $(id -u $RUNAS_TEMPLATE_VAR 2>/dev/null) ]; then
echo "The sync_gateway runtime user account does not exist \"$RUNAS_TEMPLATE_VAR\"." >/dev/stderr
exit 1
fi
Expand Down Expand Up @@ -113,9 +139,10 @@ pre_install_actions() {
#

#Figure out the OS type of the current system
ostype

#If the OS is MAC OSX, set the default user account home path to /Users/sync_gateway
if [ "$PLATFORM" = "Darwin" ]; then
if [ "$OS" = "darwin" ]; then
RUNBASE_TEMPLATE_VAR=/Users/sync_gateway
CONFIG_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/sync_gateway.json
LOGS_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/logs
Expand All @@ -138,7 +165,7 @@ while [ "$1" != "" ]; do
;;
--runas)
RUNAS_TEMPLATE_VAR=$VALUE
if [ "$PLATFORM" != "Darwin" ]; then
if [ "$OS" != "darwin" ]; then
RUNBASE_TEMPLATE_VAR=$(getent passwd "$VALUE" | cut -d: -f 6)
else
RUNBASE_TEMPLATE_VAR=$(eval "echo ~$VALUE")
Expand Down Expand Up @@ -171,19 +198,89 @@ while [ "$1" != "" ]; do
done

#Install the service for the specific platform
case ${PLATFORM} in
Linux*)
case $OS in
debian)
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 8))*)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "systemctl start ${SERVICE_NAME}"
else
pre_install_actions
mkdir -p /usr/lib/systemd/system
render_template script_templates/systemd_debian_sync_gateway.tpl >/usr/lib/systemd/system/${SERVICE_NAME}.service
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
fi
;;
esac
;;
ubuntu)
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 16))*)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "systemctl start ${SERVICE_NAME}"
else
pre_install_actions
render_template script_templates/systemd_debian_sync_gateway.tpl >/lib/systemd/system/${SERVICE_NAME}.service
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
fi
;;
$((OS_MAJOR_VERSION >= 12))*)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "service ${SERVICE_NAME} start"
else
pre_install_actions
render_template script_templates/upstart_ubuntu_sync_gateway.tpl >/etc/init/${SERVICE_NAME}.conf
service ${SERVICE_NAME} start
fi
;;
*)
echo "ERROR: Unsupported Ubuntu Version \"$VER\""
usage
exit 1
;;
esac
;;
redhat* | rhel* | centos | ol | rocky | almalinux )
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 7))*)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "systemctl start ${SERVICE_NAME}"
else
pre_install_actions
render_template script_templates/systemd_sync_gateway.tpl >/usr/lib/systemd/system/${SERVICE_NAME}.service
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
fi
;;
*)
echo "ERROR: Unsupported RedHat/CentOS/Rocky/Alma Version \"$VER\""
usage
exit 1
;;
esac
;;
amzn*)
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 2))*)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "systemctl start ${SERVICE_NAME}"
else
pre_install_actions
render_template script_templates/systemd_sync_gateway.tpl >/usr/lib/systemd/system/${SERVICE_NAME}.service
systemctl enable ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
fi
;;
Darwin*)
*)
echo "ERROR: Unsupported Amazon Linux Version \"$VER\""
usage
exit 1
;;
esac
;;
darwin)
if [ "$SERVICE_CMD_ONLY" = true ]; then
echo "launchctl start /Library/LaunchDaemons/com.couchbase.mobile.sync_gateway.plist"
else
Expand All @@ -193,7 +290,7 @@ Darwin*)
fi
;;
*)
echo "ERROR: unknown platform \"$PLATFORM\""
echo "ERROR: unknown OS \"$OS\""
usage
exit 1
;;
Expand Down
107 changes: 100 additions & 7 deletions service/sync_gateway_service_uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# licenses/APL2.txt.

# Set default values
OS=""
VER=""
SERVICE_NAME="sync_gateway"
# Determine the absolute path of the installation directory
# $( dirname "$0" ) get the directory containing this script
Expand All @@ -28,18 +30,48 @@ GATEWAY_TEMPLATE_VAR=${INSTALL_DIR}/bin/sync_gateway
CONFIG_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/sync_gateway.json
LOGS_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/logs
SERVICE_CMD_ONLY=false
PLATFORM="$(uname)"

usage() {
echo "This script removes a systemd or launchctl service to run a sync_gateway instance."
echo "This script removes a service to run a sync_gateway instance."
}

ostype() {
if [ -x "$(command -v lsb_release)" ]; then
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/os-release ]; then
. /etc/os-release
OS=$(echo "${ID}")
if [ "${OS}" = "debian" ]; then
VER=$(cat /etc/debian_version)
else
VER=$VERSION_ID
fi
elif [ -f /etc/redhat-release ]; then
OS=rhel
VER=$(cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//)
elif [ -f /etc/system-release ]; then
OS=rhel
VER=5.0
else
OS=$(uname -s)
VER=$(uname -r)
fi

OS=$(echo "${OS}" | tr "[:upper:]" "[:lower:]")
OS_MAJOR_VERSION=$(echo $VER | sed 's/\..*$//')
OS_MINOR_VERSION=$(echo $VER | sed s/[0-9]*\.//)
}

#
#script starts here
#

#Figure out the OS type of the current system
ostype

#If the OS is MAC OSX, set the default user account home path to /Users/sync_gateway
if [ "$PLATFORM" = "Darwin" ]; then
if [ "$OS" = "darwin" ]; then
RUNBASE_TEMPLATE_VAR=/Users/sync_gateway
CONFIG_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/sync_gateway.json
LOGS_TEMPLATE_VAR=${RUNBASE_TEMPLATE_VAR}/logs
Expand All @@ -52,23 +84,84 @@ if [ $(id -u) != 0 ]; then
fi

#Install the service for the specific platform
case $PLATFORM in
Linux)
case $OS in
debian)
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 8))*)
systemctl stop ${SERVICE_NAME}
systemctl disable ${SERVICE_NAME}

if [ -f /usr/lib/systemd/system/${SERVICE_NAME}.service ]; then
rm /usr/lib/systemd/system/${SERVICE_NAME}.service
fi
;;
esac
;;
ubuntu)
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 16))*)
systemctl stop ${SERVICE_NAME}
systemctl disable ${SERVICE_NAME}

if [ -f /lib/systemd/system/${SERVICE_NAME}.service ]; then
rm /lib/systemd/system/${SERVICE_NAME}.service
fi
;;
$((OS_MAJOR_VERSION >= 12))*)
service ${SERVICE_NAME} stop
if [ -f /etc/init/${SERVICE_NAME}.conf ]; then
rm /etc/init/${SERVICE_NAME}.conf
fi
;;
*)
echo "ERROR: Unsupported Ubuntu Version \"$VER\""
usage
exit 1
;;
esac
;;
redhat* | rhel* | centos | ol | rocky | almalinux )
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 7))*)
systemctl stop ${SERVICE_NAME}
systemctl disable ${SERVICE_NAME}

if [ -f /usr/lib/systemd/system/${SERVICE_NAME}.service ]; then
rm /usr/lib/systemd/system/${SERVICE_NAME}.service
fi
;;
Darwin)
*)
echo "ERROR: Unsupported RedHat/CentOS/Rocky/Alma Version \"$VER\""
usage
exit 1
;;
esac
;;
amzn*)
case 1:${OS_MAJOR_VERSION:--} in
$((OS_MAJOR_VERSION >= 2))*)
systemctl stop ${SERVICE_NAME}
systemctl disable ${SERVICE_NAME}

if [ -f /lib/systemd/system/${SERVICE_NAME}.service ]; then
rm /lib/systemd/system/${SERVICE_NAME}.service
fi
;;
*)
echo "ERROR: Unsupported Amazon Linux Version \"$VER\""
usage
exit 1
;;
esac
;;
darwin)
launchctl unload /Library/LaunchDaemons/com.couchbase.mobile.sync_gateway.plist
if [ -f /Library/LaunchDaemons/com.couchbase.mobile.sync_gateway.plist ]; then
rm /Library/LaunchDaemons/com.couchbase.mobile.sync_gateway.plist
fi
;;
*)
echo "ERROR: unknown platform \"$PLATFORM\""
echo "ERROR: unknown OS \"$OS\""
usage
exit 1
;;
Expand Down
Loading

0 comments on commit d393e9d

Please sign in to comment.