{"id":469,"date":"2016-02-29T11:06:29","date_gmt":"2016-02-29T11:06:29","guid":{"rendered":"http:\/\/www.haxed.me.uk\/?p=469"},"modified":"2016-02-29T14:25:37","modified_gmt":"2016-02-29T14:25:37","slug":"building-50-cloud-servers-bashapi-automation","status":"publish","type":"post","link":"https:\/\/haxed.me.uk\/index.php\/2016\/02\/29\/building-50-cloud-servers-bashapi-automation\/","title":{"rendered":"Building 50 Cloud Servers BASH\/API Automation"},"content":{"rendered":"<p>So, I had a good friend of mine who is a cloud Mentor at Rackspace, reach out to me concerning an easier way of deploying cloud-images, without each time a cloud server is spun up, having to unroll the image into a CBS.  His customer wanted to simply have a &#8216;primary master&#8217; CBS volume, a template of their site if you will, the equivalent of a &#8216;golden image&#8217;, the only difference was it was a CBS Volume. So I set about making this work. It would still take a few hours, at least to provision 50 to 200 servers, but it was much faster than the alternatives. Here is how I did it. I actually have some ideas for how to improve this but I&#8217;ve not yet implemented it. That goody is to come in later scripts.<\/p>\n<pre>\r\n#!\/bin\/bash\r\n\r\nUSERNAME='mycloudusername'\r\nAPIKEY='mycloudapikey'\r\nACCOUNT_NUMBER=100101011\r\nAPI_ENDPOINT=\"https:\/\/lon.blockstorage.api.rackspacecloud.com\/v1\/$ACCOUNT_NUMBER\/volumes\"\r\nMASTER_CBS_VOL_ID=\"d8a67ad1-8037-46bc-8790-efca2cb6e5bd\"\r\n\r\n\r\nTOKEN=`curl https:\/\/identity.api.rackspacecloud.com\/v2.0\/tokens -X POST -d '{ \"auth\":{\"RAX-KSKEY:apiKeyCredentials\": { \"username\":\"'$USERNAME'\", \"apiKey\": \"'$APIKEY'\" }} }' -H \"Content-type: application\/json\" |  python -mjson.tool | grep -A5 token | grep id | cut -d '\"' -f4`\r\n\r\n\r\n# Populate CBS\r\nfor i in `seq 1 2`;\r\ndo\r\n\r\necho \"Generating CBS Clone #$i\"\r\n#curl -s -vvvv  \\\r\n-X POST \"$API_ENDPOINT\" \\\r\n-H \"X-Auth-Token: $TOKEN\"  \\\r\n-H \"X-Project-Id: $ACCOUNT_NUMBER\" \\\r\n-H \"Accept: application\/json\"  \\\r\n-H \"Content-Type: application\/json\" -d '{\"volume\": {\"source_volid\": \"d8a67ad1-8037-46bc-8790-efca2cb6e5bd\", \"size\": 50, \"display_name\": \"win-'$i'\", \"volume_type\": \"SSD\"}}'  | jq .volume.id | tr -d '\"' >> cbs.created\r\n\r\ndone\r\n\r\necho \"Giving CBS 2 hour grace time for 50 CBS clone\"\r\n#sleep 7200\r\n\r\necho \"Listing all CBS Volume ID's created\"\r\ncat cbs.created\r\necho \"\"\r\n\r\n\r\n# Populate Nova\r\ncount=1;\r\necho \"Populating Nova servers with CBS disk\"\r\nwhile read n; do\r\nEcho \"Build Task $n Started:\"\r\nnova --insecure --os-username mycloudusername --os-auth-system=rackspace  --os-tenant-name 100110111 --os-auth-url https:\/\/lon.identity.api.rackspacecloud.com\/v2.0\/ --os-password myapikeygoeshere boot --flavor general1-1 --block-device-mapping vda=\"$n\":::1 Auto-win-\"$count\"\r\n((count=count+1))\r\n\r\ndone < cbs.created\r\n\r\n# Move the cbs.created.old away\r\nmv cbs.created cbs.created.old -f\r\n\r\n<\/pre>\n<p>Requirements are nova and jq.<br \/>\nhttps:\/\/stedolan.github.io\/jq\/<br \/>\nhttps:\/\/developer.rackspace.com\/blog\/getting-started-using-python-novaclient-to-manage-cloud-servers\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I had a good friend of mine who is a cloud Mentor at Rackspace, reach out to me concerning an easier way of deploying cloud-images, without each time a cloud server is spun up, having to unroll the image &hellip; <a href=\"https:\/\/haxed.me.uk\/index.php\/2016\/02\/29\/building-50-cloud-servers-bashapi-automation\/\">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":[1],"tags":[],"class_list":["post-469","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/469","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=469"}],"version-history":[{"count":3,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/469\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/469\/revisions\/472"}],"wp:attachment":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}