RockyMountainHigh
RockyMountainHigh

Reputation: 3021

Error grepping file via fabric

I am trying to write a fabric task that greps a file that contains a mapping from an internal id to an external id. Once I get that info I can do several things with it during my deploy. However, I am stuck at step one. I'm sure I'm missing something obvious. Here is the current function where it is failing:

def mapFromIntId(intId):
  sudo('grep -i "%s" /data/ids/*' % intId)

When I run this I get the following error:

[10.19.4.188] Executing task 'mapFromIntId'
[10.19.4.188] sudo: grep -i "79959cfe" /data/ids/*

Fatal error: sudo() received nonzero return code 1 while executing!

Requested: grep -i "79959cfe" /data/ids/*
Executed: sudo -S -p 'sudo password:'  /bin/bash -l -c "grep -i \"79959cfe\" /data/ids/*"

Aborting.
Disconnecting from 10.19.4.188... done.

It looks correct and if I run the displayed command myself it works fine. It takes a few seconds because the id files are fairly large. But it does complete.

Any help would be greatly appreciated. Thanks.

Upvotes: 2

Views: 1056

Answers (1)

Morgan
Morgan

Reputation: 4131

Grep returns non-zero exit codes on a number of different cases. From the man page:

EXIT STATUS The grep utility exits with one of the following values:

 0     One or more lines were selected.
 1     No lines were selected.
 >1    An error occurred.

So what you have here is a non-match exit code most likely. Just use a context manager and set the section to warn_only:

from fabric.api import settings, run

def test(path):
    with settings(warn_only=True):
        return run('grep ...')

Ref:

Upvotes: 1

Related Questions