Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Allow individual packages for stackdriver integration (#351)
Browse files Browse the repository at this point in the history
* Allow individual packages for stackdriver integration

* CS fix

* Added an integration test with individual packages.
  • Loading branch information
Takashi Matsuo authored Aug 18, 2017
1 parent 5c63342 commit f9b66c2
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 30 deletions.
11 changes: 8 additions & 3 deletions builder/gen-dockerfile/src/Builder/GenFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,15 @@ public function createDockerfile($baseImage)
);
}
if (self::isStackdriverIntegrationEnabled($envs)) {
ValidateGoogleCloud::doCheck($this->workspace);
$enableStackdriverCmd = 'RUN /bin/bash /stackdriver-files/'
. 'enable_stackdriver_integration.sh';
$envs['IS_BATCH_DAEMON_RUNNING'] = 'true';
$result = ValidateGoogleCloud::doCheck($this->workspace);
if ($result == ValidateGoogleCloud::FOUND_GOOGLE_CLOUD) {
$enableStackdriverCmd = 'RUN /bin/bash /stackdriver-files/'
. 'enable_stackdriver_integration.sh';
} else {
$enableStackdriverCmd = 'RUN /bin/bash /stackdriver-files/'
. 'enable_stackdriver_integration.sh --individual';
}
} else {
$enableStackdriverCmd = '';
}
Expand Down
88 changes: 63 additions & 25 deletions builder/gen-dockerfile/src/ValidateGoogleCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@

class ValidateGoogleCloud
{
const MINIMUM_VERSION = 'v0.33';
const MINIMUM_GOOGLE_CLOUD_VERSION = 'v0.33';
const MINIMUM_GOOGLE_LOGGING_VERSION = 'v1.3.0';
const MINIMUM_GOOGLE_ER_VERSION = 'v0.4.0';
const FOUND_GOOGLE_CLOUD = 1;
const FOUND_INDIVIDUAL_PACKAGES = 2;

/*
* @param string $workspace
* @return bool
* @return int return FOUND_GOOGLE_CLOUD when we found google/cloud,
* otherwise return FOUND_INDIVIDUAL_PACKAGES
* @throw GoogleCloudVersionException
*/
public static function doCheck($workspace)
Expand All @@ -39,52 +44,85 @@ public static function doCheck($workspace)
);
}
$composer = json_decode(file_get_contents($filename), true);
if (is_array($composer)
&& array_key_exists('require', $composer)
&& array_key_exists('google/cloud', $composer['require'])) {
$constraints = $composer['require']['google/cloud'];
$constraintsMap = [];
$minimumVersionMap = [
'google/cloud' => self::MINIMUM_GOOGLE_CLOUD_VERSION,
'google/cloud-logging' => self::MINIMUM_GOOGLE_LOGGING_VERSION,
'google/cloud-error-reporting' => self::MINIMUM_GOOGLE_ER_VERSION
];
// Make sure there is `require` field in `composer.json`.
if (!(is_array($composer) && array_key_exists('require', $composer))) {
throw new GoogleCloudVersionException(
'Required packages not found in composer.json. '
. 'Consider running `composer require google/cloud`'
);
}
// For google/cloud.
if (array_key_exists('google/cloud', $composer['require'])) {
$constraintsMap['google/cloud'] =
$composer['require']['google/cloud'];
} elseif (array_key_exists('google/cloud-logging',
$composer['require']) &&
array_key_exists('google/cloud-error-reporting',
$composer['require'])) {
// For cloud-logging and cloud-error-reporting.
$constraintsMap['google/cloud-logging'] =
$composer['require']['google/cloud-logging'];
$constraintsMap['google/cloud-error-reporting'] =
$composer['require']['google/cloud-error-reporting'];
} else {
throw new GoogleCloudVersionException(
'google/cloud not found in composer.json'
'Required packages not found in composer.json. '
. 'Consider running `composer require google/cloud`'
);
}

$versions = self::getCurrentGoogleCloudVersions();
// Now we have $constraintsMap. All should have at least the minimum
// version.

// Check all the available versions against the constraints
// and returns matched ones
$filtered = Semver::satisfiedBy($versions, $constraints);
foreach ($constraintsMap as $package => $constraints) {
$versions = self::getCurrentPackageVersions($package);

if (count($filtered) === 0) {
throw new GoogleCloudVersionException(
'no available matching version of google/cloud'
);
}
foreach ($filtered as $version) {
if (Comparator::lessThan($version, self::MINIMUM_VERSION)) {
// Check all the available versions against the constraints
// and returns matched ones
$filtered = Semver::satisfiedBy($versions, $constraints);
if (count($filtered) === 0) {
throw new GoogleCloudVersionException(
'stackdriver integration needs google/cloud '
. self::MINIMUM_VERSION . ' or higher'
"no available matching version of $package"
);
}
foreach ($filtered as $version) {
if (Comparator::lessThan($version, $minimumVersionMap[$package])) {
throw new GoogleCloudVersionException(
"stackdriver integration needs $package "
. $minimumVersionMap[$package] . ' or higher'
);
}
}
}

if (array_key_exists('google/cloud', $constraintsMap)) {
return self::FOUND_GOOGLE_CLOUD;
} else {
return self::FOUND_INDIVIDUAL_PACKAGES;
}
return true;
}

/**
* Determine available versions for google/cloud
* Determine available versions for a given package.
* @param string $package
* @return array
*/
private static function getCurrentGoogleCloudVersions()
private static function getCurrentPackageVersions($package)
{
exec(
'composer show --all google/cloud |grep \'versions : \'',
"composer show --all $package |grep 'versions : '",
$output,
$ret
);
if ($ret !== 0) {
throw new GoogleCloudVersionException(
'Failed to determine available versions of google/cloud package'
"Failed to determine available versions of $package package"
);
}
// Remove the title
Expand Down
37 changes: 37 additions & 0 deletions builder/gen-dockerfile/tests/GenFilesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,43 @@ public function dataProvider()
"enable_stackdriver_integration.sh"
]
],
[
// stackdriver individual packages
__DIR__ . '/test_data/stackdriver_individual',
null,
'',
'/app/web',
'added by the php runtime builder',
'gcr.io/google-appengine/php71:latest',
["GOOGLE_RUNTIME_RUN_COMPOSER_SCRIPT=true \\\n",
"FRONT_CONTROLLER_FILE=index.php \\\n",
"DETECTED_PHP_VERSION=7.1 \\\n",
"IS_BATCH_DAEMON_RUNNING=true \n",
"enable_stackdriver_integration.sh --individual"
]
],
[
// stackdriver old logging
__DIR__ . '/test_data/stackdriver_old_logging',
null,
'',
'/app/web',
'added by the php runtime builder',
'gcr.io/google-appengine/php71:latest',
[],
'\\Google\\Cloud\\Runtimes\\Builder\\Exception\\GoogleCloudVersionException'
],
[
// stackdriver old error_reporting
__DIR__ . '/test_data/stackdriver_old_er',
null,
'',
'/app/web',
'added by the php runtime builder',
'gcr.io/google-appengine/php71:latest',
[],
'\\Google\\Cloud\\Runtimes\\Builder\\Exception\\GoogleCloudVersionException'
],
[
// stackdriver no composer.json
__DIR__ . '/test_data/stackdriver_no_composer',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
env: flex
runtime: php

runtime_config:
enable_stackdriver_integration: true
document_root: web
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"google/cloud-logging": "^1.3.1",
"google/cloud-error-reporting": "^0.4.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
env: flex
runtime: php

runtime_config:
enable_stackdriver_integration: true
document_root: web
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"google/cloud-logging": "^1.4.1",
"google/cloud-error-reporting": "^0.3.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
env: flex
runtime: php

runtime_config:
enable_stackdriver_integration: true
document_root: web
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"google/cloud-logging": "^1.2.0",
"google/cloud-error-reporting": "^0.4.1"
}
}
9 changes: 7 additions & 2 deletions php-base/stackdriver-files/enable_stackdriver_integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ echo "Enabling stackdriver integration..."
# To start the batch daemon
cp /stackdriver-files/batch-daemon.conf /etc/supervisor/conf.d

# For enabling automatic error reporting
cp /stackdriver-files/stackdriver-errorreporting.ini ${PHP_DIR}/lib/conf.d
if [ "${1}" = "--individual" ]; then
# For enabling automatic error reporting for google/cloud-error-reporting
cp /stackdriver-files/stackdriver-errorreporting-individual.ini ${PHP_DIR}/lib/conf.d
else
# For enabling automatic error reporting for google/cloud
cp /stackdriver-files/stackdriver-errorreporting.ini ${PHP_DIR}/lib/conf.d
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Automatic error reporting
auto_prepend_file='/app/vendor/google/cloud-error-reporting/prepend.php'
14 changes: 14 additions & 0 deletions testapps/integration-individual-packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Integration Application

This application can be used for the common Cloud Languages Runtime
Integration Framework. It is primarily used to test Stackdriver
integration with individual packages; `google/cloud-logging` and
`google/cloud-error-reporting`.

## Tests

See [Tests](https://github.com/GoogleCloudPlatform/runtimes-common/blob/master/integration_tests/README.md#tests).

## Authentication

This application uses Application Default Credentials to authenticate.
6 changes: 6 additions & 0 deletions testapps/integration-individual-packages/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
runtime: php
env: flex

runtime_config:
document_root: web
enable_stackdriver_integration: true
7 changes: 7 additions & 0 deletions testapps/integration-individual-packages/cloudbuild.yaml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
steps:
- name: gcr.io/cloud-builders/docker
args: ['build', '-t', '${IMAGE}', '.']
- name: gcr.io/gcp-runtimes/structure_test
args: ['-i', '${IMAGE}', '--config', 'php_default.yaml', '-v']
- name: gcr.io/gcp-runtimes/integration_test
args: ['-i', '${IMAGE}', '--no-monitoring', '--no-exception']
21 changes: 21 additions & 0 deletions testapps/integration-individual-packages/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"autoload": {
"psr-4": {
"Google\\Cloud\\Integration\\": ["src", "test/lib"]
}
},
"require": {
"php": "5.6.*|7.0.*|7.1.*",
"silex/silex": "^1.3",
"google/cloud-logging": "^1.3.1",
"google/cloud-error-reporting": "^0.4.1"
},
"require-dev": {
"behat/mink": "^1.7",
"behat/mink-goutte-driver": "^1.2",
"phpunit/phpunit": "~4",
"symfony/browser-kit": "^3.0",
"symfony/http-kernel": "^3.0",
"google/cloud-tools": "^0.6"
}
}
6 changes: 6 additions & 0 deletions testapps/integration-individual-packages/runtimes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schema_version: 1

runtimes:
php:
target:
file: test.yaml
7 changes: 7 additions & 0 deletions testapps/integration-individual-packages/test.yaml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
steps:
- name: '${STAGING_BUILDER_IMAGE}'
args: ['--php71-image', 'gcr.io/google-appengine/php71:staging', '--php70-image', 'gcr.io/google-appengine/php70:staging', '--php56-image', 'gcr.io/google-appengine/php56:staging']
- name: 'gcr.io/cloud-builders/docker:latest'
args: ['build', '-t', '$_OUTPUT_IMAGE', '.']
images:
- '$_OUTPUT_IMAGE'
Loading

0 comments on commit f9b66c2

Please sign in to comment.