Exemple #1
0
def task_management():
    if 'compare' in request.form:
        n1, n2 = request.form['first_node'], request.form['second_node']
        t1, t2 = request.form['first_version'], request.form['second_version']
        task = get_obj(db, Task, name=request.form['task_name'])
        # get the two versions of the logs
        v1 = str_dict(task.logs[t1][n1]).splitlines()
        v2 = str_dict(task.logs[t2][n2]).splitlines()
        print(v1, v2)
        return render_template(
            'compare.html',
            ndiff='\n'.join(ndiff(v1, v2)),
            unified_diff='\n'.join(unified_diff(v1, v2)),
        )
    tasks = Task.query.all()
    form_per_task = {}
    for task in filter(lambda t: t.recurrent, tasks):
        form = CompareForm(request.form)
        form.first_node.choices = [(i, i) for (i, _, _, _) in task.nodes]
        form.second_node.choices = form.first_node.choices
        versions = [(v, v) for v in tuple(task.logs)]
        form.first_version.choices = form.second_version.choices = versions
        form_per_task[task.name] = form
    # task.logs is a dictionnary and we need to keep that way, but for properly
    # outputtng it's content, we will use str_dict
    log_per_task = {task.name: str_dict(task.logs) for task in tasks}
    return render_template('task_management.html',
                           tasks=tasks,
                           log_per_task=log_per_task,
                           form_per_task=form_per_task)
Exemple #2
0
def get_targets(nodes):
    targets = []
    for name in nodes:
        obj = get_obj(db, Node, name=name)
        targets.append((name, obj.ip_address, obj.operating_system.lower(),
                        obj.secret_password))
    return targets
Exemple #3
0
def napalm_configuration():
    form = NapalmConfigurationForm(request.form)
    # update the list of available users / nodes by querying the database
    form.nodes.choices = Node.choices()
    if 'send' in request.form:
        # if user does not select file, browser also
        # submit a empty part without filename
        filename = request.files['file'].filename
        # retrieve the raw script: we will use it as-is or update it depending
        # on whether a Jinja2 template was uploaded by the user or not
        raw_script = request.form['raw_script']
        if 'file' in request.files and filename:
            if allowed_file(filename, 'netmiko'):
                filename = secure_filename(filename)
                filepath = join(app.config['UPLOAD_FOLDER'], filename)
                with open(filepath, 'r') as f:
                    parameters = load(f)
                template = Template(raw_script)
                script = template.render(**parameters)
            else:
                flash('file {}: format not allowed'.format(filename))
        else:
            script = raw_script
        nodes_info = []
        for name in form.data['nodes']:
            obj = get_obj(current_app, Node, name=name)
            nodes_info.append((obj.ip_address, obj.operating_system.lower()))
        napalm_task = NapalmConfigTask(script, current_user, nodes_info,
                                       **form.data)
        current_app.database.session.add(napalm_task)
        current_app.database.session.commit()
        return redirect(url_for('scheduling_blueprint.task_management'))
    return _render_template('napalm_configuration.html', form=form)
Exemple #4
0
def get_targets(nodes):
    targets = []
    for id in nodes:
        obj = get_obj(db, Node, id=int(id))
        targets.append((obj.name, obj.ip_address, obj.operating_system.lower(),
                        obj.secret_password))
    return targets
Exemple #5
0
def napalm_getters():
    form = NapalmGettersForm(request.form)
    # update the list of available users / nodes by querying the database
    form.nodes.choices = Node.choices()
    if 'query' in request.form:
        nodes_info = []
        for name in form.data['nodes']:
            obj = get_obj(current_app, Node, name=name)
            nodes_info.append((obj.ip_address, obj.operating_system.lower()))
        napalm_task = NapalmGettersTask(current_user, nodes_info, **form.data)
        current_app.database.session.add(napalm_task)
        current_app.database.session.commit()
    return _render_template('napalm_getters.html', form=form)