Question

I'm new to fabric and rough around the edges with python. I'm trying my hand at writing a small script that updates a file with the ID of a public gpg keyring.

In pseudo-code:

    sed("./conf", 'GPG_KEY='085C042B'', 'GPG_KEY='`commandoutput`', use_sudo=true,backup='')

Where commandoutput is the output of the following command:

    bash$ gpg --list-keys | grep pub | grep 4096R | awk '{ print $2}' | cut -d'/' -f2

I'm looking for suggestions on how to accomplish this. Can I pipe or capture the output of a fabric run command?

Update #1: BMW offered helpful suggestions below, however I'm not quite sure how to integrate into fabric. Here's what I have now:

DUPLY_DIR = "/etc/duply/fullbackup/"
    with cd(DUPLY_DIR):
        GPG_KEY1="085C042B"
        GPG_KEY2="$(gpg --list-keys | awk '/pub/&&/4096R/{split($2,a,"/"); print a[2]}')"
sed("./conf", 'GPG_KEY1', 'GPG_KEY2', use_sudo=true, backup='')

But this produces an error:

File "/home/fabric/fabfile.py", line 44, in stage_gpg
    GPG_KEY2="$(gpg --list-keys | awk '/pub/&&/4096R/{split($2,a,"/"); print a[2]}')" TypeError: unsupported operand type(s) for /: 'str' and 'str'
Was it helpful?

Solution

first, the gpg command can be replaced by

gpg --list-keys |awk '/pub/&&/4096R/{split($2,a,"/");print a[2]}' 

replace GPG key "085C042B" by gpg ouptut on all *.conf files.

GPG_KEY1="085C042B"
GPG_KEY2=$(gpg --list-keys |awk '/pub/&&/4096R/{split($2,a,"/");print a[2]}' )
sed -i "s/$GPG_KEY1/$GPG_KEY2/" *.conf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top