Skip to content

Commit

Permalink
remote/client: autodetect place and role names from config file
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Luebbe <[email protected]>
  • Loading branch information
jluebbe committed Mar 20, 2018
1 parent 30c1a86 commit 4c91da3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
16 changes: 16 additions & 0 deletions labgrid/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ def _convert_to_named_list(self, data):
assert 'cls' in item
return result

def normalize_config(self, config):
resources = {}
drivers = {}
for item in self._convert_to_named_list(config.get('resources', {})):
resource = item.pop('cls')
name = item.pop('name', None)
args = item # remaining args
resources.setdefault(resource, {})[name] = (args, )
for item in self._convert_to_named_list(config.get('drivers', {})):
driver = item.pop('cls')
name = item.pop('name', None)
bindings = item.pop('bindings', {})
args = item # remaining args
drivers.setdefault(resource, {})[name] = (args, bindings)
return resources, drivers

def make_resource(self, target, resource, name, args):
assert isinstance(args, dict)
if not resource in self.resources:
Expand Down
39 changes: 36 additions & 3 deletions labgrid/remote/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ..resource.remote import RemotePlaceManager, RemotePlace
from ..util.dict import diff_dict, flat_dict
from ..util.yaml import dump
from .. import Target
from .. import Target, target_factory

txaio.use_asyncio()
txaio.config.loop = asyncio.get_event_loop()
Expand Down Expand Up @@ -52,6 +52,7 @@ def onConnect(self):
self.connected = self.config.extra['connected']
self.args = self.config.extra.get('args')
self.env = self.config.extra.get('env', None)
self.role = self.config.extra.get('role', None)
self.prog = self.config.extra.get('prog', os.path.basename(sys.argv[0]))
self.monitor = self.config.extra.get('monitor', False)
enable_tcp_nodelay(self)
Expand Down Expand Up @@ -554,7 +555,7 @@ def _get_target(self, place):
self._prepare_manager()
target = None
if self.env:
target = self.env.get_target(place.name)
target = self.env.get_target(self.role)
if target:
if self.args.state:
if self.args.verbose >= 2:
Expand Down Expand Up @@ -822,6 +823,22 @@ def create():
loop.run_until_complete(ready.wait())
return session[0]

def find_role_by_place(config, place):
for role, role_config in config.items():
resources, _ = target_factory.normalize_config(role_config)
remote_places = resources.get('RemotePlace', {})
remote_place = remote_places.get(place)
if remote_place:
return role
return None

def find_any_role_with_place(config):
for role, role_config in config.items():
resources, _ = target_factory.normalize_config(role_config)
remote_places = resources.get('RemotePlace', {})
for place in remote_places:
return (role, place)
return None, None

def main():
logging.basicConfig(
Expand Down Expand Up @@ -1026,17 +1043,33 @@ def main():
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

env = None
if not args.config and args.state:
print("Setting the state requires a configuration file")
exit(1)

env = None
if args.config:
env = Environment(config_file=args.config)

role = None
if env and env.config.get_targets():
if args.place:
role = find_role_by_place(env.config.get_targets(), args.place)
if not role:
print("RemotePlace {} not found in configuration file".format(args.place), file=sys.stderr)
exit(1)
print("Selected role {} from configuration file".format(role))
else:
role, args.place = find_any_role_with_place(env.config.get_targets())
if not role:
print("No RemotePlace found in configuration file", file=sys.stderr)
exit(1)
print("Selected role {} and place {} from configuration file".format(role, args.place))

extra = {
'args': args,
'env': env,
'role': role,
'prog': parser.prog,
}

Expand Down

0 comments on commit 4c91da3

Please sign in to comment.