1#!/bin/sh 2 3# This script will build a Docker image for googleapis and recompile 4# PHP and Ruby runtimes binaries to use in GAPIC generators for these 5# languages. 6# 7# Historically, PHP and Ruby keep their compiled runtimes in the generators' 8# repositories: 9# https://github.com/googleapis/gapic-generator-php/tree/main/rules_php_gapic/resources 10# https://github.com/googleapis/gapic-generator-ruby/tree/main/rules_ruby_gapic/prebuilt 11# They are needed to prevent rebuilding of PHP and Ruby runtime (each taking 12# several minutes) every time the build is executed from a clean workspace. 13# 14# Sometimes these binaries need to be updated: they are dynamically linked, 15# and may stop working if some dependency changes in an incompatible way. 16# Use this script to update both the googleapis Docker image, and those 17# binaries. 18# 19# Run from any local working directory where you have write access. 20# 21# Usage: 22# $ mkdir workdir 23# $ cd workdir 24# $ sh /path/to/.kokoro/docker_update.sh 25# 26# After the script completes, it should print out commands to push the new 27# Docker image and to create pull requests against Ruby and PHP generators. 28# Whenever the image is published, tag it in Google Cloud Console, 29# then release new versions of the generators and update the image tag in 30# start.sh scripts in Kokoro folders in all googleapis workspaces. 31 32set -e 33 34PWD="`pwd`" 35SHARED="$PWD/volume" 36SCRIPT=$0 37DIRNAME=`dirname $SCRIPT` 38 39if test -d "$SHARED"; then 40 echo "The working directory $SHARED already exists, please delete it first." 41 exit 1 42fi 43mkdir -p "$SHARED" 44 45# 1. Build the latest Docker image using the Dockerfile from google3 46 47echo "Using Dockerfile from $DIRNAME" 48cat "$DIRNAME/Dockerfile" > "$PWD/Dockerfile" 49 50echo "Building googleapis Docker image..." 51docker build -t googleapis . 52docker tag googleapis gcr.io/gapic-images/googleapis 53echo "Done." 54 55# 2. Clone Ruby and PHP generators 56cd "$SHARED" 57echo "Cloning Ruby generator..." 58git clone --depth 1 git@github.com:googleapis/gapic-generator-ruby.git 59echo "Done." 60echo "Cloning PHP generator..." 61git clone --depth 1 git@github.com:googleapis/gapic-generator-php.git 62echo "Done." 63 64# 3. Generate a script that would run Bazel to build both generators 65cat > build.sh <<EOD 66#!/bin/sh 67set -e 68export USER="$USER" 69export HOME=/volume 70cd /volume 71cd gapic-generator-ruby 72bazel build //rules_ruby_gapic/gapic-generator:gapic_generator_bundled_context 73cd /volume 74cd gapic-generator-php 75bazel build //rules_php_gapic:php_gapic_generator_binary 76cd .. 77EOD 78chmod +x build.sh 79 80# 4. Execute the script inside Docker image 81echo "Building generators inside Docker..." 82# Note: without --privileged, the container has problems accessing the filesystem. 83# We don't care much about it at this moment. Discussed here: 84# https://forums.docker.com/t/what-capabilities-are-required-for-ls/92223 85DOCKER_COMMAND="docker run --privileged --user=$UID --workdir=/volume -i --rm -v $SHARED:/volume" 86$DOCKER_COMMAND --entrypoint /volume/build.sh googleapis 87echo "Done." 88 89# Fix permissions of the mounted folder 90chmod -R u+w "$SHARED" 91 92# 5. Pack the resulting Ruby and PHP binaries 93RUBY_DIRECTORY=`ls -d .cache/bazel/*/*/external/gapic_generator_ruby_runtime` 94RUBY_VERSION=`echo 'puts RUBY_VERSION' | $DOCKER_COMMAND --entrypoint "" googleapis /volume/$RUBY_DIRECTORY/bin/ruby` 95 96echo "Ruby version: $RUBY_VERSION, packing..." 97RUBY_ARCHIVE_DIR="ruby-$RUBY_VERSION" 98RUBY_TARBALL_FILENAME="ruby-${RUBY_VERSION}_glinux_x86_64.tar.gz" 99mkdir -p "$RUBY_ARCHIVE_DIR" 100cp -r "$RUBY_DIRECTORY"/bin "$RUBY_DIRECTORY"/include "$RUBY_DIRECTORY"/lib "$RUBY_ARCHIVE_DIR" 101tar cfz "$RUBY_TARBALL_FILENAME" "$RUBY_ARCHIVE_DIR" 102echo "Done: $RUBY_TARBALL_FILENAME" 103 104PHP_DIRECTORY=`ls -d .cache/bazel/*/*/external/php_micro` 105PHP_VERSION=`echo '<? echo phpversion(); ?>' | $DOCKER_COMMAND --entrypoint "" googleapis /volume/$PHP_DIRECTORY/bin/php` 106 107echo "PHP version: $PHP_VERSION, packing..." 108PHP_ARCHIVE_DIR="php-$PHP_VERSION" 109PHP_TARBALL_FILENAME="php-${PHP_VERSION}_linux_x86_64.tar.gz" 110mkdir -p "$PHP_ARCHIVE_DIR" 111cp -r "$PHP_DIRECTORY"/bin "$PHP_DIRECTORY"/include "$PHP_DIRECTORY"/lib "$PHP_ARCHIVE_DIR" 112tar cfz "$PHP_TARBALL_FILENAME" "$PHP_ARCHIVE_DIR" 113echo "Done: $PHP_TARBALL_FILENAME" 114 115# 6. Commit the tarballs 116BRANCH="update-binary-`date +%Y%m%dT%H%M%S`" 117 118RUBY_TARBALL_LOCATION=rules_ruby_gapic/prebuilt 119cd "$SHARED/gapic-generator-ruby" 120git checkout -b "$BRANCH" 121git rm -f "$RUBY_TARBALL_LOCATION"/*.tar.gz 122cp "$SHARED/$RUBY_TARBALL_FILENAME" "$RUBY_TARBALL_LOCATION/" 123git add "$RUBY_TARBALL_LOCATION/$RUBY_TARBALL_FILENAME" 124git commit -m "fix: update Ruby prebuilt binary, version $RUBY_VERSION" 125echo "Pushing Ruby branch to GitHub..." 126git push --set-upstream origin "$BRANCH" 127echo "Done" 128 129PHP_TARBALL_LOCATION=rules_php_gapic/resources 130cd "$SHARED/gapic-generator-php" 131git checkout -b "$BRANCH" 132git rm -f "$PHP_TARBALL_LOCATION"/*.tar.gz 133cp "$SHARED/$PHP_TARBALL_FILENAME" "$PHP_TARBALL_LOCATION/" 134git add "$PHP_TARBALL_LOCATION/$PHP_TARBALL_FILENAME" 135git commit -m "fix: update PHP prebuilt binary, version $PHP_VERSION" 136echo "Pushing PHP branch to GitHub..." 137git push --set-upstream origin "$BRANCH" 138echo "Done" 139 140echo 141echo "Please create pull requests:" 142echo " https://github.com/googleapis/gapic-generator-ruby/pull/new/$BRANCH" 143echo " https://github.com/googleapis/gapic-generator-php/pull/new/$BRANCH" 144echo "and push this Docker image to gcr.io:" 145echo " docker push gcr.io/gapic-images/googleapis" 146