blob: 019531843b4cf1c01471efc0503ef7a251272d56 [file] [log] [blame]
George Engelbrechtfe63c8c2019-08-31 22:51:29 -06001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Payload API Service."""
7
8from __future__ import print_function
9
10from chromite.api import controller
11from chromite.lib import cros_build_lib
12from chromite.api import faux
13from chromite.api import validate
14from chromite.service import payload
15
16
17_VALID_IMAGE_PAIRS = (('src_signed_image', 'tgt_signed_image'),
18 ('src_unsigned_image', 'tgt_unsigned_image'),
George Engelbrecht7bea3742020-11-17 20:21:11 -070019 ('src_dlc_image', 'tgt_dlc_image'),
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060020 ('full_update', 'tgt_unsigned_image'),
George Engelbrecht7bea3742020-11-17 20:21:11 -070021 ('full_update', 'tgt_signed_image'),
22 ('full_update', 'tgt_dlc_image'))
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060023
George Engelbrechta42172f2020-11-04 12:42:04 -070024_DEFAULT_PAYGEN_CACHE_DIR = '.paygen_cache'
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060025
26# We have more fields we might validate however, they're either
27# 'oneof' or allowed to be the empty value by design. If @validate
28# gets more complex in the future we can add more here.
Michael Mortensen85d38402019-12-12 09:50:29 -070029@faux.empty_success
30@faux.empty_completed_unsuccessfully_error
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060031@validate.require('bucket')
32def GeneratePayload(input_proto, output_proto, config):
33 """Generate a update payload ('do paygen').
George Engelbrechtd2348bb2019-11-25 21:18:14 -070034
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060035 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -070036 input_proto (PayloadGenerationRequest): Input proto.
37 output_proto (PayloadGenerationResult): Output proto.
38 config (api.config.ApiConfig): The API call config.
39
40 Returns:
41 A controller return code (e.g. controller.RETURN_CODE_SUCCESS).
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060042 """
43
44 # Resolve the tgt image oneof.
45 tgt_name = input_proto.WhichOneof('tgt_image_oneof')
46 try:
47 tgt_image = getattr(input_proto, tgt_name)
48 except AttributeError:
49 cros_build_lib.Die('%s is not a known tgt image type' % (tgt_name,))
50
51 # Resolve the src image oneof.
52 src_name = input_proto.WhichOneof('src_image_oneof')
53
54 # If the source image is 'full_update' we lack a source entirely.
55 if src_name == 'full_update':
56 src_image = None
57 # Otherwise we have an image.
58 else:
59 try:
60 src_image = getattr(input_proto, src_name)
61 except AttributeError:
62 cros_build_lib.Die('%s is not a known src image type' % (src_name,))
63
64 # Ensure they are compatible oneofs.
65 if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS:
66 cros_build_lib.Die('%s and %s are not valid image pairs' %
67 (src_image, tgt_image))
68
69 # Find the value of bucket or default to 'chromeos-releases'.
70 destination_bucket = input_proto.bucket or 'chromeos-releases'
71
Navil Perezf5482be2020-04-09 23:18:14 -060072 if input_proto.dryrun:
73 keyset = ''
74 upload = False
75 else:
76 keyset = input_proto.keyset
77 upload = True
78
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060079 # There's a potential that some paygen_lib library might raise here, but since
80 # we're still involved in config we'll keep it before the validate_only.
Navil Perezf5482be2020-04-09 23:18:14 -060081 payload_config = payload.PayloadConfig(tgt_image, src_image,
82 destination_bucket, input_proto.verify,
George Engelbrechta42172f2020-11-04 12:42:04 -070083 keyset, upload,
84 cache_dir=_DEFAULT_PAYGEN_CACHE_DIR)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060085
86 # If configured for validation only we're done here.
87 if config.validate_only:
88 return controller.RETURN_CODE_VALID_INPUT
89
90 # Do payload generation.
George Engelbrecht52d0dba2020-11-12 11:49:02 -070091 local_path, remote_uri = payload_config.GeneratePayload()
92 _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060093
George Engelbrecht52d0dba2020-11-12 11:49:02 -070094 if remote_uri or not upload and local_path:
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060095 return controller.RETURN_CODE_SUCCESS
96 else:
97 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
98
99
George Engelbrecht52d0dba2020-11-12 11:49:02 -0700100def _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri):
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600101 """Set the output proto with the results from the service class.
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700102
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600103 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700104 output_proto (PayloadGenerationResult_pb2): The output proto.
George Engelbrechtdbc96742020-11-13 15:59:48 -0700105 local_path (str): set output_proto with the local path, or ''.
106 remote_uri (str): set output_proto with the remote uri, or ''.
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600107 """
George Engelbrecht52d0dba2020-11-12 11:49:02 -0700108 output_proto.success = True
George Engelbrechtdbc96742020-11-13 15:59:48 -0700109 output_proto.local_path = local_path or ''
110 output_proto.remote_uri = remote_uri or ''