6 """Script to set the value of the Connectivity Service port in the specified Session of the specified
7 OKS database file. If the new port is not specified, it is set to a random available k8s NodePort."""
9 if not session_name:
10 print("Error: the session name needs to be specified")
11 return 0
12 else:
13 try:
14 session = db.get_dal("Session", session_name)
15 except Exception:
16 print(f"Error: could not find Session {session_name} in file {oksfile}")
17 return 0
18
19 k8s_min_port, k8s_max_port = 30000, 32767
20 if connsvc_port == 0:
21 new_port = find_free_port(k8s_min_port, k8s_max_port)
22 print(f"Found free Kubernetes NodePort: {new_port}")
23 else:
24 new_port = connsvc_port
25 if not (k8s_min_port <= new_port <= k8s_max_port):
26 print(f"Warning: Port {new_port} is outside the standard k8s NodePort range ({k8s_min_port}-{k8s_max_port}).")
27
28
29 if session.connectivity_service is not None:
30 session.connectivity_service.service.port = new_port
31 db.update_dal(session.connectivity_service.service)
32 print(f"Updated Connectivity Service '{session.connectivity_service.service.id}' to use port {new_port}")
33 else:
34 print(f"Error: Session '{session_name}' has no connectivity_service defined. Skipping Service object update.")
35 return 0
36
37
38 if hasattr(session, 'environment') and session.environment is not None:
39 found_var = False
40 for item in session.environment:
41 if item.className() == 'VariableSet':
42 for var in item.contains:
43 if var.name == "CONNECTION_PORT":
44 var.value = str(new_port)
45 db.update_dal(var)
46 print(f"Updated runtime environment variable '{var.id}' to '{new_port}'")
47 found_var = True
48 break
49 elif item.className() == 'Variable':
50 if item.name == "CONNECTION_PORT":
51 item.value = str(new_port)
52 db.update_dal(item)
53 print(f"Updated runtime environment variable '{item.id}' to '{new_port}'")
54 found_var = True
55
56 if found_var:
57 break
58
59 if not found_var:
60 print("Error: Could not find a 'CONNECTION_PORT' variable in the session's environment.")
61 return 0
62 else:
63 print("Error: Session has no 'environment' configured. Cannot update CONNECTION_PORT variable.")
64 return 0
65
66 db.commit()
67 print(f"Successfully configured connectivity service port for session '{session_name}'.")
68 return new_port