Skip to content

Commit 839c813

Browse files
committed
added some setup script samples with no params restrictions
1 parent 6aa0a21 commit 839c813

File tree

23 files changed

+684
-0
lines changed

23 files changed

+684
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from cloudshell.api.cloudshell_api import CloudShellAPISession
2+
import xml.etree.ElementTree as ET
3+
4+
user = "admin"
5+
password = "admin"
6+
server = "localhost"
7+
domain = "Global"
8+
9+
api = CloudShellAPISession(host=server, username=user, password=password, domain=domain)
10+
model_data_str = api.ExportFamiliesAndModels().Configuration
11+
xml_data = ET.fromstring(model_data_str)

generic-orchestration-samples/setup_with_class_based_func/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from extended_models import CustomSandbox, CustomSetupWorkflow
2+
from blueprint_commands import BlueprintCommands
3+
4+
sandbox = CustomSandbox()
5+
6+
bp_commands = BlueprintCommands(sandbox)
7+
8+
CustomSetupWorkflow(sandbox).register()
9+
sandbox.workflow.on_configuration_ended(bp_commands.command_one)
10+
sandbox.workflow.on_configuration_ended(bp_commands.command_two)
11+
sandbox.execute_setup()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from extended_models import CustomSandbox
2+
3+
4+
class BlueprintCommands(object):
5+
def __init__(self, sandbox):
6+
"""
7+
:param CustomSandbox sandbox:
8+
"""
9+
self.sandbox = sandbox
10+
11+
def command_one(self):
12+
api = self.sandbox.automation_api
13+
api.WriteMessageToReservationOutput(self.sandbox.id, "hello from command one")
14+
15+
def command_two(self):
16+
api = self.sandbox.automation_api
17+
api.WriteMessageToReservationOutput(self.sandbox.id, "hello from command two")
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
from cloudshell.workflow.orchestration.sandbox import Sandbox
2+
from cloudshell.workflow.orchestration.setup.default_setup_logic import DefaultSetupLogic
3+
from cloudshell.workflow.orchestration.workflow import Workflow
4+
5+
6+
class CustomWorkflow(Workflow):
7+
def __init__(self, sandbox):
8+
super(CustomWorkflow, self).__init__(sandbox)
9+
10+
def _validate_function(self, func):
11+
self.sandbox.logger.info('Custom Sandbox Object being used. No validation done on Workflow functions')
12+
pass
13+
14+
15+
class CustomSandbox(Sandbox):
16+
def __init__(self):
17+
super(CustomSandbox, self).__init__()
18+
self.workflow = CustomWorkflow(self)
19+
20+
def _execute_workflow_process(self, func, components):
21+
self.logger.info("Executing method: {0}. ".format(func.__name__))
22+
execution_failed = 0
23+
try:
24+
self.logger.info("Executing workflow function with no parameters passed")
25+
func()
26+
27+
except Exception as exc:
28+
self.logger.info("except Exception as exc")
29+
execution_failed = 1
30+
if not hasattr(exc, 'message'): # python 3
31+
error = str(exc)
32+
self.logger.info("type of exc is {}".format(str(type(exc))))
33+
else:
34+
error = exc.message
35+
self._exception = exc
36+
if not error or not isinstance(error, str):
37+
try:
38+
error = str(exc)
39+
except Exception:
40+
pass
41+
42+
if self.suppress_exceptions:
43+
print (error)
44+
self.logger.exception("Error was thrown during orchestration execution: ")
45+
46+
return execution_failed
47+
48+
49+
class CustomSetupWorkflow(object):
50+
def __init__(self, sandbox):
51+
"""
52+
:param Sandbox sandbox:
53+
"""
54+
self.sandbox = sandbox
55+
self._deploy_result = None
56+
self._resource_details_cache = {}
57+
58+
def register(self, enable_provisioning=True, enable_connectivity=True, enable_configuration=True):
59+
"""
60+
:param bool enable_provisioning:
61+
:param bool enable_connectivity:
62+
:param bool enable_configuration:
63+
:return:
64+
"""
65+
self.sandbox.logger.info("Adding default setup orchestration")
66+
if enable_provisioning:
67+
self.sandbox.logger.debug("Default provisioning is added to sandbox orchestration")
68+
self.sandbox.workflow.add_to_provisioning(self.default_provisioning, None)
69+
if enable_connectivity:
70+
self.sandbox.logger.debug("Default connectivity is added to sandbox orchestration")
71+
self.sandbox.workflow.add_to_connectivity(self.default_connectivity, None)
72+
if enable_configuration:
73+
self.sandbox.logger.debug("Default configuration is added to sandbox orchestration")
74+
self.sandbox.workflow.add_to_configuration(self.default_configuration, None)
75+
76+
def default_provisioning(self):
77+
"""
78+
:return:
79+
"""
80+
api = self.sandbox.automation_api
81+
82+
self.sandbox.logger.info("Executing default provisioning")
83+
84+
reservation_details = api.GetReservationDetails(reservationId=self.sandbox.id, disableCache=True)
85+
self._deploy_result = DefaultSetupLogic.deploy_apps_in_reservation(api=api,
86+
reservation_details=reservation_details,
87+
reservation_id=self.sandbox.id,
88+
logger=self.sandbox.logger)
89+
90+
DefaultSetupLogic.validate_all_apps_deployed(deploy_results=self._deploy_result,
91+
logger=self.sandbox.logger)
92+
93+
self.sandbox.components.refresh_components(sandbox=self.sandbox)
94+
95+
DefaultSetupLogic.try_exeucte_autoload(api=api,
96+
deploy_result=self._deploy_result,
97+
resource_details_cache=self._resource_details_cache,
98+
reservation_id=self.sandbox.id,
99+
logger=self.sandbox.logger, components=self.sandbox.components)
100+
101+
def default_connectivity(self):
102+
"""
103+
:return:
104+
"""
105+
api = self.sandbox.automation_api
106+
107+
self.sandbox.logger.info("Executing default connectivity")
108+
109+
reservation_details = api.GetReservationDetails(reservationId=self.sandbox.id, disableCache=True)
110+
111+
connect_results = DefaultSetupLogic.connect_all_routes_in_reservation(api=api,
112+
reservation_details=reservation_details,
113+
reservation_id=self.sandbox.id,
114+
resource_details_cache=self._resource_details_cache,
115+
logger=self.sandbox.logger)
116+
117+
DefaultSetupLogic.activate_routes(api=api,
118+
reservation_details=reservation_details,
119+
reservation_id=self.sandbox.id,
120+
logger=self.sandbox.logger)
121+
122+
DefaultSetupLogic.run_async_power_on_refresh_ip(api=api,
123+
reservation_details=reservation_details,
124+
deploy_results=self._deploy_result,
125+
resource_details_cache=self._resource_details_cache,
126+
reservation_id=self.sandbox.id,
127+
logger=self.sandbox.logger,
128+
components=self.sandbox.components)
129+
130+
DefaultSetupLogic.refresh_vm_details(api=api,
131+
reservation_details=reservation_details,
132+
connect_results=connect_results,
133+
resource_details_cache=self._resource_details_cache,
134+
logger=self.sandbox.logger,
135+
components=self.sandbox.components)
136+
137+
def default_configuration(self):
138+
"""
139+
:return:
140+
"""
141+
self.sandbox.logger.info("Executing default configuration")
142+
DefaultSetupLogic.configure_apps(api=self.sandbox.automation_api,
143+
reservation_id=self.sandbox.id,
144+
logger=self.sandbox.logger)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cloudshell-orch-core<3.2.0.0,>=3.1.0.0

generic-orchestration-samples/setup_with_class_based_func_backwards_compatible/__init__.py

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from cloudshell.workflow.orchestration.setup.default_setup_orchestrator import DefaultSetupWorkflow
2+
from extended_models import CustomSandbox
3+
from blueprint_commands import BlueprintCommands, command_with_sandbox_param, command_with_sandbox_and_components_params
4+
5+
# instantiate custom sandbox object
6+
sandbox = CustomSandbox()
7+
8+
# instantiate class based commands container
9+
bp_commands = BlueprintCommands(sandbox)
10+
11+
# register sandbox with default workflow class
12+
DefaultSetupWorkflow().register(sandbox)
13+
14+
# pass in both custom class based and function based commands
15+
sandbox.workflow.on_configuration_ended(bp_commands.command_one)
16+
sandbox.workflow.on_configuration_ended(bp_commands.command_two)
17+
sandbox.workflow.on_configuration_ended(command_with_sandbox_param)
18+
sandbox.workflow.on_configuration_ended(command_with_sandbox_and_components_params)
19+
20+
sandbox.execute_setup()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from extended_models import CustomSandbox
2+
from cloudshell.workflow.orchestration.components import Components
3+
4+
5+
class BlueprintCommands(object):
6+
def __init__(self, sandbox):
7+
"""
8+
:param CustomSandbox sandbox:
9+
"""
10+
self.sandbox = sandbox
11+
12+
def command_one(self):
13+
api = self.sandbox.automation_api
14+
api.WriteMessageToReservationOutput(self.sandbox.id, "hello from bound method no param")
15+
16+
17+
def command_with_sandbox_param(sandbox):
18+
"""
19+
:param CustomSandbox sandbox:
20+
:return:
21+
"""
22+
api = sandbox.automation_api
23+
api.WriteMessageToReservationOutput(sandbox.id, "hello from function with sandbox param only")
24+
25+
26+
def command_with_sandbox_and_components_params(sandbox, components=None):
27+
"""
28+
:param CustomSandbox sandbox:
29+
:param Components components:
30+
:return:
31+
"""
32+
api = sandbox.automation_api
33+
api.WriteMessageToReservationOutput(sandbox.id, "hello from function with sandbox and component params")
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from cloudshell.workflow.orchestration.sandbox import Sandbox
2+
from cloudshell.workflow.orchestration.workflow import Workflow
3+
import inspect
4+
5+
6+
def _is_bound_method(method):
7+
"""
8+
check if the method or function is bound (will pass self as first arg)
9+
:param method:
10+
:return:
11+
"""
12+
return hasattr(method, '__self__')
13+
14+
15+
class CustomWorkflow(Workflow):
16+
def __init__(self, sandbox):
17+
super(CustomWorkflow, self).__init__(sandbox)
18+
19+
def _validate_function(self, func):
20+
"""
21+
changing validation from a requirement of two parameters, to restricting to max of 2 optional parameters
22+
bound class methods will receive an extra 'self' param
23+
:param function func: the user function that is passed to orchestration
24+
:return:
25+
"""
26+
args = inspect.getargspec(func).args
27+
is_bound_method = _is_bound_method(func)
28+
29+
if is_bound_method:
30+
if len(args) > 3:
31+
raise Exception("Orchestration methods can not have more than 2 parameters. (Sandbox, Components)")
32+
else:
33+
if len(args) > 2:
34+
raise Exception("Orchestration functions can not have more than 2 parameters. (Sandbox, Components)")
35+
36+
37+
class CustomSandbox(Sandbox):
38+
def __init__(self):
39+
super(CustomSandbox, self).__init__()
40+
self.workflow = CustomWorkflow(self)
41+
42+
def _execute_workflow_process(self, func, components):
43+
self.logger.info("Executing method: {0}. ".format(func.__name__))
44+
execution_failed = 0
45+
46+
args = inspect.getargspec(func).args
47+
is_bound_method = _is_bound_method(func)
48+
49+
try:
50+
if is_bound_method:
51+
if len(args) == 1:
52+
self.logger.info("Executing workflow bound method with no additional parameters passed")
53+
func()
54+
elif len(args) == 2:
55+
self.logger.info("Executing workflow bound method with 1 parameter passed (Sandbox)")
56+
func(self)
57+
else:
58+
self.logger.info("Executing workflow bound method with 2 parameters passed (Sandbox, Components)")
59+
func(self, components)
60+
else:
61+
if not len(args):
62+
self.logger.info("Executing workflow function with no parameters passed")
63+
func()
64+
elif len(args) == 1:
65+
self.logger.info("Executing workflow function with 1 parameter passed (Sandbox)")
66+
func(self)
67+
else:
68+
self.logger.info("Executing workflow function with 2 parameters passed (Sandbox, Components)")
69+
func(self, components)
70+
71+
except Exception as exc:
72+
self.logger.info("except Exception as exc")
73+
execution_failed = 1
74+
if not hasattr(exc, 'message'): # python 3
75+
error = str(exc)
76+
self.logger.info("type of exc is {}".format(str(type(exc))))
77+
else:
78+
error = exc.message
79+
self._exception = exc
80+
if not error or not isinstance(error, str):
81+
try:
82+
error = str(exc)
83+
except Exception:
84+
pass
85+
86+
if self.suppress_exceptions:
87+
print (error)
88+
self.logger.exception("Error was thrown during orchestration execution: ")
89+
90+
return execution_failed
91+
92+

0 commit comments

Comments
 (0)