blob: 4c849b1e16f2e559a90b37ab862dff07d892cd87 [file] [log] [blame]
Dan Shi6964fa52014-12-18 11:04:27 -08001# 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
8import common
9import django.core.exceptions
10from autotest_lib.client.common_lib import global_config
Dan Shi25e1fd42014-12-19 14:36:42 -080011from autotest_lib.client.common_lib.cros.graphite import es_utils
Dan Shi6964fa52014-12-18 11:04:27 -080012from autotest_lib.frontend import setup_django_environment
13from 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.
18DEFAULT = 'DEFAULT'
19
Dan Shi25e1fd42014-12-19 14:36:42 -080020# Type of metadata to store stable_version changes.
21_STABLE_VERSION_TYPE = 'stable_version'
22
Dan Shi6964fa52014-12-18 11:04:27 -080023def get_all():
24 """Get stable versions of all boards.
25
26 @return: A dictionary of boards and stable versions.
27 """
Dan Shi25e1fd42014-12-19 14:36:42 -080028 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
Dan Shi6964fa52014-12-18 11:04:27 -080036
37
Dan Shi25e1fd42014-12-19 14:36:42 -080038def get(board=DEFAULT):
Dan Shi6964fa52014-12-18 11:04:27 -080039 """Get stable version for the given board.
40
41 @param board: Name of the board, default to value `DEFAULT`.
Dan Shi25e1fd42014-12-19 14:36:42 -080042 @return: Stable version of the given board. If the given board is not listed
43 in afe_stable_versions table, DEFAULT will be used.
44 Return global_config value of CROS.stable_cros_version if
45 afe_stable_versions table does not have entry of board DEFAULT.
Dan Shi6964fa52014-12-18 11:04:27 -080046 """
47 try:
48 return models.StableVersion.objects.get(board=board).version
49 except django.core.exceptions.ObjectDoesNotExist:
50 if board == DEFAULT:
51 return global_config.global_config.get_config_value(
52 'CROS', 'stable_cros_version')
53 else:
Dan Shi25e1fd42014-12-19 14:36:42 -080054 return get(board=DEFAULT)
55
56
57def set(version, board=DEFAULT):
58 """Set stable version for the given board.
59
60 @param version: The new value of stable version for given board.
61 @param board: Name of the board, default to value `DEFAULT`.
62 """
63 try:
64 stable_version = models.StableVersion.objects.get(board=board)
65 stable_version.version = version
66 stable_version.save()
67 except django.core.exceptions.ObjectDoesNotExist:
68 models.StableVersion.objects.create(board=board, version=version)
69 es_utils.ESMetadata().post(type_str=_STABLE_VERSION_TYPE,
70 metadata={'board': board, 'version': version})
71
72
73def delete(board):
74 """Delete stable version record for the given board.
75
76 @param board: Name of the board.
77 """
78 stable_version = models.StableVersion.objects.get(board=board)
79 stable_version.delete()
80 es_utils.ESMetadata().post(type_str=_STABLE_VERSION_TYPE,
81 metadata={'board': board, 'version': get()})