Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions marmoset/pxe/client_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""file for dealing with PXE client configs"""
"""file for dealing with PXE client configs."""
import base64
import crypt
import os
Expand All @@ -11,7 +11,7 @@


class ClientConfig:
"""Class to handle PXE configs for clients"""
"""Class to handle PXE configs for clients."""

CFG_DIR = '/srv/tftp/pxelinux.cfg/'

Expand Down Expand Up @@ -53,7 +53,7 @@ def __init__(self, ip_address, password=None, script=None, uuid=None,
ipv6_address=None, ipv6_gateway=None, ipv6_prefix=None,
persistent=False):
"""
Initialize a PXE config object with provided data
Initialize a PXE config object with provided data.

We do a lot of data validation here. Besides that we do a lot of
assumptions her. Example: IPv6 is a all-or-nothing setup. All IPv6
Expand Down
8 changes: 4 additions & 4 deletions marmoset/pxe/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Module to deal with exceptions in the PXE part"""
"""Module to deal with exceptions in the PXE part."""


class Error(Exception):
"""Class to deal with it"""
"""Class to deal with it."""

def __init__(self, message):
"""Initialize the message attribute"""
"""Initialize the message attribute."""
self.message = message


class InputError(Error):
"""Dummy class"""
"""Dummy class."""

pass
6 changes: 3 additions & 3 deletions marmoset/pxe/label.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""File for dealing with PXE lables"""
"""File for dealing with PXE lables."""
from .client_config import ClientConfig
from .exceptions import InputError


class Label:
"""class to handle PXE lables"""
"""class to handle PXE lables."""

instances = []

Expand All @@ -23,7 +23,7 @@ def names(cls):

def __init__(self, name, callback=None):
"""
Initialize attributes with defaults
Initialize attributes with defaults.

This also checks if a configured callback method (from our config file)
for a PXE label actually exists as a mehod.
Expand Down
20 changes: 10 additions & 10 deletions marmoset/validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""helper fill with several validation functions"""
"""helper fill with several validation functions."""
import ipaddress
import re
from uuid import UUID
Expand All @@ -7,12 +7,12 @@


def is_mac(mac):
"""Returns True if param is a valid MAC"""
"""Return True if param is a valid MAC."""
return True if re.match("^%s$" % MAC_REGEX, mac) else False


def is_ipv4(ipaddr):
"""Returns True if param is a valid IPv4 address"""
"""Return True if param is a valid IPv4 address."""
try:
ipaddress.IPv4Address(ipaddr)
except ipaddress.AddressValueError:
Expand All @@ -21,7 +21,7 @@ def is_ipv4(ipaddr):


def is_ipv6(ipaddr):
"""Returns True if param is a valid IPv6 address"""
"""Return True if param is a valid IPv6 address."""
try:
ipaddress.IPv6Address(ipaddr)
except ipaddress.AddressValueError:
Expand All @@ -30,7 +30,7 @@ def is_ipv6(ipaddr):


def is_cidr(cidr):
"""Returns True if param is a valid IPv4 CIDR"""
"""Return True if param is a valid IPv4 CIDR."""
try:
if "/" not in cidr:
return False
Expand All @@ -42,7 +42,7 @@ def is_cidr(cidr):


def is_uuid(uuid):
"""Returns True if param is a valid UUID"""
"""Return True if param is a valid UUID."""
try:
UUID(uuid)
except Exception:
Expand All @@ -51,7 +51,7 @@ def is_uuid(uuid):


def get_cidr(cidr):
"""Returns a hash with parsed CIDR, containing IP, Netmask, Gateway"""
"""Return a hash with parsed CIDR, containing IP, Netmask, Gateway."""
interface = ipaddress.IPv4Interface(cidr)
gateway = list(interface.network.hosts())[0]
return {'ip': str(interface.ip),
Expand All @@ -60,18 +60,18 @@ def get_cidr(cidr):


def get_ip_from_cidr(cidr):
"""Returns the IP from a CIDR"""
"""Return the IP from a CIDR."""
data = get_cidr(cidr)
return data['ip']


def get_nm_from_cidr(cidr):
"""Returns the netmask from a CIDR"""
"""Return the netmask from a CIDR."""
data = get_cidr(cidr)
return data['nm']


def get_gw_from_cidr(cidr):
"""Returns the gateway from a CIDR"""
"""Return the gateway from a CIDR."""
data = get_cidr(cidr)
return data['gw']
12 changes: 6 additions & 6 deletions marmoset/webserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""initial file for providing a Flask based API"""
"""initial file for providing a Flask based API."""
from flask import Flask, jsonify
# https://flask-restful.readthedocs.io/en/latest/quickstart.html
from flask_restful import Api
Expand All @@ -11,14 +11,14 @@


def jsonify_nl(*args, **kwargs):
"""Encode data to json"""
"""Encode data to json."""
resp = jsonify(*args, **kwargs)
resp.set_data(resp.get_data() + b'\n')
return resp


def app(config):
"""Setup the initial flask app"""
"""Setup the initial flask app."""
auth.Username = config['Webserver'].get('Username')
auth.Password = config['Webserver'].get('Password')

Expand Down Expand Up @@ -83,7 +83,7 @@ def app(config):

@app.errorhandler(404)
def not_found(ex):
"""Function to generate 404 handler"""
"""Function to generate 404 handler."""
# pylint: disable-msg=unused-argument
# pylint: disable-msg=unused-variable
resp = jsonify_nl(message="Route not found.", status=404)
Expand All @@ -92,7 +92,7 @@ def not_found(ex):

@app.errorhandler(401)
def unauthorized(ex):
"""Function to generate 401 handler"""
"""Function to generate 401 handler."""
# pylint: disable-msg=unused-argument
# pylint: disable-msg=unused-variable
resp = jsonify_nl(message="Unauthorized", status=401)
Expand All @@ -103,7 +103,7 @@ def unauthorized(ex):


def run(args):
"""Function to run the app"""
"""Function to run the app."""
# pylint: disable-msg=unused-argument
webserver = app(config)
print(webserver.url_map)
Expand Down
22 changes: 11 additions & 11 deletions marmoset/webserver/dhcp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""File to handle all web interaction with DHCP records"""
"""File to handle all web interaction with DHCP records."""
from flask import request
from flask.ext.restful import Resource
from flask.ext.restful import abort
Expand All @@ -25,14 +25,14 @@


class DhcpCollection(Resource):
"""Collection class to dal with all DHCP records"""
"""Collection class to dal with all DHCP records."""

def get(self):
"""Returns all DHCP records"""
"""Return all DHCP records."""
return [vars(c) for c in dhcp.DhcpConfig.all()]

def post(self):
"""Creates a new PXE record"""
"""Create a new PXE record."""
args = parser.parse_args()

if ((args.gateway is None and config['DHCPConfig'].getboolean(
Expand Down Expand Up @@ -78,10 +78,10 @@ def post(self):


class DhcpIpv4Object(Resource):
"""Class to handle a single DHCP record based on IPv4 address"""
"""Class to handle a single DHCP record based on IPv4 address."""

def get(self, ipv4):
"""Returns a single DHCP record based on the provided ipv4"""
"""Return a single DHCP record based on the provided ipv4."""
if not validation.is_ipv4(ipv4):
return {'message': 'please provide a valid ipv4 address'}, 406

Expand All @@ -93,7 +93,7 @@ def get(self, ipv4):
return vars(dhcp_config)

def put(self, ipv4):
"""Updates a DHCP recordd"""
"""Update a DHCP recordd."""
args = parser.parse_args(request)

if not validation.is_ipv4(ipv4):
Expand Down Expand Up @@ -142,10 +142,10 @@ def delete(self, ipv4):


class DhcpMacObject(Resource):
"""Class to handle a single DHCP record based on a MAC address"""
"""Class to handle a single DHCP record based on a MAC address."""

def get(self, mac):
"""Returns a single DHCP record based on the provided MAC"""
"""Return a single DHCP record based on the provided MAC."""
if not validation.is_mac(mac):
return {'message': 'please provide a valid mac address'}, 406

Expand All @@ -157,7 +157,7 @@ def get(self, mac):
return vars(dhcp_config)

def put(self, mac):
"""Updates a DHCP record"""
"""Update a DHCP record."""
args = parser.parse_args(request)

if not validation.is_mac(mac):
Expand Down Expand Up @@ -189,7 +189,7 @@ def put(self, mac):
return vars(dhcp_config), 201, {'Location': location}

def delete(self, mac):
"""Deletes a DHCP record"""
"""Delete a DHCP record."""
if not validation.is_mac(mac):
return {'message': 'please provide a valid mac address'}, 406

Expand Down
2 changes: 1 addition & 1 deletion marmoset/webserver/flask/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""flask initial file"""
"""flask initial file."""
13 changes: 8 additions & 5 deletions marmoset/webserver/flask/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ def for_all_routes(app):
return app


# we've to disable pep8 here because it detects uppercase chars in the method name
# we've to disable pep8 here because
# it detects uppercase chars in the method name
# pep8 compliance requires lowercase only function names, but thats the case
def __check_auth(username, password): # nopep8
"""
Check username/password combination
Check username/password combination.

This function is called to check if a username /
password combination is valid.
"""
return username == Username and password == Password


# we've to disable pep8 here because it detects uppercase chars in the method name
# we've to disable pep8 here because
# it detects uppercase chars in the method name
# pep8 compliance requires lowercase only function names, but thats the case
def __is_whitelist_endpoint(endpoint): # nopep8
"""Check if a given endpoint is whitelisted in our configuration file"""
"""Check if a given endpoint is whitelisted in our configuration file."""
whitelist_conf = current_app.config['AUTH_WHITELIST_ENDPOINT']
if whitelist_conf is not None:
whitelist = whitelist_conf.split(',')
Expand All @@ -46,7 +48,8 @@ def __is_whitelist_endpoint(endpoint): # nopep8
return False


# we've to disable pep8 here because it detects uppercase chars in the method name
# we've to disable pep8 here because
# it detects uppercase chars in the method name
# pep8 compliance requires lowercase only function names, but thats the case
def __authenticate(): # nopep8
auth = request.authorization
Expand Down
8 changes: 4 additions & 4 deletions marmoset/webserver/flask/json.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Flask extension module which holds a few helper functions"""
"""Flask extension module which holds a few helper functions."""
from flask import Flask, jsonify
from werkzeug.exceptions import default_exceptions, HTTPException


# pylint: disable-msg=keyword-arg-before-vararg
def response(code=200, headers={}, *args, **kwargs):
"""Creates a json encoded response"""
"""Create a json encoded response."""
# pylint: disable-msg=dangerous-default-value
# pylint: disable-msg=redefined-outer-name
response = jsonify(*args, **kwargs)
Expand All @@ -16,15 +16,15 @@ def response(code=200, headers={}, *args, **kwargs):


def error(ex=None, code=500, headers={}):
"""Creates a proper HTTP error"""
"""Create a proper HTTP error."""
# pylint: disable-msg=dangerous-default-value
code = ex.code if isinstance(ex, HTTPException) else code
return response(code, headers, message=str(ex))


def app(import_name, **kwargs):
"""
Creates a JSON-oriented Flask app.
Create a JSON-oriented Flask app.

All error responses that you don't specifically
manage yourself will have application/json content
Expand Down
11 changes: 9 additions & 2 deletions marmoset/webserver/imagecatalog.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
"""Class for handling image metadata."""
from flask.ext.restful import Resource, abort
from ..imagecatalog.catalog import ImageCatalog


class ImageMetadataCollection(Resource):
"""Collection class to deal with metadata for images."""

def get(self):
"""List all images' metadata"""
"""List all images' metadata."""
catalog = ImageCatalog()
metadata_list = catalog.list_all_metadata()
return metadata_list


class ImageMetadata(Resource):
"""Class to get metadata for a single image."""

def get(self, image_file):
"""Get image's metadata"""
"""Get image's metadata."""
catalog = ImageCatalog()
image = catalog.get_image(image_file)
if image is not None:
Expand All @@ -22,7 +26,10 @@ def get(self, image_file):


class ImageSignature(Resource):
"""Class to get signatures for a single image."""

def get(self, image_file):
"""Return the signature as JSON, if available."""
catalog = ImageCatalog()
image = catalog.get_image(image_file)
if image is not None:
Expand Down
Loading