make openstack cli -f shell output friendly to bash

Bug #1616323 reported by Robin Cernin
14
This bug affects 2 people
Affects Status Importance Assigned to Milestone
cliff
Fix Released
Undecided
Steve Martinelli
python-openstackclient
Invalid
Undecided
Unassigned

Bug Description

openstack cli is returning a set of variables like for example:

admin_state_up="True"
id="aabd57f5-466f-43b6-9552-6313969bf057"
name="public"
provider:network_type="vxlan"

When we use eval for passing all these data to environment variables and they get parsed by script.
We do have problem when openstack returns variables like:

provider:network_type="vxlan"

Because the character : is not a a valid character for variable names, also there are variables containing the character -

We could have workaround this by using tr :- _ however then one lose the exit return code with echo $?.

Can we request to implement this a way that all variables returned from openstack cli are shell friendly without characters like : or - only characters allowed to environment variables?

Can be reproduced with:
eval `openstack network show public -f shell`

Results are:
bash: provider:network_type=vxlan: command not found

Expected results would be:
openstack network show public -f shell returning the variables in shell friendly way without characters like : or -.

Thanks!
Robin

Revision history for this message
Steve Martinelli (stevemar) wrote :

Not sure how easy this'll be to maintain. When we "show" a resource, the properties we are displaying often come from the server's response. So, although we could fix this for your specific example, I'm not sure we could actively prevent this from coming up again.

Revision history for this message
Robin Cernin (rcernin) wrote :

Hi,

Please can you comment why then we use shell formatter? We thought this is implemented to give out the variables that are shell parseable?

'''
output formatters:
-f {shell,table,value}, --format {shell,table,value} the output format, defaults to table
'''

Thanks!

Revision history for this message
Hugh Saunders (hughsaunders) wrote :

I just stumbled across this, and added |awk -F= '{gsub(/:/, "_", $1); print ($1"="$2)}' to fix my immediate problem, but it occurred to me that this could be easily fixed in openstack client.
IMO the shell output filter should ensure that keys are valid shell identifiers.

Example rules for doing so:
![a-zA-Z0-9_] --> _
^([0-9]) --> _\1

Revision history for this message
Hugh Saunders (hughsaunders) wrote :

Update to my awk in comment #3: |awk -F= '{OFS="="; gsub(/[-:]/, "_", $1); print}'

Revision history for this message
Steve Martinelli (stevemar) wrote :

Let's see if we can fix this in cliff proper

Changed in python-openstackclient:
status: New → Invalid
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to cliff (master)

Fix proposed to branch: master
Review: https://review.openstack.org/395736

Changed in python-cliff:
assignee: nobody → Steve Martinelli (stevemar)
status: New → In Progress
Revision history for this message
Steve Martinelli (stevemar) wrote :

I posted a patch, feel free to try it out and review

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to cliff (master)

Reviewed: https://review.openstack.org/395736
Committed: https://git.openstack.org/cgit/openstack/cliff/commit/?id=a89dbbb2fe305656e8b3ed60a5273ff8f50bb233
Submitter: Jenkins
Branch: master

commit a89dbbb2fe305656e8b3ed60a5273ff8f50bb233
Author: Steve Martinelli <email address hidden>
Date: Wed Nov 9 12:28:04 2016 -0500

    Replace dashes and colons when using bash formatter

    When a resource property has dashes and colons and
    the bash formatter is used, it actually breaks with
    `command not found`.

    For example, when doing:
      eval `osc network show public --format shell`

    The network resource has a property:
      provider:network_type="vxlan"

    Which results in:
      "bash: provider:network_type=vxlan: command not found"

    Change-Id: I57ca08c9dfd7ef022ab91d50ec42a98a6df08b09
    Closes-Bug: #1616323

Changed in python-cliff:
status: In Progress → Fix Released
Revision history for this message
Steve Martinelli (stevemar) wrote :

This should be fixed in the next version of OpenStackClient (3.4.0)

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/cliff 2.3.0

This issue was fixed in the openstack/cliff 2.3.0 release.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.