Sandbox ssh and rsync

Otherwise we get a ssh version missmatch on Ubuntu Xenial.  ssh and scp
pick up the openssl library from the packaged Python.  Which is really
old and doesn't work with host versions of rsync and ssh.

Change-Id: Id881b602a236b2ac2cc405a6a48fe4215e2f9c7a
diff --git a/WORKSPACE b/WORKSPACE
index 4dc3f78..961cc40 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -15,6 +15,14 @@
     patch_debs = "files",
 )
 load(
+    "//debian:rsync.bzl",
+    rsync_debs = "files",
+)
+load(
+    "//debian:ssh.bzl",
+    ssh_debs = "files",
+)
+load(
     "//debian:pandoc.bzl",
     pandoc_debs = "files",
 )
@@ -52,6 +60,10 @@
 
 generate_repositories_for_debs(clang_debs)
 
+generate_repositories_for_debs(rsync_debs)
+
+generate_repositories_for_debs(ssh_debs)
+
 generate_repositories_for_debs(patch_debs)
 
 generate_repositories_for_debs(pandoc_debs)
@@ -222,6 +234,20 @@
 )
 
 http_archive(
+    name = "rsync",
+    build_file = "@//debian:rsync.BUILD",
+    sha256 = "53be65a9214aaa6d1b9176f135184fb4a78ccefd58f95ce0da37e6a392dfeb60",
+    url = "http://www.frc971.org/Build-Dependencies/rsync.tar.gz",
+)
+
+http_archive(
+    name = "ssh",
+    build_file = "@//debian:ssh.BUILD",
+    sha256 = "43c760bc5c32d5a8c24837d1b4c356424a157f59ff8a08654651856ae90b16d2",
+    url = "http://www.frc971.org/Build-Dependencies/ssh.tar.gz",
+)
+
+http_archive(
     name = "pandoc",
     build_file = "@//debian:pandoc.BUILD",
     sha256 = "9f7a7adb3974a1f14715054c349ff3edc2909e920dbe3438fca437a83845f3c4",
diff --git a/aos/downloader/BUILD b/aos/downloader/BUILD
index 536a7ad..1aab653 100644
--- a/aos/downloader/BUILD
+++ b/aos/downloader/BUILD
@@ -1,7 +1,12 @@
 py_binary(
-  name = 'downloader',
-  visibility = ['//visibility:public'],
-  srcs = [
-    'downloader.py',
-  ],
+    name = "downloader",
+    srcs = [
+        "downloader.py",
+    ],
+    data = [
+        "@rsync",
+        "@ssh",
+        "@ssh//:scp",
+    ],
+    visibility = ["//visibility:public"],
 )
diff --git a/aos/downloader/downloader.py b/aos/downloader/downloader.py
index 9799b1f..e49ee5d 100644
--- a/aos/downloader/downloader.py
+++ b/aos/downloader/downloader.py
@@ -9,74 +9,86 @@
 import re
 import os
 
+
 def install(ssh_target, pkg):
-  """Installs a package from NI on the ssh target."""
-  print('Installing', pkg)
-  PKG_URL = 'http://download.ni.com/ni-linux-rt/feeds/2015/arm/ipk/cortexa9-vfpv3/' + pkg
-  subprocess.check_call(['wget', PKG_URL, '-O', pkg])
-  try:
-    subprocess.check_call(['scp', pkg, ssh_target + ':/tmp/' + pkg])
-    subprocess.check_call(['ssh', ssh_target, 'opkg', 'install', '/tmp/' + pkg])
-    subprocess.check_call(['ssh', ssh_target, 'rm', '/tmp/' + pkg])
-  finally:
-    subprocess.check_call(['rm', pkg])
+    """Installs a package from NI on the ssh target."""
+    print("Installing", pkg)
+    PKG_URL = "http://download.ni.com/ni-linux-rt/feeds/2015/arm/ipk/cortexa9-vfpv3/" + pkg
+    subprocess.check_call(["wget", PKG_URL, "-O", pkg])
+    try:
+        subprocess.check_call([
+            "external/ssh/usr/bin/scp", "-S", "external/ssh/usr/bin/ssh", pkg,
+            ssh_target + ":/tmp/" + pkg
+        ])
+        subprocess.check_call([
+            "external/ssh/usr/bin/ssh", ssh_target, "opkg", "install",
+            "/tmp/" + pkg
+        ])
+        subprocess.check_call(
+            ["external/ssh/usr/bin/ssh", ssh_target, "rm", "/tmp/" + pkg])
+    finally:
+        subprocess.check_call(["rm", pkg])
 
 
 def main(argv):
-  args = argv[argv.index('--') + 1:]
+    args = argv[argv.index("--") + 1:]
 
-  relative_dir = ''
-  recursive = False
+    relative_dir = ""
+    recursive = False
 
-  if '--dirs' in argv:
-    dirs_index = argv.index('--dirs')
-    srcs = argv[1:dirs_index]
-    relative_dir = argv[dirs_index + 1]
-    recursive = True
-  else:
-    srcs = argv[1:argv.index('--')]
-
-  ROBORIO_TARGET_DIR = '/home/admin/robot_code'
-  ROBORIO_USER = 'admin'
-
-  target_dir = ROBORIO_TARGET_DIR
-  user = ROBORIO_USER
-  destination = args[-1]
-
-  result = re.match('(?:([^:@]+)@)?([^:@]+)(?::([^:@]+))?', destination)
-  if not result:
-    print('Not sure how to parse destination "%s"!' % destination,
-          file=sys.stderr)
-    return 1
-  if result.group(1):
-    user = result.group(1)
-  hostname = result.group(2)
-  if result.group(3):
-    target_dir = result.group(3)
-
-  ssh_target = '%s@%s' % (user, hostname)
-
-  rsync_cmd = (['rsync', '-c', '-v', '-z', '--copy-links'] + srcs +
-               ['%s:%s/%s' % (ssh_target, target_dir, relative_dir)])
-  try:
-    subprocess.check_call(rsync_cmd)
-  except subprocess.CalledProcessError as e:
-    if e.returncode == 127:
-      print('Unconfigured roboRIO, installing rsync.')
-      install(ssh_target, 'libattr1_2.4.47-r0.36_cortexa9-vfpv3.ipk')
-      install(ssh_target, 'libacl1_2.2.52-r0.36_cortexa9-vfpv3.ipk')
-      install(ssh_target, 'rsync_3.1.0-r0.7_cortexa9-vfpv3.ipk')
-      subprocess.check_call(rsync_cmd)
+    if "--dirs" in argv:
+        dirs_index = argv.index("--dirs")
+        srcs = argv[1:dirs_index]
+        relative_dir = argv[dirs_index + 1]
+        recursive = True
     else:
-      raise e
+        srcs = argv[1:argv.index("--")]
 
-  if not recursive:
-    subprocess.check_call(
-        ('ssh', ssh_target, '&&'.join([
-            'chmod u+s %s/starter_exe' % target_dir,
-            'echo \'Done moving new executables into place\'',
-            'bash -c \'sync && sync && sync\'',
-          ])))
+    ROBORIO_TARGET_DIR = "/home/admin/robot_code"
+    ROBORIO_USER = "admin"
 
-if __name__ == '__main__':
-  main(sys.argv)
+    target_dir = ROBORIO_TARGET_DIR
+    user = ROBORIO_USER
+    destination = args[-1]
+
+    result = re.match("(?:([^:@]+)@)?([^:@]+)(?::([^:@]+))?", destination)
+    if not result:
+        print(
+            "Not sure how to parse destination \"%s\"!" % destination,
+            file=sys.stderr)
+        return 1
+    if result.group(1):
+        user = result.group(1)
+    hostname = result.group(2)
+    if result.group(3):
+        target_dir = result.group(3)
+
+    ssh_target = "%s@%s" % (user, hostname)
+
+    rsync_cmd = ([
+        "external/rsync/usr/bin/rsync", "-e", "external/ssh/usr/bin/ssh", "-c",
+        "-v", "-z", "--copy-links"
+    ] + srcs + ["%s:%s/%s" % (ssh_target, target_dir, relative_dir)])
+    try:
+        subprocess.check_call(rsync_cmd)
+    except subprocess.CalledProcessError as e:
+        if e.returncode == 127:
+            print("Unconfigured roboRIO, installing rsync.")
+            install(ssh_target, "libattr1_2.4.47-r0.36_cortexa9-vfpv3.ipk")
+            install(ssh_target, "libacl1_2.2.52-r0.36_cortexa9-vfpv3.ipk")
+            install(ssh_target, "rsync_3.1.0-r0.7_cortexa9-vfpv3.ipk")
+            subprocess.check_call(rsync_cmd)
+        else:
+            raise e
+
+    if not recursive:
+        subprocess.check_call(
+            ("external/ssh/usr/bin/ssh", ssh_target, "&&".join([
+                "chmod u+s %s/starter_exe" % target_dir,
+                "echo \'Done moving new executables into place\'",
+                "bash -c \'sync && sync && sync\'",
+            ])))
+
+
+if __name__ == "__main__":
+    main(sys.argv)
diff --git a/debian/BUILD b/debian/BUILD
index 370c388..f7d3113 100644
--- a/debian/BUILD
+++ b/debian/BUILD
@@ -13,6 +13,14 @@
     patch_debs = "files",
 )
 load(
+    ":rsync.bzl",
+    rsync_debs = "files",
+)
+load(
+    ":ssh.bzl",
+    ssh_debs = "files",
+)
+load(
     ":pandoc.bzl",
     pandoc_debs = "files",
 )
@@ -242,6 +250,16 @@
 )
 
 generate_deb_tarball(
+    name = "rsync",
+    files = rsync_debs,
+)
+
+generate_deb_tarball(
+    name = "ssh",
+    files = ssh_debs,
+)
+
+generate_deb_tarball(
     name = "pandoc",
     files = pandoc_debs,
 )
diff --git a/debian/rsync.BUILD b/debian/rsync.BUILD
new file mode 100644
index 0000000..4fe45b1
--- /dev/null
+++ b/debian/rsync.BUILD
@@ -0,0 +1,5 @@
+filegroup(
+    name = "rsync",
+    srcs = ["usr/bin/rsync"],
+    visibility = ["//visibility:public"],
+)
diff --git a/debian/rsync.bzl b/debian/rsync.bzl
new file mode 100644
index 0000000..6be610d
--- /dev/null
+++ b/debian/rsync.bzl
@@ -0,0 +1,3 @@
+files = {
+    "rsync_3.1.2-1+deb9u2_amd64.deb": "f2987623a6e5b5aedf56e679bedd2e6f7d54cdb4815ac4149c4d135da16ff9c2",
+}
diff --git a/debian/ssh.BUILD b/debian/ssh.BUILD
new file mode 100644
index 0000000..fb7f120
--- /dev/null
+++ b/debian/ssh.BUILD
@@ -0,0 +1,11 @@
+filegroup(
+    name = "ssh",
+    srcs = ["usr/bin/ssh"],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "scp",
+    srcs = ["usr/bin/scp"],
+    visibility = ["//visibility:public"],
+)
diff --git a/debian/ssh.bzl b/debian/ssh.bzl
new file mode 100644
index 0000000..005768c
--- /dev/null
+++ b/debian/ssh.bzl
@@ -0,0 +1,3 @@
+files = {
+    "openssh-client_6.7p1-5+deb8u8_amd64.deb": "50bf902cc680fd1442556325e47d892f24621d7f0c4baf826f298d737a1e8030",
+}