From 31f568535680a9a61dff2d7e12115e2b7cfb294c Mon Sep 17 00:00:00 2001 From: Zachary Lentz Date: Fri, 15 Mar 2024 11:52:36 -0700 Subject: [PATCH 1/3] DOC: add checklist item for checking dry run before future PRs --- .github/pull_request_template.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 6606661..f247a5d 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -21,6 +21,7 @@ ## Pre-merge checklist - [ ] Code works interactively +- [ ] Code works interactively in dry_run mode - [ ] Code contains descriptive docstrings, including context and API - [ ] Pre-commit passes on GitHub Actions - [ ] Documentation under https://confluence.slac.stanford.edu/display/PCDS/TcBSD has been updated appropriately From 951973961d0a57c50b20cdb85106cd36675c4814 Mon Sep 17 00:00:00 2001 From: Zachary Lentz Date: Fri, 15 Mar 2024 11:53:04 -0700 Subject: [PATCH 2/3] FIX: allow provisioning playbook to skip config check in dry run --- tcbsd-provision-playbook.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/tcbsd-provision-playbook.yaml b/tcbsd-provision-playbook.yaml index a58ef38..93c4bc7 100644 --- a/tcbsd-provision-playbook.yaml +++ b/tcbsd-provision-playbook.yaml @@ -12,6 +12,7 @@ cmd: TcSysExe.exe --mode - name: Assert that PLC is in CONFIG mode + when: not ansible_check_mode ansible.builtin.assert: that: "{{ 'CONFIG' in plc_mode.stdout }}" fail_msg: "PLC is in RUN mode! Abort!" From 5b505318bd67fcdaf045ccd10db317c2608e1701 Mon Sep 17 00:00:00 2001 From: Zachary Lentz Date: Fri, 15 Mar 2024 17:17:16 -0700 Subject: [PATCH 3/3] ENH: implement dynamic selection of ams net id at runtime, with backcompat for the vms --- group_vars/tcbsd_plcs/vars.yml | 5 +++++ group_vars/tcbsd_vms/vars.yml | 4 ++++ host_vars/plc-tmo-tmp-vac/vars.yml | 6 ++++- host_vars/plc-tst-bsd1/vars.yml | 6 ++++- host_vars/plc-tst-bsd2/vars.yml | 6 ++++- ip_macros.j2 | 2 +- scripts/make_vars.py | 36 ++++++++++++++++++++---------- tcbsd-provision-playbook.yaml | 7 +++++- 8 files changed, 55 insertions(+), 17 deletions(-) diff --git a/group_vars/tcbsd_plcs/vars.yml b/group_vars/tcbsd_plcs/vars.yml index 7bd2c3d..0466a5c 100644 --- a/group_vars/tcbsd_plcs/vars.yml +++ b/group_vars/tcbsd_plcs/vars.yml @@ -11,6 +11,11 @@ enable_freebsd_packages: false use_psproxy: true use_psntp: true +# Dynamic AMS net id = set AMS net id to ip addr .1.1 +# Static AMS net id = set AMS net id to the value of tc_ams_net_id +dynamic_ams: true +# tc_ams_net_id: 0.0.0.0.1.1 + # set static IP on x000 (mac id 2) x000_set_static_ip: true x000_static_ip: 192.168.1.10 diff --git a/group_vars/tcbsd_vms/vars.yml b/group_vars/tcbsd_vms/vars.yml index a6d6342..fac1531 100644 --- a/group_vars/tcbsd_vms/vars.yml +++ b/group_vars/tcbsd_vms/vars.yml @@ -12,6 +12,10 @@ enable_freebsd_packages: false use_psproxy: false use_psntp: false +# Dynamic AMS net id = set AMS net id to ip addr .1.1 +# Static AMS net id = set AMS net id to the value of tc_ams_net_id +dynamic_ams: false + # set static IP on x000 (mac id 2) x000_set_static_ip: false x000_static_ip: 192.168.1.10 diff --git a/host_vars/plc-tmo-tmp-vac/vars.yml b/host_vars/plc-tmo-tmp-vac/vars.yml index 067140f..2cc7220 100644 --- a/host_vars/plc-tmo-tmp-vac/vars.yml +++ b/host_vars/plc-tmo-tmp-vac/vars.yml @@ -1,6 +1,5 @@ --- ansible_host: plc-tmo-tmp-vac -tc_ams_net_id: 172.21.132.78.1.1 # Uncomment any setting below and change it to override a default setting. #ansible_user: Administrator @@ -15,6 +14,11 @@ tc_ams_net_id: 172.21.132.78.1.1 #use_psproxy: true #use_psntp: true # +## Dynamic AMS net id = set AMS net id to ip addr .1.1 +## Static AMS net id = set AMS net id to the value of tc_ams_net_id +#dynamic_ams: true +## tc_ams_net_id: 0.0.0.0.1.1 +# ## set static IP on x000 (mac id 2) #x000_set_static_ip: true #x000_static_ip: 192.168.1.10 diff --git a/host_vars/plc-tst-bsd1/vars.yml b/host_vars/plc-tst-bsd1/vars.yml index 2e07f53..0a4bf35 100644 --- a/host_vars/plc-tst-bsd1/vars.yml +++ b/host_vars/plc-tst-bsd1/vars.yml @@ -1,6 +1,5 @@ --- ansible_host: plc-tst-bsd1 -tc_ams_net_id: 172.21.148.81.1.1 # Uncomment any setting below and change it to override a default setting. #ansible_user: Administrator @@ -15,6 +14,11 @@ tc_ams_net_id: 172.21.148.81.1.1 #use_psproxy: true #use_psntp: true # +## Dynamic AMS net id = set AMS net id to ip addr .1.1 +## Static AMS net id = set AMS net id to the value of tc_ams_net_id +#dynamic_ams: true +## tc_ams_net_id: 0.0.0.0.1.1 +# ## set static IP on x000 (mac id 2) #x000_set_static_ip: true #x000_static_ip: 192.168.1.10 diff --git a/host_vars/plc-tst-bsd2/vars.yml b/host_vars/plc-tst-bsd2/vars.yml index 65e1489..edcae94 100644 --- a/host_vars/plc-tst-bsd2/vars.yml +++ b/host_vars/plc-tst-bsd2/vars.yml @@ -1,6 +1,5 @@ --- ansible_host: plc-tst-bsd2 -tc_ams_net_id: 172.21.148.94.1.1 # Uncomment any setting below and change it to override a default setting. #ansible_user: Administrator @@ -15,6 +14,11 @@ tc_ams_net_id: 172.21.148.94.1.1 #use_psproxy: true #use_psntp: true # +## Dynamic AMS net id = set AMS net id to ip addr .1.1 +## Static AMS net id = set AMS net id to the value of tc_ams_net_id +#dynamic_ams: true +## tc_ams_net_id: 0.0.0.0.1.1 +# ## set static IP on x000 (mac id 2) #x000_set_static_ip: true #x000_static_ip: 192.168.1.10 diff --git a/ip_macros.j2 b/ip_macros.j2 index 7a3dc70..b4b2112 100644 --- a/ip_macros.j2 +++ b/ip_macros.j2 @@ -1,5 +1,5 @@ {%- macro ip_to_ams_net_id(hostname) -%} -{%- set ipaddr = lookup('dig', hostname) -%} +{%- set ipaddr = lookup('community.general.dig', hostname, fail_on_error=True) -%} {{ ipaddr }}.1.1 {%- endmacro -%} diff --git a/scripts/make_vars.py b/scripts/make_vars.py index 619d3af..0c61a20 100644 --- a/scripts/make_vars.py +++ b/scripts/make_vars.py @@ -10,7 +10,6 @@ import socket from typing import Iterator from pathlib import Path -from string import Template from ruamel.yaml import YAML @@ -65,26 +64,36 @@ def get_netid(hostname: str) -> str: return ipaddr + ".1.1" +def tcbsd_vms_extra_vars( + hostname: str, +) -> dict[str, str]: + """ + Assign a starting ams net id for the tcbsd vms. + + The IPs for these can be less stable than for CDS PLCs, + so don't dynamically update it every run. + """ + return {"tc_ams_net_id": get_netid(hostname)} + + def write_host_vars( hostname: str, host_vars_path: str | Path, group_vars_path: str | Path, - template_path: str | Path, + extra_vars: dict[str, str] | None = None, ) -> None: """Write the vars.yml file given the necessary information.""" - # Load the template, sub in the values - with Path(template_path).open("r") as fd: - template = Template(fd.read()) - host_vars_text = template.substitute( - PLC_IP=hostname, - PLC_NET_ID=get_netid(hostname=hostname), - ) + if extra_vars is None: + extra_vars = {} # Load the group vars, prepend with comment with Path(group_vars_path).open("r") as fd: group_vars_lines = ["#" + line for line in fd.read().splitlines()[1:]] # Write the new file with Path(host_vars_path).open("w") as fd: - fd.write(host_vars_text) + fd.write("---\n") + fd.write(f"ansible_host: {hostname}\n") + for key, value in extra_vars.items(): + fd.write(f"{key}: {value}") fd.write( "\n" "# Uncomment any setting below and change it " @@ -104,15 +113,18 @@ def main(hostname: str) -> int: inventory_path=inventory_path, groups_path=groups_path, ) + if group == "tcbsd_vms": + extra_vars = tcbsd_vms_extra_vars(hostname) + else: + extra_vars = {} group_vars_path = groups_path / group / "vars.yml" - template_path = repo_root / "tcbsd-plc.yaml.template" host_vars_path = repo_root / "host_vars" / hostname / "vars.yml" host_vars_path.parent.mkdir(exist_ok=True) write_host_vars( hostname=hostname, host_vars_path=host_vars_path, group_vars_path=group_vars_path, - template_path=template_path, + extra_vars=extra_vars, ) print( f"Created {host_vars_path}, " diff --git a/tcbsd-provision-playbook.yaml b/tcbsd-provision-playbook.yaml index 93c4bc7..8e92b6d 100644 --- a/tcbsd-provision-playbook.yaml +++ b/tcbsd-provision-playbook.yaml @@ -156,7 +156,12 @@ community.general.xml: path: /usr/local/etc/TwinCAT/3.1/TcRegistry.xml xpath: /TcRegistry/Key[@Name='HKLM']/Key[@Name='Software']/Key[@Name='Beckhoff']/Key[@Name='TwinCAT3']/Key[@Name='System']/Value[@Name='AmsNetId'] - value: "{% import 'ip_macros.j2' as ip_macros %}{{ ip_macros.ams_net_id_to_binary(tc_ams_net_id) }}" + value: >- + {%- import 'ip_macros.j2' as ip_macros -%} + {%- if dynamic_ams -%} + {%- set tc_ams_net_id = ip_macros.ip_to_ams_net_id(ansible_host) -%} + {%- endif -%} + {{ ip_macros.ams_net_id_to_binary(tc_ams_net_id) }} register: ams_net_id - name: Adjust the locked memory size