blob: 015245d12a77934759f7accbf97f6ca704897818 [file] [log] [blame]
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06001# Copyright 2019 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"""Payload API Service."""
6
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06007from chromite.api import controller
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06008from chromite.api import faux
9from chromite.api import validate
Greg Edelston629468c2022-02-11 14:54:56 -070010from chromite.api.gen.chromite.api import payload_pb2
11from chromite.lib import cros_build_lib
12from chromite.lib.paygen import paygen_payload_lib
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060013from chromite.service import payload
14
15
16_VALID_IMAGE_PAIRS = (('src_signed_image', 'tgt_signed_image'),
17 ('src_unsigned_image', 'tgt_unsigned_image'),
George Engelbrecht7bea3742020-11-17 20:21:11 -070018 ('src_dlc_image', 'tgt_dlc_image'),
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060019 ('full_update', 'tgt_unsigned_image'),
George Engelbrecht7bea3742020-11-17 20:21:11 -070020 ('full_update', 'tgt_signed_image'),
21 ('full_update', 'tgt_dlc_image'))
Greg Edelston629468c2022-02-11 14:54:56 -070022_VALID_MINIOS_PAIRS = (('src_signed_image', 'tgt_signed_image'),
23 ('src_unsigned_image', 'tgt_unsigned_image'),
24 ('full_update', 'tgt_unsigned_image'),
25 ('full_update', 'tgt_signed_image'))
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060026
George Engelbrechta42172f2020-11-04 12:42:04 -070027_DEFAULT_PAYGEN_CACHE_DIR = '.paygen_cache'
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060028
29# We have more fields we might validate however, they're either
30# 'oneof' or allowed to be the empty value by design. If @validate
31# gets more complex in the future we can add more here.
Michael Mortensen85d38402019-12-12 09:50:29 -070032@faux.empty_success
33@faux.empty_completed_unsuccessfully_error
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060034@validate.require('bucket')
35def GeneratePayload(input_proto, output_proto, config):
36 """Generate a update payload ('do paygen').
George Engelbrechtd2348bb2019-11-25 21:18:14 -070037
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060038 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -070039 input_proto (PayloadGenerationRequest): Input proto.
40 output_proto (PayloadGenerationResult): Output proto.
41 config (api.config.ApiConfig): The API call config.
42
43 Returns:
44 A controller return code (e.g. controller.RETURN_CODE_SUCCESS).
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060045 """
46
47 # Resolve the tgt image oneof.
48 tgt_name = input_proto.WhichOneof('tgt_image_oneof')
49 try:
50 tgt_image = getattr(input_proto, tgt_name)
51 except AttributeError:
52 cros_build_lib.Die('%s is not a known tgt image type' % (tgt_name,))
53
54 # Resolve the src image oneof.
55 src_name = input_proto.WhichOneof('src_image_oneof')
56
57 # If the source image is 'full_update' we lack a source entirely.
58 if src_name == 'full_update':
59 src_image = None
60 # Otherwise we have an image.
61 else:
62 try:
63 src_image = getattr(input_proto, src_name)
64 except AttributeError:
65 cros_build_lib.Die('%s is not a known src image type' % (src_name,))
66
67 # Ensure they are compatible oneofs.
68 if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS:
69 cros_build_lib.Die('%s and %s are not valid image pairs' %
70 (src_image, tgt_image))
71
Greg Edelston629468c2022-02-11 14:54:56 -070072 # Ensure that miniOS payloads are only requested for compatible image types.
73 if input_proto.minios and (src_name, tgt_name) not in _VALID_MINIOS_PAIRS:
74 cros_build_lib.Die('%s and %s are not valid image pairs for miniOS' %
75 (src_image, tgt_image))
76
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060077 # Find the value of bucket or default to 'chromeos-releases'.
78 destination_bucket = input_proto.bucket or 'chromeos-releases'
79
80 # There's a potential that some paygen_lib library might raise here, but since
81 # we're still involved in config we'll keep it before the validate_only.
Navil Perezf5482be2020-04-09 23:18:14 -060082 payload_config = payload.PayloadConfig(tgt_image, src_image,
Greg Edelston629468c2022-02-11 14:54:56 -070083 destination_bucket, input_proto.minios,
84 input_proto.verify,
George Engelbrechte2aaad12021-11-19 14:52:38 -070085 upload=not input_proto.dryrun,
George Engelbrechta42172f2020-11-04 12:42:04 -070086 cache_dir=_DEFAULT_PAYGEN_CACHE_DIR)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060087
88 # If configured for validation only we're done here.
89 if config.validate_only:
90 return controller.RETURN_CODE_VALID_INPUT
91
92 # Do payload generation.
Greg Edelston629468c2022-02-11 14:54:56 -070093 local_path, remote_uri = '', ''
94 try:
95 local_path, remote_uri = payload_config.GeneratePayload()
96 except paygen_payload_lib.PayloadGenerationSkippedException as e:
97 # If paygen was skipped, provide a reason if possible.
98 if isinstance(e, paygen_payload_lib.NoMiniOSPartitionException):
99 reason = payload_pb2.GenerationResponse.NOT_MINIOS_COMPATIBLE
100 output_proto.failure_reason = reason
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600101
Greg Edelston629468c2022-02-11 14:54:56 -0700102 _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri)
George Engelbrechte2aaad12021-11-19 14:52:38 -0700103 if remote_uri or input_proto.dryrun and local_path:
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600104 return controller.RETURN_CODE_SUCCESS
Benjamin Shai6e97bc02022-03-07 21:34:39 +0000105 elif output_proto.failure_reason:
106 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600107 else:
108 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
109
110
George Engelbrecht52d0dba2020-11-12 11:49:02 -0700111def _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri):
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600112 """Set the output proto with the results from the service class.
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700113
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600114 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700115 output_proto (PayloadGenerationResult_pb2): The output proto.
George Engelbrechtdbc96742020-11-13 15:59:48 -0700116 local_path (str): set output_proto with the local path, or ''.
117 remote_uri (str): set output_proto with the remote uri, or ''.
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600118 """
George Engelbrecht52d0dba2020-11-12 11:49:02 -0700119 output_proto.success = True
George Engelbrechtdbc96742020-11-13 15:59:48 -0700120 output_proto.local_path = local_path or ''
121 output_proto.remote_uri = remote_uri or ''