Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Apr 22, 2018
1 parent 62a46d0 commit d723b67
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .flake8
@@ -0,0 +1,8 @@
# This is an example .flake8 config, used when developing *Black* itself.
# Keep in sync with setup.cfg which is used for source packages.

[flake8]
ignore = E266, E501, W503
max-line-length = 80
max-complexity = 15
select = B,C,E,F,W,T4,B9
7 changes: 6 additions & 1 deletion README.md
@@ -1,2 +1,7 @@
# brg-tools
Ready made tools and examples written with brigade

Ready to use tools and examples written with brigade.

The motivation behind this repo is mainly to share tools, examples and recipes to help you write your own tooling. However, some tooling might be simple and useful enough to work as is. Most of the examples under "tooling" should fit under that category.

Feel free to use, copy and modify according to your needs.
7 changes: 7 additions & 0 deletions requirements-dev.txt
@@ -0,0 +1,7 @@
pytest
pytest-cov
pylama
flake8-import-order
tox
black==18.4a1; python_version >= '3.6'
-r requirements.txt
1 change: 1 addition & 0 deletions requirements.txt
@@ -0,0 +1 @@
brigade
22 changes: 22 additions & 0 deletions setup.cfg
@@ -0,0 +1,22 @@
[bdist_wheel]
# This flag says that the code is written to work on both Python 2 and Python
# 3. If at all possible, it is good practice to do this. If you cannot, you
# will need to generate wheels for each Python version that you support.
universal=1

# E127: continuation line over-indented for visual indent
# E128: continuation line under-indented for visual indent
# E265: block comment should start with #
# E731: do not assign a lambda expression, use a def
# D10?: missing docstrings
[pylama]
linters = mccabe,pep8,pyflakes,import_order
ignore = D203,C901
skip = .tox/*

[pylama:pep8]
max_line_length = 100

[tool:pytest]
addopts = --cov=brigade --cov-report=term-missing -vs
python_paths = ./
5 changes: 5 additions & 0 deletions tests/virtual/config.yaml
@@ -0,0 +1,5 @@
---
inventory: brigade.plugins.inventory.simple.SimpleInventory
SimpleInventory:
host_file: "tests/virtual/inventory/hosts.yaml"
group_file: "tests//virtual/inventory/groups.yaml"
63 changes: 63 additions & 0 deletions tests/virtual/inventory/Vagrantfile
@@ -0,0 +1,63 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
"""
You will need the boxes:
* vEOS-4.17.5M
* JunOS - juniper/ffp-12.1X47-D20.7-packetmode
* To provision and test JunOS first you have to add the ssh vagrant ssh key into the ssh-agent. I.e.:
ssh-add /opt/vagrant/embedded/gems/gems/vagrant-`vagrant --version | awk '{ print $2 }'`/keys/vagrant
"""

Vagrant.configure(2) do |config|
config.vbguest.auto_update = false

config.vm.define "spine00" do |spine00|
spine00.vm.box = "vEOS-lab-4.17.5M"

spine00.vm.network :forwarded_port, guest: 443, host: 12444, id: 'https'

spine00.vm.network "private_network", virtualbox__intnet: "link_1", ip: "169.254.1.11", auto_config: false
spine00.vm.network "private_network", virtualbox__intnet: "link_2", ip: "169.254.1.11", auto_config: false
end

config.vm.define "spine01" do |spine01|
spine01.vm.box = "juniper/ffp-12.1X47-D20.7-packetmode"

spine01.vm.network :forwarded_port, guest: 22, host: 12204, id: 'ssh'

spine01.vm.network "private_network", virtualbox__intnet: "link_3", ip: "169.254.1.11", auto_config: false
spine01.vm.network "private_network", virtualbox__intnet: "link_4", ip: "169.254.1.11", auto_config: false
end


config.vm.define "leaf00" do |leaf00|
leaf00.vm.box = "vEOS-lab-4.17.5M"

leaf00.vm.network :forwarded_port, guest: 443, host: 12443, id: 'https'

leaf00.vm.network "private_network", virtualbox__intnet: "link_1", ip: "169.254.1.11", auto_config: false
leaf00.vm.network "private_network", virtualbox__intnet: "link_3", ip: "169.254.1.11", auto_config: false
leaf00.vm.network "private_network", virtualbox__intnet: "link_5", ip: "169.254.1.11", auto_config: false
end

config.vm.define "leaf01" do |leaf01|
leaf01.vm.box = "juniper/ffp-12.1X47-D20.7-packetmode"

leaf01.vm.network :forwarded_port, guest: 22, host: 12203, id: 'ssh'

leaf01.vm.network "private_network", virtualbox__intnet: "link_2", ip: "169.254.1.11", auto_config: false
leaf01.vm.network "private_network", virtualbox__intnet: "link_4", ip: "169.254.1.11", auto_config: false
leaf01.vm.network "private_network", virtualbox__intnet: "link_5", ip: "169.254.1.11", auto_config: false
end

config.vm.define "host00" do |host00|
host00.vm.box = "hashicorp/precise64"
host00.vm.network "private_network", virtualbox__intnet: "link_5", ip: "10.123.231.100", auto_config: false
end

config.vm.define "host01" do |host01|
host01.vm.box = "hashicorp/precise64"
host01.vm.network "private_network", virtualbox__intnet: "link_5", ip: "10.123.231.101", auto_config: false
end

end
21 changes: 21 additions & 0 deletions tests/virtual/inventory/groups.yaml
@@ -0,0 +1,21 @@
---
defaults:
domain: acme.local

global:
domain: global.local
asn: 1

eu:
asn: 65100

bma:
groups:
- eu
- global

cmh:
asn: 65000
vlans:
100: frontend
200: backend
98 changes: 98 additions & 0 deletions tests/virtual/inventory/hosts.yaml
@@ -0,0 +1,98 @@
---
spine00.cmh:
brigade_host: 127.0.0.1
brigade_username: vagrant
brigade_password: vagrant
brigade_network_api_port: 12444
site: cmh
role: spine
groups:
- cmh
brigade_nos: eos
type: network_device

spine01.cmh:
brigade_host: 127.0.0.1
brigade_username: vagrant
brigade_password: ""
brigade_network_api_port: 12204
site: cmh
role: spine
groups:
- cmh
brigade_nos: junos
type: network_device

leaf00.cmh:
brigade_host: 127.0.0.1
brigade_username: vagrant
brigade_password: vagrant
brigade_network_api_port: 12443
site: cmh
role: leaf
groups:
- cmh
brigade_nos: eos
type: network_device
asn: 65100

leaf01.cmh:
brigade_host: 127.0.0.1
brigade_username: vagrant
brigade_password: ""
brigade_network_api_port: 12203
site: cmh
role: leaf
groups:
- cmh
brigade_nos: junos
type: network_device
asn: 65101

spine00.bma:
brigade_host: 127.0.0.1
brigade_username: vagrant
brigade_password: vagrant
brigade_network_api_port: 12444
site: bma
role: spine
groups:
- bma
brigade_nos: eos
type: network_device

spine01.bma:
brigade_host: 127.0.0.1
brigade_username: vagrant
brigade_password: ""
brigade_network_api_port: 12204
site: bma
role: spine
groups:
- bma
brigade_nos: junos
type: network_device

leaf00.bma:
brigade_host: 127.0.0.1
brigade_username: vagrant
brigade_password: vagrant
brigade_network_api_port: 12443
site: bma
role: leaf
groups:
- bma
brigade_nos: eos
type: network_device

leaf01.bma:
brigade_host: 127.0.0.1
brigade_username: vagrant
brigade_password: wrong_password
brigade_network_api_port: 12203
site: bma
role: leaf
groups:
- bma
brigade_nos: junos
type: network_device
4 changes: 4 additions & 0 deletions tools/README.md
@@ -0,0 +1,4 @@
tools
=====

Ready to use tools written using brigade.
4 changes: 4 additions & 0 deletions tools/network/README.md
@@ -0,0 +1,4 @@
network
=======

Ready to use tools to do various operations in your networks.
21 changes: 21 additions & 0 deletions tools/network/backup/README.md
@@ -0,0 +1,21 @@
backup
======

Tool that retrieves the running configuration from a device and stores it locally.

For help:

$ tools/network/backup/backup.py --help
usage: backup.py [-h] [-d] [-c CONFIG] [-p PATH]

Tool to backup network equipment using napalm

optional arguments:
-h, --help show this help message and exit
-d, --debug
-c CONFIG, --config CONFIG
Path to brigade configuration. Defaults to
brigade.yaml. Can be set via env variable
BRIGADE_CONFIGURATION
-p PATH, --path PATH Path to directory where to save the configuration
backups. Defaults to './backups/'
65 changes: 65 additions & 0 deletions tools/network/backup/backup.py
@@ -0,0 +1,65 @@
#!/usr/bin/env python
import argparse
import logging
import os

from brigade.core import InitBrigade
from brigade.plugins.functions.text import print_result
from brigade.plugins.tasks.files import write_file
from brigade.plugins.tasks.networking import napalm_get


def backup(task, path):
r = task.run(
task=napalm_get,
getters=["config"],
severity=logging.DEBUG,
)
task.run(
task=write_file,
filename=f"{path}/{task.host}",
content=r.result["config"]["running"],
)


def main(config, path, debug):
brg = InitBrigade(
config_file=config,
dry_run=False,
num_workers=1 if debug else 20,
)
result = brg.run(
name="Backup configuration of devices",
task=backup,
path=path,
)
print_result(
result,
severity_level=logging.DEBUG if debug else logging.INFO,
)


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Tool to backup network equipment using napalm"
)
parser.add_argument(
"-d", "--debug", default=False, action="store_true"
)
parser.add_argument(
"-c",
"--config",
default=os.environ.get(
"BRIGADE_CONFIGURATION", "brigade.yaml"
),
help="Path to brigade configuration. Defaults to brigade.yaml. "
"Can be set via env variable BRIGADE_CONFIGURATION",
)
parser.add_argument(
"-p",
"--path",
default="backups",
help="Path to directory where to save the configuration backups. Defaults to './backups/'",
)
args = parser.parse_args()
main(args.config, args.path, args.debug)

0 comments on commit d723b67

Please sign in to comment.