Skip to content

Commit 07d8d1e

Browse files
committed
mock orchestration sample
1 parent 851e236 commit 07d8d1e

File tree

11 files changed

+210
-4
lines changed

11 files changed

+210
-4
lines changed

generic-orchestration-samples/setup/setup_start_traffic/__main__.py renamed to generic-orchestration-samples/setup/setup_start_breaking_point_traffic/__main__.py

File renamed without changes.

generic-orchestration-samples/setup/setup_start_traffic/requirements.txt renamed to generic-orchestration-samples/setup/setup_start_breaking_point_traffic/requirements.txt

File renamed without changes.

generic-orchestration-samples/setup/setup_start_traffic/start_traffic.py renamed to generic-orchestration-samples/setup/setup_start_breaking_point_traffic/start_traffic.py

File renamed without changes.

generic-orchestration-samples/setup/setup_start_traffic/update_script.py renamed to generic-orchestration-samples/setup/setup_start_breaking_point_traffic/update_script.py

File renamed without changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from cloudshell.workflow.orchestration.sandbox import Sandbox
2+
from cloudshell.helpers.scripts.cloudshell_dev_helpers import attach_to_cloudshell_as
3+
import credentials
4+
from dut_health_check import run_dut_health_check
5+
from start_traffic import start_traffic_flow
6+
7+
LIVE_SANDBOX_ID = "4f1c7f14-5e28-4a54-a611-5883ea84b39b"
8+
9+
attach_to_cloudshell_as(user=credentials.USER,
10+
password=credentials.PASSWORD,
11+
domain=credentials.DOMAIN,
12+
reservation_id=LIVE_SANDBOX_ID,
13+
server_address=credentials.SERVER)
14+
15+
sandbox = Sandbox()
16+
run_dut_health_check(sandbox=sandbox, components=None)
17+
start_traffic_flow(sandbox)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from cloudshell.workflow.orchestration.setup.default_setup_orchestrator import DefaultSetupWorkflow
2+
from cloudshell.workflow.orchestration.sandbox import Sandbox
3+
from start_traffic import start_traffic_flow
4+
from dut_health_check import run_dut_health_check
5+
6+
sandbox = Sandbox()
7+
8+
DefaultSetupWorkflow().register(sandbox)
9+
sandbox.workflow.on_connectivity_ended(run_dut_health_check)
10+
sandbox.workflow.on_configuration_ended(start_traffic_flow)
11+
sandbox.execute_setup()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from cloudshell.workflow.orchestration.sandbox import Sandbox
2+
3+
DUT_MODEL = "Putshell"
4+
5+
6+
class HealtCheckException(Exception):
7+
pass
8+
9+
10+
# ========== Primary Function ==========
11+
def run_dut_health_check(sandbox, components=None):
12+
"""
13+
Functions passed into orchestration flow MUST have (sandbox, components) signature
14+
:param Sandbox sandbox:
15+
:param components
16+
:return:
17+
"""
18+
api = sandbox.automation_api
19+
res_id = sandbox.id
20+
res_details = api.GetReservationDetails(res_id).ReservationDescription
21+
resources = res_details.Resources
22+
dut_resources = [resource for resource in resources
23+
if resource.ResourceModelName == DUT_MODEL]
24+
if not dut_resources:
25+
raise Exception("No DUT resource found")
26+
27+
for dut in dut_resources:
28+
try:
29+
api.WriteMessageToReservationOutput(res_id, f"Running health check for resource {dut.Name}")
30+
api.ExecuteCommand(reservationId=res_id,
31+
targetName=dut.Name,
32+
targetType="Resource",
33+
commandName="health_check",
34+
printOutput=True)
35+
except Exception as e:
36+
err_msg = f"Issue caught during health check. {str(e)}"
37+
api.WriteMessageToReservationOutput(err_msg)
38+
raise HealtCheckException(err_msg)
39+
api.WriteMessageToReservationOutput(res_id, f"Dut Health checks completed")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cloudshell-orch-core
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from cloudshell.workflow.orchestration.sandbox import Sandbox
2+
from cloudshell.api.cloudshell_api import InputNameValue
3+
4+
TRAFFIC_CONTROLLER_MODEL = "Trafficshell"
5+
6+
7+
def start_traffic_flow(sandbox, components=None):
8+
"""
9+
Functions passed into orchestration flow MUST have (sandbox, components) signature
10+
:param Sandbox sandbox:
11+
:param components
12+
:return:
13+
"""
14+
api = sandbox.automation_api
15+
res_id = sandbox.id
16+
res_details = api.GetReservationDetails(res_id).ReservationDescription
17+
resources = res_details.Resources
18+
traffic_controller_search = [x for x in resources if x.ResourceModelName == TRAFFIC_CONTROLLER_MODEL]
19+
if not traffic_controller_search:
20+
raise ValueError(f"Traffic Controller Service '{TRAFFIC_CONTROLLER_MODEL}' not found on canvas")
21+
22+
if len(traffic_controller_search) > 1:
23+
raise ValueError("Multiple traffic controllers found. Adjust script logic")
24+
25+
traffic_controller = traffic_controller_search[0]
26+
27+
api.WriteMessageToReservationOutput(reservationId=res_id, message=f"Starting traffic on {traffic_controller.Name}...")
28+
api.ExecuteCommand(reservationId=res_id,
29+
targetName=traffic_controller.Name,
30+
targetType="Resource",
31+
commandName="start_traffic",
32+
printOutput=True)
33+
34+
api.WriteMessageToReservationOutput(reservationId=res_id, message="load config flow finished")
35+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
NOTE: - This script is only for updating EXISTING scripts.
3+
- Scripts MUST be uploaded manually first time. (this tool can still be used to do zipping)
4+
"""
5+
from cloudshell.api.cloudshell_api import CloudShellAPISession
6+
import os
7+
import credentials # may need to create this module and set the constants for the api session
8+
9+
# ===== Optional Variables to set =======
10+
11+
# To name zip package something other than the default directory name
12+
CUSTOM_SCRIPT_NAME = ''
13+
14+
15+
# =======================================
16+
17+
18+
def get_api_session():
19+
return CloudShellAPISession(host=credentials.SERVER,
20+
username=credentials.USER,
21+
password=credentials.PASSWORD,
22+
domain=credentials.DOMAIN)
23+
24+
25+
def error_red(err_str):
26+
"""
27+
for printing errors in red in pycharm.
28+
:param err_str:
29+
:return:
30+
"""
31+
CRED = '\033[91m'
32+
CEND = '\033[0m'
33+
return CRED + err_str + CEND
34+
35+
36+
def get_zip_details():
37+
parent_dir_path = os.path.abspath('.')
38+
parent_dir_name = os.path.basename(parent_dir_path)
39+
script_name = CUSTOM_SCRIPT_NAME or parent_dir_name
40+
zip_file_name = script_name + '.zip'
41+
42+
return {"parent_dir_path": parent_dir_path,
43+
"parent_dir_name": parent_dir_name,
44+
"script_name": script_name,
45+
"zip_file_name": zip_file_name}
46+
47+
48+
def is_whitelisted(f, file_path, files_to_exclude):
49+
is_regular_file = os.path.isfile(file_path)
50+
is_not_excluded = f not in files_to_exclude
51+
is_not_pyc = not f.endswith('.pyc')
52+
return is_regular_file and is_not_excluded and is_not_pyc
53+
54+
55+
def make_zipfile(output_filename, source_dir, files_to_exclude, dirs_to_exclude):
56+
import zipfile
57+
with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as z:
58+
for root, dirs, files in os.walk(source_dir):
59+
dirs[:] = [d for d in dirs if d not in dirs_to_exclude]
60+
for f in files:
61+
file_path = os.path.join(root, f)
62+
if is_whitelisted(f, file_path, files_to_exclude):
63+
arcname = os.path.join(os.path.relpath(root, source_dir), f)
64+
z.write(file_path, arcname)
65+
66+
67+
def zip_files():
68+
zip_details = get_zip_details()
69+
zip_file_name = zip_details["zip_file_name"]
70+
dirs_to_exclude = [".git"]
71+
files_to_exclude = [zip_file_name, "venv", ".idea", "credentials.py"]
72+
try:
73+
make_zipfile(output_filename=zip_file_name,
74+
source_dir=zip_details["parent_dir_path"],
75+
files_to_exclude=files_to_exclude,
76+
dirs_to_exclude=dirs_to_exclude)
77+
except Exception as e:
78+
print(error_red("[-] error zipping up file: " + str(e)))
79+
exit(1)
80+
else:
81+
if zip_file_name in os.listdir("."):
82+
print("[+] ZIPPED UP: '{zip_name}'".format(zip_name=zip_file_name))
83+
else:
84+
print("[-] ZIP FILE NOT PRESENT")
85+
86+
87+
def update_script_api_wrapper(cs_ses, script_name, zip_address):
88+
try:
89+
cs_ses.UpdateScript(script_name, zip_address)
90+
except Exception as e:
91+
print(error_red("[-] ERROR UPDATING SCRIPT IN PORTAL\n" + str(e)) + "\n"
92+
"PLEASE LOAD SCRIPT MANUALLY THE FIRST TIME")
93+
exit(1)
94+
else:
95+
print("[+] '{script}' updated on CloudShell Successfully".format(script=script_name))
96+
97+
98+
def update_script_on_server():
99+
zip_files()
100+
cs_ses = get_api_session()
101+
zip_details = get_zip_details()
102+
update_script_api_wrapper(cs_ses=cs_ses,
103+
script_name=zip_details["script_name"],
104+
zip_address=zip_details["zip_file_name"])
105+
106+
107+
update_script_on_server()

0 commit comments

Comments
 (0)