Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 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 | |
Benjamin Shai | 3c1a232 | 2023-02-10 17:29:46 +0000 | [diff] [blame] | 7 | from typing import Dict, Tuple, TYPE_CHECKING |
Kevin Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 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 |
Brian Norris | dd2e7e6 | 2023-06-16 14:07:32 -0700 | [diff] [blame^] | 12 | from chromite.api.controller import controller_util |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 13 | from chromite.api.gen.chromite.api import payload_pb2 |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib.paygen import paygen_payload_lib |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 16 | from chromite.service import payload |
| 17 | |
| 18 | |
Kevin Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 19 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | from chromite.api import api_config |
Kevin Shelton | 6fa9486 | 2022-04-08 20:46:23 -0700 | [diff] [blame] | 21 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | _VALID_IMAGE_PAIRS = ( |
| 23 | ("src_signed_image", "tgt_signed_image"), |
| 24 | ("src_unsigned_image", "tgt_unsigned_image"), |
| 25 | ("src_dlc_image", "tgt_dlc_image"), |
| 26 | ("full_update", "tgt_unsigned_image"), |
| 27 | ("full_update", "tgt_signed_image"), |
| 28 | ("full_update", "tgt_dlc_image"), |
| 29 | ) |
| 30 | _VALID_MINIOS_PAIRS = ( |
| 31 | ("src_signed_image", "tgt_signed_image"), |
| 32 | ("src_unsigned_image", "tgt_unsigned_image"), |
| 33 | ("full_update", "tgt_unsigned_image"), |
| 34 | ("full_update", "tgt_signed_image"), |
| 35 | ) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 36 | |
Alex Klein | c07a48b | 2022-08-26 15:58:44 -0600 | [diff] [blame] | 37 | # TODO: Remove to use the standard cache directory if possible, otherwise |
| 38 | # document why it cannot be used and preferably move outside of the repo. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | _DEFAULT_PAYGEN_CACHE_DIR = ".paygen_cache" |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 40 | |
Mike Frysinger | 1647479 | 2023-03-01 01:18:00 -0500 | [diff] [blame] | 41 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 42 | # We have more fields we might validate however, they're either |
| 43 | # 'oneof' or allowed to be the empty value by design. If @validate |
| 44 | # gets more complex in the future we can add more here. |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 45 | @faux.empty_success |
| 46 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | @validate.require("bucket") |
| 48 | def GeneratePayload( |
| 49 | input_proto: payload_pb2.GenerationRequest, |
| 50 | output_proto: payload_pb2.GenerationResponse, |
| 51 | config: "api_config.ApiConfig", |
| 52 | ) -> int: |
| 53 | """Generate a update payload ('do paygen'). |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 54 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 56 | input_proto: Input proto. |
| 57 | output_proto: Output proto. |
| 58 | config: The API call config. |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 61 | A controller return code (e.g. controller.RETURN_CODE_SUCCESS). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | """ |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | # Resolve the tgt image oneof. |
| 65 | tgt_name = input_proto.WhichOneof("tgt_image_oneof") |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 66 | try: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | tgt_image = getattr(input_proto, tgt_name) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 68 | except AttributeError: |
Alex Klein | df8ee50 | 2022-10-18 09:48:15 -0600 | [diff] [blame] | 69 | cros_build_lib.Die("%s is not a known tgt image type", tgt_name) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | # Resolve the src image oneof. |
| 72 | src_name = input_proto.WhichOneof("src_image_oneof") |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | # If the source image is 'full_update' we lack a source entirely. |
| 75 | if src_name == "full_update": |
| 76 | src_image = None |
| 77 | # Otherwise we have an image. |
| 78 | else: |
| 79 | try: |
| 80 | src_image = getattr(input_proto, src_name) |
| 81 | except AttributeError: |
Alex Klein | df8ee50 | 2022-10-18 09:48:15 -0600 | [diff] [blame] | 82 | cros_build_lib.Die("%s is not a known src image type", src_name) |
Greg Edelston | 629468c | 2022-02-11 14:54:56 -0700 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | # Ensure they are compatible oneofs. |
| 85 | if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS: |
| 86 | cros_build_lib.Die( |
Alex Klein | df8ee50 | 2022-10-18 09:48:15 -0600 | [diff] [blame] | 87 | "%s and %s are not valid image pairs", src_image, tgt_image |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | ) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | # Ensure that miniOS payloads are only requested for compatible image types. |
| 91 | if input_proto.minios and (src_name, tgt_name) not in _VALID_MINIOS_PAIRS: |
| 92 | cros_build_lib.Die( |
Alex Klein | df8ee50 | 2022-10-18 09:48:15 -0600 | [diff] [blame] | 93 | "%s and %s are not valid image pairs for miniOS", |
| 94 | src_image, |
| 95 | tgt_image, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | ) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | # Find the value of bucket or default to 'chromeos-releases'. |
| 99 | destination_bucket = input_proto.bucket or "chromeos-releases" |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 100 | |
Brian Norris | dd2e7e6 | 2023-06-16 14:07:32 -0700 | [diff] [blame^] | 101 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 102 | |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 103 | # There's a potential that some paygen_lib library might raise here, but |
| 104 | # since we're still involved in config we'll keep it before the |
| 105 | # validate_only. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | payload_config = payload.PayloadConfig( |
Brian Norris | dd2e7e6 | 2023-06-16 14:07:32 -0700 | [diff] [blame^] | 107 | chroot, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | tgt_image, |
| 109 | src_image, |
| 110 | destination_bucket, |
| 111 | input_proto.minios, |
| 112 | input_proto.verify, |
| 113 | upload=not input_proto.dryrun, |
| 114 | cache_dir=_DEFAULT_PAYGEN_CACHE_DIR, |
| 115 | ) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 116 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 117 | # If configured for validation only we're done here. |
| 118 | if config.validate_only: |
| 119 | return controller.RETURN_CODE_VALID_INPUT |
| 120 | |
| 121 | # Do payload generation. |
Benjamin Shai | 3c1a232 | 2023-02-10 17:29:46 +0000 | [diff] [blame] | 122 | artifacts = {} |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | try: |
Benjamin Shai | 3c1a232 | 2023-02-10 17:29:46 +0000 | [diff] [blame] | 124 | artifacts = payload_config.GeneratePayload() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | except paygen_payload_lib.PayloadGenerationSkippedException as e: |
| 126 | # If paygen was skipped, provide a reason if possible. |
Jack Neus | 2577e2d | 2023-04-06 16:14:01 +0000 | [diff] [blame] | 127 | if isinstance(e, paygen_payload_lib.MiniOSException): |
| 128 | reason = e.return_code() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | output_proto.failure_reason = reason |
| 130 | |
Benjamin Shai | 3c1a232 | 2023-02-10 17:29:46 +0000 | [diff] [blame] | 131 | _SetGeneratePayloadOutputProto(output_proto, artifacts) |
| 132 | if _SuccessfulPaygen(artifacts, input_proto.dryrun): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | return controller.RETURN_CODE_SUCCESS |
| 134 | elif output_proto.failure_reason: |
| 135 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
| 136 | else: |
| 137 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 138 | |
| 139 | |
Benjamin Shai | 3c1a232 | 2023-02-10 17:29:46 +0000 | [diff] [blame] | 140 | def _SuccessfulPaygen( |
| 141 | artifacts: Dict[int, Tuple[str, str]], dryrun: bool |
| 142 | ) -> bool: |
| 143 | """Check to see if the payload generation was successful. |
| 144 | |
| 145 | Args: |
| 146 | artifacts: a dict containing an artifact tuple keyed by its |
| 147 | version. Artifacts tuple is (local_path, remote_uri). |
| 148 | dryrun: whether or not this was a dry run job. |
| 149 | """ |
| 150 | if not artifacts: |
| 151 | return False |
| 152 | for _, artifact in artifacts.items(): |
| 153 | if not (artifact[1] or dryrun and artifact[0]): |
| 154 | return False |
| 155 | return True |
| 156 | |
| 157 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | def _SetGeneratePayloadOutputProto( |
| 159 | output_proto: payload_pb2.GenerationResponse, |
Benjamin Shai | 3c1a232 | 2023-02-10 17:29:46 +0000 | [diff] [blame] | 160 | artifacts: Dict[int, Tuple[str, str]], |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | ): |
| 162 | """Set the output proto with the results from the service class. |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 163 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 165 | output_proto: The output proto. |
Benjamin Shai | 3c1a232 | 2023-02-10 17:29:46 +0000 | [diff] [blame] | 166 | artifacts: a dict containing an artifact tuple keyed by its |
| 167 | version. Artifacts tuple is (local_path, remote_uri). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 168 | """ |
Benjamin Shai | 3c1a232 | 2023-02-10 17:29:46 +0000 | [diff] [blame] | 169 | for version, artifact in artifacts.items(): |
| 170 | versioned_artifact = output_proto.versioned_artifacts.add() |
| 171 | versioned_artifact.version = version |
| 172 | versioned_artifact.local_path = artifact[0] or "" |
| 173 | versioned_artifact.remote_uri = artifact[1] or "" |