This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding automatic setup for stackdriver integration (#333)
- Loading branch information
Takashi Matsuo
authored
Jul 14, 2017
1 parent
8657998
commit 4138057
Showing
16 changed files
with
280 additions
and
3 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
builder/gen-dockerfile/src/Builder/Exception/GoogleCloudVersionException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* 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. | ||
*/ | ||
|
||
namespace Google\Cloud\Runtimes\Builder\Exception; | ||
|
||
class GoogleCloudVersionException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
/** | ||
* 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. | ||
*/ | ||
|
||
namespace Google\Cloud\Runtimes; | ||
|
||
use Composer\Semver\Semver; | ||
use Composer\Semver\Comparator; | ||
use Google\Cloud\Runtimes\Builder\Exception\GoogleCloudVersionException; | ||
|
||
class ValidateGoogleCloud | ||
{ | ||
const MINIMUM_VERSION = 'v0.33'; | ||
|
||
/* | ||
* @param string $workspace | ||
* @return bool | ||
* @throw GoogleCloudVersionException | ||
*/ | ||
public static function doCheck($workspace) | ||
{ | ||
$filename = $workspace . '/composer.json'; | ||
if (! file_exists($filename)) { | ||
throw new GoogleCloudVersionException( | ||
'composer.json does not exist' | ||
); | ||
} | ||
$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']; | ||
} else { | ||
throw new GoogleCloudVersionException( | ||
'google/cloud not found in composer.json' | ||
); | ||
} | ||
|
||
$versions = self::getCurrentGoogleCloudVersions(); | ||
|
||
// Check all the available versions against the constraints | ||
// and returns matched ones | ||
$filtered = Semver::satisfiedBy($versions, $constraints); | ||
|
||
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)) { | ||
throw new GoogleCloudVersionException( | ||
'stackdriver integration needs google/cloud ' | ||
. self::MINIMUM_VERSION . ' or higher' | ||
); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Determine available versions for google/cloud | ||
* @return array | ||
*/ | ||
private static function getCurrentGoogleCloudVersions() | ||
{ | ||
exec( | ||
'composer show --all google/cloud |grep \'versions : \'', | ||
$output, | ||
$ret | ||
); | ||
if ($ret !== 0) { | ||
throw new GoogleCloudVersionException( | ||
'Failed to determine available versions of google/cloud package' | ||
); | ||
} | ||
// Remove the title | ||
$output = substr($output[0], strlen('versions : ')); | ||
|
||
// Split the version strings | ||
$versions = preg_split('/[,\s]+/', $output); | ||
|
||
// Remove '*', indicator for the latest stable | ||
$versions = array_diff($versions, ['*']); | ||
return $versions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
builder/gen-dockerfile/tests/test_data/stackdriver_no_composer/app.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
builder/gen-dockerfile/tests/test_data/stackdriver_no_google_cloud/app.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
builder/gen-dockerfile/tests/test_data/stackdriver_no_google_cloud/composer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"google/cloud-logging": "*" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
builder/gen-dockerfile/tests/test_data/stackdriver_old_google_cloud/app.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
builder/gen-dockerfile/tests/test_data/stackdriver_old_google_cloud/composer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"google/cloud": "^0.32" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
builder/gen-dockerfile/tests/test_data/stackdriver_simple/app.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
builder/gen-dockerfile/tests/test_data/stackdriver_simple/composer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"google/cloud": "^0.34.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[program:batch-daemon] | ||
command = php -d auto_prepend_file='' -d disable_functions='' /app/vendor/bin/google-cloud-batch daemon | ||
stdout_logfile = /dev/stdout | ||
stdout_logfile_maxbytes=0 | ||
stderr_logfile = /dev/stderr | ||
stderr_logfile_maxbytes=0 | ||
user = www-data | ||
autostart = true | ||
autorestart = true | ||
priority = 5 | ||
stopwaitsecs = 20 |
25 changes: 25 additions & 0 deletions
25
php-base/stackdriver-files/enable_stackdriver_integration.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/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. | ||
|
||
|
||
# A shell script for installing composer | ||
set -xe | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Automatic error reporting | ||
auto_prepend_file='/app/vendor/google/cloud/src/ErrorReporting/prepend.php' |