|
7 | 7 | from compiler_admin.services.google import USER_ARCHIVE, CallGAMCommand |
8 | 8 |
|
9 | 9 |
|
10 | | -CONFIG_DIR = os.environ.get("GAMCFGDIR", "./.config") |
11 | | -CONFIG_PATH = Path(CONFIG_DIR) |
12 | | -CONFIG_PATH_NAME = str(CONFIG_PATH) |
| 10 | +GAM_CONFIG_DIR = os.environ.get("GAMCFGDIR", "./.config/gam") |
| 11 | +GAM_CONFIG_PATH = Path(GAM_CONFIG_DIR) |
| 12 | +GYB_CONFIG_PATH = GAM_CONFIG_PATH.parent / "gyb" |
13 | 13 |
|
14 | 14 |
|
15 | | -def _clean_config_dir(): |
16 | | - for path in CONFIG_PATH.glob("**/*"): |
17 | | - if path.is_file(): |
18 | | - path.unlink() |
19 | | - elif path.is_dir(): |
20 | | - rmtree(path) |
| 15 | +def _clean_config_dir(gam: bool, gyb: bool) -> None: |
| 16 | + if gam: |
| 17 | + for path in GAM_CONFIG_PATH.glob("**/*"): |
| 18 | + if path.is_file(): |
| 19 | + path.unlink() |
| 20 | + elif path.is_dir(): |
| 21 | + rmtree(path) |
| 22 | + if gyb: |
| 23 | + for path in GYB_CONFIG_PATH.glob("**/*"): |
| 24 | + if path.is_file(): |
| 25 | + path.unlink() |
| 26 | + elif path.is_dir(): |
| 27 | + rmtree(path) |
21 | 28 |
|
22 | 29 |
|
23 | | -def init(admin_user: str) -> int: |
| 30 | +def init(admin_user: str, gam: bool = False, gyb: bool = False) -> int: |
24 | 31 | """Initialize a new GAM project. |
25 | 32 |
|
26 | 33 | See https://github.com/taers232c/GAMADV-XTD3/wiki/How-to-Install-Advanced-GAM |
27 | 34 |
|
28 | 35 | Args: |
29 | 36 | admin_user (str): The Compiler admin with which to initialize a new project. |
| 37 | +
|
| 38 | + init_gam (bool): If True, initialize a new GAM project. |
| 39 | +
|
| 40 | + init_gyb (bool): If True, initialize a new GYB project. |
30 | 41 | Returns: |
31 | 42 | A value indicating if the operation succeeded or failed. |
32 | 43 | """ |
33 | | - if CONFIG_PATH.exists(): |
34 | | - _clean_config_dir() |
35 | | - |
36 | | - res = CallGAMCommand(("config", "drive_dir", CONFIG_PATH_NAME, "verify")) |
37 | | - res += CallGAMCommand(("create", "project")) |
38 | | - res += CallGAMCommand(("oauth", "create")) |
39 | | - res += CallGAMCommand(("user", admin_user, "check", "serviceaccount")) |
40 | | - |
41 | | - # download GYB installer to config directory |
42 | | - gyb = CONFIG_PATH / "gyb-install.sh" |
43 | | - with gyb.open("w+") as dest: |
44 | | - res += subprocess.call(("curl", "-s", "-S", "-L", "https://gyb-shortn.jaylee.us/gyb-install"), stdout=dest) |
45 | | - |
46 | | - # install, giving values to options that prompt by default |
47 | | - # https://github.com/GAM-team/got-your-back/blob/main/install-gyb.sh |
48 | | - res += subprocess.call((gyb, "-u", admin_user, "-r", USER_ARCHIVE)) |
| 44 | + _clean_config_dir(gam, gyb) |
| 45 | + |
| 46 | + res = RESULT_SUCCESS |
| 47 | + |
| 48 | + if gam: |
| 49 | + GAM_CONFIG_PATH.mkdir(parents=True, exist_ok=True) |
| 50 | + res += CallGAMCommand(("config", "drive_dir", str(GAM_CONFIG_PATH), "verify")) |
| 51 | + res += CallGAMCommand(("create", "project")) |
| 52 | + res += CallGAMCommand(("oauth", "create")) |
| 53 | + res += CallGAMCommand(("user", admin_user, "check", "serviceaccount")) |
| 54 | + |
| 55 | + if gyb: |
| 56 | + GYB_CONFIG_PATH.mkdir(parents=True, exist_ok=True) |
| 57 | + # download GYB installer to config directory |
| 58 | + gyb = GYB_CONFIG_PATH / "gyb-install.sh" |
| 59 | + with gyb.open("w+") as dest: |
| 60 | + res += subprocess.call(("curl", "-s", "-S", "-L", "https://gyb-shortn.jaylee.us/gyb-install"), stdout=dest) |
| 61 | + |
| 62 | + # install, giving values to some options |
| 63 | + # https://github.com/GAM-team/got-your-back/blob/main/install-gyb.sh |
| 64 | + # |
| 65 | + # use GYB_CONFIG_PATH.parent for the install directory option, otherwise we get a .config/gyb/gyb directory structure |
| 66 | + res += subprocess.call((gyb, "-u", admin_user, "-r", USER_ARCHIVE, "-d", str(GYB_CONFIG_PATH.parent))) |
49 | 67 |
|
50 | 68 | return RESULT_SUCCESS if res == RESULT_SUCCESS else RESULT_FAILURE |
0 commit comments