summaryrefslogtreecommitdiff
path: root/tradefed_cluster/configs/lab_config.py
blob: fe9b113fc05924a02bd875aabfec02e57d8f4c67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tools for tradefed satellite lab configs."""
import abc
import collections
import logging
import os

import six

import strictyaml as syaml


try:
  from google.protobuf import json_format
except ImportError:
  from google3.net.proto2.python.public import json_format


from tradefed_cluster.configs import lab_config_pb2

# Ansible package depends on certain environment variable.
ansible_import_error = None
try:
  os.environ['ANSIBLE_LOCAL_TEMP'] = '/tmp'
  # unified_lab_config depends on ansible.
  from tradefed_cluster.configs import unified_lab_config as unified_lab_config_util
except Exception as e:  
  logging.warning('Fail to import ansible package:\n%s', e)
  ansible_import_error = e


logger = logging.getLogger(__name__)

_DEFAULT_SHUTDOWN_TIMEOUT_SEC = 3600
_TF_GROUP = 'tf'


class ConfigError(Exception):
  """Error raised if config is incorrect."""
  pass


def _ProtoDescriptorToSchema(pb_descriptor):
  """Convert a proto Descriptor to a strictyaml schema."""
  d = {}
  for field in pb_descriptor.fields:
    if field.LABEL_REPEATED == field.label:
      value_schema = syaml.Seq(_ProtoFieldToSchema(field))
    else:
      value_schema = _ProtoFieldToSchema(field)
    d[syaml.Optional(field.name)] = value_schema
  return syaml.Map(d)


def _ProtoFieldToSchema(field):
  """Convert a Proto Field to strictyaml schema."""
  if field.type == field.TYPE_STRING:
    return syaml.Str()
  if field.type == field.TYPE_BOOL:
    return syaml.Bool()
  if field.type in (field.TYPE_INT32, field.TYPE_UINT32,
                    field.TYPE_INT64, field.TYPE_UINT64):
    return syaml.Int()
  if field.type in (field.TYPE_DOUBLE, field.TYPE_FLOAT):
    return syaml.Decimal()
  if field.type == field.TYPE_MESSAGE:
    return _ProtoDescriptorToSchema(field.message_type)
  if field.type == field.TYPE_ENUM:
    return syaml.Str()
  raise ConfigError('Unknown field type in lab_config_pb2: %r.' % field.type)


_YAML_SCHEMA = _ProtoDescriptorToSchema(lab_config_pb2.LabConfig.DESCRIPTOR)


def Parse(yaml_file_obj):
  """Parse yaml config.

  Args:
    yaml_file_obj: yaml file obj.
  Returns:
    a lab_config_pb2.LabConfig proto.
  Raises:
    ConfigError: if the config is incorrect.
  """
  content = six.ensure_str(yaml_file_obj.read())
  try:
    config_dict = syaml.dirty_load(
        content, schema=_YAML_SCHEMA, allow_flow_style=True).data
  except (syaml.YAMLError, TypeError) as e:
    raise ConfigError(e)
  try:
    return json_format.ParseDict(config_dict, lab_config_pb2.LabConfig())
  except json_format.ParseError as e:
    raise ConfigError(e)


class HostConfig(object):
  """A host config object.

  This class is immutable. The setter functions return a new HostConfig object.
  """

  def __init__(
      self,
      host_config_pb,
      cluster_config_pb,
      lab_config_pb):
    self.host_config_pb = lab_config_pb2.HostConfig()
    self.cluster_config_pb = lab_config_pb2.ClusterConfig()
    self.lab_config_pb = lab_config_pb2.LabConfig()

    self.host_config_pb.CopyFrom(host_config_pb)
    self.cluster_config_pb.CopyFrom(cluster_config_pb)
    self.lab_config_pb.CopyFrom(lab_config_pb)

  def Copy(self):
    """Copy the host config."""
    return HostConfig(self.host_config_pb,
                      self.cluster_config_pb,
                      self.lab_config_pb)

  @property
  def hostname(self):
    """Get host's name."""
    return self.host_config_pb.hostname

  @property
  def host_login_name(self):
    """Get host's login username."""
    return (self.host_config_pb.host_login_name or
            self.cluster_config_pb.host_login_name or
            self.lab_config_pb.host_login_name)

  @property
  def tf_global_config_path(self):
    """Get tf global config for the host."""
    return (self.host_config_pb.tf_global_config_path or
            self.cluster_config_pb.tf_global_config_path)

  @property
  def lab_name(self):
    """Get host's lab's name."""
    return self.lab_config_pb.lab_name

  @property
  def cluster_name(self):
    """Get host's cluster's name."""
    return self.cluster_config_pb.cluster_name

  @property
  def control_server_url(self):
    """Get the master server the host connect to."""
    # TODO: Deprecated, use control_server_url instead.
    return (self.cluster_config_pb.control_server_url or
            self.cluster_config_pb.master_url or
            self.lab_config_pb.control_server_url or
            self.lab_config_pb.master_url)

  @property
  def operation_mode(self):
    """Get the operation mode of the host."""
    return self.lab_config_pb.operation_mode

  @property
  def docker_image(self):
    """Get the docker image the host to use."""
    return (self.host_config_pb.docker_image or
            self.cluster_config_pb.docker_image or
            self.lab_config_pb.docker_image)

  def SetDockerImage(self, val):
    """Create a new config with given value of docker_image."""
    host_config = self.Copy()
    host_config.host_config_pb.docker_image = val
    return host_config

  @property
  def graceful_shutdown(self):
    """Graceful shutdown or not."""
    return (self.host_config_pb.graceful_shutdown or
            self.cluster_config_pb.graceful_shutdown or
            False)

  @property
  def shutdown_timeout_sec(self):
    """The dockerized TradeFed shutdown timeouts in seconds."""
    return (self.host_config_pb.shutdown_timeout_sec or
            self.cluster_config_pb.shutdown_timeout_sec)

  @property
  def enable_stackdriver(self):
    """Enable stackdriver logging and monitor."""
    return (self.cluster_config_pb.enable_stackdriver or
            self.lab_config_pb.enable_stackdriver or
            False)

  @property
  def enable_autoupdate(self):
    """Enable autoupdate mtt daemon process."""
    return (self.host_config_pb.enable_autoupdate or
            self.cluster_config_pb.enable_autoupdate or
            self.lab_config_pb.enable_autoupdate)

  @property
  def extra_docker_args(self):
    """Extra docker args."""
    return (list(self.cluster_config_pb.extra_docker_args or []) +
            list(self.host_config_pb.extra_docker_args or []))

  @property
  def service_account_json_key_path(self):
    """The file path of service account json key."""
    return self.lab_config_pb.service_account_json_key_path

  @property
  def docker_server(self):
    """Get the docker server the image is hosted on."""
    return (self.host_config_pb.docker_server or
            self.cluster_config_pb.docker_server or
            self.lab_config_pb.docker_server)

  @property
  def secret_project_id(self):
    """Get secret project id."""
    return self.lab_config_pb.secret_project_id

  @property
  def service_account_key_secret_id(self):
    """Get secret id for service account key."""
    return self.lab_config_pb.service_account_key_secret_id

  @property
  def service_account(self):
    """Get service account."""
    return self.lab_config_pb.service_account

  def SetServiceAccountJsonKeyPath(self, val):
    """Create a new config with given value of service_account_json_key_path."""
    host_config = self.Copy()
    host_config.lab_config_pb.service_account_json_key_path = val
    return host_config

  @property
  def tmpfs_configs(self):
    """Get tmpfs configs to mount into the container.

    Return the tmpfs configs merged the tmpfs configs for the cluster and
    the host. If the path of the tmpfs configs are the same, pick the one from
    the host config.

    Returns:
      a list of tmpfs configs.
    """
    path_to_tmpfs = collections.OrderedDict()
    for tmpfs_config in self.cluster_config_pb.tmpfs_configs or []:
      path_to_tmpfs[tmpfs_config.path] = tmpfs_config
    for tmpfs_config in self.host_config_pb.tmpfs_configs or []:
      path_to_tmpfs[tmpfs_config.path] = tmpfs_config
    return list(path_to_tmpfs.values())

  @property
  def enable_ui_update(self):
    """Whether host update from UI is enabled."""
    return bool(self.host_config_pb.enable_ui_update or
                self.cluster_config_pb.enable_ui_update or
                self.lab_config_pb.enable_ui_update)

  @property
  def owners(self):
    """Inherit the owners field from parent cluster or lab."""
    owners = list(self.lab_config_pb.owners)
    for owner in self.cluster_config_pb.owners:
      if owner not in owners:
        owners.append(owner)
    return owners

  @property
  def engprod_api_key(self):
    """API Key for Android Engprod API discovery."""
    return self.lab_config_pb.engprod_api_key

  @property
  def inventory_groups(self):
    return self.host_config_pb.inventory_groups

  @property
  def max_local_virtual_devices(self):
    return self.host_config_pb.max_local_virtual_devices

  @property
  def ssh_arg(self):
    """Get host's ssh arg."""
    return self.lab_config_pb.ssh_arg

  @property
  def max_concurrent_update_percentage(self):
    """Max concurrent update percentage in cluster level."""
    return self.cluster_config_pb.max_concurrent_update_percentage

  def Save(self, output_file_path):
    """Save the config to a file."""
    lab_config_pb = lab_config_pb2.LabConfig()
    lab_config_pb.CopyFrom(self.lab_config_pb)
    del lab_config_pb.cluster_configs[:]
    cluster_config_pb = lab_config_pb2.ClusterConfig()
    cluster_config_pb.CopyFrom(self.cluster_config_pb)
    del cluster_config_pb.host_configs[:]
    cluster_config_pb.host_configs.add().CopyFrom(self.host_config_pb)
    lab_config_pb.cluster_configs.add().CopyFrom(cluster_config_pb)
    with open(output_file_path, 'w') as f:
      lab_config_dict = json_format.MessageToDict(
          lab_config_pb,
          preserving_proto_field_name=True)
      f.write(syaml.as_document(lab_config_dict, schema=_YAML_SCHEMA).as_yaml())

  def __eq__(self, other):
    if not isinstance(other, HostConfig):
      return False
    property_names = [name for name, obj
                      in vars(HostConfig).items()
                      if isinstance(obj, property)]
    return all(getattr(self, property_name) == getattr(other, property_name)
               for property_name in property_names)

  def __repr__(self):
    lines = []
    for name, obj in vars(HostConfig).items():
      if not isinstance(obj, property):
        continue
      lines.append('%s: %s' % (name, getattr(self, name)))
    return '\n'.join(lines)


def CreateHostConfig(
    lab_name=None,
    cluster_name=None,
    hostname=None,
    host_login_name=None,
    tf_global_config_path=None,
    tmpfs_configs=None,
    docker_image=None,
    graceful_shutdown=False,
    shutdown_timeout_sec=_DEFAULT_SHUTDOWN_TIMEOUT_SEC,
    enable_stackdriver=False,
    enable_autoupdate=False,
    service_account_json_key_path=None,
    docker_server=None,
    extra_docker_args=(),
    control_server_url=None,
    secret_project_id=None,
    service_account_key_secret_id=None,
    service_account=None,
    enable_ui_update=None,
    engprod_api_key=None,
    ssh_arg=None,
    operation_mode=None,
    max_local_virtual_devices=None,
    max_concurrent_update_percentage=None,
):
  """Create a host config from raw data.

  Args:
    lab_name: lab name.
    cluster_name: cluster name.
    hostname: hostname.
    host_login_name: user name to login.
    tf_global_config_path: tf global config path.
    tmpfs_configs: a list of TmpfsConfig proto.
    docker_image: the docker image to use.
    graceful_shutdown: graceful shutdown the host or not.
    shutdown_timeout_sec: int, the dockerized TF shutdown timeout.
    enable_stackdriver: enable stackdriver monitor or not.
    enable_autoupdate: enable auto-update daemon or not.
    service_account_json_key_path: string or None, the file path of service
      account json key.
    docker_server: the docker server that hosts the image.
    extra_docker_args: extra docker args to pass to docker container.
    control_server_url: the control server the host connect to.
    secret_project_id: Google Cloud Project for storing secret.
    service_account_key_secret_id: Secret id for service account key.
    service_account: Service account for the lab.
    enable_ui_update: bool, whether host update from UI is enabled.
    engprod_api_key: string, API Key for Android Engprod API discovery.
    ssh_arg: string, ssh args to the host.
    operation_mode: string, host operation mode.
    max_local_virtual_devices: int, maximum number of virtual devices
    max_concurrent_update_percentage: int, maximum percentage of cluster level
      hosts being updated concurrently.
  Returns:
    a HostConfig have all those data.
  """
  host_config_pb = lab_config_pb2.HostConfig(
      hostname=hostname,
      tf_global_config_path=tf_global_config_path,
      tmpfs_configs=tmpfs_configs,
      enable_autoupdate=enable_autoupdate,
      docker_image=docker_image,
      docker_server=docker_server,
      extra_docker_args=list(extra_docker_args),
      enable_ui_update=enable_ui_update,
      shutdown_timeout_sec=shutdown_timeout_sec,
      max_local_virtual_devices=max_local_virtual_devices,)
  cluster_config_pb = lab_config_pb2.ClusterConfig(
      cluster_name=cluster_name,
      host_login_name=host_login_name,
      host_configs=[host_config_pb],
      docker_image=docker_image,
      graceful_shutdown=graceful_shutdown,
      shutdown_timeout_sec=shutdown_timeout_sec,
      enable_stackdriver=enable_stackdriver,
      control_server_url=control_server_url,
      max_concurrent_update_percentage=max_concurrent_update_percentage)
  lab_config_pb = lab_config_pb2.LabConfig(
      lab_name=lab_name,
      cluster_configs=[cluster_config_pb],
      docker_server=docker_server,
      service_account_json_key_path=service_account_json_key_path,
      secret_project_id=secret_project_id,
      service_account_key_secret_id=service_account_key_secret_id,
      service_account=service_account,
      engprod_api_key=engprod_api_key,
      ssh_arg=ssh_arg,
      operation_mode=operation_mode)
  return HostConfig(host_config_pb, cluster_config_pb, lab_config_pb)


def CreateTmpfsConfig(path, size, mode):
  """Create a TmpfsConfig object."""
  return lab_config_pb2.TmpfsConfig(path=path, size=size, mode=mode)


def IsYaml(path):
  """Is path a yaml file or not."""
  return path.endswith('.yaml')


def IsUnifiedLabConfig(path):
  """Is path a yaml file or not."""
  return path.endswith('hosts') or path.endswith('.ini')


def LocalFileEnumerator(root_path, filename_filter=None):
  """Enumerator files from local path.

  Args:
    root_path: the root of all configs.
    filename_filter: only return files that match the filter
  Yields:
    a file like obj
  Raises:
    ConfigError: raise if the config path doesn't exist.
  """
  logger.debug('Get file from root %s.', root_path)
  if not root_path:
    return
  paths = [root_path]
  while paths:
    path = paths.pop(0)
    if not os.path.exists(path):
      raise ConfigError('%s doesn\'t exist.' % path)
    if os.path.isfile(path):
      if filename_filter and not filename_filter(path):
        continue
      logger.debug('Read from %s.', path)
      with open(path, 'r') as file_obj:
        yield file_obj
    else:
      for root, _, filenames in os.walk(path):
        for filename in filenames:
          subpath = os.path.join(root, filename)
          paths.append(subpath)


class _AbstractLabConfigPool(abc.ABC):
  """Abstract lab config pool interface."""

  def __init__(self):
    self._lab_config_pb = None
    self._host_to_host_config = {}
    self._cluster_to_host_configs = collections.defaultdict(list)
    self._all_host_configs = []

  @abc.abstractmethod
  def LoadConfigs(self):
    """Load configs in the given path."""
    raise NotImplementedError()

  def GetLabConfig(self):
    """Get the lab config.

    Returns:
      a lab_config_pb2.LabConfig.
    """
    return self._lab_config_pb

  def GetHostConfigs(self, cluster_name=None):
    """Get hosts for under a certain cluster.

    Args:
      cluster_name: cluster name
    Returns:
      a list of host configs.
    """
    if cluster_name:
      return self._cluster_to_host_configs.get(cluster_name, [])
    return self._all_host_configs

  def GetHostConfig(self, hostname):
    """Get host config.

    Args:
      hostname: the host's name
    Returns:
      a HostConfig
    """
    return self._host_to_host_config.get(hostname)


class LabConfigPool(_AbstractLabConfigPool):
  """A config pool that can query configs for host and cluster."""

  def __init__(self, file_enumerator=None):
    super().__init__()
    self._file_enumerator = file_enumerator

  def LoadConfigs(self):
    """Load configs in the given path."""
    if not self._file_enumerator:
      logging.debug('Lab config is not set.')
      return
    has_config = False
    for file_obj in self._file_enumerator:
      if not has_config:
        has_config = True
        self._LoadConfig(file_obj)
      else:
        raise ConfigError('There are multiple config files.')
    if not has_config:
      raise ConfigError(
          'Lab config path is set, '
          'but there is no lab config files under the path.')

  def _LoadConfig(self, file_obj):
    """Load one config file."""
    self._lab_config_pb = Parse(file_obj)
    for cluster_config_pb in self._lab_config_pb.cluster_configs:
      for host_config_pb in cluster_config_pb.host_configs:
        host_config = HostConfig(
            host_config_pb, cluster_config_pb, self._lab_config_pb)
        self._host_to_host_config[host_config_pb.hostname] = host_config
        self._cluster_to_host_configs[cluster_config_pb.cluster_name].append(
            host_config)
        self._all_host_configs.append(host_config)


class UnifiedLabConfigPool(_AbstractLabConfigPool):
  """A config pool based on unified lab config."""

  def __init__(self, file_path):
    super().__init__()
    self._file_path = file_path

  def LoadConfigs(self):
    """Load configs in the given path."""
    if not self._file_path:
      logging.debug('Lab config is not set.')
      return
    if not os.path.exists(self._file_path):
      raise ConfigError('Config %s doesn\'t exist.' % self._file_path)
    if ansible_import_error:
      raise ansible_import_error
    unified_config_lab = unified_lab_config_util.Parse(self._file_path)
    self._lab_config_pb = json_format.ParseDict(
        unified_config_lab.ListGlobalVars(), lab_config_pb2.LabConfig(),
        ignore_unknown_fields=True)
    for h in unified_config_lab.ListHosts():
      if _TF_GROUP not in [g.name for g in h.groups]:
        continue
      host_config_pb = json_format.ParseDict(
          h.direct_vars, lab_config_pb2.HostConfig(),
          ignore_unknown_fields=True)
      host_config_pb.hostname = h.name
      cluster_config_dict = {}
      for g in h.groups:
        cluster_config_dict.update(g.direct_vars)
      cluster_config_dict['cluster_name'] = cluster_config_dict.get(
          'cluster_name', h.groups[-1].name)
      cluster_config_pb = json_format.ParseDict(
          cluster_config_dict, lab_config_pb2.ClusterConfig(),
          ignore_unknown_fields=True)
      host_config = HostConfig(
          host_config_pb, cluster_config_pb, self._lab_config_pb)
      self._host_to_host_config[host_config.hostname] = host_config
      for g in h.groups:
        self._cluster_to_host_configs[g.name].append(host_config)
      self._all_host_configs.append(host_config)