Python sh.ErrorReturnCode() Examples

The following are 30 code examples of sh.ErrorReturnCode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sh , or try the search function .
Example #1
Source File: test_values.py    From python-template with Apache License 2.0 7 votes vote down vote up
def test_dash_in_project_slug(cookies):
    ctx = {'project_slug': "my-package"}
    project = cookies.bake(extra_context=ctx)

    assert project.exit_code == 0

    with open(os.path.join(str(project.project), 'setup.py')) as f:
        setup = f.read()
    print(setup)

    cwd = os.getcwd()
    os.chdir(str(project.project))

    try:
        sh.python(['setup.py', 'install'])
        sh.python(['setup.py', 'build_sphinx'])
    except sh.ErrorReturnCode as e:
        pytest.fail(e)
    finally:
        os.chdir(cwd) 
Example #2
Source File: test_values.py    From python-template with Apache License 2.0 7 votes vote down vote up
def test_double_quotes_in_name_and_description(cookies):
    ctx = {'project_short_description': '"double quotes"',
           'full_name': '"name"name'}
    project = cookies.bake(extra_context=ctx)

    assert project.exit_code == 0

    with open(os.path.join(str(project.project), 'setup.py')) as f:
        setup = f.read()
    print(setup)

    cwd = os.getcwd()
    os.chdir(str(project.project))

    try:
        sh.python(['setup.py', 'install'])
    except sh.ErrorReturnCode as e:
        pytest.fail(e)
    finally:
        os.chdir(cwd) 
Example #3
Source File: process.py    From flyingcloud with Apache License 2.0 7 votes vote down vote up
def run_command_using_sh(cmd, env, kwargs):
    "install the sh module in the system Python to have better debugging of CbCommon module installation (pip install sh)"
    import sh
    command_stdout = ''
    command_stderr = ''
    current_working_directory = kwargs.get('cwd')
    executable_file = cmd[0]
    command_args = cmd[1:]
    command_runner = sh.Command(executable_file)
    try:
        output = command_runner(*command_args, _cwd=current_working_directory, _env=env, _out=command_stdout)
        retcode = output.exit_code
    except sh.ErrorReturnCode as e:
        print("sh e.stderr:{}".format(e.stderr))
        retcode = 1
        command_stderr = e.stderr
        print("command STDOUT:{}".format(command_stdout))
        print("command STDOUT:{}".format(command_stderr))
    return command_stderr, command_stdout, retcode 
Example #4
Source File: git.py    From WordOps with MIT License 6 votes vote down vote up
def clone(self, repo, path, branch='master'):
        """Equivalent to git clone """
        if not os.path.exists('{0}'.format(path)):
            global git
            try:
                git.clone(
                    '{0}'.format(repo),
                    '{0}'.format(path),
                    '--branch={0}'.format(branch),
                    '--depth=1')
            except ErrorReturnCode as e:
                Log.debug(self, "{0}".format(e))
                Log.error(self, "Unable to git clone at {0} "
                          .format(path))
        else:
            Log.debug(self, "WOGit: Path {0} already exist".format(path)) 
Example #5
Source File: snapshot.py    From scyllabackup with MIT License 6 votes vote down vote up
def restore_schema(self, restore_schema_path):
        """Function to restore schema in scylladb from a cql file. This can be
        done manually also directly via cqlsh. This just abstracts the
        interface and is only expected to run on a new/clean cluster.

        :param restore_schema_path: The path of the cql file to be imported in
                                    scylladb
        :returns: Nothing
        :rtype: None

        """
        try:
            self.cqlsh.bake('-f')(restore_schema_path)
        except ErrorReturnCode as e:
            logger.error("Error while restoring schema")
            log_shell_exception_and_exit(e) 
Example #6
Source File: utils.py    From ocdeployer with MIT License 6 votes vote down vote up
def apply_template(project, template):
    try:
        oc("apply", "-f", "-", "-n", project, _in=template.dump_processed_json(), _reraise=True)
    except ErrorReturnCode as err:
        # Work-around for resourceVersion errors.
        # See https://www.timcosta.io/kubernetes-service-invalid-clusterip-or-resourceversion/
        matches = INVALID_RESOURCE_REGEX.findall(err.stderr)
        if matches:
            for restype, name in matches:
                restype = restype.rstrip("s")  # remove plural language
                if template.get_processed_item(restype, name):  # ensure we sent this item's config
                    log.warning(
                        "Removing last-applied-configuration annotation from %s/%s", restype, name
                    )
                    oc(
                        "annotate",
                        restype,
                        name,
                        "kubectl.kubernetes.io/last-applied-configuration-",
                    )
            oc("apply", "-f", "-", "-n", project, _in=template.dump_processed_json())
        else:
            abort() 
Example #7
Source File: benchmark.py    From cstar_perf with Apache License 2.0 6 votes vote down vote up
def build_stress(stress_revision, name=None):
    # Build a stress revision

    try:
        git_id = sh.git('--git-dir={home}/fab/cassandra.git'
                        .format(home=HOME), 'rev-parse', stress_revision).strip()
    except sh.ErrorReturnCode:
        raise AssertionError('Invalid stress_revision: {}'.format(stress_revision))

    path = os.path.join(CASSANDRA_STRESS_PATH, git_id)
    if not os.path.exists(path):
        logger.info("Building cassandra-stress '{}' in '{}'.".format(stress_revision, path))
        os.makedirs(path)
        sh.tar(
            sh.git("--git-dir={home}/fab/cassandra.git".format(home=HOME), "archive", git_id),
            'x', '-C', path
        )
        antcmd('-Dbasedir={}'.format(path), '-f', '{}/build.xml'.format(path),
               'realclean', 'jar', _env={"JAVA_TOOL_OPTIONS": "-Dfile.encoding=UTF8",
                                         "JAVA_HOME": JAVA_HOME})

    name = name if name else stress_revision
    return {name: git_id} 
Example #8
Source File: builder.py    From stackhut with Apache License 2.0 6 votes vote down vote up
def build_dockerfile(self, tag, dockerfile='Dockerfile'):
        log.debug("Running docker build for {}".format(tag))
        cache_flag = '--no-cache=True' if self.no_cache else '--no-cache=False'
        cmds = ['-f', dockerfile, '-t', tag, '--rm', cache_flag, '.']
        log.info("Starting build, this may take some time, please wait...")

        try:
            if utils.VERBOSE:
                self.docker.run_docker_sh('build', cmds, _out=lambda x: log.debug(x.strip()))
            else:
                self.docker.run_docker_sh('build', cmds)
        except sh.ErrorReturnCode as e:
            log.error("Couldn't complete build")
            log.error("Build error - {}".format(e.stderr.decode('utf-8').strip()))
            if not utils.VERBOSE:
                log.error("Build Traceback - \n{}".format(e.stdout.decode('utf-8').strip()))
            raise RuntimeError("Docker Build failed") from None 
Example #9
Source File: test_config.py    From ldap2pg with PostgreSQL License 6 votes vote down vote up
def test_custom_yaml():
    from sh import ErrorReturnCode, chmod, ldap2pg, rm

    LDAP2PG_CONFIG = 'my-test-ldap2pg.yml'
    rm('-f', LDAP2PG_CONFIG)
    with pytest.raises(ErrorReturnCode):
        ldap2pg(_env=dict(os.environ, LDAP2PG_CONFIG=LDAP2PG_CONFIG))

    yaml = YAML_FMT % os.environ
    with open(LDAP2PG_CONFIG, 'w') as fo:
        fo.write(yaml)

    # Purge env from value set in file. Other are reads from ldaprc.
    # Ensure world readable password is denied
    with pytest.raises(ErrorReturnCode):
        ldap2pg(config=LDAP2PG_CONFIG, _env=ldapfree_env())

    # And that fixing file mode do the trick.
    chmod('0600', LDAP2PG_CONFIG)
    ldap2pg('--config', LDAP2PG_CONFIG, _env=ldapfree_env()) 
Example #10
Source File: test_executions.py    From cloudify-manager with Apache License 2.0 6 votes vote down vote up
def test_execute_and_kill_execution(self):
        """
        Tests the kill execution option by asserting the execution pid doesn't
        exist.
        """
        dsl_path = resource('dsl/write_pid_node.yaml')
        dep = self.deploy(dsl_path, wait=False, client=self.client)
        do_retries(verify_deployment_env_created, 30, deployment_id=dep.id)
        execution = self.client.executions.start(deployment_id=dep.id,
                                                 workflow_id='install')
        pid = do_retries(self.read_manager_file, file_path='/tmp/pid.txt')
        path = '/proc/{}/status'.format(pid)
        execution = self.client.executions.cancel(execution.id,
                                                  force=True, kill=True)
        self.assertEquals(Execution.KILL_CANCELLING, execution.status)
        # If the process is still running docl.read_file will raise an error.
        # We use do_retries to give the kill cancel operation time to kill
        # the process.
        do_retries(self.assertRaises, excClass=ErrorReturnCode,
                   callableObj=self.read_manager_file,
                   file_path=path) 
Example #11
Source File: openbmc-autobump.py    From openbmc-tools with Apache License 2.0 6 votes vote down vote up
def find_candidate_recipes(meta, args):
    remote_fmt_args = (args.ssh_config_host, meta)
    remote = 'ssh://{}/openbmc/{}'.format(*remote_fmt_args)
    try:
        git_clone_or_reset(meta, remote, args)
    except sh.ErrorReturnCode as e:
        log('{}'.format(e), args)
        return []

    grep_args = ['-l', '-e', '_URI', '--and', '-e', 'github.com/openbmc']
    try:
        return git.grep(*grep_args, _cwd=meta).stdout.decode('utf-8').split()
    except sh.ErrorReturnCode_1:
        pass
    except sh.ErrorReturnCode as e:
        log('{}'.format(e), args)

    return [] 
Example #12
Source File: git.py    From WordOps with MIT License 6 votes vote down vote up
def rollback(self, paths, msg="Rolling-Back"):
        """
            Rollback last commit to restore previous.
            configuration and commit changes automatically
        """
        for path in paths:
            global git
            wogit = git.bake("-C", "{0}".format(path))
            if os.path.isdir(path):
                if not os.path.isdir(path + "/.git"):
                    Log.error(
                        self, "Unable to find a git repository at {0}"
                              .format(path))
                try:
                    Log.debug(
                        self, "WOGit: git stash --include-untracked at {0}"
                              .format(path))
                    wogit.stash("push", "--include-untracked", "-m {0}"
                                .format(msg))
                except ErrorReturnCode as e:
                    Log.debug(self, "{0}".format(e))
                    Log.error(self, "Unable to git reset at {0} "
                              .format(path))
            else:
                Log.debug(self, "WOGit: Path {0} not present".format(path)) 
Example #13
Source File: fileutil_fetch.py    From dreamer with Apache License 2.0 6 votes vote down vote up
def execute_commands(commands, parallel):
  semaphore = threading.Semaphore(parallel)
  def done_fn(cmd, success, exit_code):
    print(cmd._foo)
    semaphore.release()
  running = []
  for command in commands:
    semaphore.acquire()
    running.append(command(_bg=True, _done=done_fn))
    running[-1]._foo = command._foo
  failures = 0
  outputs = []
  for command in running:
    try:
      command.wait()
      outputs.append(command.stdout.decode('utf-8'))
    except sh.ErrorReturnCode as e:
      print(e)
      failures += 1
  print('')
  return outputs, failures 
Example #14
Source File: ansible_playbook.py    From molecule with MIT License 6 votes vote down vote up
def execute(self):
        """
        Execute ``ansible-playbook`` and returns a string.

        :return: str
        """
        if self._ansible_command is None:
            self.bake()

        if not self._playbook:
            LOG.warning("Skipping, %s action has no playbook." % self._config.action)
            return

        try:
            self._config.driver.sanity_checks()
            cmd = util.run_command(self._ansible_command, debug=self._config.debug)
            return cmd.stdout.decode("utf-8")
        except sh.ErrorReturnCode as e:
            out = e.stdout.decode("utf-8")
            util.sysexit_with_message(str(out), e.exit_code) 
Example #15
Source File: test_project.py    From python-template with Apache License 2.0 6 votes vote down vote up
def test_install(cookies):
    project = cookies.bake()

    assert project.exit_code == 0
    assert project.exception is None

    cwd = os.getcwd()
    os.chdir(str(project.project))

    try:
        sh.python(['setup.py', 'install'])
    except sh.ErrorReturnCode as e:
        pytest.fail(e)
    finally:
        os.chdir(cwd) 
Example #16
Source File: test_values.py    From python-template with Apache License 2.0 6 votes vote down vote up
def test_single_quotes_in_name_and_description(cookies):
    ctx = {'project_short_description': "'single quotes'",
           'full_name': "Mr. O'Keeffe"}
    project = cookies.bake(extra_context=ctx)

    assert project.exit_code == 0

    with open(os.path.join(str(project.project), 'setup.py')) as f:
        setup = f.read()
    print(setup)

    cwd = os.getcwd()
    os.chdir(str(project.project))

    try:
        sh.python(['setup.py', 'install'])
    except sh.ErrorReturnCode as e:
        pytest.fail(e)
    finally:
        os.chdir(cwd) 
Example #17
Source File: test_values.py    From python-template with Apache License 2.0 6 votes vote down vote up
def test_space_in_project_slug(cookies):
    ctx = {'project_slug': "my package"}
    project = cookies.bake(extra_context=ctx)

    assert project.exit_code == 0

    with open(os.path.join(str(project.project), 'setup.py')) as f:
        setup = f.read()
    print(setup)

    cwd = os.getcwd()
    os.chdir(str(project.project))

    try:
        sh.python(['setup.py', 'install'])
        sh.python(['setup.py', 'build_sphinx'])
    except sh.ErrorReturnCode as e:
        pytest.fail(e)
    finally:
        os.chdir(cwd) 
Example #18
Source File: testinfra.py    From molecule with MIT License 6 votes vote down vote up
def execute(self):
        if not self.enabled:
            msg = "Skipping, verifier is disabled."
            LOG.warning(msg)
            return

        if not len(self._tests) > 0:
            msg = "Skipping, no tests found."
            LOG.warning(msg)
            return

        if self._testinfra_command is None:
            self.bake()

        msg = "Executing Testinfra tests found in {}/...".format(self.directory)
        LOG.info(msg)

        try:
            util.run_command(self._testinfra_command, debug=self._config.debug)
            msg = "Verifier completed successfully."
            LOG.success(msg)

        except sh.ErrorReturnCode as e:
            util.sysexit(e.exit_code) 
Example #19
Source File: test_project.py    From python-template with Apache License 2.0 6 votes vote down vote up
def test_building_documentation_apidocs(cookies):
    project = cookies.bake(extra_context={'apidoc': 'yes'})

    assert project.exit_code == 0
    assert project.exception is None

    cwd = os.getcwd()
    os.chdir(str(project.project))

    try:
        sh.python(['setup.py', 'build_sphinx'])
    except sh.ErrorReturnCode as e:
        pytest.fail(e)
    finally:
        os.chdir(cwd)

    apidocs = project.project.join('docs', '_build', 'html', 'apidocs')

    assert apidocs.join('my_python_project.html').isfile()
    assert apidocs.join('my_python_project.my_python_project.html').isfile() 
Example #20
Source File: device.py    From mobile-ai-bench with Apache License 2.0 6 votes vote down vote up
def __init__(self, device_dict):
        """
        init device with device dict
        """
        diff = set(device_dict.keys()) - set(YAMLKeyword.__dict__.keys())
        if len(diff) > 0:
            six.print_('Wrong key detected:')
            six.print_(diff)
            raise KeyError(str(diff))
        self.__dict__.update(device_dict)
        if self.system == SystemType.android:
            pass
        elif self.system == SystemType.arm_linux:
            try:
                sh.ssh('-q', '%s@%s' % (self.username, self.address),
                       'exit')
            except sh.ErrorReturnCode as e:
                six.print_('device connect failed, '
                           'please check your authentication',
                           file=sys.stderr)
                raise e

    #####################
    #  public interface #
    ##################### 
Example #21
Source File: test_scenarios.py    From molecule with MIT License 6 votes vote down vote up
def test_command_init_scenario_without_default_scenario_raises(temp_dir):
    role_directory = os.path.join(temp_dir.strpath, "test-role")
    options = {}
    cmd = sh.molecule.bake("init", "role", "test-role", **options)
    pytest.helpers.run_command(cmd)
    pytest.helpers.metadata_lint_update(role_directory)

    with change_dir_to(role_directory):
        molecule_directory = pytest.helpers.molecule_directory()
        scenario_directory = os.path.join(molecule_directory, "default")
        shutil.rmtree(scenario_directory)

        options = {"role_name": "test-role"}
        with pytest.raises(sh.ErrorReturnCode) as e:
            cmd = sh.molecule.bake("init", "scenario", "invalid-role-name", **options)
            pytest.helpers.run_command(cmd, log=False)

        msg = (
            "The default scenario not found.  Please create a scenario "
            "named 'default' first."
        )
        assert msg in str(e.value.stderr) 
Example #22
Source File: test_scenarios.py    From molecule with MIT License 6 votes vote down vote up
def test_command_init_scenario_with_invalid_role_raises(temp_dir):
    role_directory = os.path.join(temp_dir.strpath, "test-role")
    options = {}
    cmd = sh.molecule.bake("init", "role", "test-role", **options)
    pytest.helpers.run_command(cmd)
    pytest.helpers.metadata_lint_update(role_directory)

    with change_dir_to(role_directory):
        options = {"role_name": "invalid-role-name"}
        with pytest.raises(sh.ErrorReturnCode) as e:
            cmd = sh.molecule.bake("init", "scenario", "default", **options)
            pytest.helpers.run_command(cmd, log=False)

        msg = (
            "ERROR: The role 'invalid-role-name' not found. "
            "Please choose the proper role name."
        )
        assert msg in str(e.value.stderr) 
Example #23
Source File: docker_compose_tools.py    From postgres-elasticsearch-fdw with MIT License 5 votes vote down vote up
def set_up(pg_version, es_version):
    """ Start containers """

    compose = docker_compose(pg_version, es_version)

    show_status(
        "Starting testing environment for PostgreSQL {pg_version} with Elasticsearch {es_version}...".format(
            pg_version=pg_version, es_version=es_version
        )
    )

    show_status("Stopping and Removing any old containers...")
    compose("stop")
    compose("rm", "--force")

    show_status("Building new images...")
    try:
        compose("build")
    except ErrorReturnCode as exc:
        print("Failed to build images...")
        print(exc.stdout.decode("utf-8"))
        print()
        print(exc.stderr.decode("utf-8"))
        sys.exit(1)

    show_status("Starting new containers...")
    compose("up", "-d")

    show_status("Testing environment started") 
Example #24
Source File: test_scenarios.py    From molecule with MIT License 5 votes vote down vote up
def test_command_test_destroy_strategy_always(
    scenario_to_test, with_scenario, scenario_name, driver_name
):
    options = {"destroy": "always"}
    with pytest.raises(sh.ErrorReturnCode) as e:
        cmd = sh.molecule.bake("test", **options)
        pytest.helpers.run_command(cmd, log=False)

    stdout = util.strip_ansi_escape(e.value.stdout)

    assert "Action: 'cleanup'" in stdout
    assert "PLAY [Destroy]" in stdout
    assert 0 != e.value.exit_code 
Example #25
Source File: utils.py    From ocdeployer with MIT License 5 votes vote down vote up
def start_deployment(dc_name, timeout=180):
    if dc_ready(dc_name):
        log.info("Deployment '%s' already deployed and running, skipping deploy for it", dc_name)
        return

    log.info("Patching deployment config for '%s' to resume rollouts", dc_name)
    try:
        oc("rollout", "resume", "dc/{}".format(dc_name), _reraise=True)
    except sh.ErrorReturnCode as err:
        if "is not paused" in str(err.stderr):
            pass

    log.info("Triggering new deploy for '%s'", dc_name)
    try:
        oc("rollout", "latest", "dc/{}".format(dc_name), _reraise=True)
    except sh.ErrorReturnCode as err:
        if "already in progress" in str(err.stderr):
            pass

    log.info("Waiting for pod related to '%s' to finish deploying", dc_name)
    wait_for(
        dc_ready,
        func_args=(dc_name,),
        message="wait for deployment '{}' to be ready".format(dc_name),
        delay=5,
        timeout=timeout,
        log_on_loop=True,
    ) 
Example #26
Source File: test_scenarios.py    From molecule with MIT License 5 votes vote down vote up
def test_command_test_destroy_strategy_never(
    scenario_to_test, with_scenario, scenario_name, driver_name
):
    options = {"destroy": "never"}
    with pytest.raises(sh.ErrorReturnCode) as e:
        cmd = sh.molecule.bake("test", **options)
        pytest.helpers.run_command(cmd, log=False)

    msg = "An error occurred during the test sequence action: 'lint'. " "Cleaning up."
    assert msg not in str(e.value.stdout)

    assert 0 != e.value.exit_code 
Example #27
Source File: utils.py    From ocdeployer with MIT License 5 votes vote down vote up
def stop_deployment(dc_name, timeout=180):
    """
    Pause a deployment, delete all of its replication controllers, wait for all pods to shut down
    """
    if not any_pods_running(dc_name):
        log.info("No pods running for dc '%s', nothing to stop", dc_name)
        return

    log.info("Patching deployment config for '%s' to pause rollouts", dc_name)
    try:
        oc("rollout", "pause", "dc/{}".format(dc_name), _reraise=True)
    except sh.ErrorReturnCode as err:
        if "is already paused" in str(err.stderr):
            pass

    log.info("Removing replication controllers for '%s'", dc_name)
    rc_data = get_json("rc", label="openshift.io/deployment-config.name={}".format(dc_name))
    if not rc_data or not len(rc_data.get("items", [])):
        raise Exception("Unable to find replication controllers for '{}'".format(dc_name))
    for rc in rc_data["items"]:
        rc_name = rc["metadata"]["name"]
        oc("delete", "rc", rc_name)

    log.info("Waiting for pods related to '%s' to terminate", dc_name)
    wait_for(
        no_pods_running,
        func_args=(dc_name,),
        message="wait for deployment '{}' to be terminated".format(dc_name),
        timeout=timeout,
        delay=5,
        log_on_loop=True,
    ) 
Example #28
Source File: utils.py    From ocdeployer with MIT License 5 votes vote down vote up
def get_json(restype, name=None, label=None):
    """
    Run 'oc get' for a given resource type/name/label and return the json output.

    If name is None all resources of this type are returned

    If label is not provided, then "oc get" will not be filtered on label
    """
    restype = parse_restype(restype)

    args = ["get", restype]
    if name:
        args.append(name)
    if label:
        args.extend(["-l", label])
    try:
        output = oc(*args, o="json", _exit_on_err=False, _silent=True)
    except ErrorReturnCode as err:
        if "NotFound" in err.stderr:
            return {}

    try:
        parsed_json = json.loads(str(output))
    except ValueError:
        return {}

    return parsed_json 
Example #29
Source File: utils.py    From ocdeployer with MIT License 5 votes vote down vote up
def switch_to_project(project):
    try:
        oc("get", "project", project, _reraise=True)
    except ErrorReturnCode:
        log.error("Unable to get project '%s', trying to create it...", project)
        oc("new-project", project, _exit_on_err=True)
    oc("project", project, _exit_on_err=True) 
Example #30
Source File: utils.py    From ocdeployer with MIT License 5 votes vote down vote up
def oc(*args, **kwargs):
    """
    Run 'sh.oc' and print the command, show output, catch errors, etc.

    Optional kwargs:
        _reraise: if ErrorReturnCode is hit, don't exit, re-raise it
        _exit_on_err: sys.exit(1) if this command fails (default True)
        _silent: don't print command or resulting stdout (default False)
        _ignore_immutable: ignore errors related to immutable objects (default True)
        _retry_conflicts: retry commands if a conflict error is hit
        _stdout_log_prefix: prefix this string to stdout log output (default " |stdout| ")
        _stderr_log_prefix: prefix this string to stderr log output (default " |stderr| ")

    Returns:
        None if cmd fails and _exit_on_err is False
        command output (str) if command succeeds
    """
    _exit_on_err = kwargs.pop("_exit_on_err", True)
    _reraise = kwargs.pop("_reraise", False)
    # The _silent/_ignore_immutable/_retry_conflicts kwargs are passed on so don't pop them yet

    try:
        return _exec_oc(*args, **kwargs)
    except ErrorReturnCode:
        if _reraise:
            raise
        elif _exit_on_err:
            abort()
        else:
            if not kwargs.get("_silent"):
                log.warning("Non-zero return code ignored")