blob: f9473985bceba073dad321b1f2fd17148795311c [file] [log] [blame]
Philipp Schrader0f5d2502022-03-08 22:44:55 -08001import argparse
2from pathlib import Path
3import subprocess
4import sys
5
Ravago Jones5127ccc2022-07-31 16:32:45 -07006
Philipp Schrader0f5d2502022-03-08 22:44:55 -08007def 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 Schrader29fb3a72023-03-01 19:09:19 -080022 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 Schrader0f5d2502022-03-08 22:44:55 -080028 args = parser.parse_args(argv[1:])
29 deb = Path(args.deb)
30
Philipp Schrader29fb3a72023-03-01 19:09:19 -080031 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 Schradera25b2612023-03-02 23:49:37 -080035 shell=True,
Philipp Schrader29fb3a72023-03-01 19:09:19 -080036 # 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 Schradera25b2612023-03-02 23:49:37 -080042 f"ssh -tt {args.host}",
43 "\"sudo -u postgres psql",
Philipp Schrader29fb3a72023-03-01 19:09:19 -080044 # 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 Schradera25b2612023-03-02 23:49:37 -080050 "postgres\"",
Philipp Schrader29fb3a72023-03-01 19:09:19 -080051 ]),
52 shell=True,
53 check=True,
54 stdin=sys.stdin)
55
Philipp Schrader0f5d2502022-03-08 22:44:55 -080056 # Copy the .deb to the scouting server, install it, and delete it again.
57 subprocess.run(["rsync", "-L", args.deb, f"{args.host}:/tmp/{deb.name}"],
Ravago Jones5127ccc2022-07-31 16:32:45 -070058 check=True,
59 stdin=sys.stdin)
Philipp Schrader0f5d2502022-03-08 22:44:55 -080060 subprocess.run(f"ssh -tt {args.host} sudo dpkg -i /tmp/{deb.name}",
Ravago Jones5127ccc2022-07-31 16:32:45 -070061 shell=True,
62 check=True,
63 stdin=sys.stdin)
Philipp Schrader0f5d2502022-03-08 22:44:55 -080064 subprocess.run(f"ssh {args.host} rm -f /tmp/{deb.name}",
Ravago Jones5127ccc2022-07-31 16:32:45 -070065 shell=True,
66 check=True,
67 stdin=sys.stdin)
Philipp Schrader0f5d2502022-03-08 22:44:55 -080068
69
70if __name__ == "__main__":
71 sys.exit(main(sys.argv))