0

I am trying to find out after a cluster has been provisioned, if it is active and the IP address of the named node using shell scripting. Below is the response of the API output when using the shell commands listed here: http://docs.rackspace.com/cbd/api/v1.0/cbd-getting-started/content/viewing_Details.html.

+----------+--------------------------------------+
| Property | Value |
+----------+--------------------------------------+
| Id | 4820deb2-6212-44f9-b92f-979fe723ffb8 |
| Name | foo |
| Status | ACTIVE |
| Nodes | 3 |
| Type | HADOOP_HDP2_1 |
| Flavor | hadoop1-7 |
+----------+--------------------------------------+

and

--------------+
| Id | Name | Role | Status | Public IP | Private IP |
+--------------------------------------+--------------+----------+--------+----------------+----------------+
| f530a9f1-79a8-4378-bf2a-b7f7e0c2bdd3 | NAMENODE-1 | NAMENODE | ACTIVE | 166.78.132.85  | 10.190.240.88  |

I believe I can do this using some sort of regex or sed/awk. So just to clarify I would like to extract the current status and the public IP

Thank you in advanced

N00b3eva
  • 85
  • 1
  • 8

1 Answers1

0

In order to process your data, it might be easier to convert it in a format which is easier to read, for this, use this filter:

sed -e '/^+[-+]*$/d;s/^| //;s/ |$//;s/ | /|/g'

which will convert your first table into

Property|Value
Id|4820deb2-6212-44f9-b92f-979fe723ffb8
Name|foo
Status|ACTIVE
Nodes|3
Type|HADOOP_HDP2_1
Flavor|hadoop1-7

If you wish, you can drop the header by adjusting the filter like this:

sed -e '1,3d;/^+[-+]*$/d;s/^| //;s/ |$//;s/ | /|/g'

You can then combine this with a second filter to process this data further in any way you want:

awk -F'|' '$1 == "Status" {print($2)}"

You can improve the readability and maintainability of your script by wrapping these two steps into functions and combine these functions in your script:

# rackspace_canonize
#  Canonize output of racksapace report tools
rackspace_canonize()
{
  sed -e '1,3d;/^+[-+]*$/d;s/^| //;s/ |$//;s/ | /|/g'
}

# rackspace_extract_status
#  Extract the status field
rackspace_extract_status()
{
  awk -F'|' '$1 == "Status" {print($2)}'
}

# rackspace_extract_public_ip
#  Extract the public IP
rackspace_extract_public_ip()
{
  awk -F'|' '{print($5)}'
}

Then the following composition gives you the status:

rackspace_canonize | rackspace_extract_status

and the following composition the public IP:

rackspace_canonize | rackspace_extract_public_ip

Note that you can easily add intermediary filters to narrow down the input of the two *extract* functions.

Michaël Le Barbier
  • 6,103
  • 5
  • 28
  • 57