moodlehq / moodle-performance-comparison

Set of shell scripts to run Moodle performance tests using different hardware and configurations and compare results.
GNU General Public License v2.0
76 stars 40 forks source link

Move to set -e everywhere #36

Closed danpoltawski closed 10 years ago

danpoltawski commented 10 years ago

Any sort of shell scripts which execute and don't stop on errors can become incredibly dangerous.

We should move to activating set -e everywhere. It will be a pain, but its always worth it. Please ensure any new files start with set -e as it will make it easier in future.

danpoltawski commented 10 years ago

e.g. When i see things like this, it gives me a heart attack ;-)

./compare.sh
rm: /srv/moodleperf/datadir/*: No such file or directory
rm: -rf: No such file or directory
ERROR:  database "moodleperformancecomparison" does not exist
ERROR:  permission denied to create database
dmonllao commented 10 years ago

If we set -e there will not be any custom-hypercool error message and users will need to know more internals to know where the problem comes from, which means that everybody that finds a small problem (most of the people as there are a few config vars) will have to debug the shell scripts. The tool was tested (and also developed in the last stages) with set -e and it should all work with set -e (not all the cases have been tested as this was not spotted) IMO we should spot all those cases during development and testing and provide nice error messages for people.

set -e can be used to debug unknown problems (https://github.com/moodlehq/moodle-performance-comparison/blob/master/README.md#troubleshooting) that rm is as dangerous as it is with or without set -e.

I would write a patch to fix that rm when $dataroot has no contents.

danpoltawski commented 10 years ago

I disagree - we handle the errors, the errors don't get through to the script, and if an error occurs that the we haven't considered, then we need to stop, because without set -e, your expectations aren't met.. maybe you expect a file to be there or not and then the script as though everything worked perfectly.

This is super dangerous IMO. Its even more confusing for the user, because half the operations might not have happened, and the script continues and they need to work out why.

(And yep about the rm, I already have a patch for that).

dmonllao commented 10 years ago

Ok, how can we provide useful feedback messages when errors occurs using set -e?

danpoltawski commented 10 years ago

You simply detect the error and handle it. Set -e just stops the script from continuing with unhanded errors, and those are the errors which are really dangerous because they are unknown!

For example:

#!/bin/bash

set -e
echo "Loading stuff from config file.."
cat configfile || echo 'Config file not found'; exit 1
echo "Continuining to do dangerous stuff based on the contents of the config file."

From the bash man page:


              -e      Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status.  The shell does not exit
                      if  the  command  that  fails is part of the command list immediately following a while or until keyword, part of the
                      test in an if statement, part of a && or || list, or if the command's return value is being inverted via !.   A  trap
                      on ERR, if set, is executed before the shell exits.
dmonllao commented 10 years ago

This is ready to integrate in the 3 branches: https://github.com/dmonllao/moodle-performance-comparison/compare/moodlehq:master...issue_36

Includes:

danpoltawski commented 10 years ago

Hi David,

Haven't reviewed properly yet, but on a quick glance, I think that it would be better for readability to split some of the lines across multiple lines with backslashes.

(I am not sure of the best style, but just as an example)

${phpcmd} admin/cli/install_database.php \
                --agree-license \ 
                --fullname="$sitefullname" \
                --shortname="$siteshortname" \
                --adminuser="$siteadminusername" \
                --adminpass="$siteadminpassword" > /dev/null \
                || throw_error "Moodle can not be installed, check that the database data is correctly set"

(Actually I read about google style guide the other day, maybe we could look at that http://google-styleguide.googlecode.com/svn/trunk/shell.xml )

dmonllao commented 10 years ago

I think this has nothing to do with this issue, but I will add a commit here

danpoltawski commented 10 years ago

Perhaps, but you are adding even longer lines (and actions) be adding the || at the end of already very long lines, I can see it becoming unmanageable.

dmonllao commented 10 years ago

I've uupdated the https://github.com/dmonllao/moodle-performance-comparison/compare/moodlehq:master...issue_36 branch including a last commit to improve a few things according to the style guide.

dmonllao commented 10 years ago

Hi Dan,

Any chance you can review this before integrating it?

danpoltawski commented 10 years ago

Hi David,

I just looked at this and it looks like a great improvement to me.

dmonllao commented 10 years ago

Thanks for the review Dan, I haven't restricted the char limit to 80, I split very long lines in logical parts (CLI command options, command + throw_error()...) I've now reviewed all .sh files and I've split a couple of long error messages in multiple lines, so all lines have less than 100 chars (approximately) I've corrected the > &2 too, thanks, merging.

dmonllao commented 10 years ago

merged in 25, 26 and master