-
Notifications
You must be signed in to change notification settings - Fork 465
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
[WFCORE-6935] Improve systemd service units #6179
Conversation
0af8db3
to
5e7a85b
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall I think this looks good. Just a minor suggestion and a couple questions.
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Outdated
Show resolved
Hide resolved
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @jamezp , added some comments related
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Outdated
Show resolved
Hide resolved
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. We can figure out the version thing later and really maybe it doesn't make sense to keep it up to date. After all that is the full version and this is core.
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service
Show resolved
Hide resolved
|
||
== Start and enable WildFly | ||
|
||
# systemctl start wildfly-$MODE.service |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To respect Bash's best practises:
# systemctl start "wildfly-${MODE}.service"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
== Configure systemd | ||
|
||
# cd /opt/wildfly/bin/systemd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid hard coding /opt/wildfly everywhere, I would suggest defining a WILDFLY_HOME=/opt/wildfly at the beginning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
# cd /opt/wildfly/bin/systemd | ||
# cp wildfly-$MODE.conf /etc/sysconfig/ | ||
# cp wildfly-$MODE.service $(pkg-config systemd --variable=systemdsystemunitdir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To follow bash's best practices:
# cp "wildfly-${MODE}.conf" /etc/sysconfig/
# cp "wildfly-${MODE}.service" $(pkg-config systemd --variable=systemdsystemunitdir)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
== Remove WildFly systemd service | ||
|
||
# rm -f $(pkg-config systemd --variable=systemdsystemunitdir)/wildfly-$MODE.service |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rm -f "$(pkg-config systemd --variable=systemdsystemunitdir)/wildfly-${MODE}.service"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
JBOSS_HOME="$(realpath "$SCRIPT_DIR/../..")" | ||
JBOSS_SYSTEMD_MODE="${1}" | ||
|
||
if [ -z "$JBOSS_SYSTEMD_MODE" ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if [ -z "${JBOSS_SYSTEMD_MODE}" ]; then
for consistency on top of best practice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
exit 0; | ||
fi | ||
|
||
SYSTEMD_FILE="$SCRIPT_DIR/wildfly-${JBOSS_SYSTEMD_MODE}.service" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SYSTEMD_FILE="${SCRIPT_DIR}/wildfly-${JBOSS_SYSTEMD_MODE}.service"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
SYSTEMD_FILE="$SCRIPT_DIR/wildfly-${JBOSS_SYSTEMD_MODE}.service" | ||
JBOSS_SYSTEMD_USER="${2:-wildfly}" | ||
JBOSS_SYSTEMD_GROUP="${3:-wildfly}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference is to add single quote around literals to make it clear to the reader that it's supposed to be fixed characters strings:
JBOSS_SYSTEMD_GROUP="${3:-'wildfly'}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would move the quotes to the Systemd unit file as well, is that still valid?
JBOSS_SYSTEMD_GROUP="${3:-wildfly}" | ||
|
||
echo "INFO: Systemd Unit File to generate: ${SYSTEMD_FILE}" | ||
echo "INFO: Using JBOSS_HOME: $JBOSS_HOME" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we have JBOSS_HOME here, we should use it at the beginning, to not repeat /opt/wildfly all the time.
Also:
echo "INFO: Using JBOSS_HOME: ${JBOSS_HOME}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we have JBOSS_HOME here, we should use it at the beginning, to not repeat /opt/wildfly all the time.
JBOSS_HOME is initialized at the beginning of the script and there is no "/opt/wildfly" path hardcoded in this script, so I don't follow you here.
Also:
echo "INFO: Using JBOSS_HOME: ${JBOSS_HOME}"
Done
echo "INFO: User: ${JBOSS_SYSTEMD_USER}" | ||
echo "INFO: Group: ${JBOSS_SYSTEMD_GROUP}" | ||
|
||
while true; do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interactive shell is a bad idea. I would suggest using opts so that the user can pass a parameters if he wants to generate the systemd file.
We should also aimed at making this script idempotent. (I'll do another review in this regard, after this one)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interactive shell is a bad idea.
It is not expected this file be invoked by a third-party tool, this is just a utility that is expected to be used by a user to make his life easier when he needs to replace the JBOSS_HOME, user:group in the systemd unit files. Asking interactively for confirmation here allows the user to review what he is going to configure, so I think it is useful here.
Would that convince you to leave it as it is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No :) I really think it's a bad idea.
Bear in mind that this will be used by Ops (or DevOps), they won't expect an interactive shell and will be quite annoyed with it. Also, they are most likely going to automate around this (I've done so in the past, at customers). They may want to automate the deployment of Wildfly based and this and have script (or a tool like Ansible) automatically generate the file on the target system....
And right now, it's impossible to because the script is interactive by default. So if you really want to keep it, at least you should provide a mecanism around it.
IMHO, the best approach, that would match what people do in this kind of script, would be to test if the var is defined and act according to it:
if [ -z ${SYSTEMD_FILE} ]; then
echo "No systemd file provided, not generating one."
exit 0
fi
(last point if somehow you really, really want to keep this interaction thing, I would move into a function, because it's big chunk of code)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rpelisse Ok, you bring more experience to this from Ansible automation, so I'll remove the interactive question or add a confirm flag for skipping it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rpelisse I've added a new argument c,--confirm to avoid the interactive mode.
esac | ||
done | ||
|
||
sed -e "s|@@@JBOSS_SYSTEMD_SERVER_HOME@@@|$JBOSS_HOME|g" \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sed -e "s|@@@JBOSS_SYSTEMD_SERVER_HOME@@@|${JBOSS_HOME}|g" \
-e "s|@@@JBOSS_SYSTEMD_USER@@@|${JBOSS_SYSTEMD_USER}|g" \
-e "s|@@@JBOSS_SYSTEMD_GROUP@@@|${JBOSS_SYSTEMD_GROUP}|g" \
"${SYSTEMD_FILE}.template" > "${SYSTEMD_FILE}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
-e "s|@@@JBOSS_SYSTEMD_GROUP@@@|$JBOSS_SYSTEMD_GROUP|g" \ | ||
"${SYSTEMD_FILE}.template" > "$SYSTEMD_FILE" | ||
|
||
systemd-analyze verify --recursive-errors=no "$SYSTEMD_FILE" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
systemd-analyze verify --recursive-errors=no "${SYSTEMD_FILE}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
echo "" | ||
echo "INFO: Systemd Unit File generated." | ||
echo "INFO: The ${JBOSS_SYSTEMD_USER}:${JBOSS_SYSTEMD_GROUP} are the user:group configured to launch the server. You have to ensure this user and group exist and have the necessary permissions to read and launch the server." | ||
echo "INFO: Use $JBOSS_HOME/bin/systemd/wildfly-${JBOSS_SYSTEMD_MODE}.conf to configure the server environment." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
echo "INFO: Use ${JBOSS_HOME}/bin/systemd/wildfly-${JBOSS_SYSTEMD_MODE}.conf to configure the server environment."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
echo "sudo systemctl start $(basename $SYSTEMD_FILE)" | ||
echo "" | ||
echo "INFO: In case of issues, you can check the service logs with:" | ||
echo "sudo journalctl -u $(basename $SYSTEMD_FILE)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be mindful of where to use quote:
echo sudo journalctl -u $(basename "${SYSTEMD_FILE}")
This approach will yield a more comprehensible error message if, somehow, a space has been inserted in the variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core-feature-pack/common/src/main/resources/content/bin/systemd/launch.sh
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @rpelisse , I fixed most of the suggestion and added some minor comments
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Hello @rpelisse could you review this again? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yersan I'm super sorry to have not reviewed this PR (again) more quickly... I did find a couple of areas I think we should look into before we merge.
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service
Show resolved
Hide resolved
...ature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service.template
Show resolved
Hide resolved
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service.template
Show resolved
Hide resolved
exit 1 | ||
} | ||
|
||
SCRIPT_NAME=$(basename "$0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SCRIPT_NAME=$(basename "$0")
to SCRIPT_NAME=$(basename "${0}")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
} | ||
|
||
SCRIPT_NAME=$(basename "$0") | ||
SCRIPT_DIR="$(dirname "$(realpath "$0")")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
to SCRIPT_DIR="$(dirname "$(realpath "${0}")")"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
esac | ||
done | ||
|
||
if [ "${JBOSS_SYSTEMD_MODE}" != "standalone" ] && [ "${JBOSS_SYSTEMD_MODE}" != "domain" ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting into super nickpicking zone :)
Technically by using "" you are telling the script interpreter to look for cmd substitution or variable to be replaced by their content into the string. So it would be better to have 'standalone'
and 'domain'
instead of "standalone"
and "domain"
.
I did say it was nickpicking, right? ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right :) ... but it is a trivial change and your argument is valid, so done
echo "INFO: Using JBOSS_HOME: ${JBOSS_HOME}" | ||
echo "INFO: User: ${JBOSS_SYSTEMD_USER}" | ||
echo "INFO: Group: ${JBOSS_SYSTEMD_GROUP}" | ||
echo "INFO: Confirm: ${JBOSS_CONFIRM}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not echo this variable, as it will only be used for interactive mode. Having this value in logfile of automated install will be ... strange, and might confused ops ("Wait, why my JBOSS is not confirmed? What does it means???".
If you intend to echo the value for debugging purpose, remember that simply running the script with the -x
option will echo all the variable values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to me, I removed it.
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Show resolved
Hide resolved
if [ 'y' != "${JBOSS_CONFIRM}" ] && [ 'Y' != "${JBOSS_CONFIRM}" ];then | ||
while true; do | ||
read -p "Do you want to generate ${SYSTEMD_FILE}? (y/n): " choice | ||
case "$choice" in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case "$choice" in
to case "${choice}" in
Also, as I suspect choice
will not change value after the script starts, I would suggest to go uppercase to cleary display that (so rename it to CHOICE).
Ideally, I would prefix any vars declaration by readonly, but I'm not sure if this supported across all interpreter or just a bash feature. It's a good practise to use readonly
, because it prevent some already defined variable in the shell to override a value in the script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case "$choice" in to case "${choice}" in
Done
Also, as I suspect choice will not change value after the script starts
choice
will get a new value on each loop inside the while if the user does not write any expected answer. So I leave it untoched
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rpelisse I'm done with this, if there is anything else critital, we would like to merge this as it is now, please take another look, thank you!
exit 1 | ||
} | ||
|
||
SCRIPT_NAME=$(basename "$0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
} | ||
|
||
SCRIPT_NAME=$(basename "$0") | ||
SCRIPT_DIR="$(dirname "$(realpath "$0")")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
echo "INFO: Using JBOSS_HOME: ${JBOSS_HOME}" | ||
echo "INFO: User: ${JBOSS_SYSTEMD_USER}" | ||
echo "INFO: Group: ${JBOSS_SYSTEMD_GROUP}" | ||
echo "INFO: Confirm: ${JBOSS_CONFIRM}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to me, I removed it.
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Show resolved
Hide resolved
...ature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service.template
Show resolved
Hide resolved
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service
Show resolved
Hide resolved
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service.template
Show resolved
Hide resolved
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Outdated
Show resolved
Hide resolved
esac | ||
done | ||
|
||
if [ "${JBOSS_SYSTEMD_MODE}" != "standalone" ] && [ "${JBOSS_SYSTEMD_MODE}" != "domain" ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right :) ... but it is a trivial change and your argument is valid, so done
if [ 'y' != "${JBOSS_CONFIRM}" ] && [ 'Y' != "${JBOSS_CONFIRM}" ];then | ||
while true; do | ||
read -p "Do you want to generate ${SYSTEMD_FILE}? (y/n): " choice | ||
case "$choice" in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case "$choice" in to case "${choice}" in
Done
Also, as I suspect choice will not change value after the script starts
choice
will get a new value on each loop inside the while if the user does not write any expected answer. So I leave it untoched
This comment was marked as off-topic.
This comment was marked as off-topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I spot a potential bug, that's why I'm putting this in "Request Changes". I can run shellcheck right now, but I suspect if you do, it will confirm the issue I found. Apart from that (and a couple missing "${xxx}" around variables), I think we're good!
} | ||
|
||
SCRIPT_NAME=$(basename "${0}") | ||
SCRIPT_DIR="$(dirname "$(realpath "${0}")")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be broken:
"$(dirname "$(realpath "${0}")")"
I count 2 opening " and 3 closing one. Also, with command substitution double quotes can be omitted (by spec the $() does the same job as ""). So this should be enough, as the most important "" are around the variable replacement, to ensure the result is treated as one arguments (so a whitespace in the file path does NOT break the script):
$(dirname $(realpath "${0}"))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Outdated
Show resolved
Hide resolved
JBOSS_SYSTEMD_GROUP="wildfly" | ||
JBOSS_CONFIRM="n" | ||
|
||
while [ "$#" -gt 0 ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not saw it on the first pass, but all the variables in the args treatement are "$XXX" instead of "${XXX}". For consistency purpose and respecting best practices, it might be better to fix that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Show resolved
Hide resolved
core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh
Show resolved
Hide resolved
@@ -0,0 +1,26 @@ | |||
#!/bin/sh | |||
if [ "$WILDFLY_SYSTEMD_DEBUG" == "true" ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another missing "${" that I failed to spot the first time :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service.template
Show resolved
Hide resolved
core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service
Show resolved
Hide resolved
...ature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service.template
Show resolved
Hide resolved
Jira issue: https://issues.redhat.com/browse/WFCORE-6935 [WFCORE-6935] Minor fix for README [WFCORE-6935] Correct naming for systemd, unit, file [WFCORE-6935] Review launch.sh, add the ability to confirm the changes and allow specify areguments in any order [WFCORE-6935] Apply changes after review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rpelisse I've followed your latest review, Could you check again and approve if you are fine? thansk!
} | ||
|
||
SCRIPT_NAME=$(basename "${0}") | ||
SCRIPT_DIR="$(dirname "$(realpath "${0}")")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
JBOSS_SYSTEMD_GROUP="wildfly" | ||
JBOSS_CONFIRM="n" | ||
|
||
while [ "$#" -gt 0 ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -0,0 +1,26 @@ | |||
#!/bin/sh | |||
if [ "$WILDFLY_SYSTEMD_DEBUG" == "true" ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Jira issue: https://issues.redhat.com/browse/WFCORE-6935