Beispiel #1
0
def delete(request, topology_id):
    logger.debug('---- topology delete ----')
    topology_prefix = "t%s_" % topology_id

    if configuration.deployment_backend == "kvm":

        network_list = libvirtUtils.get_networks_for_topology(topology_prefix)
        for network in network_list:
            logger.debug("undefine network: " + network["name"])
            libvirtUtils.undefine_network(network["name"])

        domain_list = libvirtUtils.get_domains_for_topology(topology_prefix)
        for domain in domain_list:

            # remove reserved mac addresses for all domains in this topology
            mac_address = libvirtUtils.get_management_interface_mac_for_domain(domain["name"])
            libvirtUtils.release_management_ip_for_mac(mac_address)

            logger.debug("undefine domain: " + domain["name"])
            source_file = libvirtUtils.get_image_for_domain(domain["uuid"])
            if libvirtUtils.undefine_domain(domain["uuid"]):
                if source_file is not None:
                    osUtils.remove_instance(source_file)

        topology = get_object_or_404(Topology, pk=topology_id)

        osUtils.remove_instances_for_topology(topology_prefix)
        osUtils.remove_cloud_init_tmp_dirs(topology_prefix)

    topology.delete()
    messages.info(request, 'Topology %s deleted' % topology.name)
    return HttpResponseRedirect('/topologies/')
Beispiel #2
0
def delete_topology(request):
    """
    DEPRECATED
    :param request:
    :return:
    """

    logger.debug("---- delete_topology ----")
    json_string = request.body
    json_body = json.loads(json_string)

    required_fields = set(['name'])
    if not required_fields.issubset(json_body[0]):
        logger.error("Invalid parameters in json body")
        return HttpResponse(status=500)

    topology_name = json_body[0]["name"]

    try:
        # get the topology by name
        topology = Topology.objects.get(name=topology_name)

    except ObjectDoesNotExist:
        return apiUtils.return_json(False, "Topology is already deleted or does not exist")

    try:
        topology_prefix = "t%s_" % topology.id

        if hasattr(configuration, "use_openvswitch") and configuration.use_openvswitch:
            use_ovs = True
        else:
            use_ovs = False

        network_list = libvirtUtils.get_networks_for_topology(topology_prefix)
        for network in network_list:
            logger.debug("undefining network: " + network["name"])
            libvirtUtils.undefine_network(network["name"])
            if use_ovs:
                ovsUtils.delete_bridge(network["name"])

        domain_list = libvirtUtils.get_domains_for_topology(topology_prefix)
        for domain in domain_list:
            logger.debug("undefining domain: " + domain["name"])
            source_file = libvirtUtils.get_image_for_domain(domain["uuid"])
            if libvirtUtils.undefine_domain(domain["uuid"]):
                if source_file is not None:
                    osUtils.remove_instance(source_file)

            # remove reserved mac addresses for all domains in this topology
            mac_address = libvirtUtils.get_management_interface_mac_for_domain(domain["name"])
            libvirtUtils.release_management_ip_for_mac(mac_address)

        topology.delete()
        return apiUtils.return_json(True, "Topology deleted!")

    except Exception as e:
        logger.error(str(e))
        return HttpResponse(status=500)
Beispiel #3
0
def redeploy_topology(request):
    required_fields = set(['json', 'topologyId'])
    if not required_fields.issubset(request.POST):
        return render(request, 'ajax/ajaxError.html',
                      {'error': "No Topology Id in request"})

    topology_id = request.POST['topologyId']
    j = request.POST['json']
    try:
        topo = Topology.objects.get(pk=topology_id)
        topo.json = j
        topo.save()
    except ObjectDoesNotExist:
        return render(request, 'ajax/ajaxError.html',
                      {'error': "Topology doesn't exist"})

    try:
        domains = libvirtUtils.get_domains_for_topology(topology_id)
        config = wistarUtils.load_config_from_topology_json(
            topo.json, topology_id)

        logger.debug('checking for orphaned domains first')
        # find domains we no longer need
        for d in domains:
            logger.debug('checking domain: %s' % d['name'])
            found = False
            for config_device in config["devices"]:
                if config_device['name'] == d['name']:
                    found = True
                    continue

            if not found:
                logger.info("undefine domain: " + d["name"])
                source_file = libvirtUtils.get_image_for_domain(d["uuid"])
                if libvirtUtils.undefine_domain(d["uuid"]):
                    if source_file is not None:
                        osUtils.remove_instance(source_file)

                    osUtils.remove_cloud_init_seed_dir_for_domain(d['name'])

    except Exception as e:
        logger.debug("Caught Exception in redeploy")
        logger.debug(str(e))
        return render(request, 'ajax/ajaxError.html', {'error': str(e)})

    # forward onto deploy topo
    try:
        inline_deploy_topology(config)
        inventory = wistarUtils.get_topology_inventory(topo)
        wistarUtils.send_new_topology_event(inventory)
    except Exception as e:
        logger.debug("Caught Exception in inline_deploy")
        logger.debug(str(e))
        return render(request, 'ajax/ajaxError.html', {'error': str(e)})

    return refresh_deployment_status(request)
Beispiel #4
0
def delete(request, topology_id):
    logger.debug('---- topology delete ----')
    topology_prefix = "t%s_" % topology_id

    topology = get_object_or_404(Topology, pk=topology_id)

    if configuration.deployment_backend == "kvm":

        if hasattr(configuration,
                   "use_openvswitch") and configuration.use_openvswitch:
            use_ovs = True
        else:
            use_ovs = False

        network_list = libvirtUtils.get_networks_for_topology(topology_prefix)
        for network in network_list:
            logger.debug("undefine network: " + network["name"])
            libvirtUtils.undefine_network(network["name"])

            if use_ovs:
                ovsUtils.delete_bridge(network["name"])

        domain_list = libvirtUtils.get_domains_for_topology(topology_prefix)
        for domain in domain_list:

            # remove reserved mac addresses for all domains in this topology
            mac_address = libvirtUtils.get_management_interface_mac_for_domain(
                domain["name"])
            libvirtUtils.release_management_ip_for_mac(mac_address)

            logger.debug("undefine domain: " + domain["name"])
            source_file = libvirtUtils.get_image_for_domain(domain["uuid"])
            if libvirtUtils.undefine_domain(domain["uuid"]):
                if source_file is not None:
                    osUtils.remove_instance(source_file)

        osUtils.remove_instances_for_topology(topology_prefix)
        osUtils.remove_cloud_init_tmp_dirs(topology_prefix)

    elif configuration.deployment_backend == "openstack":
        stack_name = topology.name.replace(' ', '_')
        if openstackUtils.connect_to_openstack():
            logger.debug(openstackUtils.delete_stack(stack_name))

    topology.delete()
    messages.info(request, 'Topology %s deleted' % topology.name)
    return HttpResponseRedirect('/topologies/')
Beispiel #5
0
def manage_domain(request):
    required_fields = set(['domainId', 'action', 'topologyId'])
    if not required_fields.issubset(request.POST):
        return render(request, 'ajax/ajaxError.html',
                      {'error': "Invalid Parameters in POST"})

    domain_id = request.POST['domainId']
    action = request.POST['action']
    topology_id = request.POST["topologyId"]

    if action == "start":
        # force all networks to be up before we start a topology
        # potential minor optimization here to only start networks attached to domain
        networks = libvirtUtils.get_networks_for_topology(topology_id)
        for network in networks:
            if network["state"] != "running":
                libvirtUtils.start_network(network["name"])

        # prevent start up errors by missing ISO files - i.e cloud-init seed files
        domain = libvirtUtils.get_domain_by_uuid(domain_id)
        iso = libvirtUtils.get_iso_for_domain(domain.name())
        # if we have an ISO file configured, and it doesn't actually exist
        # just remove it completely
        if iso is not None and not osUtils.check_path(iso):
            logger.debug("Removing non-existent ISO from domain")
            libvirtUtils.detach_iso_from_domain(domain.name())

        # now we should be able to safely start the domain
        if libvirtUtils.start_domain(domain_id):
            return refresh_deployment_status(request)
        else:
            return render(request, 'ajax/ajaxError.html',
                          {'error': "Could not start domain!"})

    elif action == "stop":
        if libvirtUtils.stop_domain(domain_id):
            return refresh_deployment_status(request)
        else:
            return render(request, 'ajax/ajaxError.html',
                          {'error': "Could not stop domain!"})

    elif action == "suspend":
        if libvirtUtils.suspend_domain(domain_id):
            return refresh_deployment_status(request)
        else:
            return render(request, 'ajax/ajaxError.html',
                          {'error': "Could not suspend domain!"})

    elif action == "undefine":

        domain = libvirtUtils.get_domain_by_uuid(domain_id)
        domain_name = domain.name()

        source_file = libvirtUtils.get_image_for_domain(domain_id)
        if libvirtUtils.undefine_domain(domain_id):
            if source_file is not None:
                osUtils.remove_instance(source_file)
                osUtils.remove_cloud_init_seed_dir_for_domain(domain_name)

            return refresh_deployment_status(request)
        else:
            return render(request, 'ajax/ajaxError.html',
                          {'error': "Could not stop domain!"})
    else:
        return render(request, 'ajax/ajaxError.html',
                      {'error': "Unknown Parameters in POST!"})