Beispiel #1
0
def launch(request):
    """Configure and launch CloudBioLinux and CloudMan servers.
    """
    if request.method == "POST":
        form = forms.CloudManForm(request.POST)
        if form.is_valid():
            request.session["ec2data"] = form.cleaned_data
            request.session["ec2data"]['cloud_name'] = form.cleaned_data[
                'cloud'].name
            request.session["ec2data"]['cloud_type'] = form.cleaned_data[
                'cloud'].cloud_type
            # Temp code (Jan 2013) - until Queensland Cloud is operational allow only
            # test account to launch instances
            if request.session["ec2data"]['cloud_name'] == 'Qld Test Cloud' and \
                form.cleaned_data.get('access_key', '') != 'b2b2057ab3644f508ff6ff20d914f74e':
                response = {'error': "*You cannot use {0} cloud yet; it is available only for " \
                    "testing at the moment.*".format(request.session["ec2data"]['cloud_name'])}
            else:
                response = runinstance(request)
            if not response['error']:
                return redirect("/monitor")
            else:
                form.non_field_errors = "A problem starting your instance. "\
                                        "Check the {0} cloud's console: {1}"\
                                        .format(form.cleaned_data['cloud'].name,
                                                response['error'])
    else:
        # Select the first item in the clouds dropdown, thus potentially eliminating
        # that click for the most commonly used cloud. This does assume the most used
        # cloud is the first in the DB and that such an entry exists in the first place
        form = forms.CloudManForm(initial={'cloud': 1})
    return render(request,
                  "launch.html", {"form": form},
                  context_instance=RequestContext(request))
Beispiel #2
0
def launch(request):
    """
    Initiate launching of an instance. Given an empty request, render the
    ``launch`` page. Given a ``POST`` request, initiate a background task to
    launch an instance and return JSON with the task ID and ``ready`` status
    attribute set to ``False``. Following a POST request, also store data into
    the session, under `ec2data` key.
    """
    if request.method == "POST":
        data = {'task_id': '', 'ready': False, 'error': '', 'form_errors': ''}

        form = forms.CloudManForm(data=request.POST)

        if form.is_valid() and request.is_ajax:
            request.session["ec2data"] = form.cleaned_data
            request.session["ec2data"]['cloud_name'] = form.cleaned_data[
                'cloud'].name
            request.session["ec2data"]['cloud_type'] = form.cleaned_data[
                'cloud'].cloud_type

            # Initiate a background task now
            form = request.session["ec2data"]
            r = tasks.run_instance.delay(form)
            data['task_id'] = r.id
            request.session['ec2data']['task_id'] = data['task_id']
        else:
            # Make sure form errors are captured and propagaed back
            data['form_errors'] = [(k, [unicode(e) for e in v])
                                   for k, v in form.errors.items()]

        return HttpResponse(simplejson.dumps(data),
                            mimetype="application/json")

    else:
        # Select the first item in the clouds dropdown, thus potentially eliminating
        # that click for the most commonly used cloud. This does assume the most used
        # cloud is the first in the DB and that such an entry exists in the first place
        form = forms.CloudManForm(initial={'cloud': 1})

    # Include a user notice on the page if defined in the settings
    notice = None
    if hasattr(settings, 'NOTICE'):
        notice = settings.NOTICE

    return render(request,
                  "launch.html", {
                      "form": form,
                      'brand': brand,
                      'notice': notice
                  },
                  context_instance=RequestContext(request))
def launch(request):
    """Configure and launch CloudBioLinux and CloudMan servers.
    """
    if request.method == "POST":
        form = forms.CloudManForm(request.POST)
        if form.is_valid():
            print form.cleaned_data
            ec2_error = None
            try:
                # Create security group & key pair used when starting an instance
                ec2_conn = connect_ec2(form.cleaned_data['access_key'],
                                       form.cleaned_data['secret_key'],
                                       form.cleaned_data['cloud'])
                sg_name = create_cm_security_group(ec2_conn,
                                                   form.cleaned_data['cloud'])
                kp_name, kp_material = create_key_pair(ec2_conn)
            except EC2ResponseError, err:
                ec2_error = err.error_message
            # associate form data with session for starting instance
            # and supplying download files
            if ec2_error is None:
                form.cleaned_data["kp_name"] = kp_name
                form.cleaned_data["kp_material"] = kp_material
                form.cleaned_data["sg_name"] = sg_name
                form.cleaned_data["cloud_type"] = form.cleaned_data[
                    'cloud'].cloud_type
                form.cleaned_data["cloud_name"] = form.cleaned_data[
                    'cloud'].name
                request.session["ec2data"] = form.cleaned_data
                if runinstance(request):
                    return redirect("/monitor")
                else:
                    form.non_field_errors = "A problem starting your instance. " \
                                            "Check the {0} cloud's console."\
                                            .format(form.cleaned_data['cloud'].name)
            else:
                form.non_field_errors = ec2_error
                form.cleaned_data["cloud_name"] = form.cleaned_data[
                    'cloud'].name
                request.session["ec2data"] = form.cleaned_data
                if runinstance(request):
                    return redirect("/monitor")
                else:
                    form.non_field_errors = "A problem starting your instance. " \
                                            "Check the {0} cloud's console."\
                                            .format(form.cleaned_data['cloud'].name)
            else:
                form.non_field_errors = ec2_error
    else:
        # Select the first item in the clouds dropdown, thus potentially eliminating
        # that click for the most commonly used cloud. This does assume the most used
        # cloud is the first in the DB and that such an entry exists in the first place
        form = forms.CloudManForm(initial={'cloud': 1})
    return render(request,
                  "launch.html", {"form": form},
                  context_instance=RequestContext(request))


def monitor(request):
    """Monitor a launch request and return offline files for console re-runs.
    """
    return render(request,
                  "monitor.html",
                  context_instance=RequestContext(request))


def runinstance(request):
    """Run a CloudBioLinux/CloudMan instance with current session credentials.