def configuration(): netmiko_config_form = NetmikoConfigScriptForm(request.form) napalm_config_form = NapalmConfigScriptForm(request.form) napalm_getters_form = NapalmGettersForm(request.form) file_transfer_form = FileTransferScriptForm(request.form) ansible_form = AnsibleScriptForm(request.form) if request.method == 'POST': script_type = request.form['create_script'] if script_type in ('netmiko_config', 'napalm_config'): form = { 'netmiko_config': netmiko_config_form, 'napalm_config': napalm_config_form }[script_type] # retrieve the raw script: we will use it as-is or update it # depending on the type of script (jinja2-enabled template or not) real_content = request.form['content'] if form.data['content_type'] != 'simple': file = request.files['file'] filename = secure_filename(file.filename) if allowed_file(filename, {'yaml', 'yml'}): parameters = load(file.read()) template = Template(real_content) real_content = template.render(**parameters) print(request.form) script = { 'netmiko_config': NetmikoConfigScript, 'napalm_config': NapalmConfigScript }[script_type](real_content, **request.form) elif script_type == 'ansible_playbook': filename = secure_filename(request.files['file'].filename) if allowed_file(filename, {'yaml', 'yml'}): playbook_path = join(current_app.config['UPLOAD_FOLDER'], filename) request.files['file'].save(playbook_path) script = AnsibleScript(playbook_path, **request.form) else: script = { 'napalm_getters': NapalmGettersScript, 'file_transfer': FileTransferScript, }[script_type](**request.form) db.session.add(script) db.session.commit() return render_template('script_creation.html', names=pretty_names, netmiko_config_form=netmiko_config_form, napalm_config_form=napalm_config_form, napalm_getters_form=napalm_getters_form, file_transfer_form=file_transfer_form, ansible_form=ansible_form)
def objects(): add_node_form = AddNode(request.form) add_link_form = AddLink(request.form) all_nodes = Node.choices() add_link_form.source.choices = add_link_form.destination.choices = all_nodes if request.method == 'POST': file = request.files['file'] if allowed_file(secure_filename(file.filename), {'xls', 'xlsx'}): book = open_workbook(file_contents=file.read()) for obj_type, cls in object_class.items(): try: sheet = book.sheet_by_name(obj_type) # if the sheet cannot be found, there's nothing to import except XLRDError: continue properties = sheet.row_values(0) for row_index in range(1, sheet.nrows): kwargs = dict(zip(properties, sheet.row_values(row_index))) kwargs['type'] = obj_type object_factory(**kwargs) db.session.commit() return render_template('object_management.html', names=pretty_names, node_fields=node_public_properties, nodes=Node.visible_objects(), link_fields=link_public_properties, links=Link.visible_objects(), add_node_form=add_node_form, add_link_form=add_link_form)
def create_objects(): add_node_form = AddNode(request.form) add_nodes_form = AddNodes(request.form) add_link_form = AddLink(request.form) if 'add_node' in request.form or 'add_link' in request.form: object_factory(**request.form.to_dict()) elif 'add_nodes' in request.form: file = request.files['file'] if allowed_file(secure_filename(file.filename), {'xls', 'xlsx'}): book = open_workbook(file_contents=file.read()) for obj_type, cls in object_class.items(): try: sheet = book.sheet_by_name(obj_type) # if the sheet cannot be found, there's nothing to import except XLRDError: continue properties = sheet.row_values(0) for row_index in range(1, sheet.nrows): kwargs = dict(zip(properties, sheet.row_values(row_index))) kwargs['type'] = obj_type object_factory(**kwargs) db.session.commit() all_nodes = Node.visible_choices() add_link_form.source.choices = add_link_form.destination.choices = all_nodes return render_template('create_object.html', add_node_form=add_node_form, add_nodes_form=add_nodes_form, add_link_form=add_link_form)
def script_creation(): form = ScriptCreationForm(request.form) if 'create_script' in request.form: # retrieve the raw script: we will use it as-is or update it depending # on the type of script (jinja2-enabled template or not) content = request.form['text'] if form.data['type'] != 'simple': filename = request.files['file'].filename if 'file' in request.files and filename: if allowed_file(filename, {'yaml'}): filename = secure_filename(filename) filepath = join(current_app.config['UPLOAD_FOLDER'], filename) with open(filepath, 'r') as f: parameters = load(f) template = Template(content) content = template.render(**parameters) print(content) #TODO properly display that error in the GUI else: flash('file {}: format not allowed'.format(filename)) script = Script(content, **request.form) db.session.add(script) db.session.commit() return render_template( 'script_creation.html', form = form )
def create_script(script_type): script = get_obj(Script, name=request.form['name']) if script: script_factory(script_type, **request.form) db.session.commit() elif script_type in ('netmiko_config', 'napalm_config'): # retrieve the raw script: we will use it as-is or update it # depending on the type of script (jinja2-enabled template or not) real_content = request.form['content'] if request.form['content_type'] != 'simple': file = request.files['file'] filename = secure_filename(file.filename) if allowed_file(filename, {'yaml', 'yml'}): parameters = load(file.read()) template = Template(real_content) real_content = template.render(**parameters) script = { 'netmiko_config': NetmikoConfigScript, 'napalm_config': NapalmConfigScript }[script_type](real_content, **request.form) elif script_type == 'file_transfer': source_file_name = request.form['source_file'] source_file_path = join(current_app.path_file_transfer, source_file_name) script = FileTransferScript(source_file_path, **request.form) else: script = { 'ansible_playbook': AnsibleScript, 'napalm_getters': NapalmGettersScript, 'netmiko_validation': NetmikoValidationScript }[script_type](**request.form) db.session.add(script) db.session.commit() return jsonify({})
def create_objects(): add_node_form = AddNode(request.form) add_nodes_form = AddNodes(request.form) add_link_form = AddLink(request.form) if 'add_node' in request.form: node = node_class[request.form['type']](**request.form) db.session.add(node) db.session.commit() elif 'add_nodes' in request.form: filename = request.files['file'].filename if 'file' in request.files and allowed_file(filename, {'xls', 'xlsx'}): filename = secure_filename(filename) filepath = join(current_app.config['UPLOAD_FOLDER'], filename) request.files['file'].save(filepath) book = open_workbook(filepath) for obj_type, cls in object_class.items(): try: sheet = book.sheet_by_name(obj_type) # if the sheet cannot be found, there's nothing to import except XLRDError: continue properties = sheet.row_values(0) for row_index in range(1, sheet.nrows): kwargs = dict(zip(properties, sheet.row_values(row_index))) if obj_type in link_class: source = get_obj(db, Node, name=kwargs.pop('source')) destination = get_obj(db, Node, name=kwargs.pop('destination')) new_obj = link_class[obj_type]( source_id=source.id, destination_id=destination.id, source=source, destination=destination, **kwargs) else: new_obj = node_class[obj_type](**kwargs) db.session.add(new_obj) db.session.commit() else: flash('no file submitted') elif 'add_link' in request.form: source = db.session.query(Node)\ .filter_by(name=request.form['source'])\ .first() destination = db.session.query(Node)\ .filter_by(name=request.form['destination'])\ .first() new_link = EthernetLink(source_id=source.id, destination_id=destination.id, source=source, destination=destination) db.session.add(new_link) db.session.commit() all_nodes = Node.visible_choices() add_link_form.source.choices = add_link_form.destination.choices = all_nodes return render_template('create_object.html', add_node_form=add_node_form, add_nodes_form=add_nodes_form, add_link_form=add_link_form)
def create_objects(): add_node_form = AddNode(request.form) add_nodes_form = AddNodes(request.form) add_link_form = AddLink(request.form) if 'add_node' in request.form or 'add_link' in request.form: object_factory(db, **request.form.to_dict()) elif 'add_nodes' in request.form: filename = request.files['file'].filename if 'file' in request.files and allowed_file(filename, {'xls', 'xlsx'}): filename = secure_filename(filename) filepath = join(current_app.config['UPLOAD_FOLDER'], filename) request.files['file'].save(filepath) book = open_workbook(filepath) for obj_type, cls in object_class.items(): try: sheet = book.sheet_by_name(obj_type) # if the sheet cannot be found, there's nothing to import except XLRDError: continue properties = sheet.row_values(0) for row_index in range(1, sheet.nrows): kwargs = dict(zip(properties, sheet.row_values(row_index))) kwargs['type'] = obj_type object_factory(db, **kwargs) db.session.commit() else: flash('no file submitted') all_nodes = Node.visible_choices() add_link_form.source.choices = add_link_form.destination.choices = all_nodes return render_template('create_object.html', add_node_form=add_node_form, add_nodes_form=add_nodes_form, add_link_form=add_link_form)
def ansible_script(): form = AnsibleScriptForm(request.form) if request.method == 'POST': filename = secure_filename(request.files['file'].filename) if allowed_file(filename, {'yaml', 'yml'}): playbook_path = join(current_app.config['UPLOAD_FOLDER'], filename) request.files['file'].save(playbook_path) script = AnsibleScript(playbook_path, **request.form) db.session.add(script) db.session.commit() return render_template('ansible.html', form=form)
def script_creation(): form = ScriptCreationForm(request.form) if 'create_script' in request.form: # retrieve the raw script: we will use it as-is or update it depending # on the type of script (jinja2-enabled template or not) content = request.form['text'] if form.data['type'] != 'simple': file = request.files['file'] filename = secure_filename(file.filename) if allowed_file(filename, {'yaml', 'yml'}): parameters = load(file.read()) template = Template(content) content = template.render(**parameters) script = ClassicScript(content, **request.form) db.session.add(script) db.session.commit() return render_template( 'script_creation.html', form=form )
def configuration(script_type): form = { 'netmiko': NetmikoConfigScriptForm, 'napalm': NapalmConfigScriptForm }[script_type](request.form) if 'create_script' in request.form: # retrieve the raw script: we will use it as-is or update it depending # on the type of script (jinja2-enabled template or not) content = request.form['text'] if form.data['content_type'] != 'simple': file = request.files['file'] filename = secure_filename(file.filename) if allowed_file(filename, {'yaml', 'yml'}): parameters = load(file.read()) template = Template(content) content = template.render(**parameters) script = { 'netmiko': NetmikoConfigScript, 'napalm': NapalmConfigScript }[script_type](content, **request.form) db.session.add(script) db.session.commit() return render_template(script_type + '_configuration.html', form=form)