| Original | +Translation | +
|---|---|
| + + {{ translation['OriginalLanguage'] }} + + {{ translation['Original'] }} + | ++ + {{ translation['Language'] }} + + {{ translation['Translated'] }} + | +
+ +
diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 00000000..c0a95e72 --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,11 @@ +{ + "extends": [ + "config:base", + "group:all", + ":preserveSemverRanges", + ":disableDependencyDashboard" + ], + "ignorePaths": [ + "optional-kubernetes-engine" + ] +} diff --git a/.github/snippet-bot.yml b/.github/snippet-bot.yml new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/.github/snippet-bot.yml @@ -0,0 +1 @@ + diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml new file mode 100644 index 00000000..7d8eca8e --- /dev/null +++ b/.github/sync-repo-settings.yaml @@ -0,0 +1,40 @@ +# Whether or not rebase-merging is enabled on this repository. +# Defaults to `true` +rebaseMergeAllowed: true + +# Whether or not squash-merging is enabled on this repository. +# Defaults to `true` +squashMergeAllowed: true + +# Whether or not PRs are merged with a merge commit on this repository. +# Defaults to `false` +mergeCommitAllowed: false + +# Rules for main branch protection +branchProtectionRules: +# Identifies the protection rule pattern. Name of the branch to be protected. +# Defaults to `main` +- pattern: main + # Can admins overwrite branch protection. + # Defaults to `true` + isAdminEnforced: false + # Number of approving reviews required to update matching branches. + # Defaults to `1` + requiredApprovingReviewCount: 1 + # Are reviews from code owners required to update matching branches. + # Defaults to `false` + requiresCodeOwnerReviews: true + # Require up to date branches + requiresStrictStatusChecks: true + # List of required status check contexts that must pass for commits to be accepted to matching branches. + requiredStatusCheckContexts: + - "kokoro" + - "cla/google" +# List of explicit permissions to add (additive only) +permissionRules: + # Team slug to add to repository permissions + - team: yoshi-admins + # Access level required, one of push|pull|admin + permission: admin + - team: python-samples-reviewers + permission: admin diff --git a/.gitignore b/.gitignore index 6f2c88b4..1e32d951 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,9 @@ htmlcov/ .cache nosetests.xml coverage.xml +*_log.xml *,cover +sponge_log.xml *.log diff --git a/.kokoro/common.cfg b/.kokoro/common.cfg new file mode 100644 index 00000000..f58e4f76 --- /dev/null +++ b/.kokoro/common.cfg @@ -0,0 +1,28 @@ +# Format: //devtools/kokoro/config/proto/build.proto + +# Download trampoline resources. These will be in ${KOKORO_GFILE_DIR} +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" + +# Download secrets from Cloud Storage. +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/getting-started-python" + +# All builds use the trampoline script to run in docker. +build_file: "getting-started-python/.kokoro/trampoline_v2.sh" + +# Use the Python worker docker iamge. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/python/getting-started-python" +} + +# Tell the trampoline which build file to use. +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: ".kokoro/system_tests.sh" +} + +# Upload the docker image after successful builds. +env_vars: { + key: "TRAMPOLINE_IMAGE_UPLOAD" + value: "true" +} diff --git a/.kokoro/docker/Dockerfile b/.kokoro/docker/Dockerfile new file mode 100644 index 00000000..accdd0bf --- /dev/null +++ b/.kokoro/docker/Dockerfile @@ -0,0 +1,57 @@ +# Copyright 2018 Google LLC +# +# 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. + +FROM gcr.io/cloud-devrel-kokoro-resources/python-base:latest + +# Install libraries needed by third-party python packages that we depend on. +RUN apt-get update \ + && apt-get install -y \ + graphviz \ + libcurl4-openssl-dev \ + libffi-dev \ + libjpeg-dev \ + libmagickwand-dev \ + libmemcached-dev \ + libmysqlclient-dev \ + libpng-dev \ + libpq-dev \ + libssl-dev \ + libxml2-dev \ + libxslt1-dev \ + openssl \ + zlib1g-dev \ + && apt-get clean + + +###################### Check python version + +RUN python3 --version +RUN which python3 + +# Setup Cloud SDK +ENV CLOUD_SDK_VERSION 489.0.0 +# Use system python for cloud sdk. +ENV CLOUDSDK_PYTHON python3.12 +RUN wget https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-$CLOUD_SDK_VERSION-linux-x86_64.tar.gz +RUN tar xzf google-cloud-sdk-$CLOUD_SDK_VERSION-linux-x86_64.tar.gz +RUN /google-cloud-sdk/install.sh +ENV PATH /google-cloud-sdk/bin:$PATH + +# Setup the user profile for pip +ENV PATH ~/.local/bin:/root/.local/bin:$PATH + +# Install the current version of nox. +RUN python3 -m pip install --user --no-cache-dir nox==2022.1.7 + +CMD ["nox"] diff --git a/.kokoro/presubmit.cfg b/.kokoro/presubmit.cfg new file mode 100644 index 00000000..e69de29b diff --git a/.kokoro/system_tests.cfg b/.kokoro/system_tests.cfg new file mode 100644 index 00000000..e69de29b diff --git a/.kokoro/system_tests.sh b/.kokoro/system_tests.sh new file mode 100755 index 00000000..29bb4ae5 --- /dev/null +++ b/.kokoro/system_tests.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# Copyright 2017 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. + +set -eo pipefail + +export PATH=${PATH}:${HOME}/gcloud/google-cloud-sdk/bin + +cd "${PROJECT_ROOT:-github/getting-started-python}" + + +# Unencrypt and extract secrets +SECRETS_PASSWORD=$(cat "${KOKORO_GFILE_DIR}/secrets-password.txt") +./decrypt-secrets.sh "${SECRETS_PASSWORD}" + +# Setup environment variables +export GOOGLE_APPLICATION_CREDENTIALS="$(pwd)/service-account.json" + +# This block is executed only with Trampoline V2. +if [[ -n "${TRAMPOLINE_VERSION:-}" ]]; then + # Install nox as a user and add it to the PATH. + python3 -m pip install --user nox + export PATH="${PATH}:${HOME}/.local/bin" +fi + +# Run tests +nox -s lint +nox -s run_tests + +# If this is a nightly build, send the test log to the Flaky Bot. +# See https://github.com/googleapis/repo-automation-bots/tree/HEAD/packages/flakybot. +if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"system_tests"* ]]; then + chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot + $KOKORO_GFILE_DIR/linux_amd64/flakybot +fi diff --git a/.kokoro/trampoline.sh b/.kokoro/trampoline.sh new file mode 100755 index 00000000..fef7c24d --- /dev/null +++ b/.kokoro/trampoline.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# Copyright 2017 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. + +set -eo pipefail + +# Always run the cleanup script, regardless of the success of bouncing into +# the container. + +function cleanup() { + chmod +x ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh + ${KOKORO_GFILE_DIR}/trampoline_cleanup.sh + echo "cleanup"; +} +trap cleanup EXIT + +python3 "${KOKORO_GFILE_DIR}/trampoline_v1.py" diff --git a/.kokoro/trampoline_v2.sh b/.kokoro/trampoline_v2.sh new file mode 100755 index 00000000..ef6972b4 --- /dev/null +++ b/.kokoro/trampoline_v2.sh @@ -0,0 +1,489 @@ +#!/usr/bin/env bash +# Copyright 2020 Google LLC +# +# 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. + +# trampoline_v2.sh +# +# If you want to make a change to this file, consider doing so at: +# https://github.com/googlecloudplatform/docker-ci-helper +# +# This script is for running CI builds. For Kokoro builds, we +# set this script to `build_file` field in the Kokoro configuration. + +# This script does 3 things. +# +# 1. Prepare the Docker image for the test +# 2. Run the Docker with appropriate flags to run the test +# 3. Upload the newly built Docker image +# +# in a way that is somewhat compatible with trampoline_v1. +# +# These environment variables are required: +# TRAMPOLINE_IMAGE: The docker image to use. +# TRAMPOLINE_DOCKERFILE: The location of the Dockerfile. +# +# You can optionally change these environment variables: +# TRAMPOLINE_IMAGE_UPLOAD: +# (true|false): Whether to upload the Docker image after the +# successful builds. +# TRAMPOLINE_BUILD_FILE: The script to run in the docker container. +# TRAMPOLINE_WORKSPACE: The workspace path in the docker container. +# Defaults to /workspace. +# Potentially there are some repo specific envvars in .trampolinerc in +# the project root. +# +# Here is an example for running this script. +# TRAMPOLINE_IMAGE=gcr.io/cloud-devrel-kokoro-resources/node:10-user \ +# TRAMPOLINE_BUILD_FILE=.kokoro/system-test.sh \ +# .kokoro/trampoline_v2.sh + +set -euo pipefail + +TRAMPOLINE_VERSION="2.0.10" + +if command -v tput >/dev/null && [[ -n "${TERM:-}" ]]; then + readonly IO_COLOR_RED="$(tput setaf 1)" + readonly IO_COLOR_GREEN="$(tput setaf 2)" + readonly IO_COLOR_YELLOW="$(tput setaf 3)" + readonly IO_COLOR_RESET="$(tput sgr0)" +else + readonly IO_COLOR_RED="" + readonly IO_COLOR_GREEN="" + readonly IO_COLOR_YELLOW="" + readonly IO_COLOR_RESET="" +fi + +function function_exists { + [ $(LC_ALL=C type -t $1)"" == "function" ] +} + +# Logs a message using the given color. The first argument must be one +# of the IO_COLOR_* variables defined above, such as +# "${IO_COLOR_YELLOW}". The remaining arguments will be logged in the +# given color. The log message will also have an RFC-3339 timestamp +# prepended (in UTC). You can disable the color output by setting +# TERM=vt100. +function log_impl() { + local color="$1" + shift + local timestamp="$(date -u "+%Y-%m-%dT%H:%M:%SZ")" + echo "================================================================" + echo "${color}${timestamp}:" "$@" "${IO_COLOR_RESET}" + echo "================================================================" +} + +# Logs the given message with normal coloring and a timestamp. +function log() { + log_impl "${IO_COLOR_RESET}" "$@" +} + +# Logs the given message in green with a timestamp. +function log_green() { + log_impl "${IO_COLOR_GREEN}" "$@" +} + +# Logs the given message in yellow with a timestamp. +function log_yellow() { + log_impl "${IO_COLOR_YELLOW}" "$@" +} + +# Logs the given message in red with a timestamp. +function log_red() { + log_impl "${IO_COLOR_RED}" "$@" +} + +readonly tmpdir=$(mktemp -d -t ci-XXXXXXXX) +readonly tmphome="${tmpdir}/h" +mkdir -p "${tmphome}" + +function cleanup() { + rm -rf "${tmpdir}" +} +trap cleanup EXIT + +RUNNING_IN_CI="${RUNNING_IN_CI:-false}" + +# The workspace in the container, defaults to /workspace. +TRAMPOLINE_WORKSPACE="${TRAMPOLINE_WORKSPACE:-/workspace}" + +pass_down_envvars=( + # TRAMPOLINE_V2 variables. + # Tells scripts whether they are running as part of CI or not. + "RUNNING_IN_CI" + # Indicates which CI system we're in. + "TRAMPOLINE_CI" + # Indicates the version of the script. + "TRAMPOLINE_VERSION" +) + +log_yellow "Building with Trampoline ${TRAMPOLINE_VERSION}" + +# Detect which CI systems we're in. If we're in any of the CI systems +# we support, `RUNNING_IN_CI` will be true and `TRAMPOLINE_CI` will be +# the name of the CI system. Both envvars will be passing down to the +# container for telling which CI system we're in. +if [[ -n "${KOKORO_BUILD_ID:-}" ]]; then + # descriptive env var for indicating it's on CI. + RUNNING_IN_CI="true" + TRAMPOLINE_CI="kokoro" + if [[ "${TRAMPOLINE_USE_LEGACY_SERVICE_ACCOUNT:-}" == "true" ]]; then + if [[ ! -f "${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json" ]]; then + log_red "${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json does not exist. Did you forget to mount cloud-devrel-kokoro-resources/trampoline? Aborting." + exit 1 + fi + # This service account will be activated later. + TRAMPOLINE_SERVICE_ACCOUNT="${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json" + else + if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then + gcloud auth list + fi + log_yellow "Configuring Container Registry access" + gcloud auth configure-docker --quiet + fi + pass_down_envvars+=( + # KOKORO dynamic variables. + "KOKORO_BUILD_NUMBER" + "KOKORO_BUILD_ID" + "KOKORO_JOB_NAME" + "KOKORO_GIT_COMMIT" + "KOKORO_GITHUB_COMMIT" + "KOKORO_GITHUB_PULL_REQUEST_NUMBER" + "KOKORO_GITHUB_PULL_REQUEST_COMMIT" + # For Flaky Bot + "KOKORO_GITHUB_COMMIT_URL" + "KOKORO_GITHUB_PULL_REQUEST_URL" + "KOKORO_BUILD_ARTIFACTS_SUBDIR" + ) +elif [[ "${TRAVIS:-}" == "true" ]]; then + RUNNING_IN_CI="true" + TRAMPOLINE_CI="travis" + pass_down_envvars+=( + "TRAVIS_BRANCH" + "TRAVIS_BUILD_ID" + "TRAVIS_BUILD_NUMBER" + "TRAVIS_BUILD_WEB_URL" + "TRAVIS_COMMIT" + "TRAVIS_COMMIT_MESSAGE" + "TRAVIS_COMMIT_RANGE" + "TRAVIS_JOB_NAME" + "TRAVIS_JOB_NUMBER" + "TRAVIS_JOB_WEB_URL" + "TRAVIS_PULL_REQUEST" + "TRAVIS_PULL_REQUEST_BRANCH" + "TRAVIS_PULL_REQUEST_SHA" + "TRAVIS_PULL_REQUEST_SLUG" + "TRAVIS_REPO_SLUG" + "TRAVIS_SECURE_ENV_VARS" + "TRAVIS_TAG" + ) +elif [[ -n "${GITHUB_RUN_ID:-}" ]]; then + RUNNING_IN_CI="true" + TRAMPOLINE_CI="github-workflow" + pass_down_envvars+=( + "GITHUB_WORKFLOW" + "GITHUB_RUN_ID" + "GITHUB_RUN_NUMBER" + "GITHUB_ACTION" + "GITHUB_ACTIONS" + "GITHUB_ACTOR" + "GITHUB_REPOSITORY" + "GITHUB_EVENT_NAME" + "GITHUB_EVENT_PATH" + "GITHUB_SHA" + "GITHUB_REF" + "GITHUB_HEAD_REF" + "GITHUB_BASE_REF" + ) +elif [[ "${CIRCLECI:-}" == "true" ]]; then + RUNNING_IN_CI="true" + TRAMPOLINE_CI="circleci" + pass_down_envvars+=( + "CIRCLE_BRANCH" + "CIRCLE_BUILD_NUM" + "CIRCLE_BUILD_URL" + "CIRCLE_COMPARE_URL" + "CIRCLE_JOB" + "CIRCLE_NODE_INDEX" + "CIRCLE_NODE_TOTAL" + "CIRCLE_PREVIOUS_BUILD_NUM" + "CIRCLE_PROJECT_REPONAME" + "CIRCLE_PROJECT_USERNAME" + "CIRCLE_REPOSITORY_URL" + "CIRCLE_SHA1" + "CIRCLE_STAGE" + "CIRCLE_USERNAME" + "CIRCLE_WORKFLOW_ID" + "CIRCLE_WORKFLOW_JOB_ID" + "CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS" + "CIRCLE_WORKFLOW_WORKSPACE_ID" + ) +fi + +# Configure the service account for pulling the docker image. +function repo_root() { + local dir="$1" + while [[ ! -d "${dir}/.git" ]]; do + dir="$(dirname "$dir")" + done + echo "${dir}" +} + +# Detect the project root. In CI builds, we assume the script is in +# the git tree and traverse from there, otherwise, traverse from `pwd` +# to find `.git` directory. +if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then + PROGRAM_PATH="$(realpath "$0")" + PROGRAM_DIR="$(dirname "${PROGRAM_PATH}")" + PROJECT_ROOT="$(repo_root "${PROGRAM_DIR}")" +else + PROJECT_ROOT="$(repo_root $(pwd))" +fi + +log_yellow "Changing to the project root: ${PROJECT_ROOT}." +cd "${PROJECT_ROOT}" + +# To support relative path for `TRAMPOLINE_SERVICE_ACCOUNT`, we need +# to use this environment variable in `PROJECT_ROOT`. +if [[ -n "${TRAMPOLINE_SERVICE_ACCOUNT:-}" ]]; then + + mkdir -p "${tmpdir}/gcloud" + gcloud_config_dir="${tmpdir}/gcloud" + + log_yellow "Using isolated gcloud config: ${gcloud_config_dir}." + export CLOUDSDK_CONFIG="${gcloud_config_dir}" + + log_yellow "Using ${TRAMPOLINE_SERVICE_ACCOUNT} for authentication." + gcloud auth activate-service-account \ + --key-file "${TRAMPOLINE_SERVICE_ACCOUNT}" + log_yellow "Configuring Container Registry access" + gcloud auth configure-docker --quiet +fi + +required_envvars=( + # The basic trampoline configurations. + "TRAMPOLINE_IMAGE" + "TRAMPOLINE_BUILD_FILE" +) + +if [[ -f "${PROJECT_ROOT}/.trampolinerc" ]]; then + source "${PROJECT_ROOT}/.trampolinerc" +fi + +log_yellow "Checking environment variables." +for e in "${required_envvars[@]}" +do + if [[ -z "${!e:-}" ]]; then + log "Missing ${e} env var. Aborting." + exit 1 + fi +done + +# We want to support legacy style TRAMPOLINE_BUILD_FILE used with V1 +# script: e.g. "github/repo-name/.kokoro/run_tests.sh" +TRAMPOLINE_BUILD_FILE="${TRAMPOLINE_BUILD_FILE#github/*/}" +log_yellow "Using TRAMPOLINE_BUILD_FILE: ${TRAMPOLINE_BUILD_FILE}" + +# ignore error on docker operations and test execution +set +e + +log_yellow "Preparing Docker image." +# We only download the docker image in CI builds. +if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then + # Download the docker image specified by `TRAMPOLINE_IMAGE` + + # We may want to add --max-concurrent-downloads flag. + + log_yellow "Start pulling the Docker image: ${TRAMPOLINE_IMAGE}." + if docker pull "${TRAMPOLINE_IMAGE}"; then + log_green "Finished pulling the Docker image: ${TRAMPOLINE_IMAGE}." + has_image="true" + else + log_red "Failed pulling the Docker image: ${TRAMPOLINE_IMAGE}." + has_image="false" + fi +else + # For local run, check if we have the image. + if docker images "${TRAMPOLINE_IMAGE}" | grep "${TRAMPOLINE_IMAGE%:*}"; then + has_image="true" + else + has_image="false" + fi +fi + + +# The default user for a Docker container has uid 0 (root). To avoid +# creating root-owned files in the build directory we tell docker to +# use the current user ID. +user_uid="$(id -u)" +user_gid="$(id -g)" +user_name="$(id -un)" + +# To allow docker in docker, we add the user to the docker group in +# the host os. +docker_gid=$(cut -d: -f3 < <(getent group docker)) + +update_cache="false" +if [[ "${TRAMPOLINE_DOCKERFILE:-none}" != "none" ]]; then + # Build the Docker image from the source. + context_dir=$(dirname "${TRAMPOLINE_DOCKERFILE}") + docker_build_flags=( + "-f" "${TRAMPOLINE_DOCKERFILE}" + "-t" "${TRAMPOLINE_IMAGE}" + "--build-arg" "UID=${user_uid}" + "--build-arg" "USERNAME=${user_name}" + ) + if [[ "${has_image}" == "true" ]]; then + docker_build_flags+=("--cache-from" "${TRAMPOLINE_IMAGE}") + fi + + log_yellow "Start building the docker image." + if [[ "${TRAMPOLINE_VERBOSE:-false}" == "true" ]]; then + echo "docker build" "${docker_build_flags[@]}" "${context_dir}" + fi + + # ON CI systems, we want to suppress docker build logs, only + # output the logs when it fails. + if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then + if docker build "${docker_build_flags[@]}" "${context_dir}" \ + > "${tmpdir}/docker_build.log" 2>&1; then + if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then + cat "${tmpdir}/docker_build.log" + fi + + log_green "Finished building the docker image." + update_cache="true" + else + log_red "Failed to build the Docker image, aborting." + log_yellow "Dumping the build logs:" + cat "${tmpdir}/docker_build.log" + exit 1 + fi + else + if docker build "${docker_build_flags[@]}" "${context_dir}"; then + log_green "Finished building the docker image." + update_cache="true" + else + log_red "Failed to build the Docker image, aborting." + exit 1 + fi + fi +else + if [[ "${has_image}" != "true" ]]; then + log_red "We do not have ${TRAMPOLINE_IMAGE} locally, aborting." + exit 1 + fi +fi + +# We use an array for the flags so they are easier to document. +docker_flags=( + # Remove the container after it exists. + "--rm" + + # Use the host network. + "--network=host" + + # Run in priviledged mode. We are not using docker for sandboxing or + # isolation, just for packaging our dev tools. + "--privileged" + + # Run the docker script with the user id. Because the docker image gets to + # write in ${PWD} you typically want this to be your user id. + # To allow docker in docker, we need to use docker gid on the host. + "--user" "${user_uid}:${docker_gid}" + + # Pass down the USER. + "--env" "USER=${user_name}" + + # Mount the project directory inside the Docker container. + "--volume" "${PROJECT_ROOT}:${TRAMPOLINE_WORKSPACE}" + "--workdir" "${TRAMPOLINE_WORKSPACE}" + "--env" "PROJECT_ROOT=${TRAMPOLINE_WORKSPACE}" + + # Mount the temporary home directory. + "--volume" "${tmphome}:/h" + "--env" "HOME=/h" + + # Allow docker in docker. + "--volume" "/var/run/docker.sock:/var/run/docker.sock" + + # Mount the /tmp so that docker in docker can mount the files + # there correctly. + "--volume" "/tmp:/tmp" + # Pass down the KOKORO_GFILE_DIR and KOKORO_KEYSTORE_DIR + # TODO(tmatsuo): This part is not portable. + "--env" "TRAMPOLINE_SECRET_DIR=/secrets" + "--volume" "${KOKORO_GFILE_DIR:-/dev/shm}:/secrets/gfile" + "--env" "KOKORO_GFILE_DIR=/secrets/gfile" + "--volume" "${KOKORO_KEYSTORE_DIR:-/dev/shm}:/secrets/keystore" + "--env" "KOKORO_KEYSTORE_DIR=/secrets/keystore" +) + +# Add an option for nicer output if the build gets a tty. +if [[ -t 0 ]]; then + docker_flags+=("-it") +fi + +# Passing down env vars +for e in "${pass_down_envvars[@]}" +do + if [[ -n "${!e:-}" ]]; then + docker_flags+=("--env" "${e}=${!e}") + fi +done + +# If arguments are given, all arguments will become the commands run +# in the container, otherwise run TRAMPOLINE_BUILD_FILE. +if [[ $# -ge 1 ]]; then + log_yellow "Running the given commands '" "${@:1}" "' in the container." + readonly commands=("${@:1}") + if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then + echo docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" "${commands[@]}" + fi + docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" "${commands[@]}" +else + log_yellow "Running the tests in a Docker container." + docker_flags+=("--entrypoint=${TRAMPOLINE_BUILD_FILE}") + if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then + echo docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" + fi + docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" +fi + + +test_retval=$? + +if [[ ${test_retval} -eq 0 ]]; then + log_green "Build finished with ${test_retval}" +else + log_red "Build finished with ${test_retval}" +fi + +# Only upload it when the test passes. +if [[ "${update_cache}" == "true" ]] && \ + [[ $test_retval == 0 ]] && \ + [[ "${TRAMPOLINE_IMAGE_UPLOAD:-false}" == "true" ]]; then + log_yellow "Uploading the Docker image." + if docker push "${TRAMPOLINE_IMAGE}"; then + log_green "Finished uploading the Docker image." + else + log_red "Failed uploading the Docker image." + fi + # Call trampoline_after_upload_hook if it's defined. + if function_exists trampoline_after_upload_hook; then + trampoline_after_upload_hook + fi + +fi + +exit "${test_retval}" diff --git a/.trampolinerc b/.trampolinerc new file mode 100644 index 00000000..17f21195 --- /dev/null +++ b/.trampolinerc @@ -0,0 +1,50 @@ +# Copyright 2020 Google LLC +# +# 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. + +# Add required env vars here. +required_envvars+=( +) + +# Add env vars which are passed down into the container here. +pass_down_envvars+=( + # We test this envvar in tests/python/test_envvar.py. + "TEST_ENV" +) + +# Prevent unintentional override on the default image. +if [[ "${TRAMPOLINE_IMAGE_UPLOAD:-false}" == "true" ]] && \ + [[ -z "${TRAMPOLINE_IMAGE:-}" ]]; then + echo "Please set TRAMPOLINE_IMAGE if you want to upload the Docker image." + exit 1 +fi + +# Define the default value if it makes sense. +if [[ -z "${TRAMPOLINE_IMAGE_UPLOAD:-}" ]]; then + TRAMPOLINE_IMAGE_UPLOAD="" +fi + +if [[ -z "${TRAMPOLINE_IMAGE:-}" ]]; then + TRAMPOLINE_IMAGE="" +fi + +if [[ -z "${TRAMPOLINE_DOCKERFILE:-}" ]]; then + TRAMPOLINE_DOCKERFILE=".kokoro/docker/Dockerfile" +fi + +if [[ -z "${TRAMPOLINE_BUILD_FILE:-}" ]]; then + TRAMPOLINE_BUILD_FILE="" +fi + +# The build will show some commands and docker build logs. +TRAMPOLINE_VERBOSE="true" diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 02e0aadb..00000000 --- a/.travis.yml +++ /dev/null @@ -1,12 +0,0 @@ -sudo: false -language: python -env: - global: - - GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/service-account.json - - secure: S/A6XENZTy+py6kQxUX5EWgNJv3NGRaSTo4LdWVaZ7qKww1+akeMNHKvPXjp398UtsXkCWCpdy5HeLDYdmzX83uuFoPD1S/z7ilVpo/q4sRxDQmnw4diPHSpx32+XRyKpMpbjgJTMhocqZGcJFI9jxZ4QAHK3rul8ulvTojCyQf7OrePhgfBB72f/ZHho73VqmsngGyWNI6MtGGrBzMTtXlAOt8BgDhYXzPjhEWkE+OKAF9sMJqFmxUXyKkfTBfcGHDYcAOlNtvVdZGRC9NmzRvZxC5T7lQv/WdfGBAT3Zr4NSobKHK6DAv9Q7QvVgHIs7HRomgpHQ9IKnwyhR4RTYAnTtZyHe0/fUlPUXC8+Pkk634eRgYuVNMH8Cf7eRXcLRGF6wgBe9nHnx/o0w/kLbEyirQo3kVIM4Y6yfR5MhZ0Nb4oRq5tuXzqN1B5ZFyfmHhkwsJoeL5wgIGcwOxkoCTH8HqvPfW+SJuUpQC/sB89ixfTfUXYyMJxgGTOdg+1wD4IAfivGyWiUBcx06glfC3Sn+75oflgMz4M8y6zafXTzux755U5pEMa8Gw+5BqpDlkRliZwdAJ6UwHo/XHeJ2rgiTddEgSZYN0CR5XQFcEc69lauUEPkeE5fPubSgcH28xvbhWq7WJ1qdi3Wdw8RaCLOys/B6vwL8GhSJP+ROg= -before_install: -- ./decrypt-secrets.sh "$SECRETS_PASSWORD" -install: -- pip install nox-automation tox -script: -- nox --session lint travis diff --git a/1-hello-world/app.yaml b/1-hello-world/app.yaml deleted file mode 100644 index 4e3cb73b..00000000 --- a/1-hello-world/app.yaml +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright 2015 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. - -# This file specifies your Python application's runtime configuration. -# See https://cloud.google.com/appengine/docs/managed-vms/python/runtime -# for details. - -# [START runtime] -runtime: python -env: flex -entrypoint: gunicorn -b :$PORT main:app - -runtime_config: - python_version: 3 -# [END runtime] diff --git a/1-hello-world/requirements.txt b/1-hello-world/requirements.txt deleted file mode 100644 index 45c7016a..00000000 --- a/1-hello-world/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -Flask==0.11.1 -gunicorn==19.6.0 diff --git a/1-hello-world/tox.ini b/1-hello-world/tox.ini deleted file mode 100644 index 82bf72e3..00000000 --- a/1-hello-world/tox.ini +++ /dev/null @@ -1,10 +0,0 @@ -[tox] -skipsdist = True -envlist = lint - -[testenv:lint] -deps = - flake8 - flake8-import-order -commands = - flake8 --exclude=env --import-order-style=google diff --git a/2-structured-data/app.yaml b/2-structured-data/app.yaml deleted file mode 100644 index 566e6a19..00000000 --- a/2-structured-data/app.yaml +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright 2015 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. - -# This file specifies your Python application's runtime configuration. -# See https://cloud.google.com/appengine/docs/managed-vms/python/runtime -# for details. - -runtime: python -env: flex -entrypoint: gunicorn -b :$PORT main:app - -runtime_config: - python_version: 3 - -#[START cloudsql_settings] -beta_settings: - # If using Cloud SQL, uncomment and set this value to the Cloud SQL - # connection name, e.g. - # "project:region:cloudsql-instance" - # You must also update the values in config.py. - # - # cloud_sql_instances: "your-cloudsql-connection-name" -#[END cloudsql_settings] diff --git a/2-structured-data/bookshelf/__init__.py b/2-structured-data/bookshelf/__init__.py deleted file mode 100644 index 419c524c..00000000 --- a/2-structured-data/bookshelf/__init__.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright 2015 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. - -import logging - -from flask import current_app, Flask, redirect, url_for - - -def create_app(config, debug=False, testing=False, config_overrides=None): - app = Flask(__name__) - app.config.from_object(config) - - app.debug = debug - app.testing = testing - - if config_overrides: - app.config.update(config_overrides) - - # Configure logging - if not app.testing: - logging.basicConfig(level=logging.INFO) - - # Setup the data model. - with app.app_context(): - model = get_model() - model.init_app(app) - - # Register the Bookshelf CRUD blueprint. - from .crud import crud - app.register_blueprint(crud, url_prefix='/books') - - # Add a default root route. - @app.route("/") - def index(): - return redirect(url_for('crud.list')) - - # Add an error handler. This is useful for debugging the live application, - # however, you should disable the output of the exception for production - # applications. - @app.errorhandler(500) - def server_error(e): - return """ - An internal error occurred:
{}
- See logs for full stacktrace.
- """.format(e), 500
-
- return app
-
-
-def get_model():
- model_backend = current_app.config['DATA_BACKEND']
- if model_backend == 'cloudsql':
- from . import model_cloudsql
- model = model_cloudsql
- elif model_backend == 'datastore':
- from . import model_datastore
- model = model_datastore
- elif model_backend == 'mongodb':
- from . import model_mongodb
- model = model_mongodb
- else:
- raise ValueError(
- "No appropriate databackend configured. "
- "Please specify datastore, cloudsql, or mongodb")
-
- return model
diff --git a/2-structured-data/bookshelf/crud.py b/2-structured-data/bookshelf/crud.py
deleted file mode 100644
index baa3488d..00000000
--- a/2-structured-data/bookshelf/crud.py
+++ /dev/null
@@ -1,75 +0,0 @@
-# Copyright 2015 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.
-
-from bookshelf import get_model
-from flask import Blueprint, redirect, render_template, request, url_for
-
-
-crud = Blueprint('crud', __name__)
-
-
-# [START list]
-@crud.route("/")
-def list():
- token = request.args.get('page_token', None)
- if token:
- token = token.encode('utf-8')
-
- books, next_page_token = get_model().list(cursor=token)
-
- return render_template(
- "list.html",
- books=books,
- next_page_token=next_page_token)
-# [END list]
-
-
-@crud.route('/No books found
-{% endfor %} - -{% if next_page_token %} - -{% endif %} - -{% endblock %} diff --git a/2-structured-data/bookshelf/templates/view.html b/2-structured-data/bookshelf/templates/view.html deleted file mode 100644 index 5f07c51a..00000000 --- a/2-structured-data/bookshelf/templates/view.html +++ /dev/null @@ -1,48 +0,0 @@ -{# -# Copyright 2015 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. -#} - -{% extends "base.html" %} - -{% block content %} - -{{book.description}}
-{}
- See logs for full stacktrace.
- """.format(e), 500
-
- return app
-
-
-def get_model():
- model_backend = current_app.config['DATA_BACKEND']
- if model_backend == 'cloudsql':
- from . import model_cloudsql
- model = model_cloudsql
- elif model_backend == 'datastore':
- from . import model_datastore
- model = model_datastore
- elif model_backend == 'mongodb':
- from . import model_mongodb
- model = model_mongodb
- else:
- raise ValueError(
- "No appropriate databackend configured. "
- "Please specify datastore, cloudsql, or mongodb")
-
- return model
diff --git a/3-binary-data/bookshelf/crud.py b/3-binary-data/bookshelf/crud.py
deleted file mode 100644
index 6686798c..00000000
--- a/3-binary-data/bookshelf/crud.py
+++ /dev/null
@@ -1,109 +0,0 @@
-# Copyright 2015 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.
-
-from bookshelf import get_model, storage
-from flask import Blueprint, current_app, redirect, render_template, request, \
- url_for
-
-
-crud = Blueprint('crud', __name__)
-
-
-# [START upload_image_file]
-def upload_image_file(file):
- """
- Upload the user-uploaded file to Google Cloud Storage and retrieve its
- publicly-accessible URL.
- """
- if not file:
- return None
-
- public_url = storage.upload_file(
- file.read(),
- file.filename,
- file.content_type
- )
-
- current_app.logger.info(
- "Uploaded file %s as %s.", file.filename, public_url)
-
- return public_url
-# [END upload_image_file]
-
-
-@crud.route("/")
-def list():
- token = request.args.get('page_token', None)
- if token:
- token = token.encode('utf-8')
-
- books, next_page_token = get_model().list(cursor=token)
-
- return render_template(
- "list.html",
- books=books,
- next_page_token=next_page_token)
-
-
-@crud.route('/{{book.author}}
-No books found
-{% endfor %} - -{% if next_page_token %} - -{% endif %} - -{% endblock %} diff --git a/3-binary-data/config.py b/3-binary-data/config.py deleted file mode 100644 index 7d397967..00000000 --- a/3-binary-data/config.py +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright 2015 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. - -""" -This file contains all of the configuration values for the application. -Update this file with the values for your specific Google Cloud project. -You can create and manage projects at https://console.developers.google.com -""" - -import os - -# The secret key is used by Flask to encrypt session cookies. -SECRET_KEY = 'secret' - -# There are three different ways to store the data in the application. -# You can choose 'datastore', 'cloudsql', or 'mongodb'. Be sure to -# configure the respective settings for the one you choose below. -# You do not have to configure the other data backends. If unsure, choose -# 'datastore' as it does not require any additional configuration. -DATA_BACKEND = 'datastore' - -# Google Cloud Project ID. This can be found on the 'Overview' page at -# https://console.developers.google.com -PROJECT_ID = 'your-project-id' - -# CloudSQL & SQLAlchemy configuration -# Replace the following values the respective values of your Cloud SQL -# instance. -CLOUDSQL_USER = 'root' -CLOUDSQL_PASSWORD = 'your-cloudsql-password' -CLOUDSQL_DATABASE = 'bookshelf' -# Set this value to the Cloud SQL connection name, e.g. -# "project:region:cloudsql-instance". -# You must also update the value in app.yaml. -CLOUDSQL_CONNECTION_NAME = 'your-cloudsql-connection-name' - -# The CloudSQL proxy is used locally to connect to the cloudsql instance. -# To start the proxy, use: -# -# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306 -# -# Port 3306 is the standard MySQL port. If you need to use a different port, -# change the 3306 to a different port number. - -# Alternatively, you could use a local MySQL instance for testing. -LOCAL_SQLALCHEMY_DATABASE_URI = ( - 'mysql+pymysql://{user}:{password}@127.0.0.1:3306/{database}').format( - user=CLOUDSQL_USER, password=CLOUDSQL_PASSWORD, - database=CLOUDSQL_DATABASE) - -# When running on App Engine a unix socket is used to connect to the cloudsql -# instance. -LIVE_SQLALCHEMY_DATABASE_URI = ( - 'mysql+pymysql://{user}:{password}@localhost/{database}' - '?unix_socket=/cloudsql/{connection_name}').format( - user=CLOUDSQL_USER, password=CLOUDSQL_PASSWORD, - database=CLOUDSQL_DATABASE, connection_name=CLOUDSQL_CONNECTION_NAME) - -if os.environ.get('GAE_INSTANCE'): - SQLALCHEMY_DATABASE_URI = LIVE_SQLALCHEMY_DATABASE_URI -else: - SQLALCHEMY_DATABASE_URI = LOCAL_SQLALCHEMY_DATABASE_URI - -# Mongo configuration -# If using mongolab, the connection URI is available from the mongolab control -# panel. If self-hosting on compute engine, replace the values below. -MONGO_URI = 'mongodb://user:password@host:27017/database' - -# Google Cloud Storage and upload settings. -# Typically, you'll name your bucket the same as your project. To create a -# bucket: -# -# $ gsutil mb gs://{}
- See logs for full stacktrace.
- """.format(e), 500
-
- return app
-
-
-def get_model():
- model_backend = current_app.config['DATA_BACKEND']
- if model_backend == 'cloudsql':
- from . import model_cloudsql
- model = model_cloudsql
- elif model_backend == 'datastore':
- from . import model_datastore
- model = model_datastore
- elif model_backend == 'mongodb':
- from . import model_mongodb
- model = model_mongodb
- else:
- raise ValueError(
- "No appropriate databackend configured. "
- "Please specify datastore, cloudsql, or mongodb")
-
- return model
-
-
-# [START request_user_info]
-def _request_user_info(credentials):
- """
- Makes an HTTP request to the Google+ API to retrieve the user's basic
- profile information, including full name and photo, and stores it in the
- Flask session.
- """
- http = httplib2.Http()
- credentials.authorize(http)
- resp, content = http.request(
- 'https://www.googleapis.com/plus/v1/people/me')
-
- if resp.status != 200:
- current_app.logger.error(
- "Error while obtaining user profile: %s" % resp)
- return None
- session['profile'] = json.loads(content.decode('utf-8'))
-
-# [END request_user_info]
diff --git a/4-auth/bookshelf/crud.py b/4-auth/bookshelf/crud.py
deleted file mode 100644
index 8991bcfe..00000000
--- a/4-auth/bookshelf/crud.py
+++ /dev/null
@@ -1,129 +0,0 @@
-# Copyright 2015 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.
-
-from bookshelf import get_model, oauth2, storage
-from flask import Blueprint, current_app, redirect, render_template, request, \
- session, url_for
-
-
-crud = Blueprint('crud', __name__)
-
-
-def upload_image_file(file):
- """
- Upload the user-uploaded file to Google Cloud Storage and retrieve its
- publicly-accessible URL.
- """
- if not file:
- return None
-
- public_url = storage.upload_file(
- file.read(),
- file.filename,
- file.content_type
- )
-
- current_app.logger.info(
- "Uploaded file %s as %s.", file.filename, public_url)
-
- return public_url
-
-
-@crud.route("/")
-def list():
- token = request.args.get('page_token', None)
- if token:
- token = token.encode('utf-8')
-
- books, next_page_token = get_model().list(cursor=token)
-
- return render_template(
- "list.html",
- books=books,
- next_page_token=next_page_token)
-
-
-# [START list_mine]
-@crud.route("/mine")
-@oauth2.required
-def list_mine():
- token = request.args.get('page_token', None)
- if token:
- token = token.encode('utf-8')
-
- books, next_page_token = get_model().list_by_user(
- user_id=session['profile']['id'],
- cursor=token)
-
- return render_template(
- "list.html",
- books=books,
- next_page_token=next_page_token)
-# [END list_mine]
-
-
-@crud.route('/{{book.description}}
- Added by {{book.get('createdBy')|default('Anonymous', True)}} -{{book.description}}
- Added by {{book.createdBy|default('Anonymous', True)}} -{{book.author}}
-No books found
-{% endfor %} - -{% if next_page_token %} - -{% endif %} - -{% endblock %} diff --git a/6-pubsub/config.py b/6-pubsub/config.py deleted file mode 100644 index d40ef9bc..00000000 --- a/6-pubsub/config.py +++ /dev/null @@ -1,113 +0,0 @@ -# Copyright 2015 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. - -""" -This file contains all of the configuration values for the application. -Update this file with the values for your specific Google Cloud project. -You can create and manage projects at https://console.developers.google.com -""" - -import os - -# The secret key is used by Flask to encrypt session cookies. -SECRET_KEY = 'secret' - -# There are three different ways to store the data in the application. -# You can choose 'datastore', 'cloudsql', or 'mongodb'. Be sure to -# configure the respective settings for the one you choose below. -# You do not have to configure the other data backends. If unsure, choose -# 'datastore' as it does not require any additional configuration. -DATA_BACKEND = 'datastore' - -# Google Cloud Project ID. This can be found on the 'Overview' page at -# https://console.developers.google.com -PROJECT_ID = 'your-project-id' - -# Cloud Datastore dataset id, this is the same as your project id. -DATASTORE_DATASET_ID = PROJECT_ID - -# CloudSQL & SQLAlchemy configuration -# Replace the following values the respective values of your Cloud SQL -# instance. -CLOUDSQL_USER = 'root' -CLOUDSQL_PASSWORD = 'your-cloudsql-password' -CLOUDSQL_DATABASE = 'bookshelf' -# Set this value to the Cloud SQL connection name, e.g. -# "project:region:cloudsql-instance". -# You must also update the value in app.yaml. -CLOUDSQL_CONNECTION_NAME = 'your-cloudsql-connection-name' - -# The CloudSQL proxy is used locally to connect to the cloudsql instance. -# To start the proxy, use: -# -# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306 -# -# Port 3306 is the standard MySQL port. If you need to use a different port, -# change the 3306 to a different port number. - -# Alternatively, you could use a local MySQL instance for testing. -LOCAL_SQLALCHEMY_DATABASE_URI = ( - 'mysql+pymysql://{user}:{password}@127.0.0.1:3306/{database}').format( - user=CLOUDSQL_USER, password=CLOUDSQL_PASSWORD, - database=CLOUDSQL_DATABASE) - -# When running on App Engine a unix socket is used to connect to the cloudsql -# instance. -LIVE_SQLALCHEMY_DATABASE_URI = ( - 'mysql+pymysql://{user}:{password}@localhost/{database}' - '?unix_socket=/cloudsql/{connection_name}').format( - user=CLOUDSQL_USER, password=CLOUDSQL_PASSWORD, - database=CLOUDSQL_DATABASE, connection_name=CLOUDSQL_CONNECTION_NAME) - -if os.environ.get('GAE_INSTANCE'): - SQLALCHEMY_DATABASE_URI = LIVE_SQLALCHEMY_DATABASE_URI -else: - SQLALCHEMY_DATABASE_URI = LOCAL_SQLALCHEMY_DATABASE_URI - -# Mongo configuration -# If using mongolab, the connection URI is available from the mongolab control -# panel. If self-hosting on compute engine, replace the values below. -MONGO_URI = 'mongodb://user:password@host:27017/database' - -# Google Cloud Storage and upload settings. -# Typically, you'll name your bucket the same as your project. To create a -# bucket: -# -# $ gsutil mb gs://{{book.author}}
-No books found
-{% endfor %} - -{% if next_page_token %} - -{% endif %} - -{% endblock %} diff --git a/7-gce/bookshelf/templates/view.html b/7-gce/bookshelf/templates/view.html deleted file mode 100644 index e654e8ab..00000000 --- a/7-gce/bookshelf/templates/view.html +++ /dev/null @@ -1,53 +0,0 @@ -{# -# Copyright 2015 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. -#} - -{% extends "base.html" %} - -{% block content %} - -{{book.description}}
- Added by {{book.createdBy|default('Anonymous', True)}} -| Original | +Translation | +
|---|---|
| + + {{ translation['OriginalLanguage'] }} + + {{ translation['Original'] }} + | ++ + {{ translation['Language'] }} + + {{ translation['Translated'] }} + | +
{}
+ See logs for full stacktrace.
+ """.format(e), 500
+
+
+# This is only used when running locally. When running live, gunicorn runs
+# the application.
+if __name__ == '__main__':
+ app.run(host='127.0.0.1', port=8080, debug=True)
diff --git a/bookshelf/main_test.py b/bookshelf/main_test.py
new file mode 100644
index 00000000..249cba17
--- /dev/null
+++ b/bookshelf/main_test.py
@@ -0,0 +1,169 @@
+# Copyright 2019 Google LLC
+#
+# 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.
+
+import os
+import re
+
+import google.auth
+import main
+import pytest
+import requests
+from six import BytesIO
+
+
+credentials, project_id = google.auth.default()
+os.environ['GOOGLE_CLOUD_PROJECT'] = project_id
+
+
+@pytest.fixture
+def app(request):
+ """This fixture provides a Flask app instance configured for testing.
+
+ It also ensures the tests run within a request context, allowing
+ any calls to flask.request, flask.current_app, etc. to work."""
+ app = main.app
+
+ with app.test_request_context():
+ yield app
+
+
+@pytest.fixture
+def firestore():
+ """This fixture provides a modified version of the app's Firebase model
+ that tracks all created items and deletes them at the end of the test.
+
+ Any tests that directly or indirectly interact with the database should
+ use this to ensure that resources are properly cleaned up.
+ """
+
+ import firestore
+
+ # Ensure no books exist before running the tests. This typically helps if
+ # tests somehow left the database in a bad state.
+ delete_all_books(firestore)
+
+ yield firestore
+
+ # Delete all books that we created during tests.
+ delete_all_books(firestore)
+
+
+def delete_all_books(firestore):
+ while True:
+ books, _ = firestore.next_page(limit=50)
+ if not books:
+ break
+ for book in books:
+ firestore.delete(book['id'])
+
+
+def test_list(app, firestore):
+ for i in range(1, 12):
+ firestore.create({'title': u'Book {0}'.format(i)})
+
+ with app.test_client() as c:
+ rv = c.get('/')
+
+ assert rv.status == '200 OK'
+
+ body = rv.data.decode('utf-8')
+ assert 'Book 1' in body, "Should show books"
+ assert len(re.findall('{{ message }}
+ {% endfor %} +{% endwith %} +
No books found
{% endfor %} -{% if next_page_token %} +{% if last_title %} {% endif %} diff --git a/3-binary-data/bookshelf/templates/view.html b/bookshelf/templates/view.html similarity index 93% rename from 3-binary-data/bookshelf/templates/view.html rename to bookshelf/templates/view.html index 1e509ff0..476db111 100644 --- a/3-binary-data/bookshelf/templates/view.html +++ b/bookshelf/templates/view.html @@ -1,5 +1,5 @@ {# -# Copyright 2015 Google Inc. +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -37,7 +37,7 @@{{book.author}}
-No books found
-{% endfor %} - -{% if next_page_token %} - -{% endif %} - -{% endblock %} diff --git a/optional-container-engine/bookshelf/templates/view.html b/optional-container-engine/bookshelf/templates/view.html deleted file mode 100644 index e654e8ab..00000000 --- a/optional-container-engine/bookshelf/templates/view.html +++ /dev/null @@ -1,53 +0,0 @@ -{# -# Copyright 2015 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. -#} - -{% extends "base.html" %} - -{% block content %} - -{{book.description}}
- Added by {{book.createdBy|default('Anonymous', True)}} -