Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a step before build to resolve the topology graph #102

Merged
merged 1 commit into from
Oct 22, 2024
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
60 changes: 34 additions & 26 deletions lib/topology/manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2016 Hewlett Packard Enterprise Development LP
# Copyright (C) 2015-2024 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,6 @@
from sys import exc_info
from warnings import warn
from copy import deepcopy
from inspect import signature
from datetime import datetime
from traceback import format_exc
from collections import OrderedDict
Expand Down Expand Up @@ -94,6 +93,7 @@ def __init__(self, engine=DEFAULT_PLATFORM, options=None, **kwargs):

self._platform = None
self._built = False
self._resolved = False

@property
def nml(self):
Expand Down Expand Up @@ -267,6 +267,33 @@ def is_built(self):
"""
return self._built

def resolve(self):
"""
Resolve the topology.

This method resolves the topology graph by calling the platform
to resolve the topology. If the topology is not resolvable, the
platform should raise an exception.
"""
if self._built:
raise RuntimeError(
'You cannot resolve an already built topology.'
)
# Instance platform
plugin = load_platform(self.engine)
timestamp = datetime.now().replace(microsecond=0).isoformat()

self._platform = plugin(
timestamp, self.graph, **self.options
)
if not hasattr(self._platform, 'resolve'):
log.warning('Platform does not implement resolve method.')
self._resolved = True
return

self._platform.resolve()
self._resolved = True

def build(self):
"""
Build the topology hold by this manager.
Expand All @@ -279,31 +306,12 @@ def build(self):
'You cannot build a topology twice.'
)

node_enode_map = OrderedDict()
timestamp = datetime.now().replace(microsecond=0).isoformat()
stage = 'instance'

# Instance platform
plugin = load_platform(self.engine)
if not self._resolved:
# To keep backward compatibility, resolve the topology if it was
# not resolved yet.
self.resolve()

# Check if platform support to pass arguments to keep backwards
# compatibility
if any(
param.kind == param.VAR_KEYWORD
for param in reversed(signature(plugin).parameters.values())
):
self._platform = plugin(
timestamp, self.graph, **self.options
)
else:
if self.options:
msg = (
'Ignoring platform options "{}" '
'as current platform "{}" does not support to pass options'
).format(self.options, self.engine)
log.warning(msg)
warn(msg, DeprecationWarning)
self._platform = plugin(timestamp, self.graph)
node_enode_map = OrderedDict()

try:
stage = 'pre_build'
Expand Down
8 changes: 7 additions & 1 deletion lib/topology/platforms/debug.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2016 Hewlett Packard Enterprise Development LP
# Copyright (C) 2015-2024 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,6 +44,12 @@ def __init__(self, timestamp, graph, **kwargs):
timestamp, graph, **kwargs
)

def resolve(self):
"""
See :meth:`BasePlatform.resolve` for more information.
"""
log.debug('[HOOK] resolve')

def pre_build(self):
"""
See :meth:`BasePlatform.pre_build` for more information.
Expand Down
13 changes: 12 additions & 1 deletion lib/topology/platforms/platform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (C) 2015-2016 Hewlett Packard Enterprise Development LP
# Copyright (C) 2015-2024 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,6 +52,17 @@ class BasePlatform(object):
def __init__(self, timestamp, graph, **kwargs):
super(BasePlatform, self).__init__()

def resolve(self):
"""
Resolve the topology.

This method is called after the platform is created and before the
build process starts. It should resolve the topology graph, making sure
all nodes, ports and links are correctly defined and all requirements
are satisfied. If the graph is not resolvable, this method should raise
an exception.
"""

@abstractmethod
def pre_build(self):
"""
Expand Down