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