Home | History | Annotate | Download | only in site_utils
      1 # Copyright 2014 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 # This file contains utility functions to get and set stable versions for given
      6 # boards.
      7 
      8 import common
      9 import django.core.exceptions
     10 from autotest_lib.client.common_lib import global_config
     11 from autotest_lib.client.common_lib.cros.graphite import autotest_es
     12 from autotest_lib.frontend import setup_django_environment
     13 from autotest_lib.frontend.afe import models
     14 
     15 
     16 # Name of the default board. For boards that don't have stable version
     17 # explicitly set, version for the default board will be used.
     18 DEFAULT = 'DEFAULT'
     19 
     20 # Type of metadata to store stable_version changes.
     21 _STABLE_VERSION_TYPE = 'stable_version'
     22 
     23 def get_all():
     24     """Get stable versions of all boards.
     25 
     26     @return: A dictionary of boards and stable versions.
     27     """
     28     versions = dict([(v.board, v.version)
     29                      for v in models.StableVersion.objects.all()])
     30     # Set default to the global config value of CROS.stable_cros_version if
     31     # there is no entry in afe_stable_versions table.
     32     if not versions:
     33         versions = {DEFAULT: global_config.global_config.get_config_value(
     34                             'CROS', 'stable_cros_version')}
     35     return versions
     36 
     37 
     38 def get(board=DEFAULT, android=False):
     39     """Get stable version for the given board.
     40 
     41     @param board: Name of the board, default to value `DEFAULT`.
     42     @param android: If True, indicates we are looking up a Android/Brillo-based
     43                     board. There is no default version that works for all
     44                     Android/Brillo boards. If False, we are looking up a Chrome
     45                     OS based board.
     46 
     47     @return: Stable version of the given board. If the given board is not listed
     48              in afe_stable_versions table, DEFAULT will be used.
     49              Return global_config value of CROS.stable_cros_version if
     50              afe_stable_versions table does not have entry of board DEFAULT.
     51     """
     52     if board == DEFAULT and android:
     53         return None
     54     try:
     55         return models.StableVersion.objects.get(board=board).version
     56     except django.core.exceptions.ObjectDoesNotExist:
     57         if board == DEFAULT:
     58             return global_config.global_config.get_config_value(
     59                     'CROS', 'stable_cros_version')
     60         elif android:
     61             return global_config.global_config.get_config_value(
     62                     'ANDROID', 'stable_version_%s' % board, default=None)
     63         else:
     64             return get(board=DEFAULT)
     65 
     66 
     67 def set(version, board=DEFAULT):
     68     """Set stable version for the given board.
     69 
     70     @param version: The new value of stable version for given board.
     71     @param board: Name of the board, default to value `DEFAULT`.
     72     """
     73     try:
     74         stable_version = models.StableVersion.objects.get(board=board)
     75         stable_version.version = version
     76         stable_version.save()
     77     except django.core.exceptions.ObjectDoesNotExist:
     78         models.StableVersion.objects.create(board=board, version=version)
     79     autotest_es.post(type_str=_STABLE_VERSION_TYPE,
     80                      metadata={'board': board, 'version': version})
     81 
     82 
     83 def delete(board):
     84     """Delete stable version record for the given board.
     85 
     86     @param board: Name of the board.
     87     """
     88     stable_version = models.StableVersion.objects.get(board=board)
     89     stable_version.delete()
     90     autotest_es.post(type_str=_STABLE_VERSION_TYPE,
     91                      metadata={'board': board, 'version': get()})
     92