Question 25
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
from flask import Flask,jsonify,request,abort, make_response
|
||||
from werkzeug.exceptions import HTTPException
|
||||
from nornir import InitNornir
|
||||
|
||||
|
||||
import os
|
||||
from werkzeug.utils import secure_filename
|
||||
import json
|
||||
|
||||
ALLOWED_EXTENSIONS = {'conf'}
|
||||
@@ -11,15 +11,18 @@ UPLOAD_FOLDER = 'fastprod/upload_files/'
|
||||
|
||||
def init_nornir():
|
||||
app.config['nr'] = InitNornir(config_file="fastprod/inventory/config.yaml")
|
||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||
|
||||
from services.devices import ( get_inventory, add_device,get_device_by_name,delete_device,get_device_interfaces,get_device_interfaces_ip,get_device_technical_info)
|
||||
from services.config import (get_config_by_device)
|
||||
from services.config import (get_config_by_device,run_config_from_file_by_device)
|
||||
from services.tasks import (run_show_commands_by_device,run_config_commands_by_device)
|
||||
init_nornir()
|
||||
|
||||
@@ -80,6 +83,14 @@ def get_config(device_name):
|
||||
config = get_config_by_device(device)
|
||||
return jsonify(interfaces_ip=config)
|
||||
if request.method == 'POST':
|
||||
if request.files.to_dict(flat=False).get('config_file'):
|
||||
file = request.files['config_file']
|
||||
if not allowed_file(file.filename):
|
||||
abort(make_response(jsonify(message="file extension not allowed"), 403))
|
||||
filename = secure_filename(file.filename)
|
||||
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||||
result = run_config_from_file_by_device(device,file_path=os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||||
return jsonify(result=result)
|
||||
if request.get_json().get('mode') == 'enable':
|
||||
commands = request.get_json().get('commands')
|
||||
result = run_show_commands_by_device(device, commands=commands)
|
||||
@@ -90,7 +101,6 @@ def get_config(device_name):
|
||||
return jsonify(result=result, commands=commands)
|
||||
|
||||
|
||||
|
||||
@app.errorhandler(HTTPException)
|
||||
def handle_exception(e):
|
||||
"""Return JSON instead of HTML for HTTP errors."""
|
||||
|
||||
Reference in New Issue
Block a user