OpenStack One-Liners

Hi All. Here are some interesting one-liners I’ve used. Hope they help!

And by “one-liner”, of course, I mean “a bunch of commands all typed on one line” ๐Ÿ™‚

All one-liners assume you have sourced in your RC file so that OS_XXX environment variables are set correctly:

Show all active VMs

Here’s a nice little gnarly expression:


for j in $(openstack project list -c ID | tail -n +4 | head -n -1 | awk -F'|' '{print $2}'); do _s=$(openstack --os-project-id $j server list | grep '| \+ACTIVE \+|' 2>/dev/null | sed -e 's# ##g'); [ ! -z "$_s" ] && (_n=$(openstack project show $j -c name | grep -e '| \+name \+|' | awk -F'|' '{print $3}' | sed -e 's#^ \+##' -e 's# \+$##'); echo "Project $_n ($j)"; openstack --os-project-id $j server list | grep '| \+ACTIVE \+|'); sleep 1; done

Explanation? Iterate over all projects; for each one do a quick server list looking for ACTIVE VMs. Run the result through a sed to eliminate whitespace; if there is anything left, we have active servers ๐Ÿ™‚ We then do a quick lookup on the project ID to get the name (with appropriate calls to trim spaces) and show the project name, ID, and list of active VMs.

Want some output?


[root@lvosneutr120 scripts(lvosksclu120-rc-admin)]# for j in $(openstack project list -c ID | tail -n +4 | head -n -1 | awk -F'|' '{print $2}'); do _s=$(openstack --os-project-id $j server list | grep '| \+ACTIVE \+|' 2>/dev/null | sed -e 's# ##g'); [ ! -z "$_s" ] && (_n=$(openstack project show $j -c name | grep -e '| \+name \+|' | awk -F'|' '{print $3}' | sed -e 's#^ \+##' -e 's# \+$##'); echo "Project $_n ($j)"; openstack --os-project-id $j server list | grep '| \+ACTIVE \+|'); sleep 1; done
Project lmil-microstrategy (07b2183ca75d438ebdecb620acb5b2b8)
| f9057177-cc01-4041-9c05-f8b324c4d2cf | mstrat-05 | ACTIVE | lmil-microstrategy-net=10.0.0.5, 172.20.142.51 |
| bd5cdd5e-d1f6-40f7-821e-aeaf991705a0 | mstrat-03 | ACTIVE | lmil-microstrategy-net=10.0.0.4, 172.20.132.20 |
| 749f2bdb-c351-4218-b8a5-ccb0b0d039c2 | mstrat-04 | ACTIVE | lmil-microstrategy-net=10.0.0.3, 172.20.132.18 |
Project lmil-sadmin-dev (23a18e23522e4f55a08828da48e2b547)
| dda07689-1953-447e-a101-a87cdb8bb3e9 | lvtgodevx100                                 | ACTIVE  | lmil-sadmin-dev-net=10.0.0.199, 172.20.142.102 |
| 18c7c36a-c069-4fc8-8070-3446f859fe8f | test-cobol-example-ServerConfig-mpeqgmghr6ui | ACTIVE  | lmil-sadmin-dev-net=10.0.0.195, 172.20.142.86  |

I’ve cut off the rest, but you get the idea.

List permissions for a given user across all projects

Here’s the one from today. I am migrating to OpenStack Kilo from Icehouse and I want to kick some of my old users off of the original Icehouse deployment. Keeps ’em from messing things up!! So to do this here’s my line-liner:


for i in $(openstack user list -c ID -c Name | grep "| [USER]" | awk -F'|' '{print $2}'); do l_printed=0; for j in $(openstack project list -c ID | tail -n +4 | head -n -1 | awk -F'|' '{print $2}'); do l_roles=$(openstack user role list --project $j $i); [ "x$l_roles" != "x" ] && ( [ "$l_printed" -eq 0 ] && l_printed=1 && echo "Project: $j" && echo "User: $i"; openstack user role list --project $j $i; ) done; done

What it does… We start by iterating over all of the users and looking for the name of our user. We then iterate over all projects and do a lookup of user roles assigned to that user within each project. Due to a bug in openstack, if a user has no roles then a blank line is printed by openstack user role list --project $j $i so we capture it and test for it being non-empty (bash gets rid of whitespace for us by default). We then print out the results so we can easily run the openstack role remove command using the output.

Team-oriented systems mentor with deep knowledge of numerous software methodologies, technologies, languages, and operating systems. Excited about turning emerging technology into working production-ready systems. Focused on moving software teams to a higher level of world-class application development. Specialties:Software analysis and development...Product management through the entire lifecycle...Discrete product integration specialist!

Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.