Deploy the Julia runtime to the scouting server

This patch creates a .deb package to deploy Julia and the
DriverRank.jl dependencies to the scouting server.

This should let the deployed scouting server run the script without
too much trouble.

Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: I419808b1567d5d56f2052864843b7f3b53223d31
diff --git a/scouting/DriverRank/BUILD b/scouting/DriverRank/BUILD
index e82fbfb..0e8c8d5 100644
--- a/scouting/DriverRank/BUILD
+++ b/scouting/DriverRank/BUILD
@@ -1,3 +1,5 @@
+load("@rules_pkg//:pkg.bzl", "pkg_deb", "pkg_tar")
+
 filegroup(
     name = "driver_rank_script",
     srcs = [
@@ -5,3 +7,57 @@
     ],
     visibility = ["//scouting:__subpackages__"],
 )
+
+pkg_tar(
+    name = "julia_runtime",
+    package_dir = "opt/frc971/julia_runtime",
+    deps = [
+        "@julia//:runtime",
+    ],
+)
+
+pkg_tar(
+    name = "julia_manifest",
+    srcs = [
+        "Manifest.toml",
+        "Project.toml",
+        "activate.jl",
+    ],
+    package_dir = "opt/frc971/julia_manifest",
+)
+
+pkg_tar(
+    name = "julia_files",
+    deps = [
+        ":julia_manifest",
+        ":julia_runtime",
+    ],
+)
+
+pkg_deb(
+    name = "frc971-scouting-julia",
+    architecture = "amd64",
+    data = ":julia_files",
+    description = "The Julia files for the FRC971 scouting web server.",
+    maintainer = "frc971@frc971.org",
+    package = "frc971-scouting-julia",
+    postinst = "postinst",
+    version = "1",
+)
+
+py_binary(
+    name = "deploy",
+    srcs = [
+        "deploy.py",
+    ],
+    args = [
+        "--deb",
+        "$(location :frc971-scouting-julia)",
+    ],
+    data = [
+        ":frc971-scouting-julia",
+    ],
+    deps = [
+        "//scouting/deploy",
+    ],
+)
diff --git a/scouting/DriverRank/README.md b/scouting/DriverRank/README.md
new file mode 100644
index 0000000..b744755
--- /dev/null
+++ b/scouting/DriverRank/README.md
@@ -0,0 +1,15 @@
+# Driver ranking parsing script
+
+This directory contains the script that parses the raw data that the scouts
+collect on driver rankings and gives each team a score.
+
+## Deployment
+
+Whenever the Julia environment is set up for the first time or whenever the
+dependencies are updated, the Julia package needs to be redeployed. This is a
+separate step from the scouting server deployment because the Julia runtime is
+huge.
+
+```console
+$ bazel run //scouting/DriverRank:deploy -- --host scouting
+```
diff --git a/scouting/DriverRank/activate.jl b/scouting/DriverRank/activate.jl
new file mode 100644
index 0000000..741d05d
--- /dev/null
+++ b/scouting/DriverRank/activate.jl
@@ -0,0 +1,4 @@
+# A small helper to install all the dependencies on the scouting server.
+using Pkg
+Pkg.activate(ARGS[1])
+Pkg.instantiate()
diff --git a/scouting/DriverRank/deploy.py b/scouting/DriverRank/deploy.py
new file mode 100644
index 0000000..6e88edf
--- /dev/null
+++ b/scouting/DriverRank/deploy.py
@@ -0,0 +1,6 @@
+import sys
+
+from org_frc971.scouting.deploy.deploy import main
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv))
diff --git a/scouting/DriverRank/postinst b/scouting/DriverRank/postinst
new file mode 100644
index 0000000..3c21b2b
--- /dev/null
+++ b/scouting/DriverRank/postinst
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+set -o errexit
+set -o nounset
+set -o pipefail
+
+export PATH="/opt/frc971/julia_runtime/bin:${PATH}"
+export JULIA_DEPOT_PATH=/var/frc971/scouting/julia_depot/
+export JULIA_PROJECT=/opt/frc971/julia_manifest
+
+julia /opt/frc971/julia_manifest/activate.jl /opt/frc971/julia_manifest
diff --git a/scouting/deploy/BUILD b/scouting/deploy/BUILD
index 2bf4b4e..eb8b537 100644
--- a/scouting/deploy/BUILD
+++ b/scouting/deploy/BUILD
@@ -42,6 +42,9 @@
     name = "frc971-scouting-server",
     architecture = "amd64",
     data = ":deploy_tar",
+    depends = [
+        "frc971-scouting-julia",
+    ],
     description = "The FRC971 scouting web server.",
     # TODO(phil): What's a good email address for this?
     maintainer = "frc971@frc971.org",
@@ -66,4 +69,5 @@
     data = [
         ":frc971-scouting-server",
     ],
+    visibility = ["//scouting/DriverRank:__pkg__"],
 )
diff --git a/scouting/deploy/scouting.service b/scouting/deploy/scouting.service
index 5aa64b0..94582cd 100644
--- a/scouting/deploy/scouting.service
+++ b/scouting/deploy/scouting.service
@@ -8,6 +8,11 @@
 Type=simple
 WorkingDirectory=/opt/frc971/scouting_server
 Environment=RUNFILES_DIR=/opt/frc971/scouting_server
+# Add "julia" to the PATH.
+Environment=PATH=/opt/frc971/scouting/julia_runtime/bin:/usr/local/bin:/usr/bin:/bin
+# Use the Julia cache set up by the frc971-scouting-julia package.
+Environment=JULIA_DEPOT_PATH=/var/frc971/scouting/julia_depot/
+Environment=JULIA_PROJECT=/opt/frc971/julia_manifest
 ExecStart=/opt/frc971/scouting_server/scouting/scouting \
     -port 8080 \
     -db_config /var/frc971/scouting/db_config.json \