George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | from chromite.api import controller |
| 11 | from chromite.lib import cros_build_lib |
| 12 | from chromite.api import faux |
| 13 | from chromite.api import validate |
| 14 | from chromite.service import payload |
| 15 | |
| 16 | |
| 17 | _VALID_IMAGE_PAIRS = (('src_signed_image', 'tgt_signed_image'), |
| 18 | ('src_unsigned_image', 'tgt_unsigned_image'), |
| 19 | ('full_update', 'tgt_unsigned_image'), |
| 20 | ('full_update', 'tgt_signed_image')) |
| 21 | |
George Engelbrecht | a42172f | 2020-11-04 12:42:04 -0700 | [diff] [blame] | 22 | _DEFAULT_PAYGEN_CACHE_DIR = '.paygen_cache' |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 23 | |
| 24 | # We have more fields we might validate however, they're either |
| 25 | # 'oneof' or allowed to be the empty value by design. If @validate |
| 26 | # gets more complex in the future we can add more here. |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 27 | @faux.empty_success |
| 28 | @faux.empty_completed_unsuccessfully_error |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 29 | @validate.require('bucket') |
| 30 | def GeneratePayload(input_proto, output_proto, config): |
| 31 | """Generate a update payload ('do paygen'). |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 32 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 33 | Args: |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 34 | input_proto (PayloadGenerationRequest): Input proto. |
| 35 | output_proto (PayloadGenerationResult): Output proto. |
| 36 | config (api.config.ApiConfig): The API call config. |
| 37 | |
| 38 | Returns: |
| 39 | A controller return code (e.g. controller.RETURN_CODE_SUCCESS). |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 40 | """ |
| 41 | |
| 42 | # Resolve the tgt image oneof. |
| 43 | tgt_name = input_proto.WhichOneof('tgt_image_oneof') |
| 44 | try: |
| 45 | tgt_image = getattr(input_proto, tgt_name) |
| 46 | except AttributeError: |
| 47 | cros_build_lib.Die('%s is not a known tgt image type' % (tgt_name,)) |
| 48 | |
| 49 | # Resolve the src image oneof. |
| 50 | src_name = input_proto.WhichOneof('src_image_oneof') |
| 51 | |
| 52 | # If the source image is 'full_update' we lack a source entirely. |
| 53 | if src_name == 'full_update': |
| 54 | src_image = None |
| 55 | # Otherwise we have an image. |
| 56 | else: |
| 57 | try: |
| 58 | src_image = getattr(input_proto, src_name) |
| 59 | except AttributeError: |
| 60 | cros_build_lib.Die('%s is not a known src image type' % (src_name,)) |
| 61 | |
| 62 | # Ensure they are compatible oneofs. |
| 63 | if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS: |
| 64 | cros_build_lib.Die('%s and %s are not valid image pairs' % |
| 65 | (src_image, tgt_image)) |
| 66 | |
| 67 | # Find the value of bucket or default to 'chromeos-releases'. |
| 68 | destination_bucket = input_proto.bucket or 'chromeos-releases' |
| 69 | |
Navil Perez | f5482be | 2020-04-09 23:18:14 -0600 | [diff] [blame] | 70 | if input_proto.dryrun: |
| 71 | keyset = '' |
| 72 | upload = False |
| 73 | else: |
| 74 | keyset = input_proto.keyset |
| 75 | upload = True |
| 76 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 77 | # There's a potential that some paygen_lib library might raise here, but since |
| 78 | # we're still involved in config we'll keep it before the validate_only. |
Navil Perez | f5482be | 2020-04-09 23:18:14 -0600 | [diff] [blame] | 79 | payload_config = payload.PayloadConfig(tgt_image, src_image, |
| 80 | destination_bucket, input_proto.verify, |
George Engelbrecht | a42172f | 2020-11-04 12:42:04 -0700 | [diff] [blame] | 81 | keyset, upload, |
| 82 | cache_dir=_DEFAULT_PAYGEN_CACHE_DIR) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 83 | |
| 84 | # If configured for validation only we're done here. |
| 85 | if config.validate_only: |
| 86 | return controller.RETURN_CODE_VALID_INPUT |
| 87 | |
| 88 | # Do payload generation. |
George Engelbrecht | 52d0dba | 2020-11-12 11:49:02 -0700 | [diff] [blame^] | 89 | local_path, remote_uri = payload_config.GeneratePayload() |
| 90 | _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri) |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 91 | |
George Engelbrecht | 52d0dba | 2020-11-12 11:49:02 -0700 | [diff] [blame^] | 92 | if remote_uri or not upload and local_path: |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 93 | return controller.RETURN_CODE_SUCCESS |
| 94 | else: |
| 95 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 96 | |
| 97 | |
George Engelbrecht | 52d0dba | 2020-11-12 11:49:02 -0700 | [diff] [blame^] | 98 | def _SetGeneratePayloadOutputProto(output_proto, local_path, remote_uri): |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 99 | """Set the output proto with the results from the service class. |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 100 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 101 | Args: |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame] | 102 | output_proto (PayloadGenerationResult_pb2): The output proto. |
George Engelbrecht | 52d0dba | 2020-11-12 11:49:02 -0700 | [diff] [blame^] | 103 | local_path (str): set output_proto with the local path, or None. |
| 104 | remote_uri (str): set output_proto with the remote uri, or None. |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 105 | """ |
George Engelbrecht | 52d0dba | 2020-11-12 11:49:02 -0700 | [diff] [blame^] | 106 | output_proto.success = True |
| 107 | output_proto.local_path = local_path |
| 108 | output_proto.remote_uri = remote_uri |