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", |
| 35 | # In case the scouting app isn't installed, ignore the error here. |
| 36 | check=False, |
| 37 | stdin=sys.stdin) |
| 38 | print("Clearing the database.") |
| 39 | subprocess.run( |
| 40 | " ".join([ |
| 41 | f"ssh -tt {args.host} sudo -u postgres psql", |
| 42 | # Drop all tables in the same schema. |
| 43 | "-c 'drop schema public cascade;'", |
| 44 | # Create an empty schema for the scouting app to use. |
| 45 | "-c 'create schema public;'", |
| 46 | # List all tables as a sanity check. |
| 47 | "-c '\dt'", |
| 48 | "postgres", |
| 49 | ]), |
| 50 | shell=True, |
| 51 | check=True, |
| 52 | stdin=sys.stdin) |
| 53 | |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 54 | # Copy the .deb to the scouting server, install it, and delete it again. |
| 55 | subprocess.run(["rsync", "-L", args.deb, f"{args.host}:/tmp/{deb.name}"], |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 56 | check=True, |
| 57 | stdin=sys.stdin) |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 58 | 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] | 59 | shell=True, |
| 60 | check=True, |
| 61 | stdin=sys.stdin) |
Philipp Schrader | 0f5d250 | 2022-03-08 22:44:55 -0800 | [diff] [blame] | 62 | subprocess.run(f"ssh {args.host} rm -f /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 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | sys.exit(main(sys.argv)) |