def run_script(params, shell=False): """Run the script with the given params. :param list params: List of parameters to pass to Popen """ try: proc = subprocess.Popen(params, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except (OSError, ValueError): msg = "Unable to run the command: %s" % " ".join(params) logger.error(msg) raise errors.SubprocessError(msg) stdout, stderr = proc.communicate() if proc.returncode != 0: msg = "Error while running %s.\n%s\n%s" % ( " ".join(params), stdout, stderr) # Enter recovery routine... logger.error(msg) raise errors.SubprocessError(msg) return stdout, stderr
def dd(count, skip=0, seek=0, infile='/dev/zero', outfile='/dev/null', bs=512): p = ddPopen(count, skip, seek, infile, outfile, bs) #shell truth (0=success) is inverted from normal (1=True) r = p.wait() if (r): raise errors.SubprocessError('dd', r) return True
def partprobe(device=''): call = ['partprobe', device] r = subprocess.call(call) if (r): raise errors.SubprocessError('partprobe', r) else: return True
def rm(target, options=''): call = ['rm', options, target] r = subprocess.call(call) if (r): raise errors.SubprocessError('rm', r) else: return True
def umount(mountpoint): call = ['umount', mountpoint] r = subprocess.call(call) if (r): raise errors.SubprocessError('umount', r) else: return True
def mount(device, dest): call = ['mount', device, dest] r = subprocess.call(call) if (r): raise errors.SubprocessError('mount', r) else: return True
def call(self, executable, *args): """ Call ``executable`` with ``args`` using subprocess.call(). If the call exits with a non-zero exit status, `ipalib.errors.SubprocessError` is raised, from which you can retrieve the exit code by checking the SubprocessError.returncode attribute. This method does *not* return what ``executable`` sent to stdout... for that, use `Plugin.callread()`. """ argv = (executable, ) + args self.debug('Calling %r', argv) code = subprocess.call(argv) if code != 0: raise errors.SubprocessError(returncode=code, argv=argv)
def mkfs(device, fstype, dbg=False): opts = [] if fstype == 'ext2': fsprog = 'mkfs.ext2' elif fstype == 'ext3': fsprog = 'mkfs.ext3' elif fstype == 'ext4': fsprog = 'mkfs.ext4' elif fstype == 'fat16': fsprog = 'mkfs.vfat' opts = ['-F16'] elif fstype == 'fat32': fsprog = 'mkfs.vfat' opts = ['-F32'] call = [fsprog] call.extend(opts) call.append(device) stdout = None stderr = None null = 0 if not dbg: null = open('/dev/null', 'w') stdout = null stderr = null r = subprocess.call(call, stdout=stdout, stderr=stderr) if not dbg: null.close() if (r): raise errors.SubprocessError(fsprog, r) else: return True