Ejemplo n.º 1
0
def preconfig_linux_domain(request):
    response_data = {"result": True}
    required_fields = set(
        ['domain', 'hostname', 'user', 'password', 'ip', 'mgmtInterface'])
    if not required_fields.issubset(request.POST):
        return render(request, 'ajax/ajaxError.html',
                      {'error': "Invalid Parameters in POST"})

    domain = request.POST['domain']
    user = request.POST["user"]
    password = request.POST['password']
    ip = request.POST['ip']
    mgmt_interface = request.POST['mgmtInterface']
    hostname = request.POST['hostname']

    logger.debug("Configuring linux domain:" + str(domain))
    try:
        response_data["result"] = consoleUtils.preconfig_linux_domain(
            domain, hostname, user, password, ip, mgmt_interface)
        logger.debug(str(response_data))
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json")
    except WistarException as we:
        logger.debug(we)
        response_data["result"] = False
        response_data["message"] = str(we)
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json")
Ejemplo n.º 2
0
def configure_topology(request):
    """
        DEPRECATED
        configures the topology with the correct access information!
        required parameters: topology_name, id of which to clone, cloud_init data
        returns json { "status": "running|unknown|powered off", "topology_id": "0" }

    """
    context = {"status": "unknown"}

    required_fields = set(['topology_name', 'script_id', 'script_data'])
    if not required_fields.issubset(request.POST):
        context["status"] = "unknown"
        context["message"] = "Invalid parameters in POST HERE"
        return HttpResponse(json.dumps(context),
                            content_type="application/json")

    topology_name = request.POST['topology_name']
    script_id = request.POST['script_id']
    script_data = request.POST["script_data"]

    try:
        # get the topology by name
        topo = Topology.objects.get(name=topology_name)
        if apiUtils.get_domain_status_for_topology(topo.id) != "running":
            context["status"] = "unknown"
            context["message"] = "Not all domains are running"
            return HttpResponse(json.dumps(context),
                                content_type="application/json")

        raw_json = json.loads(topo.json)
        for obj in raw_json:
            if "userData" in obj and "wistarVm" in obj["userData"]:
                ip = obj["userData"]["ip"]
                password = obj["userData"]["password"]
                image_type = obj["userData"]["type"]
                mgmt_interface = obj["userData"]["mgmtInterface"]
                hostname = obj["userData"]["label"]

                domain_name = "t%s_%s" % (topo.id, hostname)

                if image_type == "linux":
                    # preconfigure the instance using the console
                    # this will set the management IP, hostname, etc
                    try:
                        consoleUtils.preconfig_linux_domain(
                            domain_name, hostname, password, ip,
                            mgmt_interface)
                        time.sleep(1)

                        # if given a script, let's copy it to the host and run it with the specified script data
                        if script_id != 0:
                            script = Script.objects.get(pk=script_id)
                            # push the
                            linuxUtils.push_remote_script(
                                ip, "root", password, script.script,
                                script.destination)
                            output = linuxUtils.execute_cli(
                                ip, "root", password,
                                script.destination + " " + script_data)
                            logger.debug(output)
                    except Exception as e:
                        logger.debug("Could not configure domain: %s" % e)
                        context["status"] = "unknown"
                        context[
                            "message"] = "Could not configure domain: %s " % e
                        return HttpResponse(json.dumps(context),
                                            content_type="application/json")

                elif image_type == "junos":
                    consoleUtils.preconfig_junos_domain(
                        domain_name, password, ip, mgmt_interface)
                else:
                    logger.debug("Skipping unknown object")

        context["status"] = "configured"
        context["message"] = "All sandbox nodes configured"
        return HttpResponse(json.dumps(context),
                            content_type="application/json")

    except ObjectDoesNotExist:
        context["status"] = "unknown"
        context["message"] = "Sandbox doesn't exist!"
        return HttpResponse(json.dumps(context),
                            content_type="application/json")