blob: 600f8de53c13c442967c1fb0effabbfd0fc0348a [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
Kevin Shelton6fa94862022-04-08 20:46:23 -07007from typing import TYPE_CHECKING
8
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06009from chromite.api import controller
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060010from chromite.api import faux
11from chromite.api import validate
Greg Edelston629468c2022-02-11 14:54:56 -070012from chromite.api.gen.chromite.api import payload_pb2
13from chromite.lib import cros_build_lib
14from chromite.lib.paygen import paygen_payload_lib
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060015from chromite.service import payload
16
17
Kevin Shelton6fa94862022-04-08 20:46:23 -070018if TYPE_CHECKING:
19 from chromite.api import api_config
20
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060021_VALID_IMAGE_PAIRS = (('src_signed_image', 'tgt_signed_image'),
22 ('src_unsigned_image', 'tgt_unsigned_image'),
George Engelbrecht7bea3742020-11-17 20:21:11 -070023 ('src_dlc_image', 'tgt_dlc_image'),
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060024 ('full_update', 'tgt_unsigned_image'),
George Engelbrecht7bea3742020-11-17 20:21:11 -070025 ('full_update', 'tgt_signed_image'),
26 ('full_update', 'tgt_dlc_image'))
Greg Edelston629468c2022-02-11 14:54:56 -070027_VALID_MINIOS_PAIRS = (('src_signed_image', 'tgt_signed_image'),
28 ('src_unsigned_image', 'tgt_unsigned_image'),
29 ('full_update', 'tgt_unsigned_image'),
30 ('full_update', 'tgt_signed_image'))
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060031
George Engelbrechta42172f2020-11-04 12:42:04 -070032_DEFAULT_PAYGEN_CACHE_DIR = '.paygen_cache'
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060033
34# We have more fields we might validate however, they're either
35# 'oneof' or allowed to be the empty value by design. If @validate
36# gets more complex in the future we can add more here.
Michael Mortensen85d38402019-12-12 09:50:29 -070037@faux.empty_success
38@faux.empty_completed_unsuccessfully_error
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060039@validate.require('bucket')
Kevin Shelton6fa94862022-04-08 20:46:23 -070040def GeneratePayload(input_proto: payload_pb2.GenerationRequest,
41 output_proto: payload_pb2.GenerationResponse,
42 config: 'api_config.ApiConfig') -> int:
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060043 """Generate a update payload ('do paygen').
George Engelbrechtd2348bb2019-11-25 21:18:14 -070044
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060045 Args:
Kevin Shelton6fa94862022-04-08 20:46:23 -070046 input_proto: Input proto.
47 output_proto: Output proto.
48 config: The API call config.
George Engelbrechtd2348bb2019-11-25 21:18:14 -070049
50 Returns:
51 A controller return code (e.g. controller.RETURN_CODE_SUCCESS).
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060052 """
53
54 # Resolve the tgt image oneof.
55 tgt_name = input_proto.WhichOneof('tgt_image_oneof')
56 try:
57 tgt_image = getattr(input_proto, tgt_name)
58 except AttributeError:
59 cros_build_lib.Die('%s is not a known tgt image type' % (tgt_name,))
60
61 # Resolve the src image oneof.
62 src_name = input_proto.WhichOneof('src_image_oneof')
63
64 # If the source image is 'full_update' we lack a source entirely.
65 if src_name == 'full_update':
66 src_image = None
67 # Otherwise we have an image.
68 else:
69 try:
70 src_image = getattr(input_proto, src_name)
71 except AttributeError:
72 cros_build_lib.Die('%s is not a known src image type' % (src_name,))
73
74 # Ensure they are compatible oneofs.
75 if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS:
76 cros_build_lib.Die('%s and %s are not valid image pairs' %
77 (src_image, tgt_image))
78
Greg Edelston629468c2022-02-11 14:54:56 -070079 # Ensure that miniOS payloads are only requested for compatible image types.
80 if input_proto.minios and (src_name, tgt_name) not in _VALID_MINIOS_PAIRS:
81 cros_build_lib.Die('%s and %s are not valid image pairs for miniOS' %
82 (src_image, tgt_image))
83
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060084 # Find the value of bucket or default to 'chromeos-releases'.
85 destination_bucket = input_proto.bucket or 'chromeos-releases'
86
87 # There's a potential that some paygen_lib library might raise here, but since
88 # we're still involved in config we'll keep it before the validate_only.
Navil Perezf5482be2020-04-09 23:18:14 -060089 payload_config = payload.PayloadConfig(tgt_image, src_image,
Greg Edelston629468c2022-02-11 14:54:56 -070090 destination_bucket, input_proto.minios,
91 input_proto.verify,
George Engelbrechte2aaad12021-11-19 14:52:38 -070092 upload=not input_proto.dryrun,
George Engelbrechta42172f2020-11-04 12:42:04 -070093 cache_dir=_DEFAULT_PAYGEN_CACHE_DIR)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060094
95 # If configured for validation only we're done here.
96 if config.validate_only:
97 return controller.RETURN_CODE_VALID_INPUT
98
99 # Do payload generation.
Greg Edelston629468c2022-02-11 14:54:56 -0700100 local_path, remote_uri = '', ''
101 try:
102 local_path, remote_uri = payload_config.GeneratePayload()
Jae Hoon Kim9cb38d42022-08-26 01:02:23 +0000103 except paygen_payload_lib.PayloadGenerationSkippedException:
Greg Edelston629468c2022-02-11 14:54:56 -0700104 # If paygen was skipped, provide a reason if possible.
Jae Hoon Kim9cb38d42022-08-26 01:02:23 +0000105 # Only reason to skip right now is missing miniOS partitions.
106 reason = payload_pb2.GenerationResponse.NOT_MINIOS_COMPATIBLE
107 output_proto.failure_reason = reason
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600108
Greg Edelston629468c2022-02-11 14:54:56 -0700109 _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri)
George Engelbrechte2aaad12021-11-19 14:52:38 -0700110 if remote_uri or input_proto.dryrun and local_path:
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600111 return controller.RETURN_CODE_SUCCESS
Benjamin Shai6e97bc02022-03-07 21:34:39 +0000112 elif output_proto.failure_reason:
113 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600114 else:
115 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
116
117
Kevin Shelton6fa94862022-04-08 20:46:23 -0700118def _SetGeneratePayloadOutputProto(output_proto: payload_pb2.GenerationResponse,
119 local_path: str,
120 remote_uri: str):
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600121 """Set the output proto with the results from the service class.
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700122
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600123 Args:
Kevin Shelton6fa94862022-04-08 20:46:23 -0700124 output_proto: The output proto.
125 local_path: set output_proto with the local path, or ''.
126 remote_uri: set output_proto with the remote uri, or ''.
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600127 """
George Engelbrecht52d0dba2020-11-12 11:49:02 -0700128 output_proto.success = True
George Engelbrechtdbc96742020-11-13 15:59:48 -0700129 output_proto.local_path = local_path or ''
130 output_proto.remote_uri = remote_uri or ''