From 27eb967f2514ab642649f4cd6d56bbe380bf728d Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 21 Nov 2016 00:40:52 +0000 Subject: [PATCH] Bed --- etc/dbq.php | 132 +++---------------------------- etc/dbq/crypt.yaml | 3 + etc/{dbq.yaml => dbq/izi.yaml} | 11 +-- etc/dbq/local.php | 8 +- etc/dbq/zzz-all.php | 104 ++++++++++++++++++++++++ etc/profile | 80 ++++++++++--------- etc/profile.d/docker.sh | 7 +- etc/profile.d/functions | 1 + etc/profile.d/sys.sh | 3 + lib/php/db.php | 16 ++-- lib/php/db/config.php | 54 +++++++------ lib/php/db/page.php | 2 +- lib/php/nb.php | 4 + share/db/{local.db => shell.db} | Bin src/Docker/Compose/dbq/index.php | 2 + 15 files changed, 217 insertions(+), 210 deletions(-) create mode 100644 etc/dbq/crypt.yaml rename etc/{dbq.yaml => dbq/izi.yaml} (80%) create mode 100644 etc/dbq/zzz-all.php create mode 100644 etc/profile.d/sys.sh rename share/db/{local.db => shell.db} (100%) diff --git a/etc/dbq.php b/etc/dbq.php index 82c0efd2..525e3d82 100644 --- a/etc/dbq.php +++ b/etc/dbq.php @@ -1,133 +1,23 @@ $db) { - if (1 - and !empty($db['type']) and $db['type'] == 'sqlite' - and !empty($db['host']) - ) $host_already_exists[$db['host']] = $id; -} - -if (!empty($DIR_SQLITE)) { - foreach (nb::ls_dir($DIR_SQLITE,'\.db$') as $file) { - - if (strpos($file,'_conflict-')) continue; - - if (isset($host_already_exists["$DIR_SQLITE/$file"])) { - $DBQ[$host_already_exists["$DIR_SQLITE/$file"]]['sql_pre'][] = 'PRAGMA journal_mode=OFF'; - continue; - } - - $name = basename($file,'.db'); - $id = "$name-sqlite"; - - $DBQ[$id] = [ - 'host' => "$DIR_SQLITE/$file", - 'type' => 'sqlite', - 'title' => 'created by dbq.php', - '_import' => $name - ]; - #if (!empty($DBQ[$name])) $DBQ[$id] = array_merge_recursive($DBQ[$name],$DBQ[$id]); +] as $DIR_SQLITE) { + if (is_readable($DIR_SQLITE)) { + break; + } else { + unset($DIR_SQLITE); } } +if (!empty($DIR_SQLITE)) $DIR_SQLITE .= '/var/lib/sqlite'; -# -# ALL - Attach all sqlite databases -# -#return; -$DBQ['all'] = [ - 'host' => ':memory:', - 'type' => 'sqlite', - 'pdo' => '', - #'order' => '0', - 'options' => [ - PDO::ATTR_PERSISTENT => true, - ], - 'tables' => [], - 'sql_pre' => [ - 'PRAGMA busy_timeout = 1000', - #'PRAGMA synchronous=NORMAL', - 'PRAGMA journal_mode=MEMORY', - ], -]; - -foreach ($DBQ as $id => $db) { - if (0 - - or empty($db['type'] ) - or $db['type']!='sqlite' - - or empty($db['host'] ) - or !is_readable($db['host']) - or !strpos($db['host'],'.db') - - ) continue; - - $fname = basename($db['host'],'.db'); - $fname = preg_replace('/\..*$/','',basename($db['host'])); - - $DBQ['all']['types']['sql_pre'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname.""; - if (!isset($DBQ[$fname]['name'])) $DBQ[$fname]['name'] = $fname; - conf_merge($DBQ[$fname],$DBQ['all']); - -} -conf_merge($DBQ['nb'],$DBQ['all']); - -# -# Functions -# -function conf_merge(&$c1,&$c2) { - - // .* - foreach ([ - 'default_table', - 'title', - 'row_parse', - #'_import', - ] as $k) { - if (isset($c1[$k])) $c2[$k] = $c1[$k]; - } - - // tables.* - if (!empty($c1['tables'])) { - foreach ((array)$c1['tables'] as $k=>$v) { - $c2['tables'][$k] = $v + ['database' => $c1['name']]; - } - } - - return [$c1,$c2]; -} ?> diff --git a/etc/dbq/crypt.yaml b/etc/dbq/crypt.yaml new file mode 100644 index 00000000..ba97cdee --- /dev/null +++ b/etc/dbq/crypt.yaml @@ -0,0 +1,3 @@ +crypt: + type: sqlite + host: /dev/shm/crypt.db diff --git a/etc/dbq.yaml b/etc/dbq/izi.yaml similarity index 80% rename from etc/dbq.yaml rename to etc/dbq/izi.yaml index 4c1f4af3..a8aae437 100644 --- a/etc/dbq.yaml +++ b/etc/dbq/izi.yaml @@ -1,6 +1,3 @@ -_mysql: - type: mysql - izi: title: Izi Admin host: admin.izideal.vpn @@ -16,19 +13,15 @@ izi: replace: idlang: lang.id -izi-sql: +'izi-sql': title: Mysql Izi Slave host: sql.izideal.vpn _import: - izi -izi-dev: +'izi-dev': host: big.cascais.loc title: Mysql Izi on Big #user: nico _import: - izi - -crypt: - type: sqlite - host: /dev/shm/crypt.db diff --git a/etc/dbq/local.php b/etc/dbq/local.php index bc08c1aa..e3cffec6 100644 --- a/etc/dbq/local.php +++ b/etc/dbq/local.php @@ -2,11 +2,11 @@ # # !!! MANDATORY !!! # -if (1) $DBQ = [ - 'local' => [ +if (true or !nb::is_vm()) $DBQ = [ + 'shell' => [ 'type' => 'sqlite', - 'name' => 'local', - 'host' => nb::ROOT_DIR.'/share/db/local.db', + 'name' => 'shell', + 'host' => nb::ROOT_DIR.'/share/db/shell.db', ], ]; ?> diff --git a/etc/dbq/zzz-all.php b/etc/dbq/zzz-all.php new file mode 100644 index 00000000..ef9ac2c3 --- /dev/null +++ b/etc/dbq/zzz-all.php @@ -0,0 +1,104 @@ + $db) { + if (1 + and !empty($db['type']) and $db['type'] == 'sqlite' + and !empty($db['host']) + ) $host_already_exists[$db['host']] = $id; +} + +if (!empty($DIR_SQLITE)) { + foreach (nb::ls_dir($DIR_SQLITE,'\.db$') as $file) { + + if (strpos($file,'_conflict-')) continue; + + if (isset($host_already_exists["$DIR_SQLITE/$file"])) { + $DBQ[$host_already_exists["$DIR_SQLITE/$file"]]['sql_pre'][] = 'PRAGMA journal_mode=OFF'; + continue; + } + + $name = basename($file,'.db'); + $id = "$name-sqlite"; + + $DBQ[$id] = [ + 'host' => "$DIR_SQLITE/$file", + 'type' => 'sqlite', + 'title' => 'created by dbq.php', + '_import' => $name + ]; + #if (!empty($DBQ[$name])) $DBQ[$id] = array_merge_recursive($DBQ[$name],$DBQ[$id]); + } +} + +# +# ALL - Attach all sqlite databases +# +#return; +#die(count($DBQ)."\n"); +$DBQ['all'] = [ + 'host' => ':memory:', + 'type' => 'sqlite', + 'pdo' => '', + #'order' => '0', + 'options' => [ + PDO::ATTR_PERSISTENT => true, + ], + 'tables' => [], + 'sql_pre' => [ + 'PRAGMA busy_timeout = 1000', + #'PRAGMA synchronous=NORMAL', + 'PRAGMA journal_mode=MEMORY', + ], +]; + +foreach ($DBQ as $id => $db) { + if (0 + + or empty($db['type'] ) + or $db['type']!='sqlite' + + or empty($db['host'] ) + or !is_readable($db['host']) + or !strpos($db['host'],'.db') + + ) continue; + + $fname = basename($db['host'],'.db'); + $fname = preg_replace('/\..*$/','',basename($db['host'])); + + $DBQ['all']['types']['sql_pre'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname.""; + if (!isset($DBQ[$fname]['name'])) $DBQ[$fname]['name'] = $fname; + conf_merge($DBQ[$fname],$DBQ['all']); + +} + +if (!empty($DBQ['nb'])) conf_merge($DBQ['nb'],$DBQ['all']); + +# +# Functions +# +function conf_merge(&$c1,&$c2) { + + // .* + foreach ([ + 'default_table', + 'title', + 'row_parse', + #'_import', + ] as $k) { + if (isset($c1[$k])) $c2[$k] = $c1[$k]; + } + + // tables.* + if (!empty($c1['tables'])) { + foreach ((array)$c1['tables'] as $k=>$v) { + $c2['tables'][$k] = $v + ['database' => $c1['name']]; + } + } + + return [$c1,$c2]; +} +?> diff --git a/etc/profile b/etc/profile index aaf44e2d..162e7912 100755 --- a/etc/profile +++ b/etc/profile @@ -2,7 +2,8 @@ # # Avoid infinit loop. Eg: profile -> .bashrc -> profile # -[ "$1" = "--debug" ] && export NB_DEBUG="echo " && shift +[ "$1" = "--debug" ] && export NB_DEBUG="echo DEBUG: " && shift + #NB_DEBUG='echo ' if [ -n "$NB_LOOP" ]; then [ -n "$NB_DEBUG" ] && $NB_DEBUG "WARN: NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0" @@ -11,7 +12,7 @@ if [ -n "$NB_LOOP" ]; then unset NB_LOOP return 2>/dev/null || exit # if executable fi -[ -n "$NB_DEBUG" ] && $NB_DEBUG "OK: BASH_SOURCE=$BASH_SOURCE \$0=$0" +[ -n "$NB_DEBUG" ] && $NB_DEBUG "ENVS: BASH_SOURCE=$BASH_SOURCE \$0=$0" #trap 'unset NB_LOOP' EXIT export NB_LOOP=1 @@ -19,47 +20,50 @@ export NB_LOOP=1 # # SEARCH ROOT # -dir='' -#NB_DEBUG="echo $0:" +CURRENT_DIR='' #[ "${BASH-no}" != "no" ] && echo Is BASH -case "$BASH_SOURCE" in - */*) dir="${BASH_SOURCE%/*}";; - *) - if [ -n "$ZSH_NAME" ]; then - dir=${(%):-%N} - dir=${dir%/*} - elif [ -r /etc/profile.d/nb.sh ]; then - return - dir=$(dirname $(cut -d'.' -f2 /etc/profile.d/nb.sh)) - fi - ;; -esac - -if [ -z "$dir" ]; then - [ -n "$NB_DEBUG" ] && $NB_DEBUG "ERR: Can't find NB_ROOT" - return 0 +if [ -n "$BASH_SOURCE" ]; then + #CURRENT_DIR="${BASH_SOURCE%/*}" + CURRENT_DIR="${BASH_SOURCE%/*}" +elif [ -n "$ZSH_VERSION" ]; then + eval 'CURRENT_DIR=${(%):-%N}' + CURRENT_DIR=${dir%/*} +elif [ "$KSH_VERSION" != "" ]; then +#elif eval '[[ -n ${.sh.file} ]]' 2>/dev/null; then + eval 'CURRENT_DIR="$(dirname "${.sh.file}")"' +elif [ -r /etc/profile.d/nb.sh ]; then + CURRENT_DIR=$(dirname $(grep '\. ' -m1 /etc/profile.d/nb.sh | cut -d'.' -f2 /etc/profile.d/nb.sh)) +elif [ -r "$0" ]; then + CURRENT_DIR="$(dirname "$0")" +else + for i in /*/*/bin/nb-install /*/*/*/bin/nb-install; do + [ -r "$i" ] && CURRENT_DIR=$(dirname "$i")/../etc && break + done fi -[ -e "$dir/profile.d/functions" ] && . "$dir/profile.d/functions" -NB_ROOT=$(realpath ${dir}/.. 2>/dev/null) -#echo "$NB_ROOT: `pwd`" +if [ -z "$CURRENT_DIR" ]; then + [ -n "$NB_DEBUG" ] && $NB_DEBUG "ERR: Unsupported shell. Please use bash, ksh93 or zsh." +else -# -# Profiles -# -if [ -n "$NB_ROOT" ]; then - [ -e "$NB_ROOT/etc/profile.d/envs" ] && . "$NB_ROOT/etc/profile.d/envs" + [ -e "$CURRENT_DIR/profile.d/functions" ] && . "$CURRENT_DIR/profile.d/functions" + export NB_ROOT=$(realpath $CURRENT_DIR/.. 2>/dev/null) - for i in \ - ${NB_ROOT}/etc/profile.d/aliases \ - $(nb-repo-dirs "etc/profile" "etc/profile.d/*.sh" | grep -vFx "${NB_ROOT}/etc/profile") \ - ;do - [ -n "$NB_DEBUG" ] && $NB_DEBUG "OK: . $i" - [ -r "$i" -a -f "$i" ] && . "$i" - done + # + # Profiles + # + if [ -n "$NB_ROOT" ]; then + [ -e "$NB_ROOT/etc/profile.d/envs" ] && . "$NB_ROOT/etc/profile.d/envs" + + for i in \ + ${NB_ROOT}/etc/profile.d/aliases \ + $(nb-repo-dirs "etc/profile" "etc/profile.d/*.sh" | grep -vFx "${NB_ROOT}/etc/profile") \ + ;do + [ -n "$NB_DEBUG" ] && $NB_DEBUG "OK: . $i" + [ -r "$i" -a -f "$i" ] && . "$i" + done + + fi fi -unset i tmp dir NB_DEBUG NB_LOOP -#unset NB_LOOP -true +unset i tmp CURRENT_DIR NB_DEBUG NB_LOOP diff --git a/etc/profile.d/docker.sh b/etc/profile.d/docker.sh index 266e5015..3e134dc9 100644 --- a/etc/profile.d/docker.sh +++ b/etc/profile.d/docker.sh @@ -7,14 +7,11 @@ which docker > /dev/null || return 0 [ -d ~/ownCloud/Docker ] && export DOCKER_HOME=~/ownCloud/Docker [ -z "$DOCKER_HUB" ] && DOCKER_HUB="" -unset DOCKER_IP -if [ -z "$MYVIMRC" ]; then +if is_prompt && which docker-machine > /dev/null && [ -z "$MYVIMRC" ]; then - if [ -z "$DOCKER_CERT_PATH" -o ! -e "$DOCKER_CERT_PATH" ] && which docker-machine>/dev/null && docker-machine ls -q 2>/dev/null |grep -qFm1 "$DOCKER_MACHINE_NAME"; then + if [ -z "$DOCKER_CERT_PATH" -o ! -e "$DOCKER_CERT_PATH" ] && docker-machine ls -q 2>/dev/null |grep -qFm1 "$DOCKER_MACHINE_NAME"; then eval "$(docker-machine env "$DOCKER_MACHINE_NAME")" 2>/dev/null - DOCKER_IP=$(docker-machine ip "$DOCKER_MACHINE_NAME") fi - export DOCKER_IP fi diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 4fabb36b..908832ae 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -527,6 +527,7 @@ shell_push_arg() { urlencode() { local length="${#1}" + local i for (( i = 0; i < length; i++ )); do #for i in `seq 0 $(($length-1))`; do local c="${1:i:1}" diff --git a/etc/profile.d/sys.sh b/etc/profile.d/sys.sh new file mode 100644 index 00000000..4b110943 --- /dev/null +++ b/etc/profile.d/sys.sh @@ -0,0 +1,3 @@ +sys_is_vm() { + grep -q ^flags.*\ hypervisor /proc/cpuinfo && echo "This machine is a VM" +} diff --git a/lib/php/db.php b/lib/php/db.php index 0766d972..bdf6f728 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -67,6 +67,9 @@ class Db extends nb { ]; # Params + protected $default_params = [ + ]; + protected static $paliases = [ 'd' => 'db', 't' => 'table', @@ -157,11 +160,11 @@ class Db extends nb { #if(!is_scalar($opt['type'])) debug($opt['type']); if (empty($this->out)) $this->out = new Out(['charset'=>$this->charset]); - # id - if (!empty($this->conf)) { - foreach ($this->conf as $id=>$conf) { - } - } +# NB 20.11.16 # id +# NB 20.11.16 if (!empty($this->conf)) { +# NB 20.11.16 foreach ($this->conf as $id=>$conf) { +# NB 20.11.16 } +# NB 20.11.16 } # Add formats from module out foreach($this->out->types() as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t; @@ -660,10 +663,11 @@ class Db extends nb { $file = self::untilde($file); + #if (!is_readable($file)) if (!is_readable($file)) continue; - if (preg_match('/\.(yaml|yml)$/i',$file) and ($yaml = self::yaml_parse_file($file))) { $dbs = array_replace_recursive($dbs,$yaml); + #If (!empty($dbs['crypt'])) debug($dbs['crypt']); } elseif (preg_match('/\.php$/i',$file)) { require($file); diff --git a/lib/php/db/config.php b/lib/php/db/config.php index 994ca07e..e3983d4f 100644 --- a/lib/php/db/config.php +++ b/lib/php/db/config.php @@ -1,34 +1,36 @@ diff --git a/lib/php/db/page.php b/lib/php/db/page.php index 6a5dbc02..8e0c0da9 100644 --- a/lib/php/db/page.php +++ b/lib/php/db/page.php @@ -28,7 +28,7 @@ $Page = new Page([ function($page){ #if (!$page->is('html')) return; global $Db; - if (!empty($Db)) echo $page->tag('div class="db-infos"',join(' | ',[ + if (!empty($Db)) echo $page->tag('p class="db-infos"',join(' | ',[ $Db->type, $Db->host, ($Db->name ? $Db->name : null), diff --git a/lib/php/nb.php b/lib/php/nb.php index 4fb4af40..82ce3a51 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -820,6 +820,10 @@ class NB { return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, substr($value, $iv_size), MCRYPT_MODE_CBC, substr($value, 0, $iv_size))); } + public static function is_vm() { + return file_exists('/proc/cpuinfo') && preg_match('/^flags.* hypervisor/m',file_get_contents('/proc/cpuinfo')); + } + } # < Class return; diff --git a/share/db/local.db b/share/db/shell.db similarity index 100% rename from share/db/local.db rename to share/db/shell.db diff --git a/src/Docker/Compose/dbq/index.php b/src/Docker/Compose/dbq/index.php index 95a54083..0425e12d 100644 --- a/src/Docker/Compose/dbq/index.php +++ b/src/Docker/Compose/dbq/index.php @@ -1,4 +1,6 @@ '."\n"; ?> -- 2.47.3