Skip to content

Instantly share code, notes, and snippets.

@hughsaunders
Created June 5, 2012 17:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hughsaunders/2876263 to your computer and use it in GitHub Desktop.
Save hughsaunders/2876263 to your computer and use it in GitHub Desktop.
Rough script for creating jmeter slaves with the rackspace uk cloud api
#!/usr/bin/env python
#Script for destroying cloudservers that match a name prefix.
from cloudservers import CloudServers
import re
ukcloud=CloudServers('YOUR RS CLOUD UK USERNAME', 'YOUR API KEY')
prefix='jmeter'
[i.delete() for i in ukcloud.servers.list() if re.match('^'+prefix,i.name)]
#!/usr/bin/env python
#Script for creating jmeter slaves using the rackspace cloud api and fabric.
#Dependencies for this script, Debian 6.x
#requires fabric:
# apt-get install fabric python-stdeb python-prettytable
#requires python-cloudservers:
# pypi-install python-cloudservers
##---Manual Library Edit Required---
# python-cloudservers library uses the US cloud endpoint by default, append lon to authurl:
# /usr/lib/python2.7/dist-packages/cloudservers/client.py: AUTH_URL = 'https://lon.auth.api.rackspacecloud.com/v1.0'
#Imports
from cloudservers import CloudServers
import fabric
from fabric.api import *
import time
from datetime import datetime
import re
from threading import Thread
import sys
#rackspace uk cloud username and api key:
username='YOUR USERNAME HERE'
api_key='YOUR API KEY HERE'
#Authenticate with cloud API
ukcloud=CloudServers(username,api_key)
#Instances will be named prefix+n
prefix='jmeter'
#number of instances to create:
number_of_instances=3
#Files to inject into instance image, useful for inserting an SSH public key.
files={'/root/.ssh/authorized_keys':'YOUR PUBLIC SSH KEY HERE'}
#login credentials for created instances
env.user='root'
env.key_filename='PATH TO YOUR PRIVATE SSH KEY' #file containing ssh private key
#------ END of edits ------
#Function to create a new 256MB Debian 6 instance, and wait for it to be ready.
def create_instance(prefix,id):
start_time=datetime.now()
#104 = Debian 6 base image
#1 = 256MB Instance
instance=ukcloud.servers.create(prefix+str(id),104,1,files=files)
print instance.name,instance.public_ip,instance.adminPass
counter=40
while counter>0:
counter=counter-1
instance.get()
if instance.status=='ACTIVE':
instances.append(instance)
active_time=datetime.now()
print instance.name,"Active. Build time:",active_time-start_time
return
elif instance.status=='ERROR':
instance.delete()
raise Exception('cloud instance failed to build')
print ".",
sys.stdout.flush() #line bufferring grrr
time.sleep(15)
raise Exception('cloud instance build timed out.')
#Get exisitng instances
instances=[instance for instance in ukcloud.servers.list() if re.match('^'+prefix,instance.name) and instance.status=='ACTIVE']
#create new instances if necesssary
if len(instances)<number_of_instances:
#create servers in parallel running threads
threads=[]
for i in range(len(instances)+1,number_of_instances+1):
thread=Thread(target=create_instance, args=(prefix,i))
thread.start()
threads.append(thread)
time.sleep(5) #api for create servers is rate-limited
#wait for all threads to finish.
for t in threads:
t.join()
#fabric tasks:
@parallel
def install_jmeter():
run('apt-get update >/dev/null')
run('apt-get -fy install jmeter dtach >/dev/null')
with settings(warn_only=True):
run('(while pgrep -f [j]meter-server >/dev/null; do pkill -9 -f [j]meter-server; sleep 1; done) ||true') #incase there are running instances alredy
run('dtach -n jmeter.$$.$RANDOM jmeter-server -Djava.rmi.server.hostname=$(curl icanhazip.com 2>/dev/null)')
#Add instance public IPs to Fabric's hosts environment variable
env.hosts=[instance.public_ip for instance in instances]
#execute Fabric tasks
fabric.tasks.execute(install_jmeter)
#show connection string for jmeter
print("Now add the folloing line to /usr/share/jmeter/bin/jmeter.properties: remote_hosts="+','.join(env.hosts))
print("run jmeter, build a test plan and use 'remote start all'")
Scripts:
* setup.py creates cloud instances,installs and starts jmeter-server
* cleanup.py destroys cloud instances who's name matches a pattern
Usage:
1) edit setup.py, and insert your username/api key/ssh keys. Define the number of instances you want
1a) install the dependencies listed at the top of setup.py
2) run setup.py
3) edit jmeter.properties and add the line specified at the end of setup.py's output
4) run jmeter, build a test plan, and start on all remotes.
5) run cleanup.py to remove the temporary instances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment