This commit is contained in:
Miodrag Seslija 2017-06-08 16:50:41 +00:00 committed by GitHub
commit 6b867b7657
18 changed files with 8027 additions and 2 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
*.pyc
/netbox/netbox/configuration.py
#/netbox/netbox/configuration.py
/netbox/netbox/ldap_config.py
/netbox/static
.idea

24
Dockerfile Normal file
View File

@ -0,0 +1,24 @@
FROM python:2.7-wheezy
WORKDIR /opt/netbox
ARG BRANCH=master
ARG URL=https://github.com/digitalocean/netbox.git
RUN git clone --depth 1 $URL -b $BRANCH . && \
apt-get update -qq && apt-get install -y libldap2-dev libsasl2-dev libssl-dev graphviz && \
pip install gunicorn==17.5 && \
pip install django-auth-ldap && \
pip install -r requirements.txt && \
pip install graphene_django && \
pip install django-filter
RUN rm -r /opt/netbox/netbox
ADD docker/docker-entrypoint.sh /docker-entrypoint.sh
ADD netbox/netbox/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py
ENTRYPOINT [ "/docker-entrypoint.sh" ]
ADD docker/gunicorn_config.py /opt/netbox/
ADD docker/nginx.conf /etc/netbox-nginx/
VOLUME ["/etc/netbox-nginx/"]

70
docker-compose.yml Normal file
View File

@ -0,0 +1,70 @@
version: '2'
services:
postgres:
image: postgres:9.6
container_name: postgres
environment:
POSTGRES_USER: netbox
POSTGRES_PASSWORD: J5brHrAXFLQSif0K
POSTGRES_DB: netbox
volumes:
- ./netbox:/opt
phppgadmin:
image: einfallstoll/phppgadmin
container_name: phppgadmin
environment:
APACHE_SERVERNAME: docker.local
POSTGRES_HOST: postgres
POSTGRES_DB: 5432
links:
- postgres
depends_on:
- postgres
ports:
- 8057:80
netbox:
build: .
image: digitalocean/netbox
links:
- postgres
container_name: netbox
depends_on:
- postgres
environment:
SUPERUSER_NAME: admin
SUPERUSER_EMAIL: admin@example.com
SUPERUSER_PASSWORD: admin
ALLOWED_HOSTS: localhost
DB_NAME: netbox
DB_USER: netbox
DB_PASSWORD: J5brHrAXFLQSif0K
DB_HOST: postgres
SECRET_KEY: r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj
EMAIL_SERVER: localhost
EMAIL_PORT: 25
EMAIL_USERNAME: foo
EMAIL_PASSWORD: bar
EMAIL_TIMEOUT: 10
EMAIL_FROM: netbox@bar.com
NETBOX_USERNAME: guest
NETBOX_PASSWORD: guest
volumes:
- netbox-static-files:/opt/netbox/netbox/static
- ./netbox:/opt/netbox/netbox
nginx:
image: nginx:1.11.1-alpine
links:
- netbox
container_name: nginx
command: nginx -g 'daemon off;' -c /etc/netbox-nginx/nginx.conf
depends_on:
- netbox
ports:
- 8055:80
volumes_from:
- netbox
volumes:
netbox-static-files:
driver: local

22
docker/docker-entrypoint.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
set -e
# run db migrations (retry on error)
while ! /opt/netbox/netbox/manage.py migrate 2>&1; do
sleep 5
done
# create superuser silently
if [[ -z ${SUPERUSER_NAME} || -z ${SUPERUSER_EMAIL} || -z ${SUPERUSER_PASSWORD} ]]; then
SUPERUSER_NAME='admin'
SUPERUSER_EMAIL='admin@example.com'
SUPERUSER_PASSWORD='admin'
echo "Using defaults: Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}, Password: ${SUPERUSER_PASSWORD}"
fi
echo "from django.contrib.auth.models import User; User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}')" | python /opt/netbox/netbox/manage.py shell
# copy static files
/opt/netbox/netbox/manage.py collectstatic --no-input
# start unicorn
gunicorn --log-level debug --debug --error-logfile /dev/stderr --log-file /dev/stdout -c /opt/netbox/gunicorn_config.py netbox.wsgi

35
docker/nginx.conf Normal file
View File

@ -0,0 +1,35 @@
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server_tokens off;
server {
listen 80;
server_name localhost;
access_log off;
location /static/ {
alias /opt/netbox/netbox/static/;
}
location / {
proxy_pass http://netbox:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
}

View File

@ -0,0 +1 @@
default_app_config = 'dcim.apps.DCIMConfig'

View File

@ -0,0 +1,90 @@
[
{
"model": "circuits.provider",
"pk": 1,
"fields": {
"created": "2017-05-10",
"last_updated": "2017-05-10T10:51:03.925Z",
"name": "Nine.ch",
"slug": "ninech",
"asn": 1,
"account": "0001",
"portal_url": "http://nine.ch",
"noc_contact": "noc contact",
"admin_contact": "Admin Contact",
"comments": "Comments bla bla"
}
},
{
"model": "circuits.provider",
"pk": 2,
"fields": {
"created": "2017-05-10",
"last_updated": "2017-05-10T10:51:49.872Z",
"name": "Telenor",
"slug": "telenor",
"asn": 2,
"account": "0002",
"portal_url": "http://www.telenor.com",
"noc_contact": "telenor noc",
"admin_contact": "telenor admin contact",
"comments": "some telenor comments"
}
},
{
"model": "circuits.circuittype",
"pk": 1,
"fields": {
"name": "Internet",
"slug": "internet"
}
},
{
"model": "circuits.circuittype",
"pk": 2,
"fields": {
"name": "Private WAN",
"slug": "private-wan"
}
},
{
"model": "circuits.circuittype",
"pk": 3,
"fields": {
"name": "Out-of-Band",
"slug": "out-of-band"
}
},
{
"model": "circuits.circuit",
"pk": 1,
"fields": {
"created": "2017-05-10",
"last_updated": "2017-05-10T10:52:56.278Z",
"cid": "Circuit1",
"provider": 1,
"type": 1,
"tenant": null,
"install_date": "2017-05-10",
"commit_rate": 126,
"description": "Description",
"comments": "C1 comments"
}
},
{
"model": "circuits.circuit",
"pk": 2,
"fields": {
"created": "2017-05-10",
"last_updated": "2017-05-10T10:53:19.204Z",
"cid": "Circuit2",
"provider": 2,
"type": 3,
"tenant": null,
"install_date": "2017-05-12",
"commit_rate": 4096,
"description": "Circuit2",
"comments": "Circuit2 comments"
}
}
]

7318
netbox/dumpdata/dump.sql Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,92 @@
#########################
# #
# Required settings #
# #
#########################
# This is a list of valid fully-qualified domain names (FQDNs) for the NetBox server. NetBox will not permit write
# access to the server via any other hostnames. The first FQDN in the list will be treated as the preferred name.
#
# Example: ALLOWED_HOSTS = ['netbox.example.com', 'netbox.internal.local']
ALLOWED_HOSTS = ['0.0.0.0', 'localhost']
# PostgreSQL database configuration.
DATABASE = {
'NAME': 'netbox', # Database name
'USER': 'netbox', # PostgreSQL username
'PASSWORD': 'J5brHrAXFLQSif0K', # PostgreSQL password
'HOST': 'postgres', # Database server
'PORT': '5432', # Database port (leave blank for default)
}
# This key is used for secure generation of random numbers and strings. It must never be exposed outside of this file.
# For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and
# symbols. NetBox will not run without this defined. For more information, see
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-SECRET_KEY
SECRET_KEY = 'yxox%0o(mm0gmzf8ez*h0#3k_b9#vifc2#h46)l*uc!#a3wd@c'
#########################
# #
# Optional settings #
# #
#########################
# Specify one or more name and email address tuples representing NetBox administrators. These people will be notified of
# application errors (assuming correct email settings are provided).
ADMINS = [
# ['John Doe', 'jdoe@example.com'],
]
# Email settings
EMAIL = {
'SERVER': 'localhost',
'PORT': 25,
'USERNAME': '',
'PASSWORD': '',
'TIMEOUT': 10, # seconds
'FROM_EMAIL': '',
}
# Setting this to True will permit only authenticated users to access any part of NetBox. By default, anonymous users
# are permitted to access most data in NetBox (excluding secrets) but not make any changes.
LOGIN_REQUIRED = False
# Base URL path if accessing NetBox within a directory. For example, if installed at http://example.com/netbox/, set:
# BASE_PATH = 'netbox/'
BASE_PATH = ''
# Setting this to True will display a "maintenance mode" banner at the top of every page.
MAINTENANCE_MODE = False
# Credentials that NetBox will use to access live devices.
NETBOX_USERNAME = ''
NETBOX_PASSWORD = ''
# Determine how many objects to display per page within a list. (Default: 50)
PAGINATE_COUNT = 50
# Time zone (default: UTC)
TIME_ZONE = 'UTC'
# Date/time formatting. See the following link for supported formats:
# https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date
DATE_FORMAT = 'N j, Y'
SHORT_DATE_FORMAT = 'Y-m-d'
TIME_FORMAT = 'g:i a'
SHORT_TIME_FORMAT = 'H:i:s'
DATETIME_FORMAT = 'N j, Y g:i a'
SHORT_DATETIME_FORMAT = 'Y-m-d H:i'
# Optionally display a persistent banner at the top and/or bottom of every page. To display the same content in both
# banners, define BANNER_TOP and set BANNER_BOTTOM = BANNER_TOP.
BANNER_TOP = ''
BANNER_BOTTOM = ''
# When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to
# prefer IPv4 instead.
PREFER_IPV4 = False
# Enforcement of unique IP space can be toggled on a per-VRF basis. To enforce unique IP space within the global table
# (all prefixes and IP addresses not assigned to a VRF), set ENFORCE_GLOBAL_UNIQUE to True.
ENFORCE_GLOBAL_UNIQUE = False

View File

@ -27,7 +27,7 @@ for setting in ['ALLOWED_HOSTS', 'DATABASE', 'SECRET_KEY']:
# Default configurations
ADMINS = getattr(configuration, 'ADMINS', [])
DEBUG = getattr(configuration, 'DEBUG', False)
DEBUG = getattr(configuration, 'DEBUG', True)
EMAIL = getattr(configuration, 'EMAIL', {})
LOGIN_REQUIRED = getattr(configuration, 'LOGIN_REQUIRED', False)
BASE_PATH = getattr(configuration, 'BASE_PATH', '')
@ -126,6 +126,12 @@ INSTALLED_APPS = (
'tenancy',
'users',
'utilities',
# test
'graphene_django',
'ninech',
)
# Middleware
@ -238,3 +244,8 @@ try:
HOSTNAME = socket.gethostname()
except:
HOSTNAME = 'localhost'
GRAPHENE = {
'SCHEMA' : 'ninech.schema.schema', #points to the schema variable in schema.py
'SCHEMA_INDENT': 2, #defines the indentation space in the output
}

View File

@ -10,6 +10,7 @@ from django.views.static import serve
from netbox.views import APIRootView, handle_500, HomeView, SearchView, trigger_500
from users.views import LoginView, LogoutView
from graphene_django.views import GraphQLView
handler500 = handle_500
swagger_view = get_swagger_view(title='NetBox API')
@ -52,6 +53,9 @@ _patterns = [
# Admin
url(r'^admin/', admin.site.urls),
#GrapQL
url(r'^graphql', GraphQLView.as_view(graphiql=True)),
]
if settings.DEBUG:

View File

View File

@ -0,0 +1,50 @@
from graphene import AbstractType
from graphene import Node
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType
from circuits.models import CircuitType, Circuit, Provider
from filter_fields import date_types, string_types, number_types
# Nodes
class ProviderNode(DjangoObjectType):
class Meta:
model = Provider
interfaces = (Node, )
filter_fields = {
'name': string_types,
'slug': ['exact'],
}
# filter_order_by = ('id')
class CircuitNode(DjangoObjectType):
class Meta:
model = Circuit
interfaces = (Node, )
filter_fields = {
'id': ['exact'],
'cid': string_types,
'commit_rate': number_types,
'install_date': date_types,
'description': string_types,
'comments': string_types,
}
# filter_order_by = ('id', 'cid')
class TypeNode(DjangoObjectType):
class Meta:
model = CircuitType
interfaces = (Node, )
filter_fields = {
'name': string_types,
'slug': string_types,
}
# filter_order_by = ('slug')
# Queries
class CircuitsQuery(AbstractType):
providers = DjangoFilterConnectionField(ProviderNode)
types = DjangoFilterConnectionField(TypeNode)
circuit = Node.Field(CircuitNode)
circuits = DjangoFilterConnectionField(CircuitNode)

View File

@ -0,0 +1,35 @@
import graphene
from graphene import AbstractType
from graphene import Node
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType
from dcim.models import Device, Interface
from graphene_django.converter import convert_django_field
from dcim.fields import ASNField, MACAddressField
from filter_fields import date_types, string_types, number_types
# Convert special field
@convert_django_field.register(MACAddressField)
def MACAddressFieldConvert(field, registry=None):
return graphene.String()
@convert_django_field.register(ASNField)
def ASNFieldConvert(field, registry=None):
return graphene.String()
# Nodes
class DeviceNode(DjangoObjectType):
class Meta:
model = Device
interfaces = (Node, )
class InterfaceNode(DjangoObjectType):
class Meta:
model = Interface
interfaces = (Node, )
# only_fields = ('name', 'device', 'mgmt_only', 'id', 'mac_address')
# Queries
class DcimQuery(AbstractType):
devices = DjangoFilterConnectionField(DeviceNode)
interfaces = DjangoFilterConnectionField(InterfaceNode)

View File

@ -0,0 +1,3 @@
number_types = ['exact', 'gte', 'lte', 'in', 'lt', 'gt', 'range']
string_types = ['exact', 'icontains', 'istartswith']
date_types = ['exact', 'gte', 'lte', 'in', 'lt', 'gt', 'range']

View File

@ -0,0 +1,30 @@
import graphene
from graphene_django.converter import convert_django_field
from graphene import AbstractType
from graphene import Node
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType
from ipam.models import IPAddress
from ipam.fields import IPNetworkField, IPAddressField
from filter_fields import date_types, string_types, number_types
@convert_django_field.register(IPNetworkField)
def iPNetworkFieldConvert(field, registry=None):
return graphene.String()
@convert_django_field.register(IPAddressField)
def iPAddressFieldConvert(field, registry=None):
return graphene.String()
# Nodes
class IPAddressNode(DjangoObjectType):
class Meta:
model = IPAddress
interfaces = (Node, )
# only_fields = ('interface', 'description', 'status', 'id')
# Queries
class IpamQuery(AbstractType):
ip_address = DjangoFilterConnectionField(IPAddressNode)

206
netbox/ninech/schema.py Normal file
View File

@ -0,0 +1,206 @@
import graphene
from .circuits_schema import CircuitsQuery
from .tenancy_schema import TenancyQuery
from .dcim_schema import DcimQuery
from .ipam_schema import IpamQuery
# Root
class RootQuery(
CircuitsQuery
, TenancyQuery
, DcimQuery
, IpamQuery
, graphene.ObjectType):
pass
schema = graphene.Schema(query=RootQuery)
""""
query {
allCircuits {
edges {
node {
id
cid
installDate
provider {
id
name
slug
}
}
}
}
}
{
allCircuits(cid_Icontains: "1") {
edges {
node {
id
cid
provider {
name
}
}
}
}
}
{
provider {
edges {
node {
id
slug
name
circuits {
edges {
node {
id
}
}
}
}
}
}
}
{
allCircuits(cid_Icontains: "1", id: "Q2lyY3VpdE5vZGU6MQ==") {
edges {
node {
id
cid
installDate
commitRate
description
commitRate
comments
provider {
name
}
}
}
}
}
{
allCircuits(commitRate_Lte: 127, commitRate_Gte: 120) {
edges {
node {
id
cid
installDate
lastUpdated
commitRate
provider {
id
}
}
}
}
}
{
allCircuits {
edges {
node {
id
cid
comments
commitRate
description
type {
id
name
slug
}
tenant {
id
slug
name
group {
id
slug
name
}
}
}
}
}
}
{
tenantGroups {
edges {
node {
id
slug
name
tenants {
edges {
node {
id
name
slug
circuits {
edges {
node {
id
cid
comments
commitRate
description
}
}
}
}
}
}
}
}
}
}
{
allCircuits {
edges {
node {
id
cid
comments
commitRate
description
created
lastUpdated
installDate
provider {
id
name
slug
}
type {
id
name
slug
}
tenant {
id
slug
name
group {
id
slug
name
}
}
}
}
}
}
"""

View File

@ -0,0 +1,34 @@
from graphene import AbstractType
from graphene import Node
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType
from filter_fields import date_types, string_types, number_types
from tenancy.models import Tenant, TenantGroup
# Nodes
class TenantGroupNode(DjangoObjectType):
class Meta:
model = TenantGroup
interfaces = (Node, )
filter_fields = {
'name': string_types,
'slug': string_types,
}
class TenantNode(DjangoObjectType):
class Meta:
model = Tenant
interfaces = (Node, )
filter_fields = {
'id': ['exact'],
'name': string_types,
# 'group__name': string_types,
'description': string_types,
'comments': string_types,
}
# Queries
class TenancyQuery(AbstractType):
tenant_groups = DjangoFilterConnectionField(TenantGroupNode)
tenants = DjangoFilterConnectionField(TenantGroupNode)