return arguments
-def command(arguments):
+def command(arguments, **kwargs):
"""
Safely execute a ``subprocess.Popen`` call making sure that the
executable exists and raising a helpful error message
.. note:: This should be the prefered way of calling ``subprocess.Popen``
since it provides the caller with the safety net of making sure that
executables *will* be found and will error nicely otherwise.
+
+ This returns the output of the command and the return code of the
+ process in a tuple: (output, returncode).
"""
arguments = _get_command_executable(arguments)
- return subprocess.Popen(
+ process = subprocess.Popen(
arguments,
stdout=subprocess.PIPE,
- stderr=subprocess.PIPE).stdout.read()
+ **kwargs)
+ out, _ = process.communicate()
+ return out, process.returncode
def command_check_call(arguments):
return osd_id
-def _check_output(*args, **kwargs):
- process = subprocess.Popen(
- stdout=subprocess.PIPE,
- *args, **kwargs)
- out, _ = process.communicate()
- ret = process.wait()
+def _check_output(args=None, **kwargs):
+ out, ret = command(args, **kwargs)
if ret:
- cmd = kwargs.get("args")
- if cmd is None:
- cmd = args[0]
+ cmd = args[0]
error = subprocess.CalledProcessError(ret, cmd)
error.output = out
raise error
:return: The variable value or None.
"""
try:
- process = subprocess.Popen(
- args=[
+ out, ret = command(
+ [
'ceph-conf',
'--cluster={cluster}'.format(
cluster=cluster,
'--lookup',
variable,
],
- stdout=subprocess.PIPE,
close_fds=True,
)
except OSError as e:
raise Error('error executing ceph-conf', e)
- (out, _err) = process.communicate()
- ret = process.wait()
if ret == 1:
# config entry not found
return None
elif ret != 0:
raise Error('getting variable from configuration failed')
- value = str(out).split('\n', 1)[0]
+ value = out.split('\n', 1)[0]
# don't differentiate between "var=" and no var set
if not value:
return None
rawdev,
]
try:
- subprocess.check_call(args)
+ command_check_call(args)
return dev
except subprocess.CalledProcessError as e:
]
try:
- subprocess.check_call(args)
+ command_check_call(args)
except subprocess.CalledProcessError as e:
raise Error('unable to unmap device', _uuid, e)
)
try:
LOG.debug('Mounting %s on %s with options %s', dev, path, options)
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'mount',
'-t', fstype,
'-o', options,
"""
try:
LOG.debug('Unmounting %s', path)
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'/bin/umount',
'--',
path,
'--mbrtogpt',
'--',
dev,
- ]
+ ],
)
except subprocess.CalledProcessError as e:
raise Error(e)
'--mbrtogpt',
'--',
journal,
- ]
+ ],
)
# try to make sure the kernel refreshes the table. note
# that if this gets ebusy, we are probably racing with
# udev because it already updated it.. ignore failure here.
LOG.debug('Calling partprobe on prepared device %s', journal)
- subprocess.call(
- args=[
+ command(
+ [
'partprobe',
journal,
],
)
# wait for udev event queue to clear
- subprocess.call(
- args=[
+ command(
+ [
'udevadm',
'settle',
],
'--typecode=1:%s' % ptype_tobe,
'--',
data,
- ]
+ ],
)
- subprocess.call(
- args=[
+ command(
+ [
# wait for udev event queue to clear
'udevadm',
'settle',
])
try:
LOG.debug('Creating %s fs on %s', fstype, dev)
- subprocess.check_call(args=args)
+ command_check_call(args)
except subprocess.CalledProcessError as e:
raise Error(e)
'--typecode=1:%s' % ptype_osd,
'--',
data,
- ]
+ ],
)
except subprocess.CalledProcessError as e:
raise Error(e)
# that if this gets ebusy, we are probably racing with
# udev because it already updated it.. ignore failure here.
LOG.debug('Calling partprobe on prepared device %s', args.data)
- subprocess.call(
- args=[
+ command(
+ [
'partprobe',
args.data,
],
keyring,
):
monmap = os.path.join(path, 'activate.monmap')
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'ceph',
'--cluster', cluster,
'--name', 'client.bootstrap-osd',
],
)
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'ceph-osd',
'--cluster', cluster,
'--mkfs',
):
try:
# try dumpling+ cap scheme
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'ceph',
'--cluster', cluster,
'--name', 'client.bootstrap-osd',
except subprocess.CalledProcessError as err:
if err.returncode == errno.EACCES:
# try old cap scheme
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'ceph',
'--cluster', cluster,
'--name', 'client.bootstrap-osd',
# /etc/mtab, which *still* isn't a symlink to /proc/mounts despite
# this being 2013. Instead, mount the original device at the final
# location.
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'/bin/mount',
'-o',
mount_options,
osd_data,
],
)
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'/bin/umount',
'-l', # lazy, in case someone else is peeking at the
# wrong moment
# upstart?
try:
if os.path.exists(os.path.join(path,'upstart')):
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
'/sbin/initctl',
# use emit, not start, because start would fail if the
# instance was already running
svc = '/usr/sbin/service'
else:
svc = '/sbin/service'
- subprocess.check_call(
- args=[
+ command_check_call(
+ [
svc,
'ceph',
'start',
return None
def get_dev_fs(dev):
- fscheck = subprocess.Popen(
+ fscheck, _ = command(
[
'blkid',
'-s',
'TYPE',
- dev
- ],
- stdout = subprocess.PIPE,
- stderr=subprocess.PIPE).stdout.read()
+ dev,
+ ],
+ )
if 'TYPE' in fscheck:
fstype = fscheck.split()[1].split('"')[1]
return fstype
def get_partition_type(part):
(base, partnum) = re.match('(\D+)(\d+)', part).group(1, 2)
- sgdisk = command(
+ sgdisk, _ = command(
[
'sgdisk',
'-p',
def get_partition_uuid(dev):
(base, partnum) = re.match('(\D+)(\d+)', dev).group(1, 2)
- out = command(['sgdisk', '-i', partnum, base])
+ out, _ = command(['sgdisk', '-i', partnum, base])
for line in out.splitlines():
m = re.match('Partition unique GUID: (\S+)', line)
if m: