PHP Deployment Scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
neatoDeploy/app/neato_fns.php

212 lines
5.4 KiB

<?php
define('PROJECT_RUN_DIR', $cwd . '/run');
function display($data) {
$str = '';
if (is_array($data)) {
foreach ($data as $s) {
$str .= $s . PHP_EOL;
}
} else {
$str = $data;
}
if (empty($str)) {
return false;
}
if (Configure::get('display')) {
echo $str . PHP_EOL;
}
if (Configure::get('syslog')) {
$access = date("Y/m/d H:i:s");
syslog(LOG_INFO, $access . " " . $str);
}
if (Configure::get('logfile')) {
$config_file = (defined('CONFIG_FILE')) ? '_' . CONFIG_FILE : '';
$logger = new Logger('neatoInstaller' . $config_file);
$logger->write($str);
}
}
function check_for_error($exit_code, $msg) {
if ($exit_code === true) {
return;
}
if ($exit_code === false || $exit_code !== 0) {
display($msg);
}
}
function get_perms($kind): int {
if (is_numeric($kind) && (strlen($kind) == 3 || strlen($kind) == 4 )) {
return intval($kind);
}
// if (is_string_found($kind, '+') || is_string_found($kind, '-')) {
// return $kind;
// }
switch ($kind) {
case 'keydir': $perm = 700;
break;
case 'dir': $perm = 775;
break;
case 'web':
case 'normal':
$perm = 0664;
break;
case 'bin': $perm = 0755;
break;
case 'sbin': $perm = 0750;
break;
case 'writeonly': $perm = 0220;
break;
case 'readonly': $perm = 0444;
break;
case 'key':
case 'secret': $perm = 0600;
break;
// config file
default:
$perm = 0644;
}
return $perm;
}
function use_me($program) {
static $did_update = false;
$installed_a = do_command('is_installed', $program);
$is_installed_b = $installed_a['installed'];
if ($is_installed_b === false) {
if ($did_update === false) {
$did = do_command('update');
if ($did !== 0) {
return false;
}
$did_update = true;
}
return do_command('install', $program);
}
return true;
}
function do_command() {
$numargs = func_num_args();
if ($numargs == 0) {
return false;
}
$arg_list = func_get_args();
if (is_string_found($arg_list[0], "::")) {
$method = "\\utils\\" . array_shift($arg_list);
} else {
$method = "neato::" . array_shift($arg_list);
}
$thingies = (isset($arg_list[0]) && is_array($arg_list[0])) ? $arg_list[0] : false;
if ($thingies === false) {
return call_user_func_array($method, $arg_list);
} else {
$retval = true;
foreach ($thingies as $item) {
$ret = call_user_func_array($method, $item);
if ($ret === false) {
$retval = false;
}
}
return $retval;
}
}
function file_loop($data) {
$retval = true;
foreach ($data as $command => $v) {
switch (strtolower($command)) {
case 'sed':
$ret = sed_loop($v);
if ($ret === false) {
$retval = false;
}
break;
case 'cp':
case 'mv':
case 'ln':
case 'rm':
case 'make_dir':
case 'chmod_file_or_dir':
foreach ($v as $a => $b) {
$a = (isset($a) && !empty($a)) ? $a : false;
$b = (isset($b) && !empty($b)) ? $b : false;
if ($a !== false) {
if ($b !== false) {
$ret = $command($a, $b);
if ($ret === false) {
$retval = false;
}
} else {
$ret = $command($a);
if ($ret === false) {
$retval = false;
}
}
}
}
break;
}
}
return $retval;
}
function sed_loop($data) {
$retval = true;
foreach ($data as $file => $data) {
foreach ($data as $find => $replace) {
$ret = do_command('sed::replace', $file, $find, $replace);
if ($ret === false) {
$retval = false;
}
}
}
return $retval;
}
function stderr() {
return ' 2>&1'; // exec redirect std errors to output for use with display....
}
function run_once($output = true, $halt = true) {
if (!is_dir(PROJECT_RUN_DIR)) {
mkdir(PROJECT_RUN_DIR, 0775);
}
$make_config_file = (defined('CONFIG_FILE')) ? CONFIG_FILE : '';
$make_config_file .= '.run.lock';
$file = PROJECT_RUN_DIR . '/' . $make_config_file;
if (file_exists($file)) {
if ($output) {
echo 'Script has already been installed!' . PHP_EOL;
}
if ($halt) {
exit(1);
} else {
return true;
}
} else {
touch($file);
return false;
}
}
function safe_cmd_quotes($data) {
$data = str_replace('"', "", $data);
$data = str_replace("'", "", $data);
return escapeshellcmd($data);
}
function safe_cmd($input, $in = '') {
return (!empty($in)) ? escapeshellcmd(escapeshellarg($input) . " " . escapeshellarg($in)) : escapeshellcmd(escapeshellarg($input));
}