From 7e5087ef1ee1a8771775cc8d175445e2784cd203 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 3 Oct 2016 13:55:59 +0000 Subject: [PATCH 1/4] support ec2 profile --- beeswithmachineguns/bees.py | 34 ++++++++++++++++++---------------- beeswithmachineguns/main.py | 7 +++++-- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/beeswithmachineguns/bees.py b/beeswithmachineguns/bees.py index ddba869..84d9bed 100644 --- a/beeswithmachineguns/bees.py +++ b/beeswithmachineguns/bees.py @@ -53,7 +53,7 @@ from sets import Set STATE_FILENAME = os.path.expanduser('~/.bees') - +HEADER = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'} # Utilities @contextmanager @@ -117,7 +117,7 @@ def _get_security_group_id(connection, security_group_name, subnet): # Methods -def up(count, group, zone, image_id, instance_type, username, key_name, subnet, bid = None): +def up(count, group, zone, image_id, instance_type, username, key_name, subnet, bid = None, profile = None): """ Startup the load testing server. """ @@ -185,7 +185,8 @@ def up(count, group, zone, image_id, instance_type, username, key_name, subnet, security_group_ids=[groupId], instance_type=instance_type, placement=placement, - subnet_id=subnet) + subnet_id=subnet, + instance_profile_arn=profile) # it can take a few seconds before the spot requests are fully processed time.sleep(5) @@ -200,14 +201,15 @@ def up(count, group, zone, image_id, instance_type, username, key_name, subnet, min_count=count, max_count=count, key_name=key_name, - security_group_ids=[groupId], + security_group_ids=[groupId],# ['sg-cabdebb0'], instance_type=instance_type, placement=placement, - subnet_id=subnet) + subnet_id=subnet, + instance_profile_arn=profile) except boto.exception.EC2ResponseError as e: print("Unable to call bees:", e.message) - print("Is your sec group available in this region?") + print("Is your sec group available in this regionXXX?") return e instances = reservation.instances @@ -233,11 +235,8 @@ def up(count, group, zone, image_id, instance_type, username, key_name, subnet, instance_ids.append(instance.id) print('Bee %s is ready for the attack.' % instance.id) - ec2_connection.create_tags(instance_ids, { "Name": "a bee!" }) - _write_server_list(username, key_name, zone, instances) - print('The swarm has assembled %i bees.' % len(instances)) def report(): @@ -333,7 +332,7 @@ def _sting(params): basic_auth = params['basic_auth'] # Create request - request = Request(url) + request = Request(url, headers=HEADER) # Need to revisit to support all http verbs. if post_file: @@ -371,8 +370,9 @@ def _sting(params): context = ssl._create_unverified_context() response = urlopen(request, context=context) else: + print repr(request) response = urlopen(request) - + response.read() @@ -383,21 +383,21 @@ def _attack(params): Intended for use with multiprocessing. """ print('Bee %i is joining the swarm.' % params['i']) - try: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - pem_path = params.get('key_name') and _get_pem_path(params['key_name']) or None - if not os.path.isfile(pem_path): + if not os.path.isfile(pem_path): client.load_system_host_keys() client.connect(params['instance_name'], username=params['username']) else: + print params['instance_name'] + print params['username'] + print pem_path client.connect( params['instance_name'], username=params['username'], key_filename=pem_path) - print('Bee %i is firing her machine gun. Bang bang!' % params['i']) options = '' @@ -499,6 +499,8 @@ def _attack(params): def _summarize_results(results, params, csv_filename): + print results + print '#######'; summarized_results = dict() summarized_results['timeout_bees'] = [r for r in results if r is None] summarized_results['exception_bees'] = [r for r in results if type(r) == socket.error] @@ -873,7 +875,7 @@ def hurl_attack(url, n, c, **options): print('Stinging URL so it will be cached for the attack.') - request = Request(url) + request = Request(url, headers=HEADER) # Need to revisit to support all http verbs. if post_file: try: diff --git a/beeswithmachineguns/main.py b/beeswithmachineguns/main.py index 329ba4c..8d1e681 100644 --- a/beeswithmachineguns/main.py +++ b/beeswithmachineguns/main.py @@ -85,6 +85,9 @@ def parse_options(): up_group.add_option('-b', '--bid', metavar="BID", nargs=1, action='store', dest='bid', type='float', default=None, help="The maximum bid price per spot instance (default: None).") + up_group.add_option('-x', '--profile', metavar="PROFILE", nargs=1, + action='store', dest='profile', type='string', default=None, + help="The iam instance profile. (default: None).") parser.add_option_group(up_group) @@ -199,11 +202,11 @@ def parse_options(): options.zone, options.instance, options.type,options.login, options.key, options.subnet, - options.bid)).start() + options.bid,options.profile)).start() #time allowed between threads time.sleep(delay) else: - bees.up(options.servers, options.group, options.zone, options.instance, options.type, options.login, options.key, options.subnet, options.bid) + bees.up(options.servers, options.group, options.zone, options.instance, options.type, options.login, options.key, options.subnet, options.bid, options.profile) elif command == 'attack': if not options.url: From a609dfcecde3ff4191fdd1227fd922217dcd7221 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 3 Oct 2016 14:20:37 +0000 Subject: [PATCH 2/4] fix formatting --- beeswithmachineguns/bees.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/beeswithmachineguns/bees.py b/beeswithmachineguns/bees.py index 84d9bed..cdc271c 100644 --- a/beeswithmachineguns/bees.py +++ b/beeswithmachineguns/bees.py @@ -186,7 +186,7 @@ def up(count, group, zone, image_id, instance_type, username, key_name, subnet, instance_type=instance_type, placement=placement, subnet_id=subnet, - instance_profile_arn=profile) + instance_profile_arn=profile) # it can take a few seconds before the spot requests are fully processed time.sleep(5) @@ -201,7 +201,7 @@ def up(count, group, zone, image_id, instance_type, username, key_name, subnet, min_count=count, max_count=count, key_name=key_name, - security_group_ids=[groupId],# ['sg-cabdebb0'], + security_group_ids=[groupId], instance_type=instance_type, placement=placement, subnet_id=subnet, @@ -209,7 +209,7 @@ def up(count, group, zone, image_id, instance_type, username, key_name, subnet, except boto.exception.EC2ResponseError as e: print("Unable to call bees:", e.message) - print("Is your sec group available in this regionXXX?") + print("Is your sec group available in this region?") return e instances = reservation.instances @@ -235,8 +235,11 @@ def up(count, group, zone, image_id, instance_type, username, key_name, subnet, instance_ids.append(instance.id) print('Bee %s is ready for the attack.' % instance.id) + ec2_connection.create_tags(instance_ids, { "Name": "a bee!" }) + _write_server_list(username, key_name, zone, instances) + print('The swarm has assembled %i bees.' % len(instances)) def report(): @@ -372,7 +375,7 @@ def _sting(params): else: print repr(request) response = urlopen(request) - + response.read() @@ -387,17 +390,15 @@ def _attack(params): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) pem_path = params.get('key_name') and _get_pem_path(params['key_name']) or None - if not os.path.isfile(pem_path): + if not os.path.isfile(pem_path): client.load_system_host_keys() client.connect(params['instance_name'], username=params['username']) else: - print params['instance_name'] - print params['username'] - print pem_path client.connect( params['instance_name'], username=params['username'], key_filename=pem_path) + print('Bee %i is firing her machine gun. Bang bang!' % params['i']) options = '' @@ -499,8 +500,6 @@ def _attack(params): def _summarize_results(results, params, csv_filename): - print results - print '#######'; summarized_results = dict() summarized_results['timeout_bees'] = [r for r in results if r is None] summarized_results['exception_bees'] = [r for r in results if type(r) == socket.error] From 3d93f79625c9aca114a0b615b875af37c3961eb3 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 3 Oct 2016 14:43:25 +0000 Subject: [PATCH 3/4] update flag and readme --- README.textile | 2 ++ beeswithmachineguns/main.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.textile b/README.textile index 57ae2a4..8aae06b 100644 --- a/README.textile +++ b/README.textile @@ -217,6 +217,8 @@ Options: launched. (default: None). -b BID, --bid=BID The maximum bid price per spot instance (default: None). + -q PROFILE, --profile=PROFILE + The iam instance profile. (default: None). attack: Beginning an attack requires only that you specify the -u option with diff --git a/beeswithmachineguns/main.py b/beeswithmachineguns/main.py index 8d1e681..5691805 100644 --- a/beeswithmachineguns/main.py +++ b/beeswithmachineguns/main.py @@ -85,7 +85,7 @@ def parse_options(): up_group.add_option('-b', '--bid', metavar="BID", nargs=1, action='store', dest='bid', type='float', default=None, help="The maximum bid price per spot instance (default: None).") - up_group.add_option('-x', '--profile', metavar="PROFILE", nargs=1, + up_group.add_option('-q', '--profile', metavar="PROFILE", nargs=1, action='store', dest='profile', type='string', default=None, help="The iam instance profile. (default: None).") From a6546c925a9d40271c7c4b22f0baeb82bf98f33b Mon Sep 17 00:00:00 2001 From: root Date: Mon, 3 Oct 2016 14:47:28 +0000 Subject: [PATCH 4/4] formatting --- beeswithmachineguns/bees.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/beeswithmachineguns/bees.py b/beeswithmachineguns/bees.py index cdc271c..81466f6 100644 --- a/beeswithmachineguns/bees.py +++ b/beeswithmachineguns/bees.py @@ -205,7 +205,7 @@ def up(count, group, zone, image_id, instance_type, username, key_name, subnet, instance_type=instance_type, placement=placement, subnet_id=subnet, - instance_profile_arn=profile) + instance_profile_arn=profile) except boto.exception.EC2ResponseError as e: print("Unable to call bees:", e.message) @@ -373,7 +373,6 @@ def _sting(params): context = ssl._create_unverified_context() response = urlopen(request, context=context) else: - print repr(request) response = urlopen(request) response.read() @@ -386,9 +385,11 @@ def _attack(params): Intended for use with multiprocessing. """ print('Bee %i is joining the swarm.' % params['i']) + try: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + pem_path = params.get('key_name') and _get_pem_path(params['key_name']) or None if not os.path.isfile(pem_path): client.load_system_host_keys()