Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipelining commands doesnt work in python fabric

I'm trying to pipeline multiple bash commands to extract an image name from the output of a command that lists the names of images but it doesnt work - it spits a blob of text, where as doing it step by step works.

Code using pipeline:

@task
def update():
    image_name = sudo('/os-updater --list | grep X86-64_ | sed "s/XXX/UEK4/" | tail -1')
    print("IMAGE NAME: "+image_name)

Output:

user@system:host-updater$ ./hu -R host-name update
[host-name] Executing task 'update'
[host-name] sudo: /os-updater --list | grep X86-64_ | sed "s/XXX/UEK4/" | tail -1
[host-name] out: 2018-01-11 23:33:28,628 INFO ==> os-updater 1.3.3 started
[host-name] out: 2018-01-11 23:33:28,629 INFO --listsnapshots:True
[host-name] out: 2018-01-11 23:33:28,705 INFO Exit 0
[host-name] out:   X86-64_20171201.01_UEK4
[host-name] out: 

IMAGE NAME: 2018-01-11 23:33:28,628 INFO ==> os-updater 1.3.3 started
2018-01-11 23:33:28,629 INFO --listsnapshots:True
2018-01-11 23:33:28,705 INFO Exit 0
  X86-64_20171201.01_UEK4

Done.

Code without pipleine:

@task
def update():
    image_list = sudo("/os-updater --list")
    out = image_list.stdout
    for line in out.splitlines():
        if 'X86-64_' in line:
            image_name = line.replace("XXX", "UEK4").strip()

    print("IMAGE NAME: " + image_name)

Output:

user@system:host-updater$ ./hu -R host-name update
[host-name] Executing task 'update'
[host-name] sudo: /os-updater --list
[host-name] out: 2018-01-11 23:36:11,752 INFO ==> os-updater 1.3.3 started
[host-name] out: 2018-01-11 23:36:11,753 INFO --listsnapshots:True
[host-name] out: 2018-01-11 23:36:11,847 INFO Exit 0
[host-name] out: Snapshots:
[host-name] out:   X86-64_20171101.01_XXX
[host-name] out:   X86-64_20171201.01_XXX
[host-name] out: Kernel trains: (XXX above)
[host-name] out:   UEK4
[host-name] out:   STOCK
[host-name] out: 

IMAGE NAME: X86-64_20171201.01_UEK4

Done.

I dont understand why the second way (literally doing the same step by step without pipeline) works but not the first returns a lot of text with the image name. I have tried a lot of things:

image_name = sudo('/opt/os-updater/bin/os-updater --list | grep X86-64_OL7_ | sed "s/XXX/UEK4/" | tail -1').stdout.strip() 

and then running the command with the fabric hiding option like # with hide('output','running','warnings'):. None of it works. It's so weird that pipelining doesn't work. Any help is appreciated.

like image 700
pritz Avatar asked Jun 17 '26 05:06

pritz


1 Answers

The pipe is a shell syntax element. You need to run it in a shell:

sudo("/os-updater --list | sed -n '/X86-64_/s/XXX/UEK4/p' | tail -1", shell=True)

PS: I took the freedom to combine the grep and sed command into one.

like image 178
hek2mgl Avatar answered Jun 18 '26 18:06

hek2mgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!