Skip to content
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6dbbc72
feat(tests): Adds testing API to capture INI entries
mfulb May 22, 2024
4b1aa4c
feat(install): Adds mapping file for env vars
mfulb May 22, 2024
60b0f93
feat(install): Start of env var inject script
mfulb May 22, 2024
8ac76b1
chore(build): Adds rules for env var injection script
mfulb May 22, 2024
8943480
feat(tests): Adds script to verify ini/envvar mappings
mfulb May 22, 2024
ae77685
chore(install): Adds rules for ini/envvar script
mfulb May 22, 2024
707292e
feat(install,tests): Adds progress for env var injection
mfulb May 24, 2024
ec92c50
fix(install): Fixes usage and message
mfulb May 24, 2024
ddc0144
fix(tests): Fixes call to inject script
mfulb May 24, 2024
4c257e8
fix(install): Handles newrelic.ini creation better
mfulb May 24, 2024
bd2f729
chore: Removes debugging output
mfulb May 24, 2024
bdd9f87
chore: Removes set -x
mfulb May 24, 2024
341b346
chore: Fixes spacing
mfulb May 24, 2024
9263753
chore: Removes trailing space
mfulb May 24, 2024
c1d55c3
fix(agent): Uses macros that works with PHP 7.x as well
mfulb May 28, 2024
e5b1583
chore(testing): Adds env var injection script tests to CI/CD
mfulb May 28, 2024
ae05cd4
chore(testing): Removes unneeded code
mfulb May 29, 2024
89719ba
fix(agent): Adds make target for installer release
mfulb Jun 4, 2024
46a93f5
fix(install): Adds proper path for running inject script
mfulb Jun 5, 2024
ca0238f
chore(installer): Renames mapping file to work with GHA limitations
mfulb Jun 5, 2024
d484f6c
chore(installer): Adds renamed mapping file
mfulb Jun 5, 2024
a10f617
fix(testing): Improves newrelic-install tests
mfulb Jun 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(install): Start of env var inject script
Start of a script that will inject environment variable
config values into a newrelic.ini config file.
  • Loading branch information
mfulb committed May 22, 2024
commit 60b0f935cdecffa4961a3f80887ab4d434c992bf
75 changes: 75 additions & 0 deletions agent/newrelic-install-inject-envvars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?PHP
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/* Purpose: Read a fresh newrelic.ini file and inject any values
* from environment variables.
*
* Not designed to work on a modified newrelic.ini and script
* is not able to detect this scenario.
*
* The mapping from INI directive name to corresponding
* environment variable name must be injected before using
* in place of the comment below containing INI_ENVVAR_MAP
*
*/
function failure(string $msg) {
die(__FILE__ . " : ERROR : " . $msg . "\n");
}

function generate_regex(string $ini_name) {
$ini_name_esc = str_replace(".", "\.", $ini_name);
return "/^\s*;*\s*" . $ini_name_esc . "\s*=.*/im";
}

/* requires PHP 7+ */
if (version_compare(PHP_VERSION, '7.0', '<')) {
failure("requires PHP >= 7.0\n");
}

/* Mapping from INI name to environment variable name
* Expected form:
* define('INI_ENVVAR_MAP', array(....));
*/
require "newrelic-php-cfg-mappings.php";

/* Verify that ini/envvar mapping was injected and is defined */
if (!defined('INI_ENVVAR_MAP')) {
failure("INI/ENVVAR mapping was not detected - cannot proceed!");
}

/* Verify input INI file exists */
if (2 != $argc) {
failure("Must supply INI file name as first argument!");
}

$ini_filename = $argv[1];
if (!file_exists($ini_filename)) {
failure("INI file \"$ini_filename\" does not exist!");
}

$data = file_get_contents(($ini_filename));
if (!$data) {
failure("Could not read INI file \"$ini_filename\"!");
}

$pattern = array();
$replace = array();

/* Go through all INI keys and see if the environment variable is defined */
foreach (INI_ENVVAR_MAP as $ini_name => $env_name) {
array_push($pattern, generate_regex($ini_name));
array_push($replace, "$ini_name=$env_name");
}

$data = preg_replace($pattern, $replace, $data);

$fh = fopen($ini_filename, "w");
if (!$fh) {
failure("Unable to write out modified INI file $ini_filename");
}
fwrite($fh, $data);
fclose($fh);