blob: 64978838eb3af4b98fbde3c929f282d3767ab7d0 [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'),
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')
28def GeneratePayload(input_proto, output_proto, config):
29 """Generate a update payload ('do paygen').
George Engelbrechtd2348bb2019-11-25 21:18:14 -070030
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060031 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -070032 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 Engelbrechtfe63c8c2019-08-31 22:51:29 -060038 """
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
91def _SetGeneratePayloadOutputProto(output_proto, generate_payload_ok):
92 """Set the output proto with the results from the service class.
George Engelbrechtd2348bb2019-11-25 21:18:14 -070093
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060094 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -070095 output_proto (PayloadGenerationResult_pb2): The output proto.
96 generate_payload_ok (bool): value to set output_proto.success.
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060097 """
98 output_proto.success = generate_payload_ok