blob: 61c0481ce98f17db05627a139bbefcefee2d621c [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 Schrader05cd01e2023-03-09 20:51:33 -080050 # Make sure we make the visualizations accessible.
51 "-c 'GRANT ALL ON SCHEMA public TO tableau;'",
Philipp Schradera25b2612023-03-02 23:49:37 -080052 "postgres\"",
Philipp Schrader29fb3a72023-03-01 19:09:19 -080053 ]),
54 shell=True,
55 check=True,
56 stdin=sys.stdin)
57
Philipp Schrader0f5d2502022-03-08 22:44:55 -080058 # 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 Jones5127ccc2022-07-31 16:32:45 -070060 check=True,
61 stdin=sys.stdin)
Philipp Schrader0f5d2502022-03-08 22:44:55 -080062 subprocess.run(f"ssh -tt {args.host} sudo dpkg -i /tmp/{deb.name}",
Ravago Jones5127ccc2022-07-31 16:32:45 -070063 shell=True,
64 check=True,
65 stdin=sys.stdin)
Philipp Schrader0f5d2502022-03-08 22:44:55 -080066 subprocess.run(f"ssh {args.host} rm -f /tmp/{deb.name}",
Ravago Jones5127ccc2022-07-31 16:32:45 -070067 shell=True,
68 check=True,
69 stdin=sys.stdin)
Philipp Schrader0f5d2502022-03-08 22:44:55 -080070
71
72if __name__ == "__main__":
73 sys.exit(main(sys.argv))