Bulk creating Rackspace cloud servers using Script

October 31, 2013

I keep having to create a large number of cloud servers on Rackspace Cloud, so I can play with things like Hadoop and Cassandra.
Using the control panel to create one server at a time, and record each login password and ip, and wait till the server goes active can get really tedious very soon.
So here’s a little script that installs the REST API python binding ‘rackspace-novaclient’ on an Ubuntu server, and prompts you for the image, flavor and number of servers to create, then goes and creates the servers.

On an Ubuntu server, first export your Rackspace Cloud auth credentials (either as root or sudo user)

export OS_USERNAME=<username>
export OS_PASSWORD=<apikey>
export OS_TENANT_NAME=<username>
export OS_AUTH_SYSTEM=rackspace
export OS_AUTH_URL=https://identity.api.rackspacecloud.com/v2.0/
export OS_REGION_NAME=DFW export OS_NO_CACHE=1

Here is the actual script:

#!/bin/bash

set -x

# Install the Client
if [[ $EUID -ne 0 ]]; then
	sudo apt-get update
	sudo apt-get install python-dev python-pip python-virtualenv
else
	apt-get update
	apt-get install python-dev python-pip python-virtualenv
fi

virtualenv ~/.env
source ~/.env/bin/activate
pip install pbr
pip install python-novaclient
pip install rackspace-novaclient

# Read AUTH Credentials
: ${OS_USERNAME:?"Need to set OS_USERNAME non-empty"}
: ${OS_PASSWORD:?"Need to set OS_PASSWORD non-empty"}
: ${OS_TENANT_NAME:?"Need to set OS_TENANT_NAME non-empty"}
: ${OS_AUTH_SYSTEM:?"Need to set OS_AUTH_SYSTEM non-empty"}
: ${OS_AUTH_URL:?"Need to set OS_AUTH_URL non-empty"}
: ${OS_REGION_NAME:?"Need to set OS_REGION_NAME non-empty"}
: ${OS_NO_CACHE:?"Need to set OS_NO_CACHE non-empty"}

# Write credentials to a file
cat > ~/novarc <> 'server_passwords.txt'
}

CLUSTER_SIZE=`expr $CLUSTER_SIZE - 1`
for i in $(eval echo "{1..$CLUSTER_SIZE}")
do
	boot $FLAVOR_ID $IMAGE_ID $CLUSTER_NAME$i
done

is_not_active() {
	status=`nova show $1 | grep 'status' | awk '{print $4}'`
	if [ "$status" != "ACTIVE" ] && [ "$status" != "ERROR" ]; then
		echo "$1 in $status"
		return 0
	else
		return 1
	fi
}

# Wait for all the instances to go ACTIVE or ERROR
while true
do
	READY=1
	for i in $(eval echo "{1..$CLUSTER_SIZE}")
	do
		if is_not_active $CLUSTER_NAME$i; then
			READY=0
		fi
	done

	echo "READY is $READY"
	if [ "$READY" -eq "1" ]; then
		break
	fi
	sleep 5
done

for i in $(eval echo "{1..$CLUSTER_SIZE}")
do
	echo $CLUSTER_NAME$i >> 'hosts.txt'
done
cat hosts.txt

record_ip(){
	private_ip=`nova show $1 | grep 'private network' | awk '{print $5}'`
	public_ip=`nova show $1 | grep 'accessIPv4' | awk '{print $4}'`
	echo $private_ip $1 >> 'etc_hosts.txt'
	echo $public_ip $1 >> 'etc_hosts.txt'
}

for i in $(eval echo "{1..$CLUSTER_SIZE}"); do record_ip $CLUSTER_NAME$i; done

cat etc_hosts.txt

echo "CLUSTER is READY"

Then, execute the script

./create-clusters.sh

Alternatively, I put the script on github, so you can also do curl, pipe, bash.

bash <(curl -s https://raw.github.com/sacharya/hadoop-101/master/rax/create-clusters.sh)

The script will wait till all the servers go to an active status and will save the the ips, hostnames and passwords for each of the servers onto these three files.

  • etc_hosts.txt
  • hosts.txt
  • server_passwords.txt
1 Comment on Bulk creating Rackspace cloud servers using Script

Respond | Trackback

  1. […] actually create the servers, I will use a slightly modified version of bulk servers create script. I will create one server for Apache Ambari and a number of servers for Apache Hadoop Cluster and […]

Respond

Comments

Comments