blob: 00d5cf19c65a765c557746509561c2abd7d86cce [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
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06007from chromite.api import controller
8from chromite.lib import cros_build_lib
9from chromite.api import faux
10from chromite.api import validate
11from chromite.service import payload
12
13
14_VALID_IMAGE_PAIRS = (('src_signed_image', 'tgt_signed_image'),
15 ('src_unsigned_image', 'tgt_unsigned_image'),
George Engelbrecht7bea3742020-11-17 20:21:11 -070016 ('src_dlc_image', 'tgt_dlc_image'),
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060017 ('full_update', 'tgt_unsigned_image'),
George Engelbrecht7bea3742020-11-17 20:21:11 -070018 ('full_update', 'tgt_signed_image'),
19 ('full_update', 'tgt_dlc_image'))
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060020
George Engelbrechta42172f2020-11-04 12:42:04 -070021_DEFAULT_PAYGEN_CACHE_DIR = '.paygen_cache'
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060022
23# We have more fields we might validate however, they're either
24# 'oneof' or allowed to be the empty value by design. If @validate
25# gets more complex in the future we can add more here.
Michael Mortensen85d38402019-12-12 09:50:29 -070026@faux.empty_success
27@faux.empty_completed_unsuccessfully_error
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060028@validate.require('bucket')
29def GeneratePayload(input_proto, output_proto, config):
30 """Generate a update payload ('do paygen').
George Engelbrechtd2348bb2019-11-25 21:18:14 -070031
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060032 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -070033 input_proto (PayloadGenerationRequest): Input proto.
34 output_proto (PayloadGenerationResult): Output proto.
35 config (api.config.ApiConfig): The API call config.
36
37 Returns:
38 A controller return code (e.g. controller.RETURN_CODE_SUCCESS).
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060039 """
40
41 # Resolve the tgt image oneof.
42 tgt_name = input_proto.WhichOneof('tgt_image_oneof')
43 try:
44 tgt_image = getattr(input_proto, tgt_name)
45 except AttributeError:
46 cros_build_lib.Die('%s is not a known tgt image type' % (tgt_name,))
47
48 # Resolve the src image oneof.
49 src_name = input_proto.WhichOneof('src_image_oneof')
50
51 # If the source image is 'full_update' we lack a source entirely.
52 if src_name == 'full_update':
53 src_image = None
54 # Otherwise we have an image.
55 else:
56 try:
57 src_image = getattr(input_proto, src_name)
58 except AttributeError:
59 cros_build_lib.Die('%s is not a known src image type' % (src_name,))
60
61 # Ensure they are compatible oneofs.
62 if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS:
63 cros_build_lib.Die('%s and %s are not valid image pairs' %
64 (src_image, tgt_image))
65
66 # Find the value of bucket or default to 'chromeos-releases'.
67 destination_bucket = input_proto.bucket or 'chromeos-releases'
68
Navil Perezf5482be2020-04-09 23:18:14 -060069 if input_proto.dryrun:
70 keyset = ''
71 upload = False
72 else:
73 keyset = input_proto.keyset
74 upload = True
75
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060076 # There's a potential that some paygen_lib library might raise here, but since
77 # we're still involved in config we'll keep it before the validate_only.
Navil Perezf5482be2020-04-09 23:18:14 -060078 payload_config = payload.PayloadConfig(tgt_image, src_image,
79 destination_bucket, input_proto.verify,
George Engelbrechta42172f2020-11-04 12:42:04 -070080 keyset, upload,
81 cache_dir=_DEFAULT_PAYGEN_CACHE_DIR)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060082
83 # If configured for validation only we're done here.
84 if config.validate_only:
85 return controller.RETURN_CODE_VALID_INPUT
86
87 # Do payload generation.
George Engelbrecht52d0dba2020-11-12 11:49:02 -070088 local_path, remote_uri = payload_config.GeneratePayload()
89 _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060090
George Engelbrecht52d0dba2020-11-12 11:49:02 -070091 if remote_uri or not upload and local_path:
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060092 return controller.RETURN_CODE_SUCCESS
93 else:
94 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
95
96
George Engelbrecht52d0dba2020-11-12 11:49:02 -070097def _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri):
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060098 """Set the output proto with the results from the service class.
George Engelbrechtd2348bb2019-11-25 21:18:14 -070099
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600100 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700101 output_proto (PayloadGenerationResult_pb2): The output proto.
George Engelbrechtdbc96742020-11-13 15:59:48 -0700102 local_path (str): set output_proto with the local path, or ''.
103 remote_uri (str): set output_proto with the remote uri, or ''.
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600104 """
George Engelbrecht52d0dba2020-11-12 11:49:02 -0700105 output_proto.success = True
George Engelbrechtdbc96742020-11-13 15:59:48 -0700106 output_proto.local_path = local_path or ''
107 output_proto.remote_uri = remote_uri or ''