George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 1 | # 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 Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 7 | from typing import TYPE_CHECKING |
| 8 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 9 | from chromite.api import controller |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 10 | from chromite.api import faux |
| 11 | from chromite.api import validate |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 12 | from chromite.api.gen.chromite.api import payload_pb2 |
| 13 | from chromite.lib import cros_build_lib |
| 14 | from chromite.lib.paygen import paygen_payload_lib |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 15 | from chromite.service import payload |
| 16 | |
| 17 | |
Kevin Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 18 | if TYPE_CHECKING: |
| 19 | from chromite.api import api_config |
| 20 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 21 | _VALID_IMAGE_PAIRS = (('src_signed_image', 'tgt_signed_image'), |
| 22 | ('src_unsigned_image', 'tgt_unsigned_image'), |
George Engelbrecht | 7bea374 | 2020-11-17 20:21:11 -0700 | [diff] [blame] | 23 | ('src_dlc_image', 'tgt_dlc_image'), |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 24 | ('full_update', 'tgt_unsigned_image'), |
George Engelbrecht | 7bea374 | 2020-11-17 20:21:11 -0700 | [diff] [blame] | 25 | ('full_update', 'tgt_signed_image'), |
| 26 | ('full_update', 'tgt_dlc_image')) |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 27 | _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 Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 31 | |
Alex Klein | c07a48b | 2022-08-26 15:58:44 -0600 | [diff] [blame^] | 32 | # TODO: Remove to use the standard cache directory if possible, otherwise |
| 33 | # document why it cannot be used and preferably move outside of the repo. |
George Engelbrecht | a42172f | 2020-11-04 12:42:04 -0700 | [diff] [blame] | 34 | _DEFAULT_PAYGEN_CACHE_DIR = '.paygen_cache' |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 35 | |
| 36 | # We have more fields we might validate however, they're either |
| 37 | # 'oneof' or allowed to be the empty value by design. If @validate |
| 38 | # gets more complex in the future we can add more here. |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 39 | @faux.empty_success |
| 40 | @faux.empty_completed_unsuccessfully_error |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 41 | @validate.require('bucket') |
Kevin Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 42 | def GeneratePayload(input_proto: payload_pb2.GenerationRequest, |
| 43 | output_proto: payload_pb2.GenerationResponse, |
| 44 | config: 'api_config.ApiConfig') -> int: |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 45 | """Generate a update payload ('do paygen'). |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 46 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 47 | Args: |
Kevin Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 48 | input_proto: Input proto. |
| 49 | output_proto: Output proto. |
| 50 | config: The API call config. |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 51 | |
| 52 | Returns: |
| 53 | A controller return code (e.g. controller.RETURN_CODE_SUCCESS). |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 54 | """ |
| 55 | |
| 56 | # Resolve the tgt image oneof. |
| 57 | tgt_name = input_proto.WhichOneof('tgt_image_oneof') |
| 58 | try: |
| 59 | tgt_image = getattr(input_proto, tgt_name) |
| 60 | except AttributeError: |
| 61 | cros_build_lib.Die('%s is not a known tgt image type' % (tgt_name,)) |
| 62 | |
| 63 | # Resolve the src image oneof. |
| 64 | src_name = input_proto.WhichOneof('src_image_oneof') |
| 65 | |
| 66 | # If the source image is 'full_update' we lack a source entirely. |
| 67 | if src_name == 'full_update': |
| 68 | src_image = None |
| 69 | # Otherwise we have an image. |
| 70 | else: |
| 71 | try: |
| 72 | src_image = getattr(input_proto, src_name) |
| 73 | except AttributeError: |
| 74 | cros_build_lib.Die('%s is not a known src image type' % (src_name,)) |
| 75 | |
| 76 | # Ensure they are compatible oneofs. |
| 77 | if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS: |
| 78 | cros_build_lib.Die('%s and %s are not valid image pairs' % |
| 79 | (src_image, tgt_image)) |
| 80 | |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 81 | # Ensure that miniOS payloads are only requested for compatible image types. |
| 82 | if input_proto.minios and (src_name, tgt_name) not in _VALID_MINIOS_PAIRS: |
| 83 | cros_build_lib.Die('%s and %s are not valid image pairs for miniOS' % |
| 84 | (src_image, tgt_image)) |
| 85 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 86 | # Find the value of bucket or default to 'chromeos-releases'. |
| 87 | destination_bucket = input_proto.bucket or 'chromeos-releases' |
| 88 | |
| 89 | # There's a potential that some paygen_lib library might raise here, but since |
| 90 | # we're still involved in config we'll keep it before the validate_only. |
Navil Perez | f5482be | 2020-04-09 23:18:14 -0600 | [diff] [blame] | 91 | payload_config = payload.PayloadConfig(tgt_image, src_image, |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 92 | destination_bucket, input_proto.minios, |
| 93 | input_proto.verify, |
George Engelbrecht | e2aaad1 | 2021-11-19 14:52:38 -0700 | [diff] [blame] | 94 | upload=not input_proto.dryrun, |
George Engelbrecht | a42172f | 2020-11-04 12:42:04 -0700 | [diff] [blame] | 95 | cache_dir=_DEFAULT_PAYGEN_CACHE_DIR) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 96 | |
| 97 | # If configured for validation only we're done here. |
| 98 | if config.validate_only: |
| 99 | return controller.RETURN_CODE_VALID_INPUT |
| 100 | |
| 101 | # Do payload generation. |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 102 | local_path, remote_uri = '', '' |
| 103 | try: |
| 104 | local_path, remote_uri = payload_config.GeneratePayload() |
Jae Hoon Kim | 81482cf | 2022-08-26 21:55:13 +0000 | [diff] [blame] | 105 | except paygen_payload_lib.PayloadGenerationSkippedException as e: |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 106 | # If paygen was skipped, provide a reason if possible. |
Jae Hoon Kim | 81482cf | 2022-08-26 21:55:13 +0000 | [diff] [blame] | 107 | if isinstance(e, paygen_payload_lib.NoMiniOSPartitionException): |
| 108 | reason = payload_pb2.GenerationResponse.NOT_MINIOS_COMPATIBLE |
| 109 | output_proto.failure_reason = reason |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 110 | |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 111 | _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri) |
George Engelbrecht | e2aaad1 | 2021-11-19 14:52:38 -0700 | [diff] [blame] | 112 | if remote_uri or input_proto.dryrun and local_path: |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 113 | return controller.RETURN_CODE_SUCCESS |
Benjamin Shai | 6e97bc0 | 2022-03-07 21:34:39 +0000 | [diff] [blame] | 114 | elif output_proto.failure_reason: |
| 115 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 116 | else: |
| 117 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 118 | |
| 119 | |
Kevin Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 120 | def _SetGeneratePayloadOutputProto(output_proto: payload_pb2.GenerationResponse, |
| 121 | local_path: str, |
| 122 | remote_uri: str): |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 123 | """Set the output proto with the results from the service class. |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 124 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 125 | Args: |
Kevin Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 126 | output_proto: The output proto. |
| 127 | local_path: set output_proto with the local path, or ''. |
| 128 | remote_uri: set output_proto with the remote uri, or ''. |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 129 | """ |
George Engelbrecht | 52d0dba | 2020-11-12 11:49:02 -0700 | [diff] [blame] | 130 | output_proto.success = True |
George Engelbrecht | dbc9674 | 2020-11-13 15:59:48 -0700 | [diff] [blame] | 131 | output_proto.local_path = local_path or '' |
| 132 | output_proto.remote_uri = remote_uri or '' |