]> scripts.mit.edu Git - wizard.git/blob - wizard/scripts.py
Refactor Deployment into Working/Production copies.
[wizard.git] / wizard / scripts.py
1 import os
2 import shlex
3
4 import wizard
5 from wizard import shell, util
6
7 def get_sql_credentials(vars=None):
8     """
9     Attempts to determine a user's MySQL credentials.  They are
10     returned as a three-tuple (host, user, password).
11     """
12     sh = shell.Shell()
13     host = os.getenv("WIZARD_MYSQL_HOST")
14     user = os.getenv("WIZARD_MYSQL_USER")
15     password = os.getenv("WIZARD_MYSQL_PASSWORD")
16     if host is not None and user is not None and password is not None:
17         return (host, user, password)
18     # XXX: this is very fragile
19     elif vars and "WIZARD_DBSERVER" in vars and "WIZARD_DBUSER" in vars and "WIZARD_DBPASSWORD" in vars:
20         return (shlex.split(vars[x])[0] for x in ("WIZARD_DBSERVER", "WIZARD_DBUSER", "WIZARD_DBPASSWORD"))
21     try:
22         tuple = sh.eval("/mit/scripts/sql/bin/get-password").split()
23         if len(tuple) == 3:
24             return tuple
25         return None
26     except shell.CallError:
27         return None
28
29 def get_web_host_and_path(dir=None):
30     """
31     Attempts to determine webhost and path for the current directory
32     as it would be accessible from the web.  Works only for scripts
33     servers.  Returns a tuple web_host, web_path, or None if it failed.
34     """
35     # XXX: THIS CODE SUCKS
36     host = os.getenv("WIZARD_WEB_HOST")
37     path = os.getenv("WIZARD_WEB_PATH")
38     if host is not None and path is not None:
39         return (host, path)
40     if not dir:
41         dir = os.getcwd()
42     _, _, web_path = dir.partition("/web_scripts")
43     if not web_path:
44         return None
45     return (util.get_dir_owner(dir) + ".scripts.mit.edu", web_path)
46
47 def get_quota_usage_and_limit(dir=None):
48     """
49     Returns a tuple (quota usage, quota limit).  Works only for scripts
50     servers.  Values are in KiB.  Returns ``(0, None)`` if we couldn't figure it out.
51     """
52     # XXX: The correct way is to implement Python modules implementing
53     # bindings for all the appropriate interfaces
54     def parse_last_quote(ret):
55         return ret.rstrip('\'').rpartition('\'')[2]
56     if dir is None:
57         dir = os.getcwd()
58     sh = shell.Shell()
59     try:
60         cell = parse_last_quote(sh.eval("fs", "whichcell", "-path", dir))
61     except shell.CallError:
62         return (0, None)
63     mount = None
64     while dir:
65         try:
66             volume = parse_last_quote(sh.eval("fs", "lsmount", dir))[1:]
67             break
68         except shell.CallError:
69             dir = os.path.dirname(dir)
70     if not volume: return (0, None)
71     try:
72         result = sh.eval("vos", "examine", "-id", volume, "-cell", cell).splitlines()
73     except shell.CallError:
74         return (0, None)
75     try:
76         usage = int(result[0].split()[3])
77         limit = int(result[3].split()[1]) # XXX: FRAGILE
78     except ValueError:
79         raise QuotaParseError("vos examine output was:\n\n" + "\n".join(result))
80     return (usage, limit)
81
82 # XXX: Possibly in the wrong module
83 def get_disk_usage(dir=None):
84     """
85     Recursively determines the disk usage of a directory, excluding
86     .git directories.  Value is in bytes.
87     """
88     if dir is None: dir = os.getcwd()
89     return int(shell.Shell().eval("du", "--exclude=.git", "-s", dir).split()[0])
90
91 class QuotaParseError(wizard.Error):
92     def __init__(self, msg):
93         self.msg = msg
94     def __str__(self):
95         return """
96
97 ERROR: Could not parse quota. %s
98 """ % self.msg