1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
"""Manage Network Application files."""
import json
import logging
import os
import pathlib
import re
import sys
import tarfile
import urllib
from http import HTTPStatus
# Disable pylint import checks that conflict with isort
# pylint: disable=ungrouped-imports,wrong-import-order
import pathspec
from jinja2 import Environment, FileSystemLoader
from ruamel.yaml import YAML
from kytos.utils.client import NAppsClient
from kytos.utils.config import KytosConfig
from kytos.utils.exceptions import KytosException
from kytos.utils.openapi import OpenAPI
from kytos.utils.settings import SKEL_PATH
LOG = logging.getLogger(__name__)
# pylint: disable=too-many-instance-attributes,too-many-public-methods
class NAppsManager:
"""Deal with NApps at filesystem level and ask Kytos to (un)load NApps."""
_NAPP_ENABLE = "api/kytos/core/napps/{}/{}/enable"
_NAPP_DISABLE = "api/kytos/core/napps/{}/{}/disable"
_NAPP_INSTALL = "api/kytos/core/napps/{}/{}/install"
_NAPP_UNINSTALL = "api/kytos/core/napps/{}/{}/uninstall"
_NAPPS_INSTALLED = "api/kytos/core/napps_installed"
_NAPPS_ENABLED = "api/kytos/core/napps_enabled"
_NAPP_METADATA = "api/kytos/core/napps/{}/{}/metadata/{}"
def __init__(self):
"""Instance a new NAppsManager.
This method do not need parameters.
"""
self._config = KytosConfig().config
self._kytos_api = self._config.get('kytos', 'api')
self.user = None
self.napp = None
self.version = None
# Automatically get from kytosd API when needed
self.__local_enabled = None
self.__local_installed = None
@property
def _enabled(self):
if self.__local_enabled is None:
self.__require_kytos_config()
return self.__local_enabled
@property
def _installed(self):
if self.__local_installed is None:
self.__require_kytos_config()
return self.__local_installed
def __require_kytos_config(self):
"""Set path locations from kytosd API.
It should not be called directly, but from properties that require a
running kytosd instance.
"""
if self.__local_enabled is None:
uri = self._kytos_api + 'api/kytos/core/config/'
try:
ops = json.loads(urllib.request.urlopen(uri).read())
except urllib.error.URLError as err:
msg = f'Error connecting to Kytos daemon: {uri} {err.reason}'
print(msg)
sys.exit(1)
self.__local_enabled = pathlib.Path(ops.get('napps'))
self.__local_installed = pathlib.Path(ops.get('installed_napps'))
def set_napp(self, user, napp, version=None):
"""Set info about NApp.
Args:
user (str): NApps Server username.
napp (str): NApp name.
version (str): NApp version.
"""
self.user = user
self.napp = napp
self.version = version or 'latest'
@property
def napp_id(self):
"""Return a Identifier of NApp."""
return '/'.join((self.user, self.napp))
@staticmethod
def _get_napps(napps_dir):
"""List of (username, napp_name) found in ``napps_dir``.
Ex: [('kytos', 'of_core'), ('kytos', 'of_lldp')]
"""
jsons = napps_dir.glob('*/*/kytos.json')
return sorted(j.parts[-3:-1] for j in jsons)
def get_enabled_local(self):
"""Sorted list of (username, napp_name) of enabled napps."""
return self._get_napps(self._enabled)
def get_installed_local(self):
"""Sorted list of (username, napp_name) of installed napps."""
return self._get_napps(self._installed)
def get_enabled(self):
"""Sorted list of (username, napp_name) of enabled napps."""
uri = self._kytos_api + self._NAPPS_ENABLED
try:
response = urllib.request.urlopen(uri)
if response.getcode() != 200:
msg = "Error calling Kytos to check enabled NApps."
raise KytosException(msg)
content = json.loads(response.read())
return sorted((c[0], c[1]) for c in content['napps'])
except urllib.error.URLError as exception:
LOG.error("Error checking installed NApps. Is Kytos running?")
raise KytosException(exception)
def get_installed(self):
"""Sorted list of (username, napp_name) of installed napps."""
uri = self._kytos_api + self._NAPPS_INSTALLED
try:
response = urllib.request.urlopen(uri)
if response.getcode() != 200:
msg = "Error calling Kytos to check installed NApps."
raise KytosException(msg)
content = json.loads(response.read())
return sorted((c[0], c[1]) for c in content['napps'])
except urllib.error.URLError as exception:
LOG.error("Error checking installed NApps. Is Kytos running?")
raise KytosException(exception)
def is_installed(self):
"""Whether a NApp is installed."""
return (self.user, self.napp) in self.get_installed()
def get_disabled(self):
"""Sorted list of (username, napp_name) of disabled napps.
The difference of installed and enabled.
"""
installed = set(self.get_installed())
enabled = set(self.get_enabled())
return sorted(installed - enabled)
def dependencies(self, user=None, napp=None):
"""Get napp_dependencies from install NApp.
Args:
user(string) A Username.
napp(string): A NApp name.
Returns:
napps(list): List with tuples with Username and NApp name.
e.g. [('kytos'/'of_core'), ('kytos/of_l2ls')]
"""
napps = self._get_napp_key('napp_dependencies', user, napp)
return [tuple(napp.split('/')) for napp in napps]
def get_description(self, user=None, napp=None):
"""Return the description from kytos.json."""
return self._get_napp_key('description', user, napp)
def get_version(self, user=None, napp=None):
"""Return the version from kytos.json."""
return self._get_napp_key('version', user, napp) or 'latest'
def _get_napp_key(self, key, user=None, napp=None):
"""Return a value from kytos.json.
Args:
user (string): A Username.
napp (string): A NApp name
key (string): Key used to get the value within kytos.json.
Returns:
meta (object): Value stored in kytos.json.
"""
if user is None:
user = self.user
if napp is None:
napp = self.napp
uri = self._kytos_api + self._NAPP_METADATA
uri = uri.format(user, napp, key)
meta = json.loads(urllib.request.urlopen(uri).read())
return meta[key]
def disable(self):
"""Disable a NApp if it is enabled."""
uri = self._kytos_api + self._NAPP_DISABLE
uri = uri.format(self.user, self.napp)
try:
json.loads(urllib.request.urlopen(uri).read())
except urllib.error.HTTPError as exception:
if exception.code == HTTPStatus.BAD_REQUEST.value:
LOG.error("NApp is not installed. Check the NApp list.")
else:
LOG.error("Error disabling the NApp")
def enable(self):
"""Enable a NApp if not already enabled."""
uri = self._kytos_api + self._NAPP_ENABLE
uri = uri.format(self.user, self.napp)
try:
json.loads(urllib.request.urlopen(uri).read())
except urllib.error.HTTPError as exception:
if exception.code == HTTPStatus.BAD_REQUEST.value:
LOG.error("NApp is not installed. Check the NApp list.")
else:
LOG.error("Error enabling the NApp")
def enabled_dir(self):
"""Return the enabled dir from current napp."""
return self._enabled / self.user / self.napp
def installed_dir(self):
"""Return the installed dir from current napp."""
return self._installed / self.user / self.napp
def is_enabled(self):
"""Whether a NApp is enabled."""
return (self.user, self.napp) in self.get_enabled()
def remote_uninstall(self):
"""Delete code inside NApp directory, if existent."""
uri = self._kytos_api + self._NAPP_UNINSTALL
uri = uri.format(self.user, self.napp)
try:
json.loads(urllib.request.urlopen(uri).read())
except urllib.error.HTTPError as exception:
if exception.code == HTTPStatus.BAD_REQUEST.value:
LOG.error("Check if the NApp is installed.")
else:
LOG.error("Error uninstalling the NApp")
@staticmethod
def valid_name(username):
"""Check the validity of the given 'name'.
The following checks are done:
- name starts with a letter
- name contains only letters, numbers or underscores
"""
return username and re.match(r'[a-zA-Z][a-zA-Z0-9_]{2,}$', username)
@staticmethod
def render_template(templates_path, template_filename, context):
"""Render Jinja2 template for a NApp structure."""
template_env = Environment(
autoescape=False, trim_blocks=False,
loader=FileSystemLoader(str(templates_path)))
return template_env.get_template(str(template_filename)) \
.render(context)
@staticmethod
def search(pattern):
"""Search all server NApps matching pattern.
Args:
pattern (str): Python regular expression.
"""
def match(napp):
"""Whether a NApp metadata matches the pattern."""
# WARNING: This will change for future versions, when 'author' will
# be removed.
username = napp.get('username', napp.get('author'))
strings = ['{}/{}'.format(username, napp.get('name')),
napp.get('description')] + napp.get('tags')
return any(pattern.match(string) for string in strings)
napps = NAppsClient().get_napps()
return [napp for napp in napps if match(napp)]
def remote_install(self):
"""Ask kytos server to install NApp."""
uri = self._kytos_api + self._NAPP_INSTALL
uri = uri.format(self.user, self.napp)
try:
json.loads(urllib.request.urlopen(uri).read())
except urllib.error.HTTPError as exception:
if exception.code == HTTPStatus.BAD_REQUEST.value:
LOG.error("NApp is not installed. Check the NApp list.")
else:
LOG.error("Error installing the NApp.")
@classmethod
def create_napp(cls, meta_package=False):
"""Bootstrap a basic NApp structure for you to develop your NApp.
This will create, on the current folder, a clean structure of a NAPP,
filling some contents on this structure.
"""
templates_path = SKEL_PATH / 'napp-structure/username/napp'
ui_templates_path = os.path.join(templates_path, 'ui')
username = None
napp_name = None
print('--------------------------------------------------------------')
print('Welcome to the bootstrap process of your NApp.')
print('--------------------------------------------------------------')
print('In order to answer both the username and the napp name,')
print('You must follow this naming rules:')
print(' - name starts with a letter')
print(' - name contains only letters, numbers or underscores')
print(' - at least three characters')
print('--------------------------------------------------------------')
print('')
while not cls.valid_name(username):
username = input('Please, insert your NApps Server username: ')
while not cls.valid_name(napp_name):
napp_name = input('Please, insert your NApp name: ')
description = input('Please, insert a brief description for your'
'NApp [optional]: ')
if not description:
# pylint: disable=fixme
description = '# TODO: <<<< Insert your NApp description here >>>>'
# pylint: enable=fixme
context = {'username': username, 'napp': napp_name,
'description': description}
#: Creating the directory structure (username/napp_name)
os.makedirs(username, exist_ok=True)
#: Creating ``__init__.py`` files
with open(os.path.join(username, '__init__.py'), 'w') as init_file:
init_file.write(f'"""Napps for the user {username}.""""')
os.makedirs(os.path.join(username, napp_name))
#: Creating the other files based on the templates
templates = os.listdir(templates_path)
templates.remove('ui')
templates.remove('openapi.yml.template')
if meta_package:
templates.remove('main.py.template')
templates.remove('settings.py.template')
for tmp in templates:
fname = os.path.join(username, napp_name,
tmp.rsplit('.template')[0])
with open(fname, 'w') as file:
content = cls.render_template(templates_path, tmp, context)
file.write(content)
if not meta_package:
NAppsManager.create_ui_structure(username, napp_name,
ui_templates_path, context)
print()
print(f'Congratulations! Your NApp has been bootstrapped!\nNow you '
'can go to the directory {username}/{napp_name} and begin to '
'code your NApp.')
print('Have fun!')
@classmethod
def create_ui_structure(cls, username, napp_name, ui_templates_path,
context):
"""Create the ui directory structure."""
for section in ['k-info-panel', 'k-toolbar', 'k-action-menu']:
os.makedirs(os.path.join(username, napp_name, 'ui', section))
templates = os.listdir(ui_templates_path)
for tmp in templates:
fname = os.path.join(username, napp_name, 'ui',
tmp.rsplit('.template')[0])
with open(fname, 'w') as file:
content = cls.render_template(ui_templates_path, tmp,
context)
file.write(content)
@staticmethod
def _check_module(folder):
"""Create module folder with empty __init__.py if it doesn't exist.
Args:
folder (pathlib.pathlib.Path): Module path.
"""
if not folder.exists():
folder.mkdir(parents=True, exist_ok=True, mode=0o755)
(folder / '__init__.py').touch()
@staticmethod
def build_napp_package(napp_name):
"""Build the .napp file to be sent to the napps server.
Args:
napp_identifier (str): Identifier formatted as
<username>/<napp_name>
Return:
file_payload (binary): The binary representation of the napp
package that will be POSTed to the napp server.
"""
def get_matches(path):
"""Return all NApp files matching any .gitignore pattern."""
ignored_files = [".git"]
with open(".gitignore", 'r') as kytosignore:
ignored_files.extend(kytosignore.readlines())
# Define Wildmatch pattern (default gitignore pattern)
pattern = pathspec.patterns.GitWildMatchPattern
spec = pathspec.PathSpec.from_lines(pattern, ignored_files)
# Get tree containing all matching files
match_tree = spec.match_tree(path)
# Create list with all absolute paths of match tree
return ["%s/%s" % (path, match) for match in match_tree]
files = []
path = os.getcwd()
for dir_file in os.walk(path):
dirname, _, arc = dir_file
files.extend([os.path.join(dirname, f) for f in arc])
# Allow the user to run `kytos napps upload` from outside the
# napp directory.
# Filter the files with the napp_name in their path
# Example: home/user/napps/kytos/, napp_name = kronos
# This filter will get all files from:
# home/user/napps/kytos/kronos/*
files = list(filter(lambda x: napp_name in x, files))
matches = get_matches(path)
for filename in files.copy():
if filename in matches:
files.remove(filename)
# Create the '.napp' package
napp_file = tarfile.open(napp_name + '.napp', 'x:xz')
for local_f in files:
# Add relative paths instead of absolute paths
napp_file.add(pathlib.PurePosixPath(local_f).relative_to(path))
napp_file.close()
# Get the binary payload of the package
file_payload = open(napp_name + '.napp', 'rb')
# remove the created package from the filesystem
os.remove(napp_name + '.napp')
return file_payload
@staticmethod
def create_metadata(*args, **kwargs): # pylint: disable=unused-argument
"""Generate the metadata to send the napp package."""
json_filename = kwargs.get('json_filename', 'kytos.json')
readme_filename = kwargs.get('readme_filename', 'README.rst')
ignore_json = kwargs.get('ignore_json', False)
metadata = {}
if not ignore_json:
try:
with open(json_filename) as json_file:
metadata = json.load(json_file)
except FileNotFoundError:
print("ERROR: Could not access kytos.json file.")
sys.exit(1)
try:
with open(readme_filename) as readme_file:
metadata['readme'] = readme_file.read()
except FileNotFoundError:
metadata['readme'] = ''
try:
yaml = YAML(typ='safe')
openapi_dict = yaml.load(pathlib.Path('openapi.yml').open())
openapi = json.dumps(openapi_dict)
except FileNotFoundError:
openapi = ''
metadata['OpenAPI_Spec'] = openapi
return metadata
def upload(self, *args, **kwargs):
"""Create package and upload it to NApps Server.
Raises:
FileNotFoundError: If kytos.json is not found.
"""
self.prepare()
metadata = self.create_metadata(*args, **kwargs)
package = self.build_napp_package(metadata.get('name'))
NAppsClient().upload_napp(metadata, package)
def delete(self):
"""Delete a NApp.
Raises:
requests.HTTPError: When there's a server error.
"""
client = NAppsClient(self._config)
client.delete(self.user, self.napp)
@classmethod
def prepare(cls):
"""Prepare NApp to be uploaded by creating openAPI skeleton."""
if cls._ask_openapi():
napp_path = pathlib.Path()
tpl_path = SKEL_PATH / 'napp-structure/username/napp'
OpenAPI(napp_path, tpl_path).render_template()
print('Please, update your openapi.yml file.')
sys.exit()
@staticmethod
def _ask_openapi():
"""Return whether we should create a (new) skeleton."""
if pathlib.Path('openapi.yml').exists():
question = 'Override local openapi.yml with a new skeleton? (y/N) '
default = False
else:
question = 'Do you have REST endpoints and wish to create an API' \
' skeleton in openapi.yml? (Y/n) '
default = True
while True:
answer = input(question)
if answer == '':
return default
if answer.lower() in ['y', 'yes']:
return True
if answer.lower() in ['n', 'no']:
return False
def reload(self, napps=None):
"""Reload a NApp or all NApps.
Args:
napps (list): NApp list to be reloaded.
Raises:
requests.HTTPError: When there's a server error.
"""
client = NAppsClient(self._config)
client.reload_napps(napps)
# pylint: enable=too-many-instance-attributes,too-many-public-methods
|