|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +osg-ce-attribs-gen |
| 5 | +
|
| 6 | +OSG CA Attributes File Generator |
| 7 | +
|
| 8 | +Standalone script to generate a condor config file for advertising a CE resource to the CE Collector. |
| 9 | +""" |
| 10 | + |
| 11 | +from argparse import ArgumentParser |
| 12 | +from configparser import ConfigParser |
| 13 | +import glob |
| 14 | +import os |
| 15 | +import sys |
| 16 | +from typing import Dict |
| 17 | + |
| 18 | +if __name__ == "__main__" and __package__ is None: |
| 19 | + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 20 | + |
| 21 | +# local imports here |
| 22 | +from osg_configure.modules.ce_attributes import BATCH_SYSTEMS, BATCH_SYSTEMS_CASE_MAP |
| 23 | +from osg_configure.modules.exceptions import Error, SettingError |
| 24 | +from osg_configure.modules import subcluster |
| 25 | +from osg_configure.modules import utilities |
| 26 | +from osg_configure.version import __version__ |
| 27 | + |
| 28 | + |
| 29 | +def warn(*args, **kwargs): |
| 30 | + kwargs['file'] = sys.stderr |
| 31 | + print("***", *args, **kwargs) |
| 32 | + |
| 33 | + |
| 34 | +def load_configs(config_location: str) -> ConfigParser: |
| 35 | + config = ConfigParser() |
| 36 | + |
| 37 | + if config_location == "-": |
| 38 | + config.read_string(sys.stdin.read(), "<stdin>") |
| 39 | + return config |
| 40 | + |
| 41 | + config_file_list = [] |
| 42 | + if os.path.isdir(config_location): |
| 43 | + config_file_list.extend(sorted(glob.glob(os.path.join(config_location, "[!.]*.ini")))) |
| 44 | + else: |
| 45 | + config_file_list.append(config_location) |
| 46 | + |
| 47 | + read_files = config.read(config_file_list) |
| 48 | + if not read_files: |
| 49 | + raise Error(f"No valid config files found in {config_location}") |
| 50 | + |
| 51 | + return config |
| 52 | + |
| 53 | + |
| 54 | +def empty_if_blank(value: str) -> str: |
| 55 | + return "" if utilities.blank(value) else value |
| 56 | + |
| 57 | + |
| 58 | +def get_resource_from_config(config: ConfigParser) -> str: |
| 59 | + return utilities.classad_quote( |
| 60 | + empty_if_blank( |
| 61 | + config.get("Site Information", "resource", fallback="") |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def get_resource_group_from_config(config: ConfigParser) -> str: |
| 67 | + return utilities.classad_quote( |
| 68 | + empty_if_blank( |
| 69 | + config.get("Site Information", "resource_group", fallback="") |
| 70 | + ) |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def get_batch_systems_from_config(config: ConfigParser) -> str: |
| 75 | + batch_systems = [] |
| 76 | + |
| 77 | + siteinfo_batch_systems = config.get("Site Information", "batch_systems", fallback=None) |
| 78 | + if siteinfo_batch_systems is not None: |
| 79 | + # Site Information.batch_systems specified -- this one wins |
| 80 | + split_batch_systems = utilities.split_comma_separated_list(siteinfo_batch_systems) |
| 81 | + for batch_system in split_batch_systems: |
| 82 | + try: |
| 83 | + batch_systems.append(BATCH_SYSTEMS_CASE_MAP[batch_system.lower()]) |
| 84 | + except KeyError: |
| 85 | + raise SettingError("Unrecognized batch system %s" % batch_system) |
| 86 | + else: |
| 87 | + # Add each batch system that's enabled from the sections in the 20-*.ini files. |
| 88 | + for batch_system in BATCH_SYSTEMS: |
| 89 | + if batch_system in config: |
| 90 | + if config.getboolean(section=batch_system, option="enabled", fallback=None): |
| 91 | + batch_systems.append(batch_system) |
| 92 | + |
| 93 | + # Special case: Bosco (see SOFTWARE-3720); use the Bosco.batch argument. |
| 94 | + if config.getboolean("Bosco", "enabled", fallback=False): |
| 95 | + bosco_batch = config.get("Bosco", "batch", fallback=None) |
| 96 | + if bosco_batch: |
| 97 | + try: |
| 98 | + batch_systems.append(BATCH_SYSTEMS_CASE_MAP[bosco_batch.lower()]) |
| 99 | + except KeyError: |
| 100 | + raise SettingError("Unrecognized batch system %s in Bosco section" % bosco_batch) |
| 101 | + |
| 102 | + return utilities.classad_quote(",".join(batch_systems)) |
| 103 | + |
| 104 | + |
| 105 | +def get_resource_catalog_from_config(config: ConfigParser) -> str: |
| 106 | + return subcluster.resource_catalog_from_config(config, default_allowed_vos=[]).format_value() |
| 107 | + |
| 108 | + |
| 109 | +def get_attributes(config: ConfigParser) -> Dict[str, str]: |
| 110 | + """Turn config from .ini files into a dict of condor settings. |
| 111 | +
|
| 112 | + """ |
| 113 | + attributes = {} |
| 114 | + |
| 115 | + resource = get_resource_from_config(config) |
| 116 | + if resource and resource != '""': |
| 117 | + attributes["OSG_Resource"] = resource |
| 118 | + |
| 119 | + resource_group = get_resource_group_from_config(config) |
| 120 | + if resource_group and resource_group != '""': |
| 121 | + attributes["OSG_ResourceGroup"] = resource_group |
| 122 | + |
| 123 | + batch_systems = get_batch_systems_from_config(config) |
| 124 | + if batch_systems and batch_systems != '""': |
| 125 | + attributes["OSG_BatchSystems"] = batch_systems |
| 126 | + |
| 127 | + resource_catalog = get_resource_catalog_from_config(config) |
| 128 | + if resource_catalog and resource_catalog != "{}": |
| 129 | + attributes["OSG_ResourceCatalog"] = resource_catalog |
| 130 | + |
| 131 | + return attributes |
| 132 | + |
| 133 | + |
| 134 | +def get_options(args): |
| 135 | + """Parse, validate, and transform command-line options.""" |
| 136 | + parser = ArgumentParser(prog="osg-ca-attrib-gen", description=__doc__) |
| 137 | + parser.add_argument("--version", action="version", version="%(prog)s " + __version__) |
| 138 | + parser.add_argument( |
| 139 | + "config_location", |
| 140 | + nargs="?", |
| 141 | + default="/etc/osg/config.d", |
| 142 | + metavar="FILE_OR_DIRECTORY", |
| 143 | + help="Where to load configuration from. " |
| 144 | + "If this is a directory, will load every *.ini file in that directory. " |
| 145 | + "Default: %(default)s. " |
| 146 | + "If '-', will read from STDIN.", |
| 147 | + ) |
| 148 | + parser.add_argument( |
| 149 | + "output", |
| 150 | + nargs="?", |
| 151 | + default="-", |
| 152 | + metavar="FILE", |
| 153 | + help="Write output to this file. If '-' or unspecified, will write to STDOUT.", |
| 154 | + ) |
| 155 | + parser.add_argument( |
| 156 | + "--resource", |
| 157 | + metavar="RESOURCE_NAME", |
| 158 | + default=None, |
| 159 | + help="The Resource name to use, which should match your Topology registration. " |
| 160 | + "Equivalent to 'resource' in the 'Site Information' section." |
| 161 | + ) |
| 162 | + parser.add_argument( |
| 163 | + "--resource-group", |
| 164 | + metavar="RESOURCE_GROUP_NAME", |
| 165 | + default=None, |
| 166 | + help="The Resource Group name to use, which should match your Topology registration. " |
| 167 | + "Equivalent to 'resource_group' in the 'Site Information' section." |
| 168 | + ) |
| 169 | + parser.add_argument( |
| 170 | + "--batch-systems", |
| 171 | + metavar="BATCH_SYSTEMS_LIST", |
| 172 | + default=None, |
| 173 | + help="A comma-separated list of batch systems used by the resource. " |
| 174 | + f'Recognized batch systems are: {", ".join(BATCH_SYSTEMS)}. ' |
| 175 | + "Equivalent to enabling the batch system sections in the 20-*.ini files, " |
| 176 | + "or, if using Bosco, setting 'batch_system' in the 'Bosco' section." |
| 177 | + ) |
| 178 | + |
| 179 | + return parser.parse_args(args) |
| 180 | + |
| 181 | + |
| 182 | +def get_ce_attributes_str( |
| 183 | + config: ConfigParser, |
| 184 | +) -> str: |
| 185 | + attributes = get_attributes(config) |
| 186 | + attributes["SCHEDD_ATTRS"] = "$(SCHEDD_ATTRS), " + ", ".join(attributes.keys()) |
| 187 | + return "\n".join(f"{key} = {value}" for key, value in attributes.items()) |
| 188 | + |
| 189 | + |
| 190 | +def main(argv): |
| 191 | + options = get_options(argv[1:]) |
| 192 | + |
| 193 | + try: |
| 194 | + config = load_configs(options.config_location) |
| 195 | + if "Site Information" not in config: |
| 196 | + config.add_section("Site Information") |
| 197 | + if options.resource is not None: |
| 198 | + config["Site Information"]["resource"] = options.resource |
| 199 | + if options.resource_group is not None: |
| 200 | + config["Site Information"]["resource_group"] = options.resource_group |
| 201 | + if options.batch_systems is not None: |
| 202 | + config["Site Information"]["batch_systems"] = options.batch_systems |
| 203 | + output_str = get_ce_attributes_str(config) |
| 204 | + if options.output and options.output != "-": |
| 205 | + with open(options.output, "w") as outfh: |
| 206 | + print(output_str, file=outfh) |
| 207 | + else: |
| 208 | + print(output_str) |
| 209 | + except Error as e: |
| 210 | + print(e, file=sys.stderr) |
| 211 | + return 1 |
| 212 | + |
| 213 | + return 0 |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == "__main__": |
| 217 | + sys.exit(main(sys.argv)) |
0 commit comments