{"id":633,"date":"2016-04-29T12:58:46","date_gmt":"2016-04-29T11:58:46","guid":{"rendered":"http:\/\/www.haxed.me.uk\/?p=633"},"modified":"2016-05-05T15:08:35","modified_gmt":"2016-05-05T14:08:35","slug":"using-bash-api-validate-set-conditions","status":"publish","type":"post","link":"https:\/\/haxed.me.uk\/index.php\/2016\/04\/29\/using-bash-api-validate-set-conditions\/","title":{"rendered":"HOWTO: Rackspace Automation, Using BASH with API (to validate conditions to perform conditional tasks)"},"content":{"rendered":"<p>In the previous article, I showed how to wipe clean the windows password from a broken Virtual Machine that you were locked out of by rescuing with a Linux image. In this article I explain steps of how you would automate this with a bash script, that looked at the STATE of the server, and accepts commandline arguments.<\/p>\n<p>It&#8217;s quite a simple script;<\/p>\n<pre>\r\n#!\/bin\/bash\r\n# Adam Bull\r\n# April 28 2016\r\n# This script automates the resetting of windows passwords\r\n# Arguments $1 == instanceuuid\r\n# Arguments $2 == username\r\n# Arguments $3 == apikey\r\n\r\nPASSWORD=mypassword\r\n\r\n# Provide an instance uuid to rescue and reset windows password\r\n\r\nUSERNAME=$1\r\nAPIKEY=$2\r\nDDI=$3\r\nINSTANCE=$4\r\n\r\n\r\nnova  --os-username $USERNAME --os-auth-system=rackspace  --os-tenant-name $DDI --os-auth-url https:\/\/lon.identity.api.rackspacecloud.com\/v2.0\/ --os-password $APIKEY --insecure rescue --password mypassword --image 7fade26a-0cca-415f-a988-49c021768fca $INSTANCE\r\n<\/pre>\n<p>The above script takes the arguments I give the executable script on the commandline, in this case the first argument passed is $1, the Rackspace mycloud username. The second argument the apikey. etc. This basically puts the server into rescue. But.. what if we wanted to run some automation AFTER it rescued? We don&#8217;t want to try and let the automation ssh to the box and run the automation early, so we could use a supernova show to find whether the VM state has changed to &#8216;rescue&#8217;. Whilst its initiating the state will be rescuing. So we have the option of using when !rescueing logic, or, when == equal to rescue. Lets use when equal to rescue in our validation loop.<\/p>\n<p>This loop will continue until the task state changes to the desired value. Here is how we achieve it<\/p>\n<pre>\r\n#!\/bin\/bash\r\n# Initialize Variable\r\nSTATE=0\r\n# Validate $STATE variable, looping UNTIL $STATE == rescued\r\nuntil [[ $STATE == rescued ]]; do\r\necho \"start rescue check\"\r\n# 'show' the servers data, and grep for rescued and extract only the correct value if it is found\r\nSTATE=`nova --os-username $USERNAME --os-auth-system=rackspace  --os-tenant-name $DDI --os-auth-url https:\/\/lon.identity.api.rackspacecloud.com\/v2.0\/ --os-password $APIKEY --insecure show $INSTANCE | grep rescued | awk '{print $4}'`\r\n\r\n# For debugging\r\necho \"STATE =\" $STATE\r\necho \"sleeping..\"\r\n\r\n# For API Limit control\r\nsleep 5\r\n# Exit the loop once until condition satisfied\r\ndone\r\n\r\n# Post Rescue\r\necho \"If you read this, it means that the program detected a rescued state\"\r\n\r\n<\/pre>\n<p>It&#8217;s quite a simple script to use. We just provide the arguments $1, $2, $3 and $4. <\/p>\n<pre> \r\n.\/rescue.sh mycloudusername mycloudapikey 10010101 e744af0f-6643-44f4-a63f-d99db1588c94\r\n<\/pre>\n<p>Where 10010101 is the tenant id and e744af0f-6643-44f4-a63f-d99db1588c94 is the UUID of your server.<\/p>\n<p>It&#8217;s really quite simple to do! But this is not enough we want to go a step further. Let&#8217;s move the rescue.sh to \/bin<\/p>\n<pre>\r\n# WARNING \/bin is not a playground this is for demonstration purposes of how to 'install' bin system applications\r\ncp rescue.sh \/bin\/rescue.sh \r\n<\/pre>\n<p>Now you can call the command &#8216;rescue&#8217;.<\/p>\n<pre>\r\nrescue mycloudusername mycloudapikey mycustomerid mycloudserveruuidgoeshere\r\n<\/pre>\n<p>nice, and quite simple too. Obviously &#8216;post rescue&#8217; in the script I can upload a script via ssh to the server, and then execute it remotely to perform the password reset.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous article, I showed how to wipe clean the windows password from a broken Virtual Machine that you were locked out of by rescuing with a Linux image. In this article I explain steps of how you would &hellip; <a href=\"https:\/\/haxed.me.uk\/index.php\/2016\/04\/29\/using-bash-api-validate-set-conditions\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,42,19,15,21,11,3,24,50],"tags":[],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-api","category-automation","category-bash","category-cloud","category-nova","category-openstack","category-security","category-supernova","category-windows"],"_links":{"self":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":3,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":642,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions\/642"}],"wp:attachment":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}