blob: 2f14f073ffc7ac0e96d880639bae4ae737b66135 [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",
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 Schrader0f5d2502022-03-08 22:44:55 -080054 # 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 Jones5127ccc2022-07-31 16:32:45 -070056 check=True,
57 stdin=sys.stdin)
Philipp Schrader0f5d2502022-03-08 22:44:55 -080058 subprocess.run(f"ssh -tt {args.host} sudo dpkg -i /tmp/{deb.name}",
Ravago Jones5127ccc2022-07-31 16:32:45 -070059 shell=True,
60 check=True,
61 stdin=sys.stdin)
Philipp Schrader0f5d2502022-03-08 22:44:55 -080062 subprocess.run(f"ssh {args.host} rm -f /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
67
68if __name__ == "__main__":
69 sys.exit(main(sys.argv))