def fix_ip_addresses_in_openstack():
    # openstack is shit; it wrote the IP address we got from the DHCP when installing it in a ton of configuration files
    # now that the IP address has changed, nothing is working anymore
    # so we need to find the new IP address, search-and-f*****g-replace it in all the files
    # restart openatack and pray it will work
    with open(RC_FILE) as fd:
        environment_text = fd.read()

    auth_url = scripts.parse_environment(environment_text)[-1]
    old_ip_address = urlparse(auth_url).netloc.split(":")[0]
    new_ip_address = gethostbyname(gethostname())

    execute_assert_success(["openstack-service", "stop"])
    execute_assert_success(["rm", "-rf", "/var/log/*/*"])
    regex = "s/{}/{}/g".format(old_ip_address.replace(".", "\."), new_ip_address)

    with open(RC_FILE, "w") as fd:
        fd.write(environment_text.replace(old_ip_address, new_ip_address))
    execute_assert_success("grep -rl {} /etc | xargs sed -ie {}".format(old_ip_address, regex), shell=True)
    fix_ip_addresses_in_openstack_keystone_database(regex)

    execute(["pkill", "-9", "keystone"])
    with logs_context(KEYSTONE_LOGDIR):
        pid = execute(["openstack-service", "start"])
        returncode = pid.get_returncode()
        if returncode is not None and returncode != 0:
            if "journalctl" in pid.get_stderr():
                logger.debug("output of journalctl -xn follows")
                logger.debug(execute(["journalctl", "-xn"]).get_stdout())
            raise ExecutionError(pid)
Exemple #2
0
 def _remove_persistent_target(self, target):
     self._create_initiator_obj_if_needed()
     for endpoint in target.get_endpoints():
         args = ['iscsicli', 'RemovePersistentTarget', str(self._initiator.get_initiator_name()), str(target.get_iqn()),
                                 '*', str(endpoint.get_ip_address()), str(endpoint.get_port())]
         logger.info("running {}".format(args))
         execute(args)
Exemple #3
0
 def _rescan():
     ''' From what I saw on 90% of the times the volume just apear on both nodes
     If it doesn't we'll rescan
     '''
     HPT_BIN_FILE = 'infinihost.exe'
     # to do need to think if we'd like to scan on remote and verify
     hpt_bin = path.realpath(path.join(PROJECTROOT, pardir, 'Host Power Tools', 'bin', HPT_BIN_FILE))
     execute([hpt_bin, 'rescan'])
Exemple #4
0
 def test_07_share_delete(self):
     for share in share_names + [limited_share]:
         cmd = [
             'smbmgr', 'share', 'delete', '--name={}'.format(share), '--yes'
         ]
         result = execute(cmd).get_stdout()
         self.assertIn(outputs.share_deleted.format(share), result)
     cmd = ['smbmgr', 'fs', 'delete', '--name=fs_test_for_shares', '--yes']
     execute(cmd)
 def tempfile_context(self, filesystem="ext3"):
     from tempfile import mkstemp
     fd, name = mkstemp()
     from os import write, close
     ZERO_MB = '\x00' * 1024 * 1024
     write(fd, ZERO_MB * 10)
     close(fd)
     execute("mkfs.ext3 -F {}".format(name).split())
     yield name
Exemple #6
0
 def _reomve_target_portal(self, target):
     for endpoint in target.get_endpoints():
         args = [
             'iscsicli', 'RemoveTargetPortal',
             str(endpoint.get_ip_address()),
             str(endpoint.get_port())
         ]
         logger.info("running {}".format(args))
         execute(args)
 def test_sync_with_another_greenlet_running(self):
     import gevent
     event = gevent.event.Event()
     def func():
         event.set()
     greenlet = gevent.spawn(func)
     execute("sleep 1", shell=True)
     self.assertTrue(event.is_set())
     greenlet.join()
 def tempfile_context(self, filesystem="ext3"):
     from tempfile import mkstemp
     fd, name = mkstemp()
     from os import write, close
     ZERO_MB = '\x00' * 1024 * 1024
     write(fd, ZERO_MB * 10)
     close(fd)
     execute("mkfs.ext3 -F {}".format(name).split())
     yield name
Exemple #9
0
 def test_fs_query(self):
     cmd = ['smbmgr', 'fs', 'query']
     result = execute(cmd)
     self.assertIn(outputs.fs_query_header, result.get_stdout())
     ps_cmd._perform_cluster_failover()
     result = execute(cmd)
     result_out = result.get_stdout()
     self.assertNotIn(outputs.fs_query_header, result_out)
     self.assertIn(outputs.not_active_node, result_out)
     ps_cmd._perform_cluster_failover()
Exemple #10
0
 def is_installed(self):
     ''' Return True if iSCSI initiator sw is installed otherwise return False
     '''
     if 'solaris' in get_platform_string():
         process1 = execute('pkginfo', '-q', 'SUNWiscsir')
         process2 = execute('pkginfo', '-q', 'SUNWiscsiu')
         if process1.get_returncode() == process2.get_returncode() == 0:
             return True
         else:
             return False
Exemple #11
0
 def is_installed(self):
     ''' Return True if iSCSI initiator sw is installed otherwise return False
     '''
     if 'solaris' in get_platform_string():
         process1 = execute('pkginfo', '-q', 'SUNWiscsir')
         process2 = execute('pkginfo', '-q', 'SUNWiscsiu')
         if process1.get_returncode() == process2.get_returncode() == 0:
             return True
         else:
             return False
Exemple #12
0
    def test_sync_with_another_greenlet_running(self):
        import gevent
        event = gevent.event.Event()

        def func():
            event.set()

        greenlet = gevent.spawn(func)
        execute("sleep 1", shell=True)
        self.assertTrue(event.is_set())
        greenlet.join()
 def rescan_method(self):
     res = execute_command("cfgadm -lao show_SCSI_LUN".split(), check_returncode=False)
     if res.get_returncode() not in (0, 2):
         raise ExecutionError(res)
     execute_assert_success("devfsadm -vC".split())
     execute_assert_success("devfsadm -r / -p /etc/path_to_inst".split())
     try:
         execute("vxdctl enable".split()) # TODO execute only if veritas is installed
     except OSError:
         pass
     return 0
Exemple #14
0
 def _remove_persistent_target(self, target):
     self._create_initiator_obj_if_needed()
     for endpoint in target.get_endpoints():
         args = [
             'iscsicli', 'RemovePersistentTarget',
             str(self._initiator.get_initiator_name()),
             str(target.get_iqn()), '*',
             str(endpoint.get_ip_address()),
             str(endpoint.get_port())
         ]
         logger.info("running {}".format(args))
         execute(args)
 def rescan_method(self):
     res = execute_command("cfgadm -lao show_SCSI_LUN".split(),
                           check_returncode=False)
     if res.get_returncode() not in (0, 2):
         raise ExecutionError(res)
     execute_assert_success("devfsadm -vC".split())
     execute_assert_success("devfsadm -r / -p /etc/path_to_inst".split())
     try:
         execute("vxdctl enable".split()
                 )  # TODO execute only if veritas is installed
     except OSError:
         pass
     return 0
Exemple #16
0
 def _execute_n_log(self, cmd, log_prefix='running: ', log_level='debug'):
     try:
         getattr(logger, str(log_level))(
             log_prefix + (cmd if isinstance(cmd, six.string_types) else ' '.join(cmd)))
     except AttributeError as e:
         logger.error("logger.{} doesn't exist, {!r}".format(log_level, e))
     return execute(cmd)
Exemple #17
0
 def _execute_mkfs(self, filesystem_name, partition_access_path):
     from infi.execute import execute
     log.info("executing mkfs.{} for {}".format(filesystem_name, partition_access_path))
     mkfs = execute(["mkfs.{}".format(filesystem_name), "-F", partition_access_path])
     if mkfs.get_returncode() != 0:
         raise RuntimeError(mkfs.get_stderr())
     log.info("filesystem formatted")
Exemple #18
0
 def _execute_n_log(self, cmd, log_prefix='running: ', log_level='debug'):
     try:
         getattr(logger, str(log_level))(log_prefix + (
             cmd if isinstance(cmd, basestring) else ' '.join(cmd)))
     except AttributeError as e:
         logger.error("logger.{} doesn't exist, {!r}".format(log_level, e))
     return execute(cmd)
Exemple #19
0
def execute_assert_success(args, env=None):
    from infi import execute
    logger.info("Executing {}".format(' '.join(args)))
    result = execute.execute(args, env=env)
    if result.get_returncode() is not None and result.get_returncode() != 0:
        logger.error(result.get_stderr())
        raise PrettyExecutionError(result)
Exemple #20
0
def execute_assert_success(args, env=None):
    from infi import execute
    logger.info("Executing {}".format(' '.join(args)))
    result = execute.execute(args, env=env)
    if result.get_returncode() is not None and result.get_returncode() != 0:
        logger.error(result.get_stderr())
        raise PrettyExecutionError(result)
Exemple #21
0
def execute_parted(args):
    """This function calls the parted utility and returns its machine parsable output, without user intervention
    :returns: if the call returned success, parted's standard output is returned.
    If the call returned an error, an :class:`PartedException` is raised with the return code and the error mesage"""
    from infi.execute import execute
    commandline_arguments = ["parted", ]
    commandline_arguments.extend(PARTED_REQUIRED_ARGUMENTS)
    commandline_arguments.extend(args)
    log.debug("executing {}".format(" ".join([repr(item) for item in commandline_arguments])))
    try:
        parted = execute(commandline_arguments)
    except OSError:
        raise PartedNotInstalledException()
    parted.wait()
    if parted.get_returncode() != 0:
        log.debug("parted returned non-zero exit code: {}, stderr and stdout to follow".format(parted.get_returncode()))
        log.debug(parted.get_stderr())
        log.debug(parted.get_stdout())
        if "device-mapper: create ioctl" in parted.get_stderr():
            # this happens sometimes on redhat-7
            # at first we added a retry, but the repeating execution printed:
            # You requested a partition from 65536B to 999934464B (sectors 128..1952997).
            # The closest location we can manage is 65024B to 65024B (sectors 127..127).
            # meaning the first execution suceeded to create the partition
            # so now we're just ignore the return code in case we see this message
            return parted.get_stdout()
        if "WARNING" in parted.get_stdout():
            # don't know what's the error code in this case, and failed to re-create it
            return parted.get_stdout()
        if "aligned for best performance" in parted.get_stdout():
            # HIP-330 we something get. according to parted's source, this is a warning
            return parted.get_stdout()
        raise PartedRuntimeError(parted.get_returncode(),
                                 _get_parted_error_message_from_stderr(parted.get_stdout()))
    return parted.get_stdout()
Exemple #22
0
 def tearDownClass(cls):
     log(logger, "Starting Teardown")
     sdk = InfiSdkObjects()
     for fs in fs_names + ['fs_test_for_shares']:
         try:
             cmd = [
                 'smbmgr', 'fs', 'delete', '--name={}'.format(fs), '--yes'
             ]
             execute(cmd)
         except:
             pass
     try:
         for fs in fs_names + ['fs_test_for_shares']:
             vol = sdk.volumes.choose(name=fs)
             vol.delete
     except:
         pass
Exemple #23
0
 def analyze_server_core_according_to_dism(self):
     # http://stackoverflow.com/questions/13065479/how-to-detect-windows-2012-core-edition-c
     from os import environ, path
     from infi.execute import execute
     dism = path.join(environ.get("SYSTEMROOT"), "System32", "dism.exe")
     pid = execute([dism, "/online", "/get-features", "/format:table"])
     self.server_core = any("ServerCore-FullServer" in line and "Disabled" in line for
                            line in pid.get_stdout().splitlines())
Exemple #24
0
 def analyze_server_core_according_to_dism(self):
     # http://stackoverflow.com/questions/13065479/how-to-detect-windows-2012-core-edition-c
     from os import environ, path
     from infi.execute import execute
     dism = path.join(environ.get("SYSTEMROOT"), "System32", "dism.exe")
     pid = execute([dism, "/online", "/get-features", "/format:table"])
     self.server_core = any(
         "ServerCore-FullServer" in line and "Disabled" in line
         for line in pid.get_stdout().splitlines())
Exemple #25
0
 def _e2label(self, block_access_path, new_label=None):
     from infi.execute import execute
     args = ["e2label", block_access_path]
     if new_label is not None:
         args.append(new_label)
     pid = execute(args)
     if pid.get_returncode() != 0:
         raise LabelNotSupported()
     return pid.get_stdout().strip()
def upload_package_to_local_pypi(distribution_format, index_server):
    from infi.execute import execute
    command = ['python', 'setup.py', 'register', '-r', index_server,
                          distribution_format, 'upload', '-r', index_server]
    logger.info("Executing {}".format(' '.join(command)))
    subprocess = execute(command)
    logger.info(subprocess.get_stdout())
    logger.info(subprocess.get_stderr())
    assert subprocess.get_returncode() == 0
Exemple #27
0
def is_disk_in_cluster(disk_win_id):
    is_disk_in_cluster_script = path.realpath(path.join(PROJECTROOT, 'src', 'smb', 'cli', 'powershell',
                                                        'DiskToClusterDiskResource.ps1'))
    output = execute(['powershell', '-c', '$Disk =' 'Get-Disk', '-Number', str(disk_win_id), ';',
                      '.', pad_text(is_disk_in_cluster_script), '-Disk', '$Disk'])
    if 'MSCluster' in output.get_stdout():
        return True
    else:
        return False
Exemple #28
0
 def test_04_share_create(self):
     cmd = [
         'smbmgr', 'fs', 'create', '--name=fs_test_for_shares', '--size=1GB'
     ]
     result = execute(cmd)
     for share in share_names:
         cmd = [
             'smbmgr', 'share', 'create', '--name={}'.format(share),
             '--path=g:\\fs_test_for_shares\\{}'.format(share), '--mkdir'
         ]
         result = execute(cmd).get_stdout()
         self.assertIn(outputs.share_created.format(share), result)
     cmd = [
         'smbmgr', 'share', 'create', '--name={}'.format(limited_share),
         '--path=g:\\fs_test_for_shares\\{}'.format(limited_share),
         '--size=100MB', '--mkdir'
     ]
     result = execute(cmd).get_stdout()
     self.assertIn(outputs.share_created.format(limited_share), result)
Exemple #29
0
def run_as(argv=argv[1:]):
    from infi.execute import execute
    username, password, args = argv[0], argv[1], argv[2:]
    with subprocess_runas_context(username, password):
        pid = execute(args)
    stdout.write(pid.get_stdout())
    stdout.flush()
    stderr.write(pid.get_stderr())
    stderr.flush()
    return pid.get_returncode()
Exemple #30
0
def _get_parted_version():
    from infi.execute import execute
    parted = execute(["parted", "--version", ])
    parted.wait()
    # stdout possibilities
    # GNU Parted 1.8.1
    # or
    # parted (GNU parted) 2.1
    # Copyright ..
    return parted.get_stdout().splitlines()[0].split()[-1]
Exemple #31
0
def run(cmd, error_prefix):
    result = execute(cmd)
    if result.get_returncode() == 0:
        return result.get_stdout()
    if "You do not have administrative privileges on the cluster" in result.get_stderr(
    ):
        log_n_raise(logger,
                    "{} Cluster Permissions issue".format(error_prefix))
    log_n_raise(logger,
                "{} {}".format(error_prefix, result.get_stderr()),
                disable_print=True)
def execute_command(cmd, check_returncode=True): # pragma: no cover
    from infi.execute import execute
    logger.info("executing {}".format(cmd))
    process = execute(cmd)
    process.wait()
    logger.info("execution returned {}".format(process.get_returncode()))
    logger.debug("stdout: {}".format(process.get_stdout()))
    logger.debug("stderr: {}".format(process.get_stderr()))
    if check_returncode and process.get_returncode() != 0:
        raise RuntimeError("execution of {} failed. see log file for more details".format(cmd))
    return process
Exemple #33
0
    def _execute_mkfs(self, filesystem_name, partition_access_path, **extended_options):
        from infi.execute import execute

        args = ["mkfs.{}".format(filesystem_name), partition_access_path]
        args.extend(self._str_extended_options(extended_options))
        log.info("executing {}".format(' '.join(args)))
        mkfs = execute(args)
        if mkfs.get_returncode() != 0:
            log.debug("mkfs failed ({}): {} {}".format(mkfs.get_returncode(), mkfs.get_stdout(), mkfs.get_stderr()))
            raise RuntimeError(mkfs.get_stderr())
        log.info("filesystem formatted")
Exemple #34
0
def _run_get_winid_by_serial(luid):
    '''logical unit id (luid) is also the infinibox volume serial
    '''
    cmd_output = execute([
        'powershell', '-c', 'Get-Disk', '-SerialNumber',
        str(luid), '|', 'Select-Object -ExpandProperty number'
    ]).get_stdout()
    try:
        return int(cmd_output)
    except ValueError:
        return
Exemple #35
0
def execute_command(cmd, check_returncode=True, timeout=WAIT_TIME):  # pragma: no cover
    from infi.execute import execute, ExecutionError
    logger.info("executing {}".format(cmd))
    process = execute(cmd)
    process.wait(WAIT_TIME)
    logger.info("execution of cmd {} (pid {}) returned {}".format(cmd, process.get_pid(), process.get_returncode()))
    logger.debug("stdout: {}".format(process.get_stdout()))
    logger.debug("stderr: {}".format(process.get_stderr()))
    if check_returncode and process.get_returncode() != 0:
        formatted_cmd = cmd if isinstance(cmd, six.string_types) else repr(' '.join(cmd))
        raise ExecutionError(process)
    return process
Exemple #36
0
def is_iscsiadm_installed():
    from infi.os_info import get_platform_string
    if 'centos' in get_platform_string() or 'redhat' in get_platform_string():
        logger.debug("checking if iSCSI sw is installed")
        process = execute(['/bin/rpm', '-q', '--quiet', 'iscsi-initiator-utils'])
        if process.get_returncode() != 0:
            logger.debug("iscsi sw isn't installed")
            return False
        else:
            logger.debug("iscsi sw installed")
            return True
    return False
Exemple #37
0
 def test_06_share_resize(self):
     for share in share_names:
         cmd = [
             'smbmgr', 'share', 'resize', '--name={}'.format(share),
             '--size={}'.format(self._get_random_size()), '--yes'
         ]
         result = execute(cmd).get_stdout()
         if (outputs.bad_share_resize in result) or (outputs.share_limited
                                                     in result):
             pass
         else:
             self.assertTrue(False)  # Test Failure
Exemple #38
0
 def _execute_mkfs(self, filesystem_name, partition_access_path):
     from infi.execute import execute
     log.info("executing mkfs.{} for {}".format(filesystem_name,
                                                partition_access_path))
     mkfs = execute(
         ["mkfs.{}".format(filesystem_name), "-F", partition_access_path])
     if mkfs.get_returncode() != 0:
         log.debug("mkfs failed ({}): {} {}".format(mkfs.get_returncode(),
                                                    mkfs.get_stdout(),
                                                    mkfs.get_stderr()))
         raise RuntimeError(mkfs.get_stderr())
     log.info("filesystem formatted")
Exemple #39
0
 def _time_vbscript(self, subtree, read_all):
     assert exists(WALK_VBS)
     start = clock()
     vbs = execute(['cscript', WALK_VBS] + ['2000', '1' if subtree else '0',
                                            '1' if read_all else '0'])
     end = clock()
     print(vbs.get_stdout())
     print(vbs.get_stderr())
     self.assertEqual(vbs.get_returncode(), 0)
     output = vbs.get_stderr() + vbs.get_stdout()
     self.assertFalse(WALK_VBS in output, 'cscript returned an error')
     return end - start
Exemple #40
0
 def _executeGitCommand(self, command, cwd=None):
     if cwd is None:
         cwd = self._getWorkingDirectory()
     command = str(command)
     self._logGitCommand(command, cwd)
     returned = execute(command, shell=True, cwd=cwd)
     returned.wait()
     # API compatability
     returned.returncode = returned.get_returncode()
     returned.stdout = StringIO(returned.get_stdout().decode("UTF-8"))
     returned.stderr = StringIO(returned.get_stderr().decode("UTF-8"))
     return returned
Exemple #41
0
def is_iscsiadm_installed():
    from infi.os_info import get_platform_string
    if 'centos' in get_platform_string() or 'redhat' in get_platform_string():
        logger.debug("checking if iSCSI sw is installed")
        process = execute(
            ['/bin/rpm', '-q', '--quiet', 'iscsi-initiator-utils'])
        if process.get_returncode() != 0:
            logger.debug("iscsi sw isn't installed")
            return False
        else:
            logger.debug("iscsi sw installed")
            return True
    return False
Exemple #42
0
 def execute(cls, commandline_arguments, check_return_code=True):
     """ executes mpclaim.exe with the command-line arguments.
     if mpclaim's return value is non-zero, a RuntimeError exception is raised with stderr
     """
     from infi.execute import execute
     from logging import debug
     arguments = [cls.path()]
     arguments.extend(commandline_arguments)
     process = execute(arguments)
     process.wait()
     if process.get_returncode() != 0 and check_return_code:
         raise RuntimeError(arguments, process.get_returncode(), process.get_stdout(), process.get_stderr())
     return process.get_stdout()
Exemple #43
0
 def test__sync_execute_with_huge_output(self):
     part_a = 'legen'
     part_b = 'wait for it'
     part_c = 'dary'
     number = 65536
     command = ["-c", "from sys import stdout; from time import sleep; stdout.write('%s'*%s);stdout.flush(); sleep(2); stdout.write('%s'*%s);stdout.flush(); sleep(2); stdout.write('%s'*%s); stdout.flush(); sleep(2)" % \
                     (part_a, number, part_b, number, part_c, number)]
     command.insert(0,sys.executable)
     num_secs = 6
     result = execute(command)
     self.assertIn(part_a * number, result.get_stdout())
     self.assertIn(part_b * number, result.get_stdout())
     self.assertIn(part_c * number, result.get_stdout())
 def test__sync_execute_with_huge_output_one_shot(self):
     part_a = 'legen'
     part_b = 'wait for it'
     part_c = 'dary'
     number = 65536
     command = ["-c", "from sys import stdout; from time import sleep; stdout.write('%s'*%s); stdout.write('%s'*%s); stdout.write('%s'*%s);" % \
                     (part_a, number, part_b, number, part_c, number)]
     command.insert(0,sys.executable)
     result = execute(command)
     self.assertEquals(len(result.get_stdout()), len(part_a + part_b + part_c) * number)
     self.assertIn(part_a * number, result.get_stdout())
     self.assertIn(part_b * number, result.get_stdout())
     self.assertIn(part_c * number, result.get_stdout())
 def format(self, block_device, *args, **kwargs):
     """currently we ignore args and kwargs"""
     from .partition import LinuxPartition
     if isinstance(block_device, LinuxPartition):
         disk = block_device._containing_disk
         partition = block_device._parted_partition
         number = partition.get_number()
         disk._format_partition(number, self.get_name())
     else:
         from infi.execute import execute
         mkfs = execute(["mkfs.{}", "-F", block_device.get_block_access_path()])
         if mkfs.get_returncode() != 0:
             raise RuntimeError(mkfs.get_stderr())
Exemple #46
0
def _get_parted_version():
    from infi.execute import execute
    try:
        parted = execute(["parted", "--version", ])
    except OSError:
        raise PartedNotInstalledException()
    parted.wait()
    # stdout possibilities
    # GNU Parted 1.8.1
    # or
    # parted (GNU parted) 2.1
    # Copyright ..
    return parted.get_stdout().splitlines()[0].split()[-1].decode("ascii")
def execute_command(cmd, check_returncode=True, timeout=WAIT_TIME): # pragma: no cover
    from infi.execute import execute
    from os import environ
    logger.info("executing {}".format(cmd))
    env = environ.copy()
    env.pop('PYTHONPATH', 1)
    process = execute(cmd, env=env)
    process.wait(WAIT_TIME)
    logger.info("execution returned {}".format(process.get_returncode()))
    logger.debug("stdout: {}".format(process.get_stdout()))
    logger.debug("stderr: {}".format(process.get_stderr()))
    if check_returncode and process.get_returncode() != 0:
        raise RuntimeError("execution of {} failed. see log file for more details".format(cmd))
    return process
Exemple #48
0
def _winid_serial_table_to_dict():
    import re
    disk_list = []
    cmd_output = execute([
        'powershell', '-c', 'Get-Disk', '|', 'Select-Object',
        'Number,SerialNumber'
    ]).get_stdout()
    cmd_output = cmd_output.replace("Number SerialNumber", "").replace("-", "")
    regex = re.compile(r'(?P<winid>\d+)\ (?P<serial>\w+)')
    for line in cmd_output.splitlines():
        result = re.search(regex, line)
        if result:
            disk_list.append(result.groupdict())
    log(logger, "winid serial dict: {}".format(disk_list))
    return disk_list
Exemple #49
0
def execute_command(cmd,
                    check_returncode=True,
                    timeout=WAIT_TIME):  # pragma: no cover
    from infi.execute import execute, ExecutionError
    logger.info("executing {}".format(cmd))
    process = execute(cmd)
    process.wait(WAIT_TIME)
    logger.info("execution of cmd {} (pid {}) returned {}".format(
        cmd, process.get_pid(), process.get_returncode()))
    logger.debug("stdout: {}".format(process.get_stdout()))
    logger.debug("stderr: {}".format(process.get_stderr()))
    if check_returncode and process.get_returncode() != 0:
        formatted_cmd = cmd if isinstance(cmd, six.string_types) else repr(
            ' '.join(cmd))
        raise ExecutionError(process)
    return process
Exemple #50
0
def _get_parted_version():
    from infi.execute import execute
    try:
        parted = execute([
            "parted",
            "--version",
        ])
    except OSError:
        raise PartedNotInstalledException()
    parted.wait()
    # stdout possibilities
    # GNU Parted 1.8.1
    # or
    # parted (GNU parted) 2.1
    # Copyright ..
    return parted.get_stdout().splitlines()[0].split()[-1]
Exemple #51
0
    def force_kernel_to_re_read_partition_table(self):
        from infi.execute import execute
        from os import path

        log.info("executing: partprobe {}".format(self._device_access_path))
        execute(["partprobe", format(self._device_access_path)])

        log.info("executing: multipath -f {}".format(path.basename(self._device_access_path)))
        execute(["multipath", '-f', format(path.basename(self._device_access_path))])

        log.info("executing: multipath")
        execute(["multipath"])
Exemple #52
0
 def test__sync_execute_with_huge_output_one_shot(self):
     part_a = b'legen'
     part_b = b'wait for it'
     part_c = b'dary'
     number = 65536
     if PY3:
         command = ["-c", "from sys import stdout; from time import sleep; stdout.buffer.write(b'%s'*%s); stdout.buffer.write(b'%s'*%s); stdout.buffer.write(b'%s'*%s);" % \
                    (part_a.decode("ASCII"), number, part_b.decode("ASCII"), number, part_c.decode("ASCII"), number)]
     else:
         command = ["-c", "from sys import stdout; from time import sleep; stdout.write('%s'*%s); stdout.write('%s'*%s); stdout.write('%s'*%s);" % \
                    (part_a, number, part_b, number, part_c, number)]
     command.insert(0, sys.executable)
     result = execute(command)
     self.assertEquals(len(result.get_stdout()),
                       len(part_a + part_b + part_c) * number)
     self.assertIn(part_a * number, result.get_stdout())
     self.assertIn(part_b * number, result.get_stdout())
     self.assertIn(part_c * number, result.get_stdout())
    def test__sync_execute_with_huge_output(self):
        part_a = b'legen'
        part_b = b'wait for it'
        part_c = b'dary'
        number = 65536
        if PY3:
            command = ["-c", "from sys import stdout; from time import sleep; stdout.buffer.write(b'%s'*%s);stdout.flush(); sleep(2); stdout.buffer.write(b'%s'*%s);stdout.flush(); sleep(2); stdout.buffer.write(b'%s'*%s); stdout.flush(); sleep(2)" % \
                       (part_a.decode("ASCII"), number, part_b.decode("ASCII"), number, part_c.decode("ASCII"), number)]
        else:
            command = ["-c", "from sys import stdout; from time import sleep; stdout.write('%s'*%s);stdout.flush(); sleep(2); stdout.write('%s'*%s);stdout.flush(); sleep(2); stdout.write('%s'*%s); stdout.flush(); sleep(2)" % \
                       (part_a, number, part_b, number, part_c, number)]

        command.insert(0,sys.executable)
        result = execute(command)
        self.assertEquals(len(result.get_stdout()), len(part_a + part_b + part_c) * number)
        self.assertIn(part_a * number, result.get_stdout())
        self.assertIn(part_b * number, result.get_stdout())
        self.assertIn(part_c * number, result.get_stdout())
Exemple #54
0
def execute_command(cmd,
                    check_returncode=True,
                    timeout=WAIT_TIME):  # pragma: no cover
    from infi.execute import execute
    from os import environ
    logger.info("executing {}".format(cmd))
    env = environ.copy()
    env.pop('PYTHONPATH', 1)
    process = execute(cmd, env=env)
    process.wait(WAIT_TIME)
    logger.info("execution returned {}".format(process.get_returncode()))
    logger.debug("stdout: {}".format(process.get_stdout()))
    logger.debug("stderr: {}".format(process.get_stderr()))
    if check_returncode and process.get_returncode() != 0:
        raise RuntimeError(
            "execution of {} failed. see log file for more details".format(
                cmd))
    return process
Exemple #55
0
def execute_parted(args):
    """This function calls the parted utility and returns its machine parsable output, without user intervention
    :returns: if the call returned success, parted's standard output is returned.
    If the call returned an error, an :class:`PartedException` is raised with the return code and the error mesage"""
    from infi.execute import execute
    commandline_arguments = [
        "parted",
    ]
    commandline_arguments.extend(PARTED_REQUIRED_ARGUMENTS)
    commandline_arguments.extend(args)
    log.debug("executing {}".format(" ".join(
        [repr(item) for item in commandline_arguments])))
    try:
        parted = execute(commandline_arguments)
    except OSError:
        raise PartedNotInstalledException()
    parted.wait()
    if parted.get_returncode() != 0:
        log.debug(
            "parted returned non-zero exit code: {}, stderr and stdout to follow"
            .format(parted.get_returncode()))
        log.debug(parted.get_stderr())
        log.debug(parted.get_stdout())
        if "device-mapper: create ioctl" in parted.get_stderr():
            # this happens sometimes on redhat-7
            # at first we added a retry, but the repeating execution printed:
            # You requested a partition from 65536B to 999934464B (sectors 128..1952997).
            # The closest location we can manage is 65024B to 65024B (sectors 127..127).
            # meaning the first execution suceeded to create the partition
            # so now we're just ignore the return code in case we see this message
            return parted.get_stdout()
        if "WARNING" in parted.get_stdout():
            # don't know what's the error code in this case, and failed to re-create it
            return parted.get_stdout()
        if "aligned for best performance" in parted.get_stdout():
            # HIP-330 we something get. according to parted's source, this is a warning
            return parted.get_stdout()
        raise PartedRuntimeError(
            parted.get_returncode(),
            _get_parted_error_message_from_stderr(parted.get_stdout()))
    return parted.get_stdout()
Exemple #56
0
def execute_parted(args):
    """This function calls the parted utility and returns its machine parsable output, without user intervention
    :returns: if the call returned success, parted's standard output is returned.
    If the call returned an error, an :class:`PartedException` is raised with the return code and the error mesage"""
    from infi.execute import execute
    commandline_arguments = ["parted", ]
    commandline_arguments.extend(PARTED_REQUIRED_ARGUMENTS)
    commandline_arguments.extend(args)
    log.debug("executing {}".format(" ".join([repr(item) for item in commandline_arguments])))
    parted = execute(commandline_arguments)
    parted.wait()
    if parted.get_returncode() != 0:
        log.debug("parted returned non-zero exit code: {}".format(parted.get_returncode()))
        if "WARNING" in parted.get_stdout():
            # don't know what's the error code in this case, and failed to re-create it
            return parted.get_stdout()
        if "aligned for best performance" in parted.get_stdout():
            # HIP-330 we something get. according to parted's source, this is a warning
            return parted.get_stdout()
        raise PartedRuntimeError(parted.get_returncode(),
                                 _get_parted_error_message_from_stderr(parted.get_stdout()))
    return parted.get_stdout()
Exemple #57
0
 def test__sync_execute_stdin_through_string(self):
     result = execute("cat | cat", stdin=b"hello", shell=True)
     self.assertEquals(result.get_stdout(), b"hello")
Exemple #58
0
 def is_product_installed(self):
     return 0 == execute(["pkginfo", self.package_name]).get_returncode()
Exemple #59
0
 def force_kernel_to_re_read_partition_table(self):
     from infi.execute import execute
     execute(["partprobe", format(self._disk_block_access_path)]).wait()