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() |
Austin Schuh | dcffd09 | 2021-10-17 21:51:39 -0700 | [diff] [blame^] | 31 | parser.add_argument( |
| 32 | "--target", |
| 33 | type=str, |
| 34 | default="roborio-971-frc.local", |
| 35 | help="Target to deploy code to.") |
| 36 | parser.add_argument( |
| 37 | "--type", |
| 38 | type=str, |
| 39 | choices=["roborio", "pi"], |
| 40 | required=True, |
| 41 | help="Target type for deployment") |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 42 | parser.add_argument( |
| 43 | "--dir", |
| 44 | type=str, |
| 45 | help="Directory within robot_code to copy the files to.") |
Austin Schuh | dcffd09 | 2021-10-17 21:51:39 -0700 | [diff] [blame^] | 46 | parser.add_argument( |
| 47 | "srcs", type=str, nargs='+', help="List of files to copy over") |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 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: |
Austin Schuh | dcffd09 | 2021-10-17 21:51:39 -0700 | [diff] [blame^] | 62 | print( |
| 63 | "Not sure how to parse destination \"%s\"!" % destination, |
| 64 | file=sys.stderr) |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 65 | return 1 |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 66 | user = None |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 67 | if result.group(1): |
| 68 | user = result.group(1) |
| 69 | hostname = result.group(2) |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 70 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 71 | if result.group(3): |
| 72 | target_dir = result.group(3) |
| 73 | |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 74 | if user is None: |
| 75 | if args.type == "pi": |
James Kuszmaul | be46f7f | 2020-10-03 13:40:57 -0700 | [diff] [blame] | 76 | user = "pi" |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 77 | elif args.type == "roborio": |
James Kuszmaul | be46f7f | 2020-10-03 13:40:57 -0700 | [diff] [blame] | 78 | user = "admin" |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 79 | target_dir = "/home/" + user + "/robot_code" |
| 80 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 81 | ssh_target = "%s@%s" % (user, hostname) |
| 82 | |
Brian Silverman | bd7860e | 2020-01-05 17:52:40 -0800 | [diff] [blame] | 83 | ssh_path = "external/ssh/ssh" |
| 84 | scp_path = "external/ssh/scp" |
| 85 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 86 | rsync_cmd = ([ |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 87 | "external/rsync/usr/bin/rsync", "-e", ssh_path, "-c", "-v", "-z", |
James Kuszmaul | be46f7f | 2020-10-03 13:40:57 -0700 | [diff] [blame] | 88 | "--perms", "--copy-links" |
Austin Schuh | dcffd09 | 2021-10-17 21:51:39 -0700 | [diff] [blame^] | 89 | ] + srcs) |
| 90 | |
| 91 | # If there is only 1 file to transfer, we would overwrite the destination |
| 92 | # folder. In that case, specify the full path to the target. |
| 93 | if len(srcs) == 1: |
| 94 | rsync_cmd += [ |
| 95 | "%s:%s/%s/%s" % (ssh_target, target_dir, relative_dir, srcs[0]) |
| 96 | ] |
| 97 | else: |
| 98 | rsync_cmd += ["%s:%s/%s" % (ssh_target, target_dir, relative_dir)] |
| 99 | |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 100 | try: |
| 101 | subprocess.check_call(rsync_cmd) |
| 102 | except subprocess.CalledProcessError as e: |
Brian Silverman | bd7860e | 2020-01-05 17:52:40 -0800 | [diff] [blame] | 103 | if e.returncode == 127 or e.returncode == 12: |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 104 | print("Unconfigured roboRIO, installing rsync.") |
Brian Silverman | bd7860e | 2020-01-05 17:52:40 -0800 | [diff] [blame] | 105 | install(ssh_target, "libattr1_2.4.47-r0.36_cortexa9-vfpv3.ipk", |
| 106 | ssh_path, scp_path) |
| 107 | install(ssh_target, "libacl1_2.2.52-r0.36_cortexa9-vfpv3.ipk", |
| 108 | ssh_path, scp_path) |
| 109 | install(ssh_target, "rsync_3.1.0-r0.7_cortexa9-vfpv3.ipk", |
| 110 | ssh_path, scp_path) |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 111 | subprocess.check_call(rsync_cmd) |
Austin Schuh | dcffd09 | 2021-10-17 21:51:39 -0700 | [diff] [blame^] | 112 | elif e.returncode == 11: |
| 113 | # Directory wasn't created, make it and try again. This keeps the happy path fast. |
| 114 | subprocess.check_call( |
| 115 | [ssh_path, ssh_target, "mkdir", "-p", target_dir]) |
| 116 | subprocess.check_call(rsync_cmd) |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 117 | else: |
| 118 | raise e |
| 119 | |
| 120 | if not recursive: |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 121 | subprocess.check_call((ssh_path, ssh_target, "&&".join([ |
Austin Schuh | 44e0b14 | 2021-10-16 15:51:10 -0700 | [diff] [blame] | 122 | "chmod u+s %s/starterd.stripped" % target_dir, |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 123 | "echo \'Done moving new executables into place\'", |
Austin Schuh | dcffd09 | 2021-10-17 21:51:39 -0700 | [diff] [blame^] | 124 | "bash -c \'sync\'", |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 125 | ]))) |
Austin Schuh | 71f6fa7 | 2019-08-31 18:23:02 -0700 | [diff] [blame] | 126 | |
| 127 | |
| 128 | if __name__ == "__main__": |
| 129 | main(sys.argv) |