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