Esempio n. 1
0
def remove_bond():
    try:
        os.system('clear')
        interfaces, err = networking.get_interfaces()
        if err:
            raise Exception('Error retrieving interface information : %s' %
                            err)
        if not interfaces:
            raise Exception('No interfaces detected')

        print
        print
        print 'IntegralSTOR NIC Bonding'
        print '---------------------------------'
        print
        print
        print 'Active bond(s): \n'

        bm, err = networking.get_bonding_masters()
        if err:
            raise Exception(err)
        bid, err = networking.get_bonding_info_all()
        if err:
            raise Exception(err)

        avail_if = []
        for if_name, iface in interfaces.items():
            if if_name in bm:
                print '\t- %s' % if_name
                avail_if.append(if_name)
        print "\n"
        if not avail_if:
            raise Exception('There is nothing to remove!')

        bond_name = None
        is_name = False
        while is_name is False:
            bond_name = raw_input('To remove a bond, provide its name: ')
            if bond_name not in avail_if:
                print "\t- Can't remove %s, no such bond exists. Please provide another one.\n" % bond_name
            else:
                is_name = True

        ret, err = networking.delete_bond(bond_name)
        if not ret:
            if err:
                raise Exception('Error removing bond: %s' % err)
            else:
                raise Exception("Couldn't remove bond")
        if ret:
            print "\n\tBond removed!\n"

        print
        print 'Regenerating manifest and status.'
        python_scripts_path, err = config.get_python_scripts_path()
        if err:
            raise Exception(err)
        common_python_scripts_path, err = config.get_common_python_scripts_path(
        )
        if err:
            raise Exception(err)
        status_path, err = config.get_system_status_path()
        if err:
            raise Exception(err)

        ret, err = command.get_command_output(
            "python %s/generate_manifest.py %s" %
            (common_python_scripts_path, status_path))
        if err:
            raise Exception(err)
        ret, err = command.get_command_output(
            "python %s/generate_status.py %s" %
            (common_python_scripts_path, status_path))
        if err:
            raise Exception(err)
        print 'Regenerating manifest and status... Done'
        print
    except Exception, e:
        print "Error: %s" % e
        print
        return -1
def create_remote_replication(request):
    return_dict = {}
    try:

        if request.method == "GET":
            datasets = []
            pools, err = zfs.get_all_datasets_and_pools()
            if err:
                raise Exception(err)
            for pool in pools:
                if "/" in pool:
                    datasets.append(pool)
            return_dict["datasets"] = datasets
            return django.shortcuts.render_to_response(
                'update_remote_replication.html',
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))

        elif request.method == "POST":
            source_dataset = request.POST.get('source_dataset')
            scheduler = request.POST.get('scheduler')
            schedule = scheduler.split()
            destination_ip = request.POST.get('destination_ip')
            destination_pool = request.POST.get('destination_pool')
            destination_username = "******"

            if (not destination_ip) or (not destination_pool) or (
                    not source_dataset):
                raise Exception("Incomplete request.")

            existing_repl, err = remote_replication.get_remote_replications_with(
                source_dataset, destination_ip, destination_pool)
            print existing_repl
            if err:
                raise Exception(err)
            if existing_repl:
                raise Exception(
                    "A replication schedule already exists with matching entires/options."
                )

            py_scripts_path, err = config.get_python_scripts_path()
            if err:
                raise Exception(err)

            cmd = '%s/add_remote_replication_task.py %s %s %s %s' % (
                py_scripts_path, source_dataset, destination_ip,
                destination_username, destination_pool)
            description = 'Replication of %s to pool %s on machine %s' % (
                source_dataset, destination_pool, destination_ip)
            cron_task_id, err = scheduler_utils.create_cron_task(
                cmd, description, schedule[0], schedule[1], schedule[2],
                schedule[3], schedule[4])
            if err:
                raise Exception(err)

            remote_replication_id, err = remote_replication.add_remote_replication(
                source_dataset, destination_ip, destination_username,
                destination_pool, cron_task_id)
            if err:
                raise Exception(err)

            crons, err = scheduler_utils.get_cron_tasks(cron_task_id)
            if err:
                raise Exception(err)
            description += ' Scheduled for %s' % crons[0][
                'schedule_description']

            audit.audit("create_remote_replication", description, request)

            return django.http.HttpResponseRedirect(
                '/view_remote_replications?ack=created')
    except Exception as e:
        return_dict['base_template'] = "snapshot_replication_base.html"
        return_dict["page_title"] = 'Configure ZFS replication'
        return_dict['tab'] = 'view_remote_replications_tab'
        return_dict["error"] = 'Error configuring ZFS replication'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def update_remote_replication(request):
    return_dict = {}
    try:

        ret, err = django_utils.get_request_parameter_values(
            request, ['remote_replication_id'])
        if err:
            raise Exception(err)
        if 'remote_replication_id' not in ret:
            raise Exception(
                "Requested remote replication not found, please use the menus."
            )
        remote_replication_id = ret['remote_replication_id']
        replications, err = remote_replication.get_remote_replications(
            remote_replication_id)
        if err:
            raise Exception(err)
        if not replications:
            raise Exception('Specified replication definition not found')

        if request.method == "GET":
            return_dict['replication'] = replications[0]
            return django.shortcuts.render_to_response(
                'update_remote_replication.html',
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        elif request.method == "POST":
            if 'scheduler' not in request.POST:
                raise Exception("Incomplete request.")
            scheduler = request.POST.get('scheduler')
            schedule = scheduler.split()

            replication = replications[0]

            description = 'Replication of %s to pool %s on machine %s' % (
                replication['source_dataset'], replication['destination_pool'],
                replication['destination_ip'])

            py_scripts_path, err = config.get_python_scripts_path()
            if err:
                raise Exception(err)

            cmd = '%s/add_remote_replication_task.py %s %s %s %s' % (
                py_scripts_path, replications[0]['source_dataset'],
                replications[0]['destination_ip'],
                replications[0]['destination_user_name'],
                replications[0]['destination_pool'])
            # print cmd
            new_cron_task_id, err = scheduler_utils.create_cron_task(
                cmd, description, schedule[0], schedule[1], schedule[2],
                schedule[3], schedule[4])
            if err:
                raise Exception(err)
            ret, err = remote_replication.update_remote_replication(
                replications[0]['remote_replication_id'], new_cron_task_id)
            if err:
                raise Exception(err)

            cron_remove, err = scheduler_utils.delete_cron(
                int(replication['cron_task_id']))
            if err:
                raise Exception(err)
            crons, err = scheduler_utils.get_cron_tasks(new_cron_task_id)
            if err:
                raise Exception(err)
            description += ' Scheduled for %s' % crons[0][
                'schedule_description']

            audit.audit("modify_remote_replication", description, request)
            return django.http.HttpResponseRedirect(
                '/view_remote_replications?ack=updated')
    except Exception as e:
        return_dict['base_template'] = "snapshot_replication_base.html"
        return_dict["page_title"] = 'Configure ZFS replication'
        return_dict['tab'] = 'view_remote_replications_tab'
        return_dict["error"] = 'Error configuring ZFS replication'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def update_hostname(request):
    return_dict = {}
    try:

        hostname = socket.gethostname()
        if request.method == "GET":
            hostname, err = networking.get_hostname()
            if err:
                raise Exception(err)
            domain_name, err = networking.get_domain_name()
            if err:
                raise Exception(err)

            initial = {}
            initial['hostname'] = hostname
            initial['domain_name'] = domain_name

            form = networking_forms.EditHostnameForm(initial=initial)
            return_dict['form'] = form
            return django.shortcuts.render_to_response(
                "update_hostname.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        else:
            form = networking_forms.EditHostnameForm(request.POST)
            return_dict['form'] = form
            if not form.is_valid():
                return django.shortcuts.render_to_response(
                    "update_hostname.html",
                    return_dict,
                    context_instance=django.template.context.RequestContext(
                        request))
            cd = form.cleaned_data
            result_str = ""
            domain_name = None
            if 'domain_name' in cd:
                domain_name = cd['domain_name']
            result, err = networking.update_hostname(cd['hostname'],
                                                     domain_name)
            if not result:
                if err:
                    raise Exception(err)
                else:
                    raise Exception('Error setting hostname')
            result, err = networking.update_domain_name(domain_name)
            if not result:
                if err:
                    raise Exception(err)
                else:
                    raise Exception('Error setting domain name')
            python_scripts_path, err = config.get_python_scripts_path()
            if err:
                raise Exception(err)
            common_python_scripts_path, err = config.get_common_python_scripts_path(
            )
            if err:
                raise Exception(err)
            ss_path, err = config.get_system_status_path()
            if err:
                raise Exception(err)

            ret, err = command.get_command_output(
                "python %s/generate_manifest.py %s" %
                (common_python_scripts_path, ss_path))
            if err:
                raise Exception(err)

            ret, err = command.get_command_output(
                "python %s/generate_status.py %s" %
                (common_python_scripts_path, ss_path))

            audit_str = "Hostname set to %s." % cd['hostname']
            if 'domain_name' in cd:
                audit_str += 'Domain name set to %s' % cd['domain_name']
            ret, err = audit.audit("edit_hostname", audit_str, request)
            if err:
                raise Exception(err)

            return django.http.HttpResponseRedirect(
                '/view_hostname?result=saved')
    except Exception, e:
        return_dict['base_template'] = "networking_base.html"
        return_dict["page_title"] = 'Modify system hostname'
        return_dict['tab'] = 'view_hostname_tab'
        return_dict["error"] = 'Error modifying system hostname'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def delete_bond(request):

    return_dict = {}
    try:
        req_ret, err = django_utils.get_request_parameter_values(
            request, ['name'])
        if err:
            raise Exception(err)
        if 'name' not in req_ret:
            raise Exception('Invalid request, please use the menus')
        name = req_ret['name']
        return_dict["name"] = name

        if request.method == "GET":
            # Return the conf page
            return django.shortcuts.render_to_response(
                "delete_bond_conf.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        else:
            result, err = networking.delete_bond(name)
            if not result:
                if not err:
                    raise Exception("Error removing bond")
                else:
                    raise Exception(err)

            python_scripts_path, err = config.get_python_scripts_path()
            if err:
                raise Exception(err)
            common_python_scripts_path, err = config.get_common_python_scripts_path(
            )
            if err:
                raise Exception(err)
            status_path, err = config.get_system_status_path()
            if err:
                raise Exception(err)

            ret, err = command.get_command_output(
                "python %s/generate_manifest.py %s" %
                (common_python_scripts_path, status_path))
            if err:
                raise Exception(err)

            ret, err = command.get_command_output(
                "python %s/generate_status.py %s" %
                (common_python_scripts_path, status_path))
            if err:
                raise Exception(err)

            audit_str = "Removed network bond %s" % (name)
            audit.audit("remove_bond", audit_str, request)
            return django.http.HttpResponseRedirect(
                '/view_interfaces?ack=removed_bond')
    except Exception, e:
        return_dict['base_template'] = "networking_base.html"
        return_dict["page_title"] = 'Remove a network interface bond'
        return_dict['tab'] = 'view_interfaces_tab'
        return_dict["error"] = 'Error removing a network interface bond'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))
def create_bond(request):
    return_dict = {}
    try:

        interfaces, err = networking.get_interfaces()
        if err:
            raise Exception(err)
        if not interfaces:
            raise Exception(
                "Error loading network interface information : No interfaces found"
            )

        bm, err = networking.get_bonding_masters()
        if err:
            raise Exception(err)
        bid, err = networking.get_bonding_info_all()
        if err:
            raise Exception(err)

        return_dict['interfaces'] = interfaces
        iface_list = []
        existing_bonds = []
        for if_name, iface in interfaces.items():
            if if_name.startswith('lo') or if_name in bid['by_slave']:
                continue
            if if_name in bm:
                existing_bonds.append(if_name)
                continue
            iface_list.append(if_name)

        return_dict['is_iface_avail'] = False
        if iface_list:
            return_dict['is_iface_avail'] = True

        if request.method == "GET":
            form = networking_forms.CreateBondForm(
                interfaces=iface_list, existing_bonds=existing_bonds)
            return_dict['form'] = form
            return django.shortcuts.render_to_response(
                "create_bond.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        else:
            form = networking_forms.CreateBondForm(
                request.POST,
                interfaces=iface_list,
                existing_bonds=existing_bonds)
            return_dict['form'] = form
            if not form.is_valid():
                return django.shortcuts.render_to_response(
                    "create_bond.html",
                    return_dict,
                    context_instance=django.template.context.RequestContext(
                        request))
            cd = form.cleaned_data
            print cd
            result, err = networking.create_bond(cd['name'], cd['slaves'],
                                                 int(cd['mode']))
            if not result:
                if err:
                    raise Exception(err)
                else:
                    raise Exception('Bond creation failed!')
            python_scripts_path, err = config.get_python_scripts_path()
            if err:
                raise Exception(err)
            common_python_scripts_path, err = config.get_common_python_scripts_path(
            )
            if err:
                raise Exception(err)
            status_path, err = config.get_system_status_path()
            if err:
                raise Exception(err)

            ret, err = command.get_command_output(
                "python %s/generate_manifest.py %s" %
                (common_python_scripts_path, status_path))
            if err:
                raise Exception(err)

            ret, err = command.get_command_output(
                "python %s/generate_status.py %s" %
                (common_python_scripts_path, status_path))
            if err:
                raise Exception(err)

            audit_str = "Created a network bond named %s with slaves %s" % (
                cd['name'], ','.join(cd['slaves']))
            audit.audit("create_bond", audit_str, request)
            return django.http.HttpResponseRedirect(
                '/view_interfaces?ack=created_bond')
    except Exception, e:
        return_dict['base_template'] = "networking_base.html"
        return_dict["page_title"] = 'Create a network interface bond'
        return_dict['tab'] = 'view_interfaces_tab'
        return_dict["error"] = 'Error creating a network interface bond'
        return_dict["error_details"] = str(e)
        return django.shortcuts.render_to_response(
            "logged_in_error.html",
            return_dict,
            context_instance=django.template.context.RequestContext(request))