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 | |
| 22 | |
| 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. |
| 26 | @faux.all_empty |
| 27 | @validate.require('bucket') |
| 28 | def GeneratePayload(input_proto, output_proto, config): |
| 29 | """Generate a update payload ('do paygen'). |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame^] | 30 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 31 | Args: |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame^] | 32 | input_proto (PayloadGenerationRequest): Input proto. |
| 33 | output_proto (PayloadGenerationResult): Output proto. |
| 34 | config (api.config.ApiConfig): The API call config. |
| 35 | |
| 36 | Returns: |
| 37 | A controller return code (e.g. controller.RETURN_CODE_SUCCESS). |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 38 | """ |
| 39 | |
| 40 | # Resolve the tgt image oneof. |
| 41 | tgt_name = input_proto.WhichOneof('tgt_image_oneof') |
| 42 | try: |
| 43 | tgt_image = getattr(input_proto, tgt_name) |
| 44 | except AttributeError: |
| 45 | cros_build_lib.Die('%s is not a known tgt image type' % (tgt_name,)) |
| 46 | |
| 47 | # Resolve the src image oneof. |
| 48 | src_name = input_proto.WhichOneof('src_image_oneof') |
| 49 | |
| 50 | # If the source image is 'full_update' we lack a source entirely. |
| 51 | if src_name == 'full_update': |
| 52 | src_image = None |
| 53 | # Otherwise we have an image. |
| 54 | else: |
| 55 | try: |
| 56 | src_image = getattr(input_proto, src_name) |
| 57 | except AttributeError: |
| 58 | cros_build_lib.Die('%s is not a known src image type' % (src_name,)) |
| 59 | |
| 60 | # Ensure they are compatible oneofs. |
| 61 | if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS: |
| 62 | cros_build_lib.Die('%s and %s are not valid image pairs' % |
| 63 | (src_image, tgt_image)) |
| 64 | |
| 65 | # Find the value of bucket or default to 'chromeos-releases'. |
| 66 | destination_bucket = input_proto.bucket or 'chromeos-releases' |
| 67 | |
| 68 | # There's a potential that some paygen_lib library might raise here, but since |
| 69 | # we're still involved in config we'll keep it before the validate_only. |
| 70 | payload_config = payload.PayloadConfig( |
| 71 | tgt_image, |
| 72 | src_image, |
| 73 | destination_bucket, |
| 74 | input_proto.verify, |
| 75 | input_proto.keyset) |
| 76 | |
| 77 | # If configured for validation only we're done here. |
| 78 | if config.validate_only: |
| 79 | return controller.RETURN_CODE_VALID_INPUT |
| 80 | |
| 81 | # Do payload generation. |
| 82 | paygen_ok = payload_config.GeneratePayload() |
| 83 | _SetGeneratePayloadOutputProto(output_proto, paygen_ok) |
| 84 | |
| 85 | if paygen_ok: |
| 86 | return controller.RETURN_CODE_SUCCESS |
| 87 | else: |
| 88 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 89 | |
| 90 | |
| 91 | def _SetGeneratePayloadOutputProto(output_proto, generate_payload_ok): |
| 92 | """Set the output proto with the results from the service class. |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame^] | 93 | |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 94 | Args: |
George Engelbrecht | d2348bb | 2019-11-25 21:18:14 -0700 | [diff] [blame^] | 95 | output_proto (PayloadGenerationResult_pb2): The output proto. |
| 96 | generate_payload_ok (bool): value to set output_proto.success. |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 97 | """ |
| 98 | output_proto.success = generate_payload_ok |