Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 1 | # This file is run by shell scripts generated by the aos_downloader Skylark |
| 2 | # macro. Everything before the first -- is a hard-coded list of files to |
| 3 | # download. |
| 4 | |
Brian Silverman | d54068e | 2015-10-14 17:56:05 -0400 | [diff] [blame] | 5 | from __future__ import print_function |
| 6 | |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 7 | import argparse |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 8 | import sys |
| 9 | import subprocess |
Brian Silverman | d54068e | 2015-10-14 17:56:05 -0400 | [diff] [blame] | 10 | import re |
| 11 | import os |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 12 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 13 | |
Brian Silverman | bd7860e | 2020-01-05 17:52:40 -0800 | [diff] [blame] | 14 | def install(ssh_target, pkg, ssh_path, scp_path): |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 15 | """Installs a package from NI on the ssh target.""" |
| 16 | print("Installing", pkg) |
| 17 | PKG_URL = "http://download.ni.com/ni-linux-rt/feeds/2015/arm/ipk/cortexa9-vfpv3/" + pkg |
| 18 | subprocess.check_call(["wget", PKG_URL, "-O", pkg]) |
| 19 | try: |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 20 | subprocess.check_call( |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 21 | [scp_path, "-S", ssh_path, pkg, ssh_target + ":/tmp/" + pkg]) |
| 22 | subprocess.check_call( |
| 23 | [ssh_path, ssh_target, "opkg", "install", "/tmp/" + pkg]) |
| 24 | subprocess.check_call([ssh_path, ssh_target, "rm", "/tmp/" + pkg]) |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 25 | finally: |
| 26 | subprocess.check_call(["rm", pkg]) |
Austin Schuh | b64799c | 2015-12-19 22:35:51 -0800 | [diff] [blame] | 27 | |
| 28 | |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 29 | def main(argv): |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 30 | parser = argparse.ArgumentParser() |
| 31 | parser.add_argument("--target", |
| 32 | type=str, |
| 33 | default="roborio-971-frc.local", |
| 34 | help="Target to deploy code to.") |
| 35 | parser.add_argument("--type", |
| 36 | type=str, |
| 37 | choices=["roborio", "pi"], |
| 38 | required=True, |
| 39 | help="Target type for deployment") |
| 40 | parser.add_argument( |
| 41 | "--dir", |
| 42 | type=str, |
| 43 | help="Directory within robot_code to copy the files to.") |
| 44 | parser.add_argument("srcs", |
| 45 | type=str, |
| 46 | nargs='+', |
| 47 | help="List of files to copy over") |
| 48 | args = parser.parse_args(argv[1:]) |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 49 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 50 | relative_dir = "" |
| 51 | recursive = False |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 52 | |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 53 | srcs = args.srcs |
| 54 | if args.dir is not None: |
| 55 | relative_dir = args.dir |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 56 | recursive = True |
Austin Schuh | b64799c | 2015-12-19 22:35:51 -0800 | [diff] [blame] | 57 | |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 58 | destination = args.target |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 59 | |
| 60 | result = re.match("(?:([^:@]+)@)?([^:@]+)(?::([^:@]+))?", destination) |
| 61 | if not result: |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 62 | print("Not sure how to parse destination \"%s\"!" % destination, |
| 63 | file=sys.stderr) |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 64 | return 1 |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 65 | user = None |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 66 | if result.group(1): |
| 67 | user = result.group(1) |
| 68 | hostname = result.group(2) |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 69 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 70 | if result.group(3): |
| 71 | target_dir = result.group(3) |
| 72 | |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 73 | if user is None: |
| 74 | if args.type == "pi": |
| 75 | user = "pi" |
| 76 | elif args.type == "roborio": |
| 77 | user = "admin" |
| 78 | target_dir = "/home/" + user + "/robot_code" |
| 79 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 80 | ssh_target = "%s@%s" % (user, hostname) |
| 81 | |
Brian Silverman | bd7860e | 2020-01-05 17:52:40 -0800 | [diff] [blame] | 82 | ssh_path = "external/ssh/ssh" |
| 83 | scp_path = "external/ssh/scp" |
| 84 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 85 | rsync_cmd = ([ |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 86 | "external/rsync/usr/bin/rsync", "-e", ssh_path, "-c", "-v", "-z", |
| 87 | "--copy-links" |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 88 | ] + srcs + ["%s:%s/%s" % (ssh_target, target_dir, relative_dir)]) |
| 89 | try: |
| 90 | subprocess.check_call(rsync_cmd) |
| 91 | except subprocess.CalledProcessError as e: |
Brian Silverman | bd7860e | 2020-01-05 17:52:40 -0800 | [diff] [blame] | 92 | if e.returncode == 127 or e.returncode == 12: |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 93 | print("Unconfigured roboRIO, installing rsync.") |
Brian Silverman | bd7860e | 2020-01-05 17:52:40 -0800 | [diff] [blame] | 94 | install(ssh_target, "libattr1_2.4.47-r0.36_cortexa9-vfpv3.ipk", |
| 95 | ssh_path, scp_path) |
| 96 | install(ssh_target, "libacl1_2.2.52-r0.36_cortexa9-vfpv3.ipk", |
| 97 | ssh_path, scp_path) |
| 98 | install(ssh_target, "rsync_3.1.0-r0.7_cortexa9-vfpv3.ipk", |
| 99 | ssh_path, scp_path) |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 100 | subprocess.check_call(rsync_cmd) |
| 101 | else: |
| 102 | raise e |
| 103 | |
| 104 | if not recursive: |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame^] | 105 | subprocess.check_call((ssh_path, ssh_target, "&&".join([ |
| 106 | "chmod u+s %s/starter_exe" % target_dir, |
| 107 | "echo \'Done moving new executables into place\'", |
| 108 | "bash -c \'sync && sync && sync\'", |
| 109 | ]))) |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 110 | |
| 111 | |
| 112 | if __name__ == "__main__": |
| 113 | main(sys.argv) |