Skip to content

Commit

Permalink
Adding flexibility for sceptre app (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaliar authored Feb 15, 2024
1 parent 0f6301d commit af7c8ef
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 18 deletions.
65 changes: 56 additions & 9 deletions src/python/phenix_apps/apps/sceptre/sceptre.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ def configure(self):
})
self.add_inject(hostname=engineer_workstation.hostname, inject=kwargs)

# Create auto putty connection startup script injection
if "connect_interval" in engineer_workstation.metadata:
kwargs = self.find_override(f"{engineer_workstation.hostname}_auto_winscp.ps1")
if kwargs is None:
kwargs = {"src": f"{engineer_directory}/auto_winscp.ps1"}
kwargs.update({
"dst": "/sceptre/startup/40-auto_winscp.ps1",
"description": "engineer_workstation auto putty connections",
})
self.add_inject(hostname=engineer_workstation.hostname, inject=kwargs)

# Create sceptre startup scheduler injections
# Mirrors the phenix startup scheduler but is needed in order to run things as local user for UI automation
kwargs = {
Expand Down Expand Up @@ -1306,9 +1317,25 @@ def pre_start(self):
######################## HMI pre-start ###################################
# Create hmi files
hmis = self.extract_nodes_type("hmi")
scada_servers = self.extract_nodes_type("scada-server")

for hmi in hmis:
hmi_ip = hmi.topology.network.interfaces[0].address
#add scada connection based on metadata or based on subnet if not metadata
hmi_scada_ips = []
if "connected_scadas" in hmi.metadata:
for hmi_scada_server in hmi.metadata.connected_scadas:
for scada_server in scada_servers:
if hmi_scada_server == scada_server.hostname:
hmi_scada_ips.append(scada_server.topology.network.interfaces[0].address)
else:
if len(scada_ips) == 1:
hmi_scada_ips = scada_ips
else:
for scada_ip in scada_ips:
if scada_ip.split('.')[:-1] == hmi_ip.split('.')[:-1]:
hmi_scada_ips.append(scada_ip)

hmi_directory = f"{self.sceptre_dir}/{hmi.hostname}"
os.makedirs(hmi_directory, exist_ok=True)

Expand All @@ -1320,33 +1347,39 @@ def pre_start(self):
hmi_mako,
self.mako_templates_path,
file_,
scada_ips=scada_ips,
scada_ips=hmi_scada_ips,
hmi_ip=hmi_ip,
)

######################## Engineer Workstation pre-start ###################################
# Create engineer workstation files
rtus = self.extract_nodes_type("rtu")
rtus = self.extract_nodes_type("fd-server")
eng_fd = []

for rtu in rtus:
eng_fd.append(rtu)

engineer_workstations = self.extract_nodes_type(
"engineering-workstation"
"engineer-workstation"
)

#get rtus that engineer workstation can connect to. Connect to all if none given.
for engineer_workstation in engineer_workstations:
if "connected_rtus" in engineer_workstation.metadata:
for engineer_rtu in engineer_workstation.metadata.connected_rtus:
for rtu in rtus:
if engineer_rtu == rtu.hostname:
eng_fd.append(rtu)
else:
for rtu in rtus:
eng_fd.append(rtu)

engineer_directory = (
f"{self.sceptre_dir}/{engineer_workstation.hostname}"
)

os.makedirs(engineer_directory, exist_ok=True)

# Wrtie engineer workstation putty injection
auto_engineer = f"{engineer_directory}/putty.ps1"
putty = f"{engineer_directory}/putty.ps1"
engineer_mako = "putty.mako"
with open(auto_engineer, "w") as file_:
with open(putty, "w") as file_:
utils.mako_serve_template(
engineer_mako,
self.mako_templates_path,
Expand All @@ -1355,6 +1388,20 @@ def pre_start(self):
eng_fd=eng_fd,
)

#automation script to actively create putty connection
if "connect_interval" in engineer_workstation.metadata:
auto_winscp = f"{engineer_directory}/auto_winscp.ps1"
auto_winscp_mako = "auto_winscp.mako"
with open(auto_winscp, "w") as file_:
utils.mako_serve_template(
auto_winscp_mako,
self.mako_templates_path,
file_,
eng_fd=eng_fd,
connect_interval=engineer_workstation.metadata.connect_interval
)


######################## Historian pre-start ###################################
# Determine primary/secondary historian
secondary_historian_ips = {"historian": []}
Expand Down
32 changes: 32 additions & 0 deletions src/python/phenix_apps/apps/sceptre/templates/auto_winscp.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function Phenix-StartupComplete {
$key = Get-Item -LiteralPath 'HKLM:\Software\phenix' -ErrorAction SilentlyContinue

if ($key) {
$val = $key.GetValue('startup')

if ($val) {
return $val -eq 'done'
}

return $false
}

return $false
}
while (-Not (Phenix-StartupComplete)) {
Start-Sleep -s 30
}

while ($true)
{
Start-Sleep -s ${connect_interval}
% for fd in eng_fd:
% for iface in fd.topology.network.interfaces:
% if not iface.type == 'serial' and iface.vlan.lower() != 'mgmt':
$winscp = Start-Process -FilePath "C:\Program Files (x86)\WinSCP\WinSCP.exe" -ArgumentList "/console /open -hostkey=* scp://root:SiaSd3te@${iface.address}" -passthru
Start-Sleep -s 5
Stop-Process $winscp.Id
% endif
% endfor
% endfor
}
2 changes: 0 additions & 2 deletions src/python/phenix_apps/apps/sceptre/templates/hmi.mako
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ Show-Sleep(200)
Echo ''
Echo 'Opening MySCADA Interface...'
% for scada_addr in scada_ips:
% if scada_addr.split('.')[:-1] == hmi_ip.split('.')[:-1] or len(scada_ips) == 1:
Start-Process -FilePath "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -ArgumentList ${scada_addr} -WindowStyle Maximized
% endif
% endfor
Echo 'Done.'

Expand Down
12 changes: 6 additions & 6 deletions src/python/phenix_apps/apps/sceptre/templates/putty.mako
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ while (-Not (Phenix-StartupComplete)) {
new-item -path "HKCU:\Software\SimonTatham\"
new-item -path "HKCU:\Software\SimonTatham\PuTTY\"
new-item -path "HKCU:\Software\SimonTatham\PuTTY\Sessions"
new-item -path "HKCU:\Software\SimonTatham\PuTTY\Sessions\${fd.name}"
% for iface in fd.interfaces:
% if not iface.type_ == 'serial' and iface.vlan_alias.lower() != 'mgmt':
new-itemproperty -path "HKCU:\Software\SimonTatham\PuTTY\Sessions\${fd.name}" -name Hostname -value ${iface.ipv4_address}
new-itemproperty -path "HKCU:\Software\SimonTatham\PuTTY\Sessions\${fd.name}" -name Protocol -value telnet
new-itemproperty -path "HKCU:\Software\SimonTatham\PuTTY\Sessions\${fd.name}" -name PortNumber -value 1337 -type DWord
new-item -path "HKCU:\Software\SimonTatham\PuTTY\Sessions\${fd.hostname}"
% for iface in fd.topology.network.interfaces:
% if not iface.type == 'serial' and iface.vlan.lower() != 'mgmt':
new-itemproperty -path "HKCU:\Software\SimonTatham\PuTTY\Sessions\${fd.hostname}" -name Hostname -value ${iface.address}
new-itemproperty -path "HKCU:\Software\SimonTatham\PuTTY\Sessions\${fd.hostname}" -name Protocol -value telnet
new-itemproperty -path "HKCU:\Software\SimonTatham\PuTTY\Sessions\${fd.hostname}" -name PortNumber -value 1337 -type DWord
<% break %>
% endif
% endfor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% if os == 'linux':
% for interface in ips:
% if interface['vlan'] and interface['vlan'].lower() == 'mgmt':
% if interface['vlan']:
echo 'ListenAddress ${interface['address']}' >> /etc/ssh/sshd_config
% endif
% endfor
Expand Down

0 comments on commit af7c8ef

Please sign in to comment.