-
Notifications
You must be signed in to change notification settings - Fork 1
/
reconcile-resource.sh
executable file
·55 lines (50 loc) · 2.27 KB
/
reconcile-resource.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/php
<?php
# This script is used to consolidate settings in config files.
# For example, as NEMS evolves, config settings in the resource.cfg file may be added.
# Traditional "replacement" of the config file would result in those new settings missing.
# So this script consolidates the data between the source (backup.nems) and destination (resource.cfg).
echo "reconcile-resource... " . PHP_EOL;
$resourcesrc = $argv[1];
$resourcedest = $argv[2];
$source = '/tmp/nems_migrator_restore' . $resourcesrc . '/resource.cfg';
$dest = $resourcedest . '/resource.cfg';
$data = new stdClass();
if (file_exists($source)) $data->source = file($source);
if (file_exists($dest)) { $data->dest = file($dest); } else { echo 'ERROR. Is this NEMS?' . PHP_EOL; exit(); }
if (isset($data->source) && is_array($data->source)) {
foreach ($data->source as $line) {
$line = trim($line);
if (substr($line,0,1) == '$') {
$import = new stdClass();
echo ' Importing: ' . $line . PHP_EOL;
$tmp = explode('$',$line);
// Check if user actually has a dollar sign in their password and append if yes
$append='';
$check=3; // starting key is 3 for the check
while (isset($tmp[$check])) {
$append .= $tmp[$check];
$check++; // it's dynamic, in case they have many $$ in their Pa$$word.
}
if (isset($tmp[3])) unset($tmp[3]);
$tmp[3] = explode('=',$tmp[2]);
$import->variable = $tmp[1];
$import->value = $tmp[3][1];
foreach ($data->dest as $line=>$destdata) {
if (substr($destdata,0,(2+strlen($import->variable))) == '$' . $import->variable . '$') {
// Only replace if it's not USER1/USER2 (NEMS system paths) to prevent a user setting breaking NEMS if paths are different
if ($import->variable != 'USER1' && $import->variable != 'USER2') {
// Replace the matching line in the destination
$data->dest[$line] = '$' . $import->variable . '$=' . $import->value . PHP_EOL;
}
}
}
}
}
file_put_contents($dest,$data->dest);
echo 'Consolidation complete.';
} else {
echo 'No data source found. Leaving NEMS-SST configuration as is.';
}
echo PHP_EOL;
?>