blob: e273f2119a0ea1c36d0e8e8412e07d466f21e638 [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
Mike Frysingeref94e4c2020-02-10 23:59:54 -050010import sys
11
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060012from chromite.api import controller
13from chromite.lib import cros_build_lib
14from chromite.api import faux
15from chromite.api import validate
16from chromite.service import payload
17
18
Mike Frysingeref94e4c2020-02-10 23:59:54 -050019assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
20
21
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060022_VALID_IMAGE_PAIRS = (('src_signed_image', 'tgt_signed_image'),
23 ('src_unsigned_image', 'tgt_unsigned_image'),
24 ('full_update', 'tgt_unsigned_image'),
25 ('full_update', 'tgt_signed_image'))
26
27
28# We have more fields we might validate however, they're either
29# 'oneof' or allowed to be the empty value by design. If @validate
30# gets more complex in the future we can add more here.
Michael Mortensen85d38402019-12-12 09:50:29 -070031@faux.empty_success
32@faux.empty_completed_unsuccessfully_error
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060033@validate.require('bucket')
34def GeneratePayload(input_proto, output_proto, config):
35 """Generate a update payload ('do paygen').
George Engelbrechtd2348bb2019-11-25 21:18:14 -070036
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060037 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -070038 input_proto (PayloadGenerationRequest): Input proto.
39 output_proto (PayloadGenerationResult): Output proto.
40 config (api.config.ApiConfig): The API call config.
41
42 Returns:
43 A controller return code (e.g. controller.RETURN_CODE_SUCCESS).
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060044 """
45
46 # Resolve the tgt image oneof.
47 tgt_name = input_proto.WhichOneof('tgt_image_oneof')
48 try:
49 tgt_image = getattr(input_proto, tgt_name)
50 except AttributeError:
51 cros_build_lib.Die('%s is not a known tgt image type' % (tgt_name,))
52
53 # Resolve the src image oneof.
54 src_name = input_proto.WhichOneof('src_image_oneof')
55
56 # If the source image is 'full_update' we lack a source entirely.
57 if src_name == 'full_update':
58 src_image = None
59 # Otherwise we have an image.
60 else:
61 try:
62 src_image = getattr(input_proto, src_name)
63 except AttributeError:
64 cros_build_lib.Die('%s is not a known src image type' % (src_name,))
65
66 # Ensure they are compatible oneofs.
67 if (src_name, tgt_name) not in _VALID_IMAGE_PAIRS:
68 cros_build_lib.Die('%s and %s are not valid image pairs' %
69 (src_image, tgt_image))
70
71 # Find the value of bucket or default to 'chromeos-releases'.
72 destination_bucket = input_proto.bucket or 'chromeos-releases'
73
Navil Perezf5482be2020-04-09 23:18:14 -060074 if input_proto.dryrun:
75 keyset = ''
76 upload = False
77 else:
78 keyset = input_proto.keyset
79 upload = True
80
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060081 # There's a potential that some paygen_lib library might raise here, but since
82 # we're still involved in config we'll keep it before the validate_only.
Navil Perezf5482be2020-04-09 23:18:14 -060083 payload_config = payload.PayloadConfig(tgt_image, src_image,
84 destination_bucket, input_proto.verify,
85 keyset, upload)
George Engelbrechtfe63c8c2019-08-31 22:51:29 -060086
87 # If configured for validation only we're done here.
88 if config.validate_only:
89 return controller.RETURN_CODE_VALID_INPUT
90
91 # Do payload generation.
92 paygen_ok = payload_config.GeneratePayload()
93 _SetGeneratePayloadOutputProto(output_proto, paygen_ok)
94
95 if paygen_ok:
96 return controller.RETURN_CODE_SUCCESS
97 else:
98 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
99
100
101def _SetGeneratePayloadOutputProto(output_proto, generate_payload_ok):
102 """Set the output proto with the results from the service class.
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700103
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600104 Args:
George Engelbrechtd2348bb2019-11-25 21:18:14 -0700105 output_proto (PayloadGenerationResult_pb2): The output proto.
106 generate_payload_ok (bool): value to set output_proto.success.
George Engelbrechtfe63c8c2019-08-31 22:51:29 -0600107 """
108 output_proto.success = generate_payload_ok