Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 1 | import argparse |
| 2 | from pathlib import Path |
| 3 | import subprocess |
| 4 | import sys |
| 5 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 6 | |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 7 | def main(argv): |
| 8 | """Installs the scouting application on the scouting server.""" |
| 9 | parser = argparse.ArgumentParser() |
| 10 | parser.add_argument( |
| 11 | "--deb", |
| 12 | type=str, |
| 13 | required=True, |
| 14 | help="The .deb file to deploy.", |
| 15 | ) |
| 16 | parser.add_argument( |
| 17 | "--host", |
| 18 | type=str, |
| 19 | default="scouting.frc971.org", |
| 20 | help="The SSH host to install the scouting web server to.", |
| 21 | ) |
Philipp Schrader | 29fb3a7 | 2023-03-01 19:09:19 -0800 | [diff] [blame] | 22 | parser.add_argument( |
| 23 | "--clear-db", |
| 24 | action="store_true", |
| 25 | help=("If set, will stop the existing scouting server and clear the " |
| 26 | "database before deploying the new one."), |
| 27 | ) |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 28 | args = parser.parse_args(argv[1:]) |
| 29 | deb = Path(args.deb) |
| 30 | |
Philipp Schrader | 29fb3a7 | 2023-03-01 19:09:19 -0800 | [diff] [blame] | 31 | if args.clear_db: |
| 32 | print("Stopping the scouting app.") |
| 33 | subprocess.run( |
| 34 | f"ssh -tt {args.host} sudo systemctl stop scouting.service", |
Philipp Schrader | a25b261 | 2023-03-02 23:49:37 -0800 | [diff] [blame] | 35 | shell=True, |
Philipp Schrader | 29fb3a7 | 2023-03-01 19:09:19 -0800 | [diff] [blame] | 36 | # In case the scouting app isn't installed, ignore the error here. |
| 37 | check=False, |
| 38 | stdin=sys.stdin) |
| 39 | print("Clearing the database.") |
| 40 | subprocess.run( |
| 41 | " ".join([ |
Philipp Schrader | a25b261 | 2023-03-02 23:49:37 -0800 | [diff] [blame] | 42 | f"ssh -tt {args.host}", |
| 43 | "\"sudo -u postgres psql", |
Philipp Schrader | 29fb3a7 | 2023-03-01 19:09:19 -0800 | [diff] [blame] | 44 | # Drop all tables in the same schema. |
| 45 | "-c 'drop schema public cascade;'", |
| 46 | # Create an empty schema for the scouting app to use. |
| 47 | "-c 'create schema public;'", |
| 48 | # List all tables as a sanity check. |
| 49 | "-c '\dt'", |
Philipp Schrader | 05cd01e | 2023-03-09 20:51:33 -0800 | [diff] [blame] | 50 | # Make sure we make the visualizations accessible. |
| 51 | "-c 'GRANT ALL ON SCHEMA public TO tableau;'", |
Philipp Schrader | a25b261 | 2023-03-02 23:49:37 -0800 | [diff] [blame] | 52 | "postgres\"", |
Philipp Schrader | 29fb3a7 | 2023-03-01 19:09:19 -0800 | [diff] [blame] | 53 | ]), |
| 54 | shell=True, |
| 55 | check=True, |
| 56 | stdin=sys.stdin) |
| 57 | |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 58 | # Copy the .deb to the scouting server, install it, and delete it again. |
| 59 | subprocess.run(["rsync", "-L", args.deb, f"{args.host}:/tmp/{deb.name}"], |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 60 | check=True, |
| 61 | stdin=sys.stdin) |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 62 | subprocess.run(f"ssh -tt {args.host} sudo dpkg -i /tmp/{deb.name}", |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 63 | shell=True, |
| 64 | check=True, |
| 65 | stdin=sys.stdin) |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 66 | subprocess.run(f"ssh {args.host} rm -f /tmp/{deb.name}", |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 67 | shell=True, |
| 68 | check=True, |
| 69 | stdin=sys.stdin) |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 70 | |
| 71 | |
| 72 | if __name__ == "__main__": |
| 73 | sys.exit(main(sys.argv)) |