blob: 58d0fefd5b2fffa81366979993e16c37898005f0 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06002# 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:
Alex Klein1699fab2022-09-08 08:46:06 -060019 from chromite.api import api_config
Kevin Shelton6fa94862022-04-08 20:46:23 -070020
Alex Klein1699fab2022-09-08 08:46:06 -060021_VALID_IMAGE_PAIRS = (
22 ("src_signed_image", "tgt_signed_image"),
23 ("src_unsigned_image", "tgt_unsigned_image"),
24 ("src_dlc_image", "tgt_dlc_image"),
25 ("full_update", "tgt_unsigned_image"),
26 ("full_update", "tgt_signed_image"),
27 ("full_update", "tgt_dlc_image"),
28)
29_VALID_MINIOS_PAIRS = (
30 ("src_signed_image", "tgt_signed_image"),
31 ("src_unsigned_image", "tgt_unsigned_image"),
32 ("full_update", "tgt_unsigned_image"),
33 ("full_update", "tgt_signed_image"),
34)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060035
Alex Kleinc07a48b2022-08-26 15:58:44 -060036# TODO: Remove to use the standard cache directory if possible, otherwise
37# document why it cannot be used and preferably move outside of the repo.
Alex Klein1699fab2022-09-08 08:46:06 -060038_DEFAULT_PAYGEN_CACHE_DIR = ".paygen_cache"
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060039
40# We have more fields we might validate however, they're either
41# 'oneof' or allowed to be the empty value by design. If @validate
42# gets more complex in the future we can add more here.
Michael Mortensen85d38402019-12-12 09:50:29 -070043@faux.empty_success
44@faux.empty_completed_unsuccessfully_error
Alex Klein1699fab2022-09-08 08:46:06 -060045@validate.require("bucket")
46def GeneratePayload(
47 input_proto: payload_pb2.GenerationRequest,
48 output_proto: payload_pb2.GenerationResponse,
49 config: "api_config.ApiConfig",
50) -> int:
51 """Generate a update payload ('do paygen').
George Engelbrechtd2348bb2019-11-25 21:18:14 -070052
Alex Klein1699fab2022-09-08 08:46:06 -060053 Args:
Alex Klein611dddd2022-10-11 17:02:01 -060054 input_proto: Input proto.
55 output_proto: Output proto.
56 config: The API call config.
George Engelbrechtd2348bb2019-11-25 21:18:14 -070057
Alex Klein1699fab2022-09-08 08:46:06 -060058 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -060059 A controller return code (e.g. controller.RETURN_CODE_SUCCESS).
Alex Klein1699fab2022-09-08 08:46:06 -060060 """
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060061
Alex Klein1699fab2022-09-08 08:46:06 -060062 # Resolve the tgt image oneof.
63 tgt_name = input_proto.WhichOneof("tgt_image_oneof")
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060064 try:
Alex Klein1699fab2022-09-08 08:46:06 -060065 tgt_image = getattr(input_proto, tgt_name)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060066 except AttributeError:
Alex Kleindf8ee502022-10-18 09:48:15 -060067 cros_build_lib.Die("%s is not a known tgt image type", tgt_name)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060068
Alex Klein1699fab2022-09-08 08:46:06 -060069 # Resolve the src image oneof.
70 src_name = input_proto.WhichOneof("src_image_oneof")
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060071
Alex Klein1699fab2022-09-08 08:46:06 -060072 # If the source image is 'full_update' we lack a source entirely.
73 if src_name == "full_update":
74 src_image = None
75 # Otherwise we have an image.
76 else:
77 try:
78 src_image = getattr(input_proto, src_name)
79 except AttributeError:
Alex Kleindf8ee502022-10-18 09:48:15 -060080 cros_build_lib.Die("%s is not a known src image type", src_name)
Greg Edelston629468c2022-02-11 14:54:56 -070081
Alex Klein1699fab2022-09-08 08:46:06 -060082 # Ensure they are compatible oneofs.
83 if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS:
84 cros_build_lib.Die(
Alex Kleindf8ee502022-10-18 09:48:15 -060085 "%s and %s are not valid image pairs", src_image, tgt_image
Alex Klein1699fab2022-09-08 08:46:06 -060086 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060087
Alex Klein1699fab2022-09-08 08:46:06 -060088 # Ensure that miniOS payloads are only requested for compatible image types.
89 if input_proto.minios and (src_name, tgt_name) not in _VALID_MINIOS_PAIRS:
90 cros_build_lib.Die(
Alex Kleindf8ee502022-10-18 09:48:15 -060091 "%s and %s are not valid image pairs for miniOS",
92 src_image,
93 tgt_image,
Alex Klein1699fab2022-09-08 08:46:06 -060094 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060095
Alex Klein1699fab2022-09-08 08:46:06 -060096 # Find the value of bucket or default to 'chromeos-releases'.
97 destination_bucket = input_proto.bucket or "chromeos-releases"
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060098
Alex Klein1699fab2022-09-08 08:46:06 -060099 # There's a potential that some paygen_lib library might raise here, but since
100 # we're still involved in config we'll keep it before the validate_only.
101 payload_config = payload.PayloadConfig(
102 tgt_image,
103 src_image,
104 destination_bucket,
105 input_proto.minios,
106 input_proto.verify,
107 upload=not input_proto.dryrun,
108 cache_dir=_DEFAULT_PAYGEN_CACHE_DIR,
109 )
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 # If configured for validation only we're done here.
112 if config.validate_only:
113 return controller.RETURN_CODE_VALID_INPUT
114
115 # Do payload generation.
116 local_path, remote_uri = "", ""
117 try:
118 local_path, remote_uri = payload_config.GeneratePayload()
119 except paygen_payload_lib.PayloadGenerationSkippedException as e:
120 # If paygen was skipped, provide a reason if possible.
121 if isinstance(e, paygen_payload_lib.NoMiniOSPartitionException):
122 reason = payload_pb2.GenerationResponse.NOT_MINIOS_COMPATIBLE
123 output_proto.failure_reason = reason
124
125 _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri)
126 if remote_uri or input_proto.dryrun and local_path:
127 return controller.RETURN_CODE_SUCCESS
128 elif output_proto.failure_reason:
129 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
130 else:
131 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600132
133
Alex Klein1699fab2022-09-08 08:46:06 -0600134def _SetGeneratePayloadOutputProto(
135 output_proto: payload_pb2.GenerationResponse,
136 local_path: str,
137 remote_uri: str,
138):
139 """Set the output proto with the results from the service class.
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700140
Alex Klein1699fab2022-09-08 08:46:06 -0600141 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600142 output_proto: The output proto.
143 local_path: set output_proto with the local path, or ''.
144 remote_uri: set output_proto with the remote uri, or ''.
Alex Klein1699fab2022-09-08 08:46:06 -0600145 """
Alex Klein1699fab2022-09-08 08:46:06 -0600146 output_proto.local_path = local_path or ""
147 output_proto.remote_uri = remote_uri or ""