Philipp Schrader | d0e33a4 | 2022-01-22 21:55:15 -0800 | [diff] [blame] | 1 | def _apache_binary_impl(ctx): |
| 2 | binary_path = ctx.attr.binary.files_to_run.executable.short_path |
| 3 | |
| 4 | out = ctx.actions.declare_file(ctx.label.name) |
| 5 | ctx.actions.write(out, """\ |
| 6 | #!/bin/bash |
| 7 | |
| 8 | exec ./tools/build_rules/apache_runner --binary "{}" "$@" |
| 9 | """.format(binary_path), is_executable = True) |
| 10 | |
| 11 | # Collect files and runfiles for the tools that we're wrapping. |
| 12 | files = depset(transitive = [ |
| 13 | ctx.attr._apache_runner.files, |
| 14 | ctx.attr.binary.files, |
| 15 | ]) |
| 16 | |
| 17 | runfiles = ctx.attr._apache_runner.default_runfiles |
| 18 | runfiles = runfiles.merge(ctx.attr.binary.default_runfiles) |
| 19 | |
| 20 | return [ |
| 21 | DefaultInfo( |
| 22 | executable = out, |
| 23 | files = files, |
| 24 | runfiles = runfiles, |
| 25 | ), |
| 26 | ] |
| 27 | |
| 28 | apache_wrapper = rule( |
| 29 | implementation = _apache_binary_impl, |
| 30 | attrs = { |
| 31 | "binary": attr.label( |
| 32 | mandatory = True, |
| 33 | executable = True, |
| 34 | cfg = "target", |
| 35 | doc = "The binary that we're wrapping with LDAP+HTTPS.", |
| 36 | ), |
| 37 | "_apache_runner": attr.label( |
| 38 | default = "@//tools/build_rules:apache_runner", |
| 39 | executable = True, |
| 40 | cfg = "target", |
| 41 | ), |
| 42 | }, |
| 43 | doc = """\ |
| 44 | This rule wraps another web server and provides LDAP and HTTPS support. |
| 45 | |
| 46 | It's not intended to be used in production. It's intended to provide team |
| 47 | members with a way to test their code in a production-like environment. E.g. to |
| 48 | test whether your server makes use of LDAP credentials correctly. |
| 49 | |
| 50 | Write this as a wrapper around another binary like so: |
| 51 | |
| 52 | apache_wrapper( |
| 53 | name = "wrapped_server", |
| 54 | binary = "//path/to:server_binary", |
| 55 | ) |
| 56 | |
| 57 | Then you can run Apache and the wrapped binary like so: |
| 58 | |
| 59 | $ bazel run :wrapped_server |
| 60 | |
| 61 | The wrapped binary can find the port that Apache is wrapping via the |
| 62 | APACHE_WRAPPED_PORT environment variable. |
| 63 | |
| 64 | This rule assumes that you have a file at the root of the workspace called |
| 65 | "ldap.json". You can customize this path with the `--ldap_info` argument. The |
| 66 | JSON file has to have these three entries in it: |
| 67 | |
| 68 | { |
| 69 | "ldap_bind_dn": "...", |
| 70 | "ldap_url": "...", |
| 71 | "ldap_password": "..." |
| 72 | } |
| 73 | |
| 74 | where the "..." values are replaced with the information to connect to an LDAP |
| 75 | server. If you want to connect to our FRC971 LDAP server, please contact a |
| 76 | Software mentor. Or ask on the `#coding` Slack channel. |
| 77 | |
| 78 | If the default ports of 7000 and 7500 are already taken, you can change them via |
| 79 | the `--https_port` and `--wrapped_port` arguments. |
| 80 | """, |
| 81 | executable = True, |
| 82 | ) |