'Create a new EC2 keypair and optionally save to output_file
Returns boto.ec2.keypair.KeyPair'

def create_keypair(self, name, output_file=None):

    if output_file:
        output_dir = os.path.dirname(output_file)
        if (output_dir and (not os.path.exists(output_dir))):
            raise exception.BaseException('output    directory    does    not    exist')
        if os.path.exists(output_file):
            raise exception.BaseException(('cannot    save    keypair    %s:    file    already    exists' % output_file))
    try:
        kp = self.conn.create_key_pair(name)
    except boto.exception.EC2ResponseError as e:
        if (e.error_code == 'InvalidKeyPair.Duplicate'):
            raise exception.KeyPairAlreadyExists(name)
        raise
    if output_file:
        try:
            kfile = open(output_file, 'wb')
            kfile.write(kp.material)
            kfile.close()
            os.chmod(output_file, 256)
        except IOError as e:
            raise exception.BaseException(str(e))
    return kp
