bootstrap-vz/bootstrapvz/common/tasks/host.py
Brendan Harley df3a200df3 Fix unfailing CheckExternalCommands
On Unix, with shell=True, the shell default to /bin/sh.
Using Popen(['type', command], shell=True) is equivalent to calling
Popen(['/bin/sh', '-c', 'type', command]).
In this case 'command' becomes a positional parameter to the shell,
and not an argument to the command 'type'.

The solution is to pass a single string as parameter.

The problem is that with shell=True, we are never safe from a shell injection,
so it is wiser to use a python only solution.

The package distutils is part of the standard distribution, so it doesn't add
extra dependencies.
The method find_executable has the same behaviour as 'which' on bash.
2017-05-31 22:28:04 +02:00

31 lines
1.2 KiB
Python

from bootstrapvz.base import Task
from .. import phases
from ..exceptions import TaskError
class CheckExternalCommands(Task):
description = 'Checking availability of external commands'
phase = phases.validation
@classmethod
def run(cls, info):
import re
import logging
from distutils.spawn import find_executable
missing_packages = []
log = logging.getLogger(__name__)
for command, package in info.host_dependencies.items():
log.debug('Checking availability of ' + command)
if find_executable(command) is None:
if re.match('^https?:\/\/', package):
msg = ('The command `{command}\' is not available, '
'you can download the software at `{package}\'.'
.format(command=command, package=package))
else:
msg = ('The command `{command}\' is not available, '
'it is located in the package `{package}\'.'
.format(command=command, package=package))
missing_packages.append(msg)
if len(missing_packages) > 0:
msg = '\n'.join(missing_packages)
raise TaskError(msg)