Skip to content

Commit c9a9224

Browse files
committed
Rename osg-resource-catalog-generator to osg-ce-attribs-gen and do a lot of restructuring
Use positional arguments instead of --config and --output (as described in SOFTWARE-4895). Create a stub ce_attributes.py; will later move code from osg-ce-attribs-gen into it.
1 parent 1136f5b commit c9a9224

File tree

6 files changed

+234
-212
lines changed

6 files changed

+234
-212
lines changed

osg_configure/configure_modules/infoservices.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from osg_configure.modules import exceptions
1010
from osg_configure.modules import utilities
1111
from osg_configure.modules import configfile
12-
from osg_configure.modules.baseconfiguration import BaseConfiguration, BATCH_SYSTEMS, BOSCO_BATCH_SYSTEMS_MAP
12+
from osg_configure.modules.baseconfiguration import BaseConfiguration
13+
from osg_configure.modules.ce_attributes import BATCH_SYSTEMS, BATCH_SYSTEMS_CASE_MAP
1314
from osg_configure.modules import subcluster
1415
from osg_configure.modules import reversevomap
1516

@@ -123,13 +124,13 @@ def csgbool(section, option):
123124

124125
if csgbool("Bosco", "enabled"):
125126
try:
126-
bosco_batch = BOSCO_BATCH_SYSTEMS_MAP[csg("Bosco", "batch").lower()]
127+
bosco_batch = BATCH_SYSTEMS_CASE_MAP[csg("Bosco", "batch").lower()]
127128
if bosco_batch not in self.enabled_batch_systems:
128129
self.enabled_batch_systems.append(bosco_batch)
129130
except (KeyError, AttributeError):
130131
# This is a warning; the bosco module raises the error.
131132
self.log("Bosco.batch does not have a recognized value -- should be one of %s" %
132-
list(BOSCO_BATCH_SYSTEMS_MAP.keys()),
133+
list(BATCH_SYSTEMS_CASE_MAP.keys()),
133134
level=logging.WARNING)
134135

135136
self.htcondor_gateway_enabled = csgbool('Gateway', 'htcondor_gateway_enabled')

osg_configure/modules/baseconfiguration.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,11 @@
1010
from osg_configure.modules import utilities
1111
from osg_configure.modules import exceptions
1212

13-
__all__ = ['BaseConfiguration', 'BATCH_SYSTEMS']
13+
__all__ = ['BaseConfiguration']
1414

1515
HOSTCERT_PATH = "/etc/grid-security/hostcert.pem"
1616
HOSTKEY_PATH = "/etc/grid-security/hostkey.pem"
1717

18-
# BATCH_SYSTEMS here is both the config sections for the batch systems
19-
# and the values in the OSG_BatchSystems attribute since they are
20-
# coincidentally the same. If they ever change, make a mapping.
21-
BATCH_SYSTEMS = ['Condor', 'LSF', 'PBS', 'SGE', 'SLURM']
22-
BOSCO_BATCH_SYSTEMS_MAP = {
23-
'condor': 'Condor',
24-
'lsf': 'LSF',
25-
'pbs': 'PBS',
26-
'sge': 'SGE',
27-
'slurm': 'SLURM',
28-
}
2918

3019
class BaseConfiguration:
3120
"""Base class for inheritance by configuration"""
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BATCH_SYSTEMS here is both the config sections for the batch systems
2+
# and the values in the OSG_BatchSystems attribute since they are
3+
# coincidentally the same. If they ever change, make a mapping.
4+
BATCH_SYSTEMS_CASE_MAP = {
5+
'condor': 'Condor',
6+
'lsf': 'LSF',
7+
'pbs': 'PBS',
8+
'sge': 'SGE',
9+
'slurm': 'SLURM',
10+
}
11+
BATCH_SYSTEMS = list(BATCH_SYSTEMS_CASE_MAP.values())

scripts/osg-ce-attribs-gen

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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

Comments
 (0)