<commit_before>from eutester import Eutester
from eucaops_api import Eucaops_api
from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType
import time
import re
import sys

class Eucaops(Eutester,Eucaops_api):
    
    def __init__(self, config_file=None, hostname=None, password=None, keypath=None, credpath=None, aws_access_key_id=None, aws_secret_access_key = None,account="eucalyptus",user="admin", boto_debug=2):
        super(Eucaops, self).__init__(config_file, hostname, password, keypath, credpath, aws_access_key_id, aws_secret_access_key,account, user, boto_debug)
        self.poll_count = 24
        self.test_resources = {}
        self.test_resources["snapshots"] = []
        self.test_resources["instances"] = []
        self.test_resources["keypairs"] = []
        self.test_resources["groups"] = []
        self.test_resources["addresses"] = []
        self.test_resources["buckets"] = []
        self.test_resources["keys"] = []
           
    def create_bucket(self,bucket_name):
        """
        Create a bucket.  If the bucket already exists and you have
        access to it, no error will be returned by AWS.
        Note that bucket names are global to S3
        so you need to choose a unique name.
        """
        # First let's see if we already have a bucket of this name.
        # The lookup method will return a Bucket object if the
        # bucket exists and we have access to it or None.
        bucket = self.walrus.lookup(bucket_name)
        if bucket:
            self.tee( 'Bucket (%s) already exists' % bucket_name )
        else:
                # Let's try to create the bucket.  This will fail if
                # the bucket has already been created by someone else.
            try:
                bucket = self.walrus.create_bucket(bucket_name)
            except self.walrus.provider.storage_create_error, e:
                self.tee( 'Bucket (%s) is owned by another user' % bucket_name )
                return None
            self.my_buckets.append(bucket)
        return bucket
    
    def upload_object_file(self, bucket_name, key_name, path_to_file):
        """
        Write the contents of a local file to walrus and also store custom
        metadata with the object.
        bucket_name   The name of the walrus Bucket.
        key_name      The name of the object containing the data in walrus.
        path_to_file  Fully qualified path to local file.
        """
        bucket = self.s3.lookup(bucket_name)
        # Get a new, blank Key object from the bucket.  This Key object only
        # exists locally until we actually store data in it.
        key = bucket.new_key(key_name)
        key.set_contents_from_filename(path_to_file)
        return key
    
    def add_keypair(self,key_name=None):
        """
        Add a keypair with name key_name unless it already exists
        key_name      The name of the keypair to add and download.
        """
        if key_name==None:
            key_name = "keypair-" + str(int(time.time())) 
        self.tee(  "Looking up keypair " + key_name )
        key = self.ec2.get_all_key_pairs(keynames=[key_name])    
        if key == []:
            self.tee( 'Creating keypair: %s' % key_name)
            # Create an SSH key to use when logging into instances.
            key = self.ec2.create_key_pair(key_name)
            # AWS will store the public key but the private key is
            # generated and returned and needs to be stored locally.
            # The save method will also chmod the file to protect
            # your private key.
            key.save(self.key_dir)
            return key
        else:
            self.tee(  "Key " + key_name + " already exists")
    
    def delete_keypair(self,keypair):
        """
        Delete the keypair object passed in and check that it no longer shows up
        keypair      Keypair object to delete and check
        """
        name = keypair.name
        self.tee(  "Sending delete for keypair: " + name)
        keypair.delete()
        keypair = self.ec2.get_all_key_pairs(keynames=[name])
        if len(keypair) > 0:
            self.fail("Keypair found after attempt to delete it")
        return
    
    def add_group(self, group_name=None ):
        """
        Add a security group to the system with name group_name, if it exists dont create it
        group_name      Name of the security group to create
        """
        if group_name == None:
            group_name = "group-" + str(int(time.time()))
        if self.check_group(group_name):
            self.tee(  "Group " + group_name + " already exists")
            return group_name
        else:
            self.tee( 'Creating Security Group: %s' % group_name)
            # Create a security group to control access to instance via SSH.
            group = self.ec2.create_security_group(group_name, group_name)
            return group
    
    def delete_group(self, group):
        """
        Delete the group object passed in and check that it no longer shows up
        group      Group object to delete and check
        """
        name = group.name
        self.tee( "Sending delete for group: " + name )
        group.delete()
        if self.check_group(name):
            self.fail("Group found after attempt to delete it")
        return
    
    def check_group(self, group_name):
        """
        Check if a group with group_name exists in the system
        group_name      Group name to check for existence
        """
        self.tee( "Looking up group " + group_name )
        group = self.ec2.get_all_security_groups(groupnames=[group_name])
        if group == []:
            return False
        else:
            return True
    
    def authorize_group(self,group_name="default", port=22, protocol="tcp", cidr_ip="0.0.0.0/0"):
        """
        Authorize the group with group_name, 
        group_name      Name of the group to authorize, default="default"
        port            Port to open, default=22
        protocol        Protocol to authorize, default=tcp
        cidr_ip         CIDR subnet to authorize, default="0.0.0.0/0" everything
        """
        try:
            self.tee( "Attempting authorization of group" )
            self.ec2.authorize_security_group_deprecated(group_name,ip_protocol=protocol, from_port=port, to_port=port, cidr_ip=cidr_ip)
        except self.ec2.ResponseError, e:
            if e.code == 'InvalidPermission.Duplicate':
                self.tee( 'Security Group: %s already authorized' % group_name )
            else:
                raise
    
    def wait_for_instance(self,instance, state="running"):
        """
        Wait for the instance to enter the state
        instance      Boto instance object to check the state on
        state        state that we are looking for
        """
        poll_count = self.poll_count
        self.tee( "Beginning poll loop for instance " + str(instance) + " to go to " + state )
        instance.update()
        instance_original_state = instance.state
        ### If the instance changes state or goes to the desired state before my poll count is complete
        while poll_count > 0:
            poll_count -= 1
            time.sleep(10)
            instance.update()
            if (instance.state == state) or (instance.state != instance_original_state):
                break
        self.tee( "Waited a total of " + str( (self.poll_count - poll_count) * 10 ) + " seconds" )
        if instance.state != state:
                self.fail(str(instance) + " did not enter the proper state and was left in " + instance.state)
        self.tee( str(instance) + ' is now in ' + instance.state )

    def wait_for_reservation(self,reservation, state="running"):
        """
        Wait for the an entire reservation to enter the state
        reservation  Boto reservation object to check the state on
        state        state that we are looking for
        """
        self.tee( "Beginning poll loop for the " + str(len(reservation.instances))   + " found in " + str(reservation) )
        for instance in reservation.instances:
            self.wait_for_instance(instance, state)
    <commit_msg>Change wait_for_instance logic<commit_after>from eutester import Eutester
from eucaops_api import Eucaops_api
from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType
import time
import re
import sys

class Eucaops(Eutester,Eucaops_api):
    
    def __init__(self, config_file=None, hostname=None, password=None, keypath=None, credpath=None, aws_access_key_id=None, aws_secret_access_key = None,account="eucalyptus",user="admin", boto_debug=2):
        super(Eucaops, self).__init__(config_file, hostname, password, keypath, credpath, aws_access_key_id, aws_secret_access_key,account, user, boto_debug)
        self.poll_count = 24
        self.test_resources = {}
        self.test_resources["snapshots"] = []
        self.test_resources["instances"] = []
        self.test_resources["keypairs"] = []
        self.test_resources["groups"] = []
        self.test_resources["addresses"] = []
        self.test_resources["buckets"] = []
        self.test_resources["keys"] = []
           
    def create_bucket(self,bucket_name):
        """
        Create a bucket.  If the bucket already exists and you have
        access to it, no error will be returned by AWS.
        Note that bucket names are global to S3
        so you need to choose a unique name.
        """
        # First let's see if we already have a bucket of this name.
        # The lookup method will return a Bucket object if the
        # bucket exists and we have access to it or None.
        bucket = self.walrus.lookup(bucket_name)
        if bucket:
            self.tee( 'Bucket (%s) already exists' % bucket_name )
        else:
                # Let's try to create the bucket.  This will fail if
                # the bucket has already been created by someone else.
            try:
                bucket = self.walrus.create_bucket(bucket_name)
            except self.walrus.provider.storage_create_error, e:
                self.tee( 'Bucket (%s) is owned by another user' % bucket_name )
                return None
            self.my_buckets.append(bucket)
        return bucket
    
    def upload_object_file(self, bucket_name, key_name, path_to_file):
        """
        Write the contents of a local file to walrus and also store custom
        metadata with the object.
        bucket_name   The name of the walrus Bucket.
        key_name      The name of the object containing the data in walrus.
        path_to_file  Fully qualified path to local file.
        """
        bucket = self.s3.lookup(bucket_name)
        # Get a new, blank Key object from the bucket.  This Key object only
        # exists locally until we actually store data in it.
        key = bucket.new_key(key_name)
        key.set_contents_from_filename(path_to_file)
        return key
    
    def add_keypair(self,key_name=None):
        """
        Add a keypair with name key_name unless it already exists
        key_name      The name of the keypair to add and download.
        """
        if key_name==None:
            key_name = "keypair-" + str(int(time.time())) 
        self.tee(  "Looking up keypair " + key_name )
        key = self.ec2.get_all_key_pairs(keynames=[key_name])    
        if key == []:
            self.tee( 'Creating keypair: %s' % key_name)
            # Create an SSH key to use when logging into instances.
            key = self.ec2.create_key_pair(key_name)
            # AWS will store the public key but the private key is
            # generated and returned and needs to be stored locally.
            # The save method will also chmod the file to protect
            # your private key.
            key.save(self.key_dir)
            return key
        else:
            self.tee(  "Key " + key_name + " already exists")
    
    def delete_keypair(self,keypair):
        """
        Delete the keypair object passed in and check that it no longer shows up
        keypair      Keypair object to delete and check
        """
        name = keypair.name
        self.tee(  "Sending delete for keypair: " + name)
        keypair.delete()
        keypair = self.ec2.get_all_key_pairs(keynames=[name])
        if len(keypair) > 0:
            self.fail("Keypair found after attempt to delete it")
        return
    
    def add_group(self, group_name=None ):
        """
        Add a security group to the system with name group_name, if it exists dont create it
        group_name      Name of the security group to create
        """
        if group_name == None:
            group_name = "group-" + str(int(time.time()))
        if self.check_group(group_name):
            self.tee(  "Group " + group_name + " already exists")
            return group_name
        else:
            self.tee( 'Creating Security Group: %s' % group_name)
            # Create a security group to control access to instance via SSH.
            group = self.ec2.create_security_group(group_name, group_name)
            return group
    
    def delete_group(self, group):
        """
        Delete the group object passed in and check that it no longer shows up
        group      Group object to delete and check
        """
        name = group.name
        self.tee( "Sending delete for group: " + name )
        group.delete()
        if self.check_group(name):
            self.fail("Group found after attempt to delete it")
        return
    
    def check_group(self, group_name):
        """
        Check if a group with group_name exists in the system
        group_name      Group name to check for existence
        """
        self.tee( "Looking up group " + group_name )
        group = self.ec2.get_all_security_groups(groupnames=[group_name])
        if group == []:
            return False
        else:
            return True
    
    def authorize_group(self,group_name="default", port=22, protocol="tcp", cidr_ip="0.0.0.0/0"):
        """
        Authorize the group with group_name, 
        group_name      Name of the group to authorize, default="default"
        port            Port to open, default=22
        protocol        Protocol to authorize, default=tcp
        cidr_ip         CIDR subnet to authorize, default="0.0.0.0/0" everything
        """
        try:
            self.tee( "Attempting authorization of group" )
            self.ec2.authorize_security_group_deprecated(group_name,ip_protocol=protocol, from_port=port, to_port=port, cidr_ip=cidr_ip)
        except self.ec2.ResponseError, e:
            if e.code == 'InvalidPermission.Duplicate':
                self.tee( 'Security Group: %s already authorized' % group_name )
            else:
                raise
    
    def wait_for_instance(self,instance, state="running"):
        """
        Wait for the instance to enter the state
        instance      Boto instance object to check the state on
        state        state that we are looking for
        """
        poll_count = self.poll_count
        self.tee( "Beginning poll loop for instance " + str(instance) + " to go to " + state )
        instance.update()
        instance_original_state = instance.state
        ### If the instance changes state or goes to the desired state before my poll count is complete
        while poll_count > 0:
            poll_count -= 1
            time.sleep(10)
            instance.update()
            if (instance.state != instance_original_state):
                break
        self.tee( "Waited a total of " + str( (self.poll_count - poll_count) * 10 ) + " seconds" )
        if instance.state != state:
                self.fail(str(instance) + " did not enter the proper state and was left in " + instance.state)
        self.tee( str(instance) + ' is now in ' + instance.state )

    def wait_for_reservation(self,reservation, state="running"):
        """
        Wait for the an entire reservation to enter the state
        reservation  Boto reservation object to check the state on
        state        state that we are looking for
        """
        self.tee( "Beginning poll loop for the " + str(len(reservation.instances))   + " found in " + str(reservation) )
        for instance in reservation.instances:
            self.wait_for_instance(instance, state)
    
{'fraction_non_alphanumeric': 0.08085547160614184, 'fraction_numerical': 0.004143309773336583, 'mean_word_length': 2.1120591581342434, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '467257', 'n_tokens_mistral': 4599, 'n_tokens_neox': 4482, 'n_words': 1481}