Commit db19c6df authored by Wout De Nolf's avatar Wout De Nolf

[oarresource] allow custom resource properties

parent b93f1053
......@@ -272,18 +272,13 @@ class Job(object):
@property
def resource(self):
with self.fixed_stats():
initial_request = self['initial_request']
wanted_resources = self['wanted_resources']
properties = self['properties']
if initial_request:
cli = initial_request
cmd = self['initial_request']
if cmd:
properties = None
else:
cli = wanted_resources
res = oarresource.Resource.from_cli(cli)
for s in [wanted_resources, properties]:
if "gpu='YES'" in s:
res.gpu = True
return res
cmd = self['wanted_resources']
properties = self['properties']
return oarresource.Resource.from_cli(cmd, properties)
def __str__(self):
try:
......@@ -489,8 +484,18 @@ class JobFactory(object):
return Job(jobid)
def search(name=None, project=None, owner=None, start=None, end=None,
state=None, **kwargs):
def search(name=None, project=None, owner=None, state=None,
start=None, end=None, **properties):
"""
:param str name:
:param str project:
:param str owner:
:param datetime start:
:param datetime end:
:param str state:
:return list: list of jobs
"""
# https://github.com/oar-team/oar/blob/2.5/sources/core/database/mysql_structure.sql
sqlquery = []
if name:
......@@ -507,8 +512,8 @@ def search(name=None, project=None, owner=None, start=None, end=None,
if end:
end = timeutils.totimestamp(end)
sqlquery.append("start_time<='{}'".format(end))
for k, (op, v) in kwargs.items():
sqlquery.append("{}{}'{}'".format(k, op, v))
for k, (op,v) in properties.items():
sqlquery.append("{}{}'{}'".format(k, v))
if not sqlquery:
return []
......
......@@ -40,12 +40,15 @@ def str2walltime(s):
h, m, s = s.split(':')
return datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
property_pattern = re.compile('(\w+) *([>=<]+) *\'?([\w\d]+)\'?')
class Resource(object):
def __init__(self, host=None, nodes=None, cpu=None, core=None, gpu=False,
walltime=None, mem_core_mb=None):
walltime=None, mem_core_mb=None, **properties):
"""
Custom properties can be defined: for example cpu_vendor=('=',INTEL)"
:param int host: number of hosts to be used (Optional)
:param int nodes: number of nodes to be used (Optional)
......@@ -65,6 +68,7 @@ class Resource(object):
self.gpu = gpu
self.walltime = walltime
self.mem_core_mb = mem_core_mb
self.properties = properties
@property
def walltime(self):
......@@ -119,6 +123,8 @@ class Resource(object):
properties.append("gpu='YES'")
if self.mem_core_mb:
properties.append("mem_core_mb>={:d}".format(self.mem_core_mb))
for k,(op,v) in self.properties.items():
properties.append("{}{}'{}'".format(k, op, v))
cliarg = ' and '.join(properties)
return cliarg
......@@ -138,10 +144,9 @@ class Resource(object):
return oarshell.cli_args2str(*self.cli_arguments)
@classmethod
def from_cli(cls, cmd):
def from_cli(cls, cmd, properties = None):
kwargs = {}
cli_arguments = list(oarshell.cli_str2args(cmd))
arg = oarshell._cli_getarg(cli_arguments, '-l')
if arg:
for key in ['host', 'nodes', 'cpu', 'core']:
......@@ -152,14 +157,17 @@ class Resource(object):
if match:
kwargs['walltime'] = str2walltime(match.groups()[0])
arg = oarshell._cli_getarg(cli_arguments, '-p')
if not arg and properties:
arg = properties
if arg:
match = re.search('gpu=\'?YES', arg)
if match:
kwargs['gpu'] = True
match = re.search('mem_core_mb[>=<]+(\d+)', arg)
if match:
kwargs['mem_core_mb'] = int(match.groups()[0])
for match in property_pattern.finditer(arg):
k,op,v = match.groups()
if k=='gpu':
kwargs['gpu'] = v=='YES'
elif k=='mem_core_mb':
kwargs['mem_core_mb'] = int(v)
else:
kwargs[k] = op,v
return cls(**kwargs)
def __eq__(self, other):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment