blob: 31155dd1ead9c18f20a0d9b4f87fe154d68fd82e [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()
Austin Schuhdcffd092021-10-17 21:51:39 -070031 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 Kuszmaul2d8fa2a2020-03-01 13:51:50 -080042 parser.add_argument(
43 "--dir",
44 type=str,
45 help="Directory within robot_code to copy the files to.")
Austin Schuhdcffd092021-10-17 21:51:39 -070046 parser.add_argument(
47 "srcs", type=str, nargs='+', help="List of files to copy over")
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080048 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:
Austin Schuhdcffd092021-10-17 21:51:39 -070062 print(
63 "Not sure how to parse destination \"%s\"!" % destination,
64 file=sys.stderr)
Austin Schuh71f6fa72019-08-31 18:23:02 -070065 return 1
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080066 user = None
Austin Schuh71f6fa72019-08-31 18:23:02 -070067 if result.group(1):
68 user = result.group(1)
69 hostname = result.group(2)
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080070
Austin Schuh71f6fa72019-08-31 18:23:02 -070071 if result.group(3):
72 target_dir = result.group(3)
73
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080074 if user is None:
75 if args.type == "pi":
James Kuszmaulbe46f7f2020-10-03 13:40:57 -070076 user = "pi"
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080077 elif args.type == "roborio":
James Kuszmaulbe46f7f2020-10-03 13:40:57 -070078 user = "admin"
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080079 target_dir = "/home/" + user + "/robot_code"
80
Austin Schuh71f6fa72019-08-31 18:23:02 -070081 ssh_target = "%s@%s" % (user, hostname)
82
Brian Silvermanbd7860e2020-01-05 17:52:40 -080083 ssh_path = "external/ssh/ssh"
84 scp_path = "external/ssh/scp"
85
Austin Schuh71f6fa72019-08-31 18:23:02 -070086 rsync_cmd = ([
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080087 "external/rsync/usr/bin/rsync", "-e", ssh_path, "-c", "-v", "-z",
James Kuszmaulbe46f7f2020-10-03 13:40:57 -070088 "--perms", "--copy-links"
Austin Schuhdcffd092021-10-17 21:51:39 -070089 ] + 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 Schuh71f6fa72019-08-31 18:23:02 -0700100 try:
101 subprocess.check_call(rsync_cmd)
102 except subprocess.CalledProcessError as e:
Brian Silvermanbd7860e2020-01-05 17:52:40 -0800103 if e.returncode == 127 or e.returncode == 12:
Austin Schuh71f6fa72019-08-31 18:23:02 -0700104 print("Unconfigured roboRIO, installing rsync.")
Brian Silvermanbd7860e2020-01-05 17:52:40 -0800105 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 Schuh71f6fa72019-08-31 18:23:02 -0700111 subprocess.check_call(rsync_cmd)
Austin Schuhdcffd092021-10-17 21:51:39 -0700112 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 Schuh71f6fa72019-08-31 18:23:02 -0700117 else:
118 raise e
119
120 if not recursive:
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -0800121 subprocess.check_call((ssh_path, ssh_target, "&&".join([
Austin Schuh44e0b142021-10-16 15:51:10 -0700122 "chmod u+s %s/starterd.stripped" % target_dir,
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -0800123 "echo \'Done moving new executables into place\'",
Austin Schuhdcffd092021-10-17 21:51:39 -0700124 "bash -c \'sync\'",
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -0800125 ])))
Austin Schuh71f6fa72019-08-31 18:23:02 -0700126
127
128if __name__ == "__main__":
129 main(sys.argv)