|
1 | | -from argparse import ArgumentParser |
| 1 | +from pathlib import Path |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import typer |
| 5 | +from jinja2 import Template |
| 6 | +from ruamel.yaml import YAML |
2 | 7 |
|
3 | 8 | from . import __version__ |
| 9 | +from .copy import copy_rtems |
| 10 | +from .globals import GLOBALS |
| 11 | +from .telnet import ioc_connect |
4 | 12 |
|
5 | 13 | __all__ = ["main"] |
6 | 14 |
|
| 15 | +cli = typer.Typer() |
| 16 | + |
| 17 | + |
| 18 | +def version_callback(value: bool): |
| 19 | + if value: |
| 20 | + typer.echo(__version__) |
| 21 | + raise typer.Exit() |
| 22 | + |
| 23 | + |
| 24 | +@cli.callback() |
| 25 | +def main( |
| 26 | + version: Optional[bool] = typer.Option( |
| 27 | + None, |
| 28 | + "--version", |
| 29 | + callback=version_callback, |
| 30 | + is_eager=True, |
| 31 | + help="Print the version of ibek and exit", |
| 32 | + ), |
| 33 | +): |
| 34 | + """ |
| 35 | + Proxy for RTEMS IOCs controlling and monitoring |
| 36 | + """ |
| 37 | + |
| 38 | + |
| 39 | +@cli.command() |
| 40 | +def start( |
| 41 | + copy: bool = typer.Option( |
| 42 | + True, "--copy/--no-copy", help="copy binaries before connecting" |
| 43 | + ), |
| 44 | + reboot: bool = typer.Option( |
| 45 | + True, "--reboot/--no-reboot", help="reboot the IOC first" |
| 46 | + ), |
| 47 | +): |
| 48 | + """ |
| 49 | + Starts an RTEMS IOC. Places the IOC binaries in the expected location, |
| 50 | + restarts the IOC and connects stdio to the IOC console. |
| 51 | +
|
| 52 | + This should be called inside of a runtime IOC container after ibek |
| 53 | + has generated the runtime assets for the IOC. |
| 54 | +
|
| 55 | + The standard 'start.sh' in the runtime IOC will call this entry point if |
| 56 | + it detects that EPICS_HOST_ARCH==RTEMS-beatnik |
| 57 | +
|
| 58 | + args: |
| 59 | + copy: Copy the RTEMS binaries to the IOCs TFTP and NFS directories first |
| 60 | + reboot: Reboot the IOC once the binaries are copied and the connection is made |
| 61 | + """ |
| 62 | + print( |
| 63 | + f"Remote control startup of RTEMS IOC {GLOBALS.IOC_NAME}" |
| 64 | + f" at {GLOBALS.RTEMS_IOC_IP}" |
| 65 | + ) |
| 66 | + if copy: |
| 67 | + copy_rtems() |
| 68 | + ioc_connect(GLOBALS.RTEMS_CONSOLE, reboot=reboot) |
| 69 | + |
| 70 | + |
| 71 | +@cli.command() |
| 72 | +def dev( |
| 73 | + ioc_repo: Path = typer.Argument( |
| 74 | + ..., |
| 75 | + help="The beamline/accelerator repo holding the IOC instance", |
| 76 | + file_okay=False, |
| 77 | + exists=True, |
| 78 | + ), |
| 79 | + ioc_name: str = typer.Argument( |
| 80 | + ..., |
| 81 | + help="The name of the IOC instance to work on", |
| 82 | + ), |
| 83 | +): |
| 84 | + """ |
| 85 | + Sets up a devcontainer to work on an IOC instance. Must be run from within |
| 86 | + the developer container for the generic IOC that the instance uses. |
| 87 | +
|
| 88 | + args: |
| 89 | + ioc_repo: The path to the IOC repository that holds the instance |
| 90 | + ioc_name: The name of the IOC instance to work on |
| 91 | + """ |
| 92 | + |
| 93 | + ioc_path = ioc_repo / "services" / ioc_name |
| 94 | + |
| 95 | + values = ioc_repo / "helm/shared/values.yaml" |
| 96 | + if not values.exists(): |
| 97 | + typer.echo(f"Global settings file {values} not found. Exiting") |
| 98 | + raise typer.Exit(1) |
| 99 | + |
| 100 | + ioc_values = ioc_path / "values.yaml" |
| 101 | + if not ioc_values.exists(): |
| 102 | + typer.echo(f"Instance settings file {ioc_values} not found. Exiting") |
| 103 | + raise typer.Exit(1) |
| 104 | + |
| 105 | + env_vars = {} |
| 106 | + # TODO in future use pydantic and make a model for this but for now let's cheese it. |
| 107 | + with open(values) as fp: |
| 108 | + yaml = YAML(typ="safe").load(fp) |
| 109 | + try: |
| 110 | + ioc_group = yaml["ioc-instance"]["ioc_group"] |
| 111 | + for item in yaml["ioc-instance"]["globalEnv"]: |
| 112 | + env_vars[item["name"]] = item["value"] |
| 113 | + except KeyError: |
| 114 | + typer.echo(f"{values} not in expected format") |
| 115 | + raise typer.Exit(1) from None |
| 116 | + |
| 117 | + with open(ioc_values) as fp: |
| 118 | + yaml = YAML(typ="safe").load(fp) |
| 119 | + try: |
| 120 | + for item in yaml["shared"]["ioc-instance"]["iocEnv"]: |
| 121 | + env_vars[item["name"]] = item["value"] |
| 122 | + except KeyError: |
| 123 | + typer.echo(f"{ioc_values} not in expected format") |
| 124 | + raise typer.Exit(1) from None |
| 125 | + |
| 126 | + this_dir = Path(__file__).parent |
| 127 | + template = Path(this_dir / "rsync.sh.jinja").read_text() |
| 128 | + |
| 129 | + script = Template(template).render( |
| 130 | + env_vars=env_vars, |
| 131 | + ioc_group=ioc_group, |
| 132 | + ioc_name=ioc_name, |
| 133 | + ioc_path=ioc_path, |
| 134 | + ) |
| 135 | + |
| 136 | + script_file = Path("/tmp/dev_proxy.sh") |
| 137 | + script_file.write_text(script) |
7 | 138 |
|
8 | | -def main(args=None): |
9 | | - parser = ArgumentParser() |
10 | | - parser.add_argument("-v", "--version", action="version", version=__version__) |
11 | | - args = parser.parse_args(args) |
| 139 | + typer.echo(f"\nIOC {ioc_name} dev environment prepared for {ioc_repo}") |
| 140 | + typer.echo("You can now change and compile support module or iocs.") |
| 141 | + typer.echo("Then start the ioc with '/epics/ioc/start.sh'") |
| 142 | + typer.echo(f"\n\nPlease first source {script_file} to set up the dev environment.") |
12 | 143 |
|
13 | 144 |
|
14 | | -# test with: python -m rtems_proxy |
| 145 | +# test with: |
| 146 | +# pipenv run python -m ibek |
15 | 147 | if __name__ == "__main__": |
16 | | - main() |
| 148 | + cli() |
0 commit comments