blob: 5ebf417ace505517fa27ebd05adeae551102e96f [file] [log] [blame]
Brian Silvermanaf485d62015-10-12 00:37:14 -04001# 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 Silvermand54068e2015-10-14 17:56:05 -04005from __future__ import print_function
6
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -08007import argparse
Brian Silvermanaf485d62015-10-12 00:37:14 -04008import sys
9import subprocess
Brian Silvermand54068e2015-10-14 17:56:05 -040010import re
11import os
Brian Silvermanaf485d62015-10-12 00:37:14 -040012
Austin Schuh71f6fa72019-08-31 18:23:02 -070013
Brian Silvermanbd7860e2020-01-05 17:52:40 -080014def install(ssh_target, pkg, ssh_path, scp_path):
Austin Schuh71f6fa72019-08-31 18:23:02 -070015 """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 Schuh71f6fa72019-08-31 18:23:02 -070020 subprocess.check_call(
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080021 [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 Schuh71f6fa72019-08-31 18:23:02 -070025 finally:
26 subprocess.check_call(["rm", pkg])
Austin Schuhb64799c2015-12-19 22:35:51 -080027
28
Brian Silvermanaf485d62015-10-12 00:37:14 -040029def main(argv):
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080030 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 Silvermanaf485d62015-10-12 00:37:14 -040049
Austin Schuh71f6fa72019-08-31 18:23:02 -070050 relative_dir = ""
51 recursive = False
Comran Morshed38967332016-04-23 19:26:48 -070052
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080053 srcs = args.srcs
54 if args.dir is not None:
55 relative_dir = args.dir
Austin Schuh71f6fa72019-08-31 18:23:02 -070056 recursive = True
Austin Schuhb64799c2015-12-19 22:35:51 -080057
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080058 destination = args.target
Austin Schuh71f6fa72019-08-31 18:23:02 -070059
60 result = re.match("(?:([^:@]+)@)?([^:@]+)(?::([^:@]+))?", destination)
61 if not result:
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080062 print("Not sure how to parse destination \"%s\"!" % destination,
63 file=sys.stderr)
Austin Schuh71f6fa72019-08-31 18:23:02 -070064 return 1
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080065 user = None
Austin Schuh71f6fa72019-08-31 18:23:02 -070066 if result.group(1):
67 user = result.group(1)
68 hostname = result.group(2)
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080069
Austin Schuh71f6fa72019-08-31 18:23:02 -070070 if result.group(3):
71 target_dir = result.group(3)
72
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080073 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 Schuh71f6fa72019-08-31 18:23:02 -070080 ssh_target = "%s@%s" % (user, hostname)
81
Brian Silvermanbd7860e2020-01-05 17:52:40 -080082 ssh_path = "external/ssh/ssh"
83 scp_path = "external/ssh/scp"
84
Austin Schuh71f6fa72019-08-31 18:23:02 -070085 rsync_cmd = ([
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080086 "external/rsync/usr/bin/rsync", "-e", ssh_path, "-c", "-v", "-z",
87 "--copy-links"
Austin Schuh71f6fa72019-08-31 18:23:02 -070088 ] + 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 Silvermanbd7860e2020-01-05 17:52:40 -080092 if e.returncode == 127 or e.returncode == 12:
Austin Schuh71f6fa72019-08-31 18:23:02 -070093 print("Unconfigured roboRIO, installing rsync.")
Brian Silvermanbd7860e2020-01-05 17:52:40 -080094 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 Schuh71f6fa72019-08-31 18:23:02 -0700100 subprocess.check_call(rsync_cmd)
101 else:
102 raise e
103
104 if not recursive:
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -0800105 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 Schuh71f6fa72019-08-31 18:23:02 -0700110
111
112if __name__ == "__main__":
113 main(sys.argv)