summaryrefslogtreecommitdiff
path: root/tests/chutney/setup
blob: 2030448871dd7e57ed8baa34476b0007933ddea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3

# stdlib imports
import argparse
import os
import shutil
import subprocess
import sys
import textwrap

# stdlib "from" imports
from argparse import ArgumentParser, Namespace
from pathlib import Path

# "local" imports
from config import Config
from typing import Optional

_SCRIPT_NAME = Path(sys.argv[0]).name
_SCRIPT_DIR = Path(sys.argv[0]).parent.resolve()
_TOP_LEVEL = Path(
    subprocess.check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip()
)


class ChutneyBinResolver:
    _DEST = "chutney_bin"

    def __init__(self, parser: ArgumentParser) -> None:
        parser.add_argument("--chutney-bin", dest=self._DEST, help="chutney executable")

    def resolve(self, args: Namespace) -> Path:
        res_str = getattr(args, self._DEST) or shutil.which("chutney")
        if res_str is None:
            print(textwrap.dedent("""
                    chutney (https://gitlab.torproject.org/tpo/core/chutney) not found.
                    Try setting --chutney-bin or ensuring it's on your PATH.
                    quick install:
                    python3 -m pip install git+https://gitlab.torproject.org/tpo/core/chutney.git
                    """))
            sys.exit(1)
        res = Path(res_str)
        if not os.access(res, os.X_OK):
            print(f"chutney resolved to {res}, but it isn't an executable file")
            sys.exit(1)
        return res


class ArtiBinResolver:
    _DEST = "arti_bin"

    def __init__(self, parser: ArgumentParser) -> None:
        parser.add_argument("--arti-bin", dest=self._DEST, help="arti executable")

    def resolve(self, args: Namespace) -> Path:
        res_str = getattr(args, self._DEST)
        if res_str is None:
            for s in [
                "target/x86_64-unknown-linux-gnu/quicktest/arti",
                "target/quicktest/arti",
                "target/x86_64-unknown-linux-gnu/debug/arti",
                "target/debug/arti",
            ]:
                p = _TOP_LEVEL.joinpath(Path(s))
                if p.exists():
                    res_str = str(p)
                    break
        if res_str is None:
            print(textwrap.dedent("""
                    arti client not found.
                    Try setting --arti-bin or building one in this repo:
                    cargo build --locked --profile=quicktest -p arti
                    """))
            sys.exit(1)
        res = Path(res_str)
        if not os.access(res, os.X_OK):
            print(f"arti resolved to {res}, but it isn't an executable file")
            sys.exit(1)
        return res


class ArtiExtraBinResolver:
    _DEST = "arti_extra_bin"

    def __init__(self, parser: ArgumentParser) -> None:
        parser.add_argument(
            "--arti-extra-bin",
            dest=self._DEST,
            help="arti executable with extra features",
        )

    def resolve(self, args: Namespace) -> Path:
        res_str = getattr(args, self._DEST)
        if res_str is None:
            for s in [
                "target/x86_64-unknown-linux-gnu/quicktest/arti-extra",
                "target/quicktest/arti-extra",
                "target/x86_64-unknown-linux-gnu/debug/arti-extra",
                "target/debug/arti-extra",
            ]:
                p = _TOP_LEVEL.joinpath(Path(s))
                if p.exists():
                    res_str = str(p)
                    break
        if res_str is None:
            print(textwrap.dedent("""
                    arti-extra client not found.
                    Try setting --arti-extra-bin or building one in this repo.

                    # See .rust-recent-arti-extra-features-template in .gitlab-ci.yml for current
                    # --features list.
                    $ cargo build --locked --profile=quicktest -p arti --features=full,experimental
                    $ mv target/quicktest/arti target/quicktest/arti-extra
                    """))
            sys.exit(1)
        res = Path(res_str)
        if not os.access(res, os.X_OK):
            print(f"arti resolved to {res}, but it isn't an executable file")
            sys.exit(1)
        return res


class ArtiBenchBinResolver:
    _DEST = "arti_bench_bin"

    def __init__(self, parser: ArgumentParser) -> None:
        parser.add_argument(
            "--arti-bench-bin", dest=self._DEST, help="arti-bench executable"
        )

    def resolve(self, args: Namespace) -> Path:
        res_str = getattr(args, self._DEST)
        if res_str is None:
            for s in [
                "target/x86_64-unknown-linux-gnu/release/arti-bench",
                "target/release/arti-bench",
                "target/x86_64-unknown-linux-gnu/quicktest/arti-bench",
                "target/quicktest/arti-bench",
                "target/x86_64-unknown-linux-gnu/debug/arti-bench",
                "target/debug/arti-bench",
            ]:
                p = _TOP_LEVEL.joinpath(Path(s))
                if p.exists():
                    res_str = str(p)
                    break
        if res_str is None:
            print(textwrap.dedent("""
                    arti-bench not found.
                    Try setting --arti-bench-bin or building one in this repo:
                    cargo build --locked --profile=quicktest -p arti-bench
                    """))
            sys.exit(1)
        res = Path(res_str)
        if not os.access(res, os.X_OK):
            print(f"arti-bench resolved to {res}, but it isn't an executable file")
            sys.exit(1)
        return res


class ChutneyDataDirResolver:
    _DEST = "chutney_data_dir"

    def __init__(self, parser: ArgumentParser) -> None:
        parser.add_argument(
            "--chutney-data-dir",
            dest=self._DEST,
            help="directory for chutney to put its data",
        )

    def resolve(self, args: Namespace) -> Path:
        return Path(
            getattr(args, self._DEST)
            or os.getenv("CHUTNEY_DATA_DIR")
            or _TOP_LEVEL.joinpath("chutney-net")
        )


class ChutneyNetworkResolver:
    _DEST = "network"

    def __init__(self, parser: ArgumentParser) -> None:
        parser.add_argument(
            "--network",
            "-n",
            dest=self._DEST,
            help="flag to 'chutney init' specifying network to use."
            " (Default: built-in network-builder)",
        )

    def resolve(self, args: Namespace) -> Optional[str]:
        res: Optional[str] = getattr(args, self._DEST)
        return res


class ConfigResolver:
    def __init__(self, parser: ArgumentParser) -> None:
        self._chutney_resolver = ChutneyBinResolver(parser)
        self._arti_resolver = ArtiBinResolver(parser)
        self._arti_extra_resolver = ArtiExtraBinResolver(parser)
        self._arti_bench_resolver = ArtiBenchBinResolver(parser)
        self._chutney_data_dir_resolver = ChutneyDataDirResolver(parser)
        self._network_resolver = ChutneyNetworkResolver(parser)

    def resolve(self, args: Namespace) -> Config:
        return Config(
            chutney=str(self._chutney_resolver.resolve(args)),
            arti=str(self._arti_resolver.resolve(args)),
            arti_extra=str(self._arti_extra_resolver.resolve(args)),
            arti_bench=str(self._arti_bench_resolver.resolve(args)),
            chutney_data_dir=str(self._chutney_data_dir_resolver.resolve(args)),
            network=self._network_resolver.resolve(args),
        )


def _main() -> None:
    # Set up command-line parser, registering config options
    parser = argparse.ArgumentParser(
        prog=_SCRIPT_NAME, description="Configure a chutney testbed"
    )
    config_resolver = ConfigResolver(parser)
    args = parser.parse_args()
    config = config_resolver.resolve(args)
    config.dump_json(_SCRIPT_DIR.joinpath("arti.run.json"))


if __name__ == "__main__":
    _main()