Merge lp:~brendan-donegan/checkbox/bug1066094 into lp:checkbox
Status: | Work in progress |
---|---|
Proposed branch: | lp:~brendan-donegan/checkbox/bug1066094 |
Merge into: | lp:checkbox |
Diff against target: |
171 lines (+65/-23) 2 files modified
debian/changelog (+2/-0) scripts/network_restart (+63/-23) |
To merge this branch: | bzr merge lp:~brendan-donegan/checkbox/bug1066094 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Marc Tardif (community) | Needs Fixing | ||
Review via email: mp+129880@code.launchpad.net |
Commit message
Fix bug 1066094 by dealing with each case where superuser is needed in network_restart individually
Description of the change
As pointed out in the linked bug, the network_restart script took a very brute-force approach to informing the user of the script that superuser permissions may be required for specific operations - i.e. it assumes that they are and doesn't allow the script to be run without them. This has been changed so that for each command that may require superuser permissions, if an error occurs then a check is done to see if the script is not being run as the superuser and the user of the script is informed if that's the case.
Unmerged revisions
- 1778. By Brendan Donegan
-
Tweaks to the code.
- 1777. By Brendan Donegan
-
scripts/
network_ restart - be specific about where superuser permissions
are required (LP: #1066094)
> - output = check_output( ['/sbin/ ifconfig' , '-s', '-a']) ['ifconfig' , '-s', '-a'])
> + output = check_output(
Thank you for changing the path to ifconfig and the other commands from
absolute to just their names!
> - for interface in self.interfaces:
> + for iface in self.interfaces:
What was wrong with the name "interface"?
> try: ['/sbin/ ifconfig' , interface, 'up']) ['ifconfig' , iface, 'up'])
> - check_output(
> + check_output(
> except CalledProcessError:
This will still output the stderr of ifconfig to the terminal. The
exception raised by the check_output function only has one output
attribute, so you need to pipe stderr into stdout:
check_output(
['/sbin/ ifconfig' , interface, 'up'],
stderr= subprocess. STDOUT)
> - logging. error(' Unable to bring up interface {0!r}' format( iface) error(message)
> - .format(interface))
> + message = 'Unable to bring up interface {0!r}'.
> +
> + if os.getuid():
> + message += self.SUPERUSER_MSG
> +
> + message += '.'
> +
> + print(message, file=sys.stderr)
> + logging.
> raise
How do you know that just because check_output raised a CalledProcessError
that it was because of missing superuser privileges? Here's another error
that has nothing to do with privileges:
$ ifconfig eth8 up
eth8: ERROR while getting interface flags: No such device
So, once you piped stderr into stdout, you should use the output variable
in the exception to provide a useful and accurate error message to the
user:
message = "Unable to bring up interface {0!r}:\ n".format( interface) output. decode( "utf-8" ) error(message)
message += exception.
print(message, file=sys.stderr)
logging.
raise
the same applies for the other calls that assumer that any error is because
of missing superuser privileges.
> - default='/var/log',
> + default='.',
> help='The path to the log directory. \
> Default is /var/log')
The problem here is that the default kwarg was changed but the help wasn't
changed accordingly. The solution would be to use %(default)s in the help
instead of hard coding a duplicate value.