예제 #1
0
def manage_instances(request, task, appid, instancerecord):
    response_data = {}

    print 'Request received by manage instances.'

    if task == 'status':
        print 'Request to undertake management task <' + task + '> for application id <' + appid + '>'

        application = ApplicationBasicInfo.objects.get(id=appid)
        instances_openstack = CloudInstancesOpenstack.objects.filter(
            owner=request.user, application=application)
        instances_ec2 = CloudInstancesAWS.objects.filter(
            owner=request.user, application=application)

        if len(instances_openstack) + len(instances_ec2) == 0:
            return HttpResponse(simplejson.dumps(response_data),
                                content_type="application/json")

        # Prepare Openstack connection
        if len(instances_openstack) != 0:
            for instance in instances_openstack:
                print 'Getting updated data for EC2 instance: ' + instance.instance_id
                credentials = instance.credentials
                (access_key,
                 secret_key) = utils.decrypt_cred_pair(credentials.access_key,
                                                       credentials.secret_key)
                parsed_url = urlparse(credentials.endpoint)
                ip = parsed_url.netloc.split(':')[0]
                local_region = boto.ec2.regioninfo.RegionInfo(name="openstack",
                                                              endpoint=ip)
                conn = boto.connect_ec2(aws_access_key_id=access_key,
                                        aws_secret_access_key=secret_key,
                                        is_secure=False,
                                        region=local_region,
                                        port=parsed_url.port,
                                        path=parsed_url.path)
                reservations = conn.get_all_instances(
                    instance_ids=[instance.instance_id])
                if len(reservations) == 0:
                    CloudInstancesOpenstack.objects.filter(
                        instance_id=instance.instance_id).delete()
                else:
                    for reservation in reservations:
                        print 'Reservation ID: ' + reservation.id
                        for instanceOS in reservation.instances:
                            print 'Instance ID: ' + instanceOS.id
                            instance_info = {}
                            instance_info['id'] = instanceOS.id
                            instance_info['state'] = instanceOS.state
                            # Update instance state in DB
                            if instance_info['state'] != instance.status:
                                instance.status = instance_info['state']
                                instance.save()
                                instance_info[
                                    'ip'] = instanceOS.public_dns_name
                            response_data[instanceOS.id] = instance_info

        if len(instances_ec2) != 0:
            for instance in instances_ec2:
                print 'Getting updated data for EC2 instance: ' + instance.instance_id
                credentials = instance.credentials
                (access_key,
                 secret_key) = utils.decrypt_cred_pair(credentials.access_key,
                                                       credentials.secret_key)
                parsed_url = urlparse(credentials.endpoint)
                ip = parsed_url.netloc.split(':')[0]
                local_region = boto.ec2.regioninfo.RegionInfo(name="openstack",
                                                              endpoint=ip)
                conn = boto.connect_ec2(aws_access_key_id=access_key,
                                        aws_secret_access_key=secret_key,
                                        is_secure=False,
                                        region=local_region,
                                        port=parsed_url.port,
                                        path=parsed_url.path)
                reservations = conn.get_all_instances(
                    instance_ids=[instance.instance_id])
                if len(reservations) == 0:
                    CloudInstancesAWS.objects.filter(
                        instance_id=instance.instance_id).delete()
                else:
                    for reservation in reservations:
                        print 'Reservation ID: ' + reservation.id
                        for instanceEC2 in reservation.instances:
                            print 'Instance ID: ' + instanceEC2.id
                            instance_info = {}
                            instance_info['id'] = instanceEC2.id
                            instance_info['state'] = instanceEC2.state
                            instance_info['ip'] = instanceEC2.public_dns_name
                            # Update instance state in DB
                            if instance_info['state'] != instance.status:
                                instance.status = instance_info['state']
                                instance.save()
                                instance_info[
                                    'ip'] = instanceEC2.public_dns_name
                            response_data[instanceEC2.id] = instance_info

    elif task == 'shutdown':
        print 'Request to shutdown instance ' + instancerecord + ' for application: ' + appid

        application = ApplicationBasicInfo.objects.get(id=appid)
        instance = None
        try:
            instance = CloudInstancesOpenstack.objects.get(
                owner=request.user,
                application=application,
                instance_id=instancerecord)
        except CloudInstancesOpenstack.DoesNotExist:
            print 'The OpenStack cloud instance could not be found for ID ' + instancerecord + ' for application: ' + appid + " for the current user."

        if instance == None:
            try:
                instance = CloudInstancesAWS.objects.get(
                    owner=request.user,
                    application=application,
                    instance_id=instancerecord)
            except CloudInstancesOpenstack.DoesNotExist:
                print 'The AWS cloud instance could not be found for ID ' + instancerecord + ' for application: ' + appid + " for the current user."

        if instance != None:
            #print '<' + str(instance.length) + '> matching instances found. Shutting down the first matching instance.'
            print 'Matching instance found. Shutting this instance down.'

            inst_for_shutdown = instance
            # Prepare Openstack/EC2 connection
            credentials = inst_for_shutdown.credentials
            (access_key,
             secret_key) = utils.decrypt_cred_pair(credentials.access_key,
                                                   credentials.secret_key)
            parsed_url = urlparse(credentials.endpoint)
            ip = parsed_url.netloc.split(':')[0]
            local_region = boto.ec2.regioninfo.RegionInfo(name="openstack",
                                                          endpoint=ip)
            conn = boto.connect_ec2(aws_access_key_id=access_key,
                                    aws_secret_access_key=secret_key,
                                    is_secure=False,
                                    region=local_region,
                                    port=parsed_url.port,
                                    path=parsed_url.path)

            terminated_instances = conn.terminate_instances(
                str(instancerecord))
            print "Shutting down " + str(
                len(terminated_instances)) + " instances."

            terminate_instance_result = terminated_instances[0].id

            response_data['result'] = 'OK'
            response_data['terminated'] = terminate_instance_result

        else:
            response_data['result'] = 'ERROR'
            response_data[
                'reason'] = 'Instance not found for application <' + appid + '> and instance ID <' + instancerecord + '>'

    return HttpResponse(simplejson.dumps(response_data),
                        content_type="application/json")
예제 #2
0
def manage_instances(request, task, appid, instancerecord):
    response_data = {}

    print 'Request received by manage instances.'

    if task == 'status':
        print 'Request to undertake management task <' + task + '> for application id <' + appid + '>'

        application = ApplicationBasicInfo.objects.get(id=appid)
        instances_openstack = CloudInstancesOpenstack.objects.filter(owner=request.user, application=application)
        instances_ec2 = CloudInstancesAWS.objects.filter(owner=request.user, application=application)

        if len(instances_openstack) + len(instances_ec2) == 0:
            return HttpResponse(simplejson.dumps(response_data), content_type="application/json")

        # Prepare Openstack connection
        if len(instances_openstack) != 0:
            for instance in instances_openstack:
                print 'Getting updated data for EC2 instance: ' + instance.instance_id
                credentials = instance.credentials
                (access_key, secret_key) = utils.decrypt_cred_pair(credentials.access_key, credentials.secret_key)
                parsed_url = urlparse(credentials.endpoint)
                ip = parsed_url.netloc.split(':')[0]
                local_region = boto.ec2.regioninfo.RegionInfo(name="openstack", endpoint=ip)
                conn = boto.connect_ec2(aws_access_key_id=access_key,aws_secret_access_key=secret_key,is_secure=False,region=local_region,port=parsed_url.port,path=parsed_url.path)
                reservations = conn.get_all_instances(instance_ids=[instance.instance_id])
                if len(reservations) == 0:
                    CloudInstancesOpenstack.objects.filter(instance_id=instance.instance_id).delete()
                else:
                    for reservation in reservations:
                        print 'Reservation ID: ' + reservation.id
                        for instanceOS in reservation.instances:
                            print 'Instance ID: ' + instanceOS.id
                            instance_info = {}
                            instance_info['id'] = instanceOS.id
                            instance_info['state'] = instanceOS.state
                            # Update instance state in DB
                            if instance_info['state'] != instance.status:
                                instance.status = instance_info['state']
                                instance.save()
                                instance_info['ip'] = instanceOS.public_dns_name
                            response_data[instanceOS.id] = instance_info


        if len(instances_ec2) != 0:
            for instance in instances_ec2:
                print 'Getting updated data for EC2 instance: ' + instance.instance_id
                credentials = instance.credentials
                (access_key, secret_key) = utils.decrypt_cred_pair(credentials.access_key, credentials.secret_key)
                parsed_url = urlparse(credentials.endpoint)
                ip = parsed_url.netloc.split(':')[0]
                local_region = boto.ec2.regioninfo.RegionInfo(name="openstack", endpoint=ip)
                conn = boto.connect_ec2(aws_access_key_id=access_key,aws_secret_access_key=secret_key,is_secure=False,region=local_region,port=parsed_url.port,path=parsed_url.path)
                reservations = conn.get_all_instances(instance_ids=[instance.instance_id])
                if len(reservations) == 0:
                    CloudInstancesAWS.objects.filter(instance_id=instance.instance_id).delete()
                else:
                    for reservation in reservations:
                        print 'Reservation ID: ' + reservation.id
                        for instanceEC2 in reservation.instances:
                            print 'Instance ID: ' + instanceEC2.id
                            instance_info = {}
                            instance_info['id'] = instanceEC2.id
                            instance_info['state'] = instanceEC2.state
                            instance_info['ip'] = instanceEC2.public_dns_name
                            # Update instance state in DB
                            if instance_info['state'] != instance.status:
                                instance.status = instance_info['state']
                                instance.save()
                                instance_info['ip'] = instanceEC2.public_dns_name
                            response_data[instanceEC2.id] = instance_info

    elif task == 'shutdown':
        print 'Request to shutdown instance ' + instancerecord + ' for application: ' + appid

        application = ApplicationBasicInfo.objects.get(id=appid)
        instance = None
        try:
            instance = CloudInstancesOpenstack.objects.get(owner=request.user, application=application, instance_id=instancerecord)
        except CloudInstancesOpenstack.DoesNotExist:
            print 'The OpenStack cloud instance could not be found for ID ' + instancerecord + ' for application: ' + appid + " for the current user."

        if instance == None:
            try:
                instance = CloudInstancesAWS.objects.get(owner=request.user, application=application, instance_id=instancerecord)
            except CloudInstancesOpenstack.DoesNotExist:
                print 'The AWS cloud instance could not be found for ID ' + instancerecord + ' for application: ' + appid + " for the current user."

        if instance != None:
            #print '<' + str(instance.length) + '> matching instances found. Shutting down the first matching instance.'
            print 'Matching instance found. Shutting this instance down.'

            inst_for_shutdown = instance
            # Prepare Openstack/EC2 connection
            credentials = inst_for_shutdown.credentials
            (access_key, secret_key) = utils.decrypt_cred_pair(credentials.access_key, credentials.secret_key)
            parsed_url = urlparse(credentials.endpoint)
            ip = parsed_url.netloc.split(':')[0]
            local_region = boto.ec2.regioninfo.RegionInfo(name="openstack", endpoint=ip)
            conn = boto.connect_ec2(aws_access_key_id=access_key,aws_secret_access_key=secret_key,is_secure=False,region=local_region,port=parsed_url.port,path=parsed_url.path)

            terminated_instances = conn.terminate_instances(str(instancerecord))
            print "Shutting down " + str(len(terminated_instances)) + " instances."

            terminate_instance_result = terminated_instances[0].id

            response_data['result'] = 'OK'
            response_data['terminated'] = terminate_instance_result

        else:
            response_data['result'] = 'ERROR'
            response_data['reason'] = 'Instance not found for application <' + appid + '> and instance ID <' + instancerecord + '>'



    return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
예제 #3
0
def start_server(appid, cred, endpoint, cloud, imageRecord, appTag,
                 instance_type, image_info, request):

    contextData = ""
    if appTag != "NONE":
        print "Start server with Group ID <" + appTag + ">"
        (tagname, tagid) = str(appTag).split("-")

        if tagname.lower() == 'vas':
            print "Create  VAS contextualization"
            contextData = "[amiconfig]\nplugins=cernvm\n\n[cernvm]\ncontextualization_key=cfcbde8ad2d4431d8ecc6dd801015252\nliveq_queue_id="
            contextData += tagid
            print "Context Data \n" + contextData
        elif tagname.lower() == 'boinc':
            print "Create Boinc contextualization"
        else:
            pass

    if cred:
        print 'Retrieved credentials successfully.'
    else:
        # TODO: Add full error handling
        print 'ERROR: Unable to retrieve credentials'
        return HttpResponseNotFound
    if image_info:
        print 'Retrieved image info for image <' + image_info.image_id + '> successfully.'
    else:
        # TODO: Add full error handling
        print 'ERROR: Unable to retrieve image data'
        return HttpResponseNotFound()
    (access_key, secret_key) = utils.decrypt_cred_pair(cred.access_key,
                                                       cred.secret_key)
    print 'Access key: ' + access_key
    print 'Secret key: ' + secret_key
    print 'About to connect to region <' + endpoint + '> with access_key <' + str(
        access_key) + '> and secret_key <' + str(secret_key) + '>'
    parsed_url = urlparse(endpoint)
    ip = parsed_url.netloc.split(':')[0]
    local_region = boto.ec2.regioninfo.RegionInfo(name="openstack",
                                                  endpoint=ip)
    conn = boto.connect_ec2(aws_access_key_id=access_key,
                            aws_secret_access_key=secret_key,
                            is_secure=False,
                            region=local_region,
                            port=parsed_url.port,
                            path=parsed_url.path)
    images = conn.get_all_images()
    # Check the list of images to see that we have access to the specified image ID.
    image_data = None
    for image in images:
        if image.id == image_info.image_id:
            image_data = image
    if not image_data:
        # TODO: Add full error handling
        print 'ERROR: Unable to find requested image ID on the selected cloud platform'
        return HttpResponseNotFound()
    else:
        print 'Image is available, about to start image with id <' + image_data.id + '>...'
    response_data = {}

    reservation = conn.run_instances(image_data.id,
                                     1,
                                     1,
                                     key_name=None,
                                     security_groups=None,
                                     user_data=contextData,
                                     addressing_type=None,
                                     instance_type=instance_type,
                                     placement=None)
    print 'Instance pending with reservation ID: ' + reservation.id
    instance_ids = []
    for i in reservation.instances:
        instance_ids.append(i.id)
    app = ApplicationBasicInfo.objects.get(id=appid)
    #appimg = ApplicationOpenstackImages.objects.get(id=imageRecord)
    creds = UserCloudCredentials.objects.get(id=cred.id)
    for id in instance_ids:
        if cloud == 'os':
            instanceRecord = CloudInstancesOpenstack(
                owner=request.user,
                application=app,
                application_image=image_info,
                credentials=creds,
                instance_id=id,
                status='PENDING')
        elif cloud == 'ec2':
            instanceRecord = CloudInstancesAWS(owner=request.user,
                                               application=app,
                                               application_image=image_info,
                                               credentials=creds,
                                               instance_id=id,
                                               status='PENDING')
        else:
            pass
        instanceRecord.save()
    response_data['result'] = 'OK'
    response_data['reservationId'] = reservation.id
    response_data['instanceIds'] = instance_ids
    response_data['instanceRecord'] = instanceRecord.id
    return HttpResponse(simplejson.dumps(response_data),
                        content_type="application/json")
예제 #4
0
def start_server(appid, cred, endpoint, cloud, imageRecord, appTag, instance_type, image_info, request):

    contextData = ""
    if appTag != "NONE":
            print "Start server with Group ID <" + appTag + ">"
            (tagname,tagid) = str(appTag).split("-")

            if tagname.lower() == 'vas':
                print "Create  VAS contextualization"
                contextData ="[amiconfig]\nplugins=cernvm\n\n[cernvm]\ncontextualization_key=cfcbde8ad2d4431d8ecc6dd801015252\nliveq_queue_id="
                contextData += tagid
                print "Context Data \n" + contextData
            elif tagname.lower() == 'boinc':
                print "Create Boinc contextualization"
            else:
                pass

    if cred:
        print 'Retrieved credentials successfully.'
    else:
        # TODO: Add full error handling
        print 'ERROR: Unable to retrieve credentials'
        return HttpResponseNotFound
    if image_info:
        print 'Retrieved image info for image <' + image_info.image_id + '> successfully.'
    else:
        # TODO: Add full error handling
        print 'ERROR: Unable to retrieve image data'
        return HttpResponseNotFound()
    (access_key, secret_key) = utils.decrypt_cred_pair(cred.access_key, cred.secret_key)
    print 'Access key: ' + access_key
    print 'Secret key: ' + secret_key
    print 'About to connect to region <' + endpoint + '> with access_key <' + str(
        access_key) + '> and secret_key <' + str(secret_key) + '>'
    parsed_url = urlparse(endpoint)
    ip = parsed_url.netloc.split(':')[0]
    local_region = boto.ec2.regioninfo.RegionInfo(name="openstack", endpoint=ip)
    conn = boto.connect_ec2(aws_access_key_id=access_key, aws_secret_access_key=secret_key, is_secure=False,
                            region=local_region, port=parsed_url.port, path=parsed_url.path)
    images = conn.get_all_images()
    # Check the list of images to see that we have access to the specified image ID.
    image_data = None
    for image in images:
        if image.id == image_info.image_id:
            image_data = image
    if not image_data:
        # TODO: Add full error handling
        print 'ERROR: Unable to find requested image ID on the selected cloud platform'
        return HttpResponseNotFound()
    else:
        print 'Image is available, about to start image with id <' + image_data.id + '>...'
    response_data = {}

    reservation = conn.run_instances(image_data.id, 1, 1, key_name=None, security_groups=None, user_data=contextData,
                                     addressing_type=None, instance_type=instance_type, placement=None)
    print 'Instance pending with reservation ID: ' + reservation.id
    instance_ids = []
    for i in reservation.instances:
        instance_ids.append(i.id)
    app = ApplicationBasicInfo.objects.get(id=appid)
    #appimg = ApplicationOpenstackImages.objects.get(id=imageRecord)
    creds = UserCloudCredentials.objects.get(id=cred.id)
    for id in instance_ids:
        if cloud == 'os':
            instanceRecord = CloudInstancesOpenstack(owner=request.user,
                                                 application=app,
                                                 application_image=image_info,
                                                 credentials=creds,
                                                 instance_id=id,
                                                 status='PENDING')
        elif cloud == 'ec2':
            instanceRecord = CloudInstancesAWS(owner=request.user,
                                                 application=app,
                                                 application_image=image_info,
                                                 credentials=creds,
                                                 instance_id=id,
                                                 status='PENDING')
        else:
            pass
        instanceRecord.save()
    response_data['result'] = 'OK'
    response_data['reservationId'] = reservation.id
    response_data['instanceIds'] = instance_ids
    response_data['instanceRecord'] = instanceRecord.id
    return HttpResponse(simplejson.dumps(response_data), content_type="application/json")