Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated systemd svc.sh to accept custom service file #1612

Merged
merged 3 commits into from
Feb 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 91 additions & 19 deletions src/Misc/layoutbin/systemd.svc.sh.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ SVC_NAME=${SVC_NAME// /_}
SVC_DESCRIPTION="{{SvcDescription}}"

SVC_CMD=$1
arg_2=${2}
SVC_RUN_AS_USER=

SCRIPT_NAME=$0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just read from an environment variable and overwrite TEMPLATE_PATH based on that env?
Then we don't need to change the rest of the svc.sh template file, also, we probably want to make same change to darwin.svc.sh.template as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but then we would have to rely on users remembering that they set the path. But regardless, by using flags, it is more explicit that you want to provide your own template, and it is nice for documentation purposes.

Also, I guess it is more convenient to provide in the command you are starting a service your template than exporting or setting env var and then running it. Let's say I exported a variable to run my template, and I made a mistake. Now, to run the default svc config, I must unset the TEMPLATE_PATH env variable since it would be used again on the next run. With this, I would just run the same command without the flag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

svc.sh is used when you install/uninstall/start/top the service, but the TEMPLATE_PATH is only used during install.

I don't think we want the customer to install using 1 template and start the service as a different template.

Also, I am trying to minimize the change as much as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can do it that way then. I will revert to the previous state and add fix it that way 😊


RUNNER_ROOT=`pwd`

Expand All @@ -16,6 +18,87 @@ CONFIG_PATH=.service

user_id=`id -u`

# is_custom_template is a controll variable stating the custom service template file is used
# or not. 0 means default run, 1 means that custom template is used
is_custom_template=0

function usage()
{
echo
echo "Usage:"
echo "$SCRIPT_NAME install|start|stop|status|uninstall"
echo
echo "Subcommands:"
echo
echo " install: Install runner service as Root or specified user."
echo " start: Manually start the runner service."
echo " stop: Manually stop the runner service."
echo " status: Display status of runner service."
echo " uninstall: Uninstall runner service."
echo
}

function installUsage()
{
echo
echo "Usage: $SCRIPT_NAME install"
echo
echo " -f string:"
echo " Path to your custom service file"
echo " -u value: "
echo " Run service as specified user"
echo
}

function uninstallUsage()
{
echo
echo "Usage: $SCRIPT_NAME uninstall"
echo
echo " -f string:"
echo " Path to your custom service file"
echo
}

function genericSubcommandUsage()
{
echo
echo "Usage: $SCRIPT_NAME $SVC_CMD"
echo
}


while getopts :f:u:h opt ${@:2}; do
case $opt in
h)

case $SVC_CMD in
"install") installUsage;;
"uninstall"|"status"|"start"|"stop") genericSubcommandUsage;;
*) usage;;
esac
exit 0
;;
u)
SVC_RUN_AS_USER=$OPTARG
;;
f)
if [[ $1 = 'install' ]]; then
TEMPLATE_PATH=$OPTARG
is_custom_template=1
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument" >&2
;;
esac
done


# systemctl must run as sudo
# this script is a convenience wrapper around systemctl
if [ $user_id -ne 0 ]; then
Expand Down Expand Up @@ -51,15 +134,18 @@ function install()
rm "${TEMP_PATH}" || failed "failed to delete ${TEMP_PATH}"
fi

log_message_suffix=
[[ $is_custom_template -eq 1 ]] && log_message_suffix="if not specified otherwise by the service template"
# can optionally use username supplied
run_as_user=${arg_2:-$SUDO_USER}
echo "Run as user: ${run_as_user}"
echo $SVC_RUN_AS_USER
run_as_user=${SVC_RUN_AS_USER:-$SUDO_USER}
echo "Run as user ${log_message_suffix}: ${run_as_user}"

run_as_uid=$(id -u ${run_as_user}) || failed "User does not exist"
echo "Run as uid: ${run_as_uid}"
echo "Run as uid ${log_message_suffix}: ${run_as_uid}"

run_as_gid=$(id -g ${run_as_user}) || failed "Group not available"
echo "gid: ${run_as_gid}"
echo "Run as gid ${log_message_suffix}: ${run_as_gid}"

sed "s/{{User}}/${run_as_user}/g; s/{{Description}}/$(echo ${SVC_DESCRIPTION} | sed -e 's/[\/&]/\\&/g')/g; s/{{RunnerRoot}}/$(echo ${RUNNER_ROOT} | sed -e 's/[\/&]/\\&/g')/g;" "${TEMPLATE_PATH}" > "${TEMP_PATH}" || failed "failed to create replacement temp file"
mv "${TEMP_PATH}" "${UNIT_PATH}" || failed "failed to copy unit file"
Expand Down Expand Up @@ -142,20 +228,6 @@ function status()
systemctl --no-pager status ${SVC_NAME}
}

function usage()
{
echo
echo Usage:
echo "./svc.sh [install, start, stop, status, uninstall]"
echo "Commands:"
echo " install [user]: Install runner service as Root or specified user."
echo " start: Manually start the runner service."
echo " stop: Manually stop the runner service."
echo " status: Display status of runner service."
echo " uninstall: Uninstall runner service."
echo
}

case $SVC_CMD in
"install") install;;
"status") status;;
Expand Down