aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/build/functions/base_images.py
blob: 20fffbe394a7a49946e4c36ac2169d62c94a9abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
"""Cloud function to build base images on Google Cloud Builder."""

import logging

import google.auth
from googleapiclient.discovery import build

import build_base_images
import build_msan_libs

BASE_PROJECT = 'oss-fuzz-base'


# pylint: disable=no-member
def run_build(steps, images):
  """Execute the retrieved build steps in gcp."""
  credentials, _ = google.auth.default()
  build_body = {
      'steps': steps,
      'timeout': str(6 * 3600) + 's',
      'options': {
          'machineType': 'N1_HIGHCPU_32'
      },
      'images': images
  }
  cloudbuild = build('cloudbuild',
                     'v1',
                     credentials=credentials,
                     cache_discovery=False)
  build_info = cloudbuild.projects().builds().create(project_id=BASE_PROJECT,
                                                     body=build_body).execute()
  build_id = build_info['metadata']['build']['id']
  logging.info('Build ID: %s', build_id)
  logging.info('Logs: %s',
               build_base_images.get_logs_url(build_id, BASE_PROJECT))


def base_builder(event, context):
  """Cloud function to build base images."""
  del event, context

  tag_prefix = f'gcr.io/{BASE_PROJECT}/'
  steps = build_base_images.get_steps(build_base_images.BASE_IMAGES, tag_prefix)
  images = [
      tag_prefix + base_image for base_image in build_base_images.BASE_IMAGES
  ]

  run_build(steps, images)


def base_msan_builder(event, context):
  """Cloud function to build base images."""
  del event, context
  image = f'gcr.io/{BASE_PROJECT}/msan-libs-builder'
  steps = build_msan_libs.get_steps(image)
  images = [
      f'gcr.io/{BASE_PROJECT}/base-sanitizer-libs-builder',
      image,
  ]

  run_build(steps, images)