diff options
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/_ak_checks | 45 | ||||
-rwxr-xr-x | lib/_ak_cjdns | 7 | ||||
-rwxr-xr-x | lib/_ak_coin | 2 | ||||
-rwxr-xr-x | lib/_ak_config | 14 | ||||
-rwxr-xr-x | lib/_ak_datetime | 46 | ||||
-rwxr-xr-x | lib/_ak_fm | 6 | ||||
-rwxr-xr-x | lib/_ak_fs | 79 | ||||
-rwxr-xr-x | lib/_ak_gpg | 28 | ||||
-rwxr-xr-x | lib/_ak_hash_exchange | 8 | ||||
-rwxr-xr-x | lib/_ak_html | 5 | ||||
-rwxr-xr-x | lib/_ak_ipfs | 60 | ||||
-rwxr-xr-x | lib/_ak_irc | 10 | ||||
-rwxr-xr-x | lib/_ak_lib_load | 32 | ||||
-rwxr-xr-x | lib/_ak_log | 106 | ||||
-rwxr-xr-x | lib/_ak_maintainance | 86 | ||||
-rwxr-xr-x | lib/_ak_network | 18 | ||||
-rwxr-xr-x | lib/_ak_network_cjdns | 16 | ||||
-rwxr-xr-x | lib/_ak_network_incoming | 95 | ||||
-rwxr-xr-x | lib/_ak_network_ipfs | 11 | ||||
-rwxr-xr-x | lib/_ak_network_stellar | 50 | ||||
-rwxr-xr-x | lib/_ak_network_utils | 81 | ||||
-rwxr-xr-x | lib/_ak_network_yggdrasil | 47 | ||||
-rwxr-xr-x | lib/_ak_node | 8 | ||||
-rwxr-xr-x | lib/_ak_ns | 202 | ||||
-rwxr-xr-x | lib/_ak_pkg | 231 | ||||
-rwxr-xr-x | lib/_ak_sblock | 6 | ||||
-rwxr-xr-x | lib/_ak_schain | 2 | ||||
-rwxr-xr-x | lib/_ak_script | 64 | ||||
-rwxr-xr-x | lib/_ak_settings | 18 | ||||
-rwxr-xr-x | lib/_ak_sh | 6 | ||||
-rwxr-xr-x | lib/_ak_smfiles | 5 | ||||
-rwxr-xr-x | lib/_ak_wallet | 28 | ||||
-rwxr-xr-x | lib/_ak_yggdrasil | 228 | ||||
-rwxr-xr-x | lib/_ak_zblock | 149 | ||||
-rwxr-xr-x | lib/_ak_zchain | 71 |
35 files changed, 1701 insertions, 169 deletions
diff --git a/lib/_ak_checks b/lib/_ak_checks new file mode 100755 index 0000000..97b3d8a --- /dev/null +++ b/lib/_ak_checks @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +### +### arching-kaos-tools +### Tools to interact and build an Arching Kaos Infochain +### Copyright (C) 2021 - 2025 kaotisk +### +### This program is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### This program is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with this program. If not, see <http://www.gnu.org/licenses/>. +### +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log + +function _ak_checks_example_check_for_base64_strings(){ + + if [ ! -z $1 ] && [ -n "$1" ] + then + cat $1 | base64 | while read line + do + if [ $(echo "$line" | tr -d $'\n' | wc -c) -lt 77 ] + then + if [ $(echo "$line" | tr -d $'\n' | sed -e 's/[0-9A-Za-z]//g; s/[=\/+]//g' | wc -c) -gt 0 ] + then + echo "$line" + _ak_log_error "Not a base64 line" + fi + else + _ak_log_error "Not a base64 line" + fi + done + else + _ak_log_error "No argument given" + fi +} + +_ak_log_debug "_ak_checks loaded $(caller)" diff --git a/lib/_ak_cjdns b/lib/_ak_cjdns index 69e5ffc..51832d3 100755 --- a/lib/_ak_cjdns +++ b/lib/_ak_cjdns @@ -17,9 +17,10 @@ ### You should have received a copy of the GNU General Public License ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log +_ak_lib_load _ak_network -source $AK_LIBDIR/_ak_log -source $AK_LIBDIR/_ak_network rust_sh_install_url="https://sh.rustup.rs" cjdns_src_git_repo_url="https://github.com/cjdelisle/cjdns" cjdnstoolspath="$HOME/cjdns/tools" @@ -198,3 +199,5 @@ function _ak_cjdns_get_ip(){ awk '{print $2}' | \ cut -d'/' -f1 } + +_ak_log_debug "_ak_cjdns loaded $(caller)" diff --git a/lib/_ak_coin b/lib/_ak_coin index 4c58e2d..413d575 100755 --- a/lib/_ak_coin +++ b/lib/_ak_coin @@ -30,3 +30,5 @@ function _ak_coin_stats(){ echo "Coinbase: $COINBASE" echo "Genesis: $GENESIS" } + +_ak_log_debug "_ak_coin loaded $(caller)" diff --git a/lib/_ak_config b/lib/_ak_config index d8705d8..6c71e9c 100755 --- a/lib/_ak_config +++ b/lib/_ak_config @@ -17,7 +17,8 @@ ### You should have received a copy of the GNU General Public License ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### -source $AK_LIBDIR/_ak_node +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_node function _ak_config_show(){ # We will be using our public key also to put it in the block later @@ -31,7 +32,10 @@ function _ak_config_show(){ { "profile":'$profile', "genesis":"'$(cat $AK_WORKDIR/config/zgenesis)'", - "gpg":"'$GPG_PUB_KEY'", + "gpg":{ + "ipfs":"'$GPG_PUB_KEY'", + "fingerprint":"'$(ak gpg --get-key-fingerprint-from-ipfs $GPG_PUB_KEY)'" + }, "zchain":"'$(cat $AK_WORKDIR/config/zchain)'", "zlatest":"'$(ak zchain --get-latest)'" }'| jq; @@ -43,8 +47,9 @@ function _ak_config_publish(){ _ak_ipfs_config_publish $ipfs_hash if [ $? != 0 ] then - echo -e "\033[0;34mError on publishing\033[0;0m\nYour information:\n" - cat tmpfile + _ak_log_error "Error on publishing" + _ak_log_debug "Your information:" + cat tmpfile | while read line; do _ak_log_debug "$line"; done exit 1 fi rm tmpfile @@ -54,3 +59,4 @@ function _ak_config_published(){ _ak_ipfs_cat $(_ak_node_info_ipfs_hash) | jq } +_ak_log_debug "_ak_config loaded $(caller)" diff --git a/lib/_ak_datetime b/lib/_ak_datetime new file mode 100755 index 0000000..a25696f --- /dev/null +++ b/lib/_ak_datetime @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +### +### arching-kaos-tools +### Tools to interact and build an Arching Kaos Infochain +### Copyright (C) 2021 - 2025 kaotisk +### +### This program is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### This program is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with this program. If not, see <http://www.gnu.org/licenses/>. +### + +function _ak_datetime_unix(){ + date -u +%s | tr -d '\n' +} + +function _ak_datetime_unix_nanosecs(){ + date -u +%s.%N | tr -d '\n' +} + +function _ak_datetime_human(){ + date -u +%Y%m%d_%H%M%S +} + +function _ak_datetime_human_date_only(){ + date -u +%Y%m%d +} + +function _ak_datetime_human_date_only_yesterday(){ + date --date=yesterday -u +%Y%m%d 2>/dev/null || date -v -1d -u +%Y%m%d 2>/dev/null +} + +function _ak_datetime_unix_to_human(){ + if [ ! -z $1 ] && [ -n "$1" ] && [ "$(echo $1 | sed -e 's/[0-9]\{10\}//g')" == "" ] + then + date --date=@$1 +%Y%m%d_%H%M%S 2>/dev/null || date -r $1 +%Y%m%d_%H%M%S 2>/dev/null + fi +} @@ -18,6 +18,10 @@ ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log +_ak_lib_load _ak_script + function _ak_fm_remove_line_from_file(){ tempfile="$(_ak_make_temp_file)" if [ ! -f "$2" ] @@ -49,3 +53,5 @@ function _ak_fm_sort_uniq_file(){ _ak_log_info "Sorting $1... Done!" fi } + +_ak_log_debug "_ak_fm loaded $(caller)" @@ -18,8 +18,9 @@ ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### -source $AK_LIBDIR/_ak_log -source $AK_LIBDIR/_ak_hash_exchange +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log +_ak_lib_load _ak_hash_exchange function _ak_fs_dir_init_setup(){ _ak_check_and_create_dir $AK_MAPSDIR @@ -188,7 +189,7 @@ function _ak_fs_import(){ done if [ -f level.1.map ] then - sha512sum level.1.map + # sha512sum level.1.map sha512sum level.1.map >> $TEMPDIR/3rd_gen_map else ak_log_error "Got error an error, level.1.map is missing" @@ -364,7 +365,7 @@ function _ak_fs_from_map_get_original_filename(){ then if [ -f "$AK_MAPSDIR/$1" ] then - cat $AK_MAPSDIR/$1 | tr '\n' ' ' | awk '{ print $2 }' + cat $AK_MAPSDIR/$1 | sed -e 's/ / /g;' | tr '\n' ' ' | awk '{ print $2 }' else _ak_log_error "Map $1 was not found" fi @@ -428,7 +429,7 @@ function _ak_net_try_leaf(){ if [ "$(cat $1 | grep '^[0-9a-z]\{128\}$' | wc -l)" == 2 ] then _ak_log_info "File has two lines which are 2 hashes" - cat $1 + # cat $1 cp $1 ${AK_LEAFSDIR}/$1 exit break @@ -441,7 +442,7 @@ function _ak_net_try_leaf(){ rm $1 fi else - cat $1 | jq >&2 + # cat $1 | jq >&2 _ak_log_warning "File $1 doesn't match expected hash: $(sha512sum $1 | awk '{print $1}')" rm $1 fi @@ -476,7 +477,7 @@ function _ak_net_try_chunk(){ if [ "$(cat $1 | grep -v '^[-A-Za-z0-9+/]*=\{0,3\}$')" == "" ] then _ak_log_info "File is base64" - cat $1 + # cat $1 cp $1 ${AK_CHUNKSDIR}/$1 exit break @@ -485,7 +486,7 @@ function _ak_net_try_chunk(){ rm $1 fi else - cat $1 | jq >&2 + # cat $1 | jq >&2 _ak_log_warning "File $1 doesn't match expected hash: $(sha512sum $1 | awk '{print $1}')" rm $1 fi @@ -617,6 +618,21 @@ function _ak_fs_net_cat(){ fi } +function _ak_fs_net_get(){ + if [ -z $1 ] + then + echo "Please provide a SHA512 hash" + exit 1 + fi + echo $1 | grep "[0123456789abcdef]\{128\}" > /dev/null 2>&1 + if [ $? -ne 0 ] + then + echo "Look, I asked for a SHA512 hash, please try again" + exit 1 + fi + _ak_fs_net_cat $1 $2 > $3 +} + function _ak_fs_from_map_net_get_original_hash(){ TEMPDIR=$(_ak_make_temp_directory) cd $TEMPDIR @@ -655,6 +671,44 @@ function _ak_fs_from_map_net_get_original_hash(){ fi } +function _ak_fs_from_map_net_get_original_filename(){ + TEMPDIR=$(_ak_make_temp_directory) + cd $TEMPDIR + if _ak_fs_verify_input_is_hash "$1" + then + _ak_he_list_peers | while read peer + do + _ak_log_debug "Trying $(_ak_he_url_request_map_hash_from_peer ${peer} $1)..." + curl -s --connect-timeout 3 -o ${TEMPDIR}/peer_${peer}.reply $(_ak_he_url_request_map_hash_from_peer ${peer} $1) + done + find . -type f | while read reply + do + cat ${reply} | jq >/dev/null 2>&1 + if [ $? -eq 0 ] + then + _ak_log_error "Found error in reply: $(cat ${reply} | jq -r '.error')" + else + if [ $(cat ${reply} | wc -l) -ne 2 ] + then + _ak_log_error "${reply} is not two lines long" + exit 1 + else + _ak_log_info "${reply} is two lines long" + fi + if [ $(cat ${reply} | grep '^[0-9a-z]\{128\} ' | wc -l) -eq 2 ] + then + _ak_log_info "${reply} contains two hashes" + else + _ak_log_error "${reply} doesn't contain the appropriate info" + exit 1 + fi + cat ${reply} | head -n 1 | awk '{print $2}' + break + fi + done + fi +} + function _ak_fs_from_map_net_get_root_hash(){ TEMPDIR=$(_ak_make_temp_directory) cd $TEMPDIR @@ -693,9 +747,18 @@ function _ak_fs_from_map_net_get_root_hash(){ fi } +function _ak_fs_net_get_from_map_hash(){ + if _ak_fs_verify_input_is_hash "$1" + then + _ak_fs_net_get `_ak_fs_from_map_net_get_root_hash $1` `_ak_fs_from_map_net_get_original_hash $1` `_ak_fs_from_map_get_original_filename $1` + fi +} + function _ak_fs_net_cat_from_map_hash(){ if _ak_fs_verify_input_is_hash "$1" then _ak_fs_net_cat `_ak_fs_from_map_net_get_root_hash $1` `_ak_fs_from_map_net_get_original_hash $1` fi } + +_ak_log_debug "_ak_fs loaded $(caller)" diff --git a/lib/_ak_gpg b/lib/_ak_gpg index bdb8829..7832ca1 100755 --- a/lib/_ak_gpg +++ b/lib/_ak_gpg @@ -17,10 +17,11 @@ ### You should have received a copy of the GNU General Public License ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### -source $AK_LIBDIR/_ak_log -source $AK_LIBDIR/_ak_ipfs -source $AK_LIBDIR/_ak_config -source $AK_LIBDIR/_ak_settings +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log +_ak_lib_load _ak_ipfs +_ak_lib_load _ak_config +_ak_lib_load _ak_settings export AK_FINGERPRINT="$(_ak_settings_get gpg.fingerprint)" @@ -133,10 +134,19 @@ function _ak_gpg_sign_clear(){ fi } +function _ak_gpg_sign_clear_with_key(){ + if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ] && [ ! -z $3 ] && [ -n "$3" ] + then + _ak_gpg --clear-sign --sign-with $3 --armor --output $1 $2 + else + exit 1 + fi +} + function _ak_gpg_encrypt_sign(){ if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ] && [ ! -z $3 ] && [ -n "$3" ] then - _ak_gpg --sign-with $AK_FINGERPRINT --encypt -r $3 --armor --output $1 $2 + _ak_gpg --sign-with $AK_FINGERPRINT --encrypt -r $3 --armor --output $1 $2 else exit 1 fi @@ -145,7 +155,7 @@ function _ak_gpg_encrypt_sign(){ function _ak_gpg_encrypt_sign_for_self(){ if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ] then - _ak_gpg --sign-with $AK_FINGERPRINT --encypt -r $AK_FINGERPRINT --armor --output $1 $2 + _ak_gpg --sign-with $AK_FINGERPRINT --encrypt -r $AK_FINGERPRINT --armor --output $1 $2 else exit 1 fi @@ -154,7 +164,7 @@ function _ak_gpg_encrypt_sign_for_self(){ function _ak_gpg_encrypt(){ if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ] && [ ! -z $3 ] && [ -n "$3" ] then - _ak_gpg --encypt -r $3 --armor --output $1 $2 + _ak_gpg --encrypt -r $3 --armor --output $1 $2 else exit 1 fi @@ -163,7 +173,7 @@ function _ak_gpg_encrypt(){ function _ak_gpg_encrypt_for_self(){ if [ ! -z $1 ] && [ -n "$1" ] && [ ! -z $2 ] && [ -n "$2" ] then - _ak_gpg --encypt -r $AK_FINGERPRINT --armor --output $1 $2 + _ak_gpg --encrypt -r $AK_FINGERPRINT --armor --output $1 $2 else exit 1 fi @@ -299,3 +309,5 @@ function _ak_gpg_delete_secret_key(){ fi fi } + +_ak_log_debug "_ak_gpg loaded $(caller)" diff --git a/lib/_ak_hash_exchange b/lib/_ak_hash_exchange index 2e39658..3751a8e 100755 --- a/lib/_ak_hash_exchange +++ b/lib/_ak_hash_exchange @@ -17,8 +17,10 @@ ### You should have received a copy of the GNU General Public License ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### -source $AK_LIBDIR/_ak_log -source $AK_LIBDIR/_ak_fm +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log +_ak_lib_load _ak_fm + AK_TREEFS="$AK_WORKDIR/tree" AK_SESSIONSDIR="$AK_WORKDIR/sessions" @@ -73,3 +75,5 @@ function _ak_he_url_request_map_hash_from_random_peer(){ _ak_he_url_request_map_hash_from_peer "$(_ak_he_select_random_peer)" "$1" fi } + +_ak_log_debug "_ak_he loaded $(caller)" diff --git a/lib/_ak_html b/lib/_ak_html index 491cb6c..281790e 100755 --- a/lib/_ak_html +++ b/lib/_ak_html @@ -17,7 +17,8 @@ ### You should have received a copy of the GNU General Public License ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### -source $AK_LIBDIR/_ak_log +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log function _ak_generate_html_header(){ printf '<!DOCTYPE html>\n<html>\n<head>\n' @@ -305,3 +306,5 @@ function _ak_generate_html_zblock(){ printf ' </table>\n' fi } + +_ak_log_debug "_ak_html loaded $(caller)" diff --git a/lib/_ak_ipfs b/lib/_ak_ipfs index f2e0629..07595ae 100755 --- a/lib/_ak_ipfs +++ b/lib/_ak_ipfs @@ -17,7 +17,9 @@ ### You should have received a copy of the GNU General Public License ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### -source $AK_LIBDIR/_ak_log +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log + AK_IPFS_REPO="$AK_WORKDIR/ipfsrepo" AK_IPFS_ARTIFACTS="$AK_WORKDIR/ipfs_artifacts" if [ ! -d $AK_IPFS_ARTIFACTS ] @@ -34,6 +36,15 @@ function _ak_ipfs(){ fi } +function _ak_ipfs_runner(){ + export IPFS_PATH=$AK_IPFS_REPO; kubo $* + if [ $? -ne 0 ] + then + _ak_log_error "Some error occured when running: kubo $*" + #exit 1 + fi +} + function _ak_ipfs_daemon(){ _ak_ipfs daemon --routing=dht --migrate & printf '%s' "$!" > $AK_WORKDIR/akipfsd.pid @@ -107,7 +118,7 @@ function _ak_ipfs_scanner(){ function _ak_ipfs_add(){ if [ -z $1 ] || [ ! -n "$1" ] then - _ak_log_error "no argument given" + _ak_log_error "_ak_ipfs_add: no argument given" exit 1 fi # Receives a file @@ -123,7 +134,7 @@ function _ak_ipfs_add(){ function _ak_ipfs_block_stat(){ if [ -z $1 ] || [ ! -n "$1" ] then - _ak_log_error "no argument given" + _ak_log_error "_ak_ipfs_block_stat: no argument given" exit 1 fi _ak_ipfs block stat "$1" @@ -247,7 +258,7 @@ function _ak_ipfs_get(){ function _ak_ipfs_cat(){ if [ -z $1 ] || [ ! -n "$1" ] then - _ak_log_error "no argument given" + _ak_log_error "_ak_ipfs_cat: no argument given" exit 1 fi if [ ! -f $AK_IPFS_ARTIFACTS/$1 ] @@ -449,7 +460,7 @@ function _ak_ipfs_init(){ } function _ak_ipfs_download(){ - _ak_log_info "Attempting to install IPFS..." + _ak_log_info "Figuring latest compatible version of kubo (IPFS)..." IPFS_VERSION="$(curl \ --connect-timeout 3 \ -s https://dist.ipfs.tech/kubo/versions | tail -1)" @@ -480,6 +491,43 @@ function _ak_ipfs_download(){ fi } +function _ak_ipfs_install(){ + _ak_log_info "Making temp files" + pwd > pwd + mktemp -d > tempassin + ak_curdir=$(cat pwd) + TEMPASSIN=$(cat tempassin) + cd $TEMPASSIN + tar xf $AK_ARCHIVESDIR/$IPFS_TARGET_FILE + cp kubo/ipfs $AK_BINDIR/kubo + cd $ak_curdir + rm -rf $TEMPASSIN + _ak_log_info "ipfs installed on $AK_BINDIR" +} + +function _ak_ipfs_check_and_install(){ + which kubo > /dev/null 2>&1 + if [ $? -ne 0 ] + then + _ak_ipfs_download + _ak_ipfs_install + _ak_ipfs init + _ak_ipfs_swarm_install + else + _ak_log_info "Found IPFS! Skipping downloading..." + _ak_ipfs_swarm_install + fi + if [ ! -d $AK_IPFS_REPO ] + then + _ak_ipfs init + _ak_ipfs_swarm_install + fi + if [ ! -f $AK_ZGENESIS ] + then + _ak_ipfs add -Q $AK_GENESIS > $AK_ZGENESIS + fi +} + function _ak_ipfs_cid_v0_check(){ if [ -z $1 ] || [ ! -n "$1" ] then @@ -518,3 +566,5 @@ function _ak_ipfs_swarm_install(){ function _ak_ipfs_get_config_ipns_key(){ _ak_ipfs_key_list_full | grep 'ak-config' | cut -d ' ' -f 1 } + +_ak_log_debug "_ak_ipfs loaded $(caller)" diff --git a/lib/_ak_irc b/lib/_ak_irc new file mode 100755 index 0000000..85e8bc7 --- /dev/null +++ b/lib/_ak_irc @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +source $AK_LIBDIR/_ak_lib_load +_ak_lib_load _ak_log +_ak_lib_load _ak_script + +function _ak_irc_connect(){ + _ak_not_implemented "${FUNCNAME}" +} + +_ak_log_debug "_ak_irc loaded $(caller)" diff --git a/lib/_ak_lib_load b/lib/_ak_lib_load new file mode 100755 index 0000000..2790458 --- /dev/null +++ b/lib/_ak_lib_load @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +### +### arching-kaos-tools +### Tools to interact and build an Arching Kaos Infochain +### Copyright (C) 2021 - 2025 kaotisk +### +### This program is free software: you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### This program is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with this program. If not, see <http://www.gnu.org/licenses/>. +### + +function _ak_lib_load(){ + typeset -F | grep $1 2>/dev/null 1>&2 || if [ -f "$AK_LIBDIR/$1" ] + then + source $AK_LIBDIR/$1 + elif [ -f "./lib/$1" ] + then + source ./lib/$1 + else + printf "Error: Could not load $1 lib" + fi + +} diff --git a/lib/_ak_log b/lib/_ak_log index 8d6972d..51064e7 100755 --- a/lib/_ak_log +++ b/lib/_ak_log @@ -18,6 +18,48 @@ ### along with this program. If not, see <http://www.gnu.org/licenses/>. ### +source ./lib/_ak_lib_load 2>/dev/null || source $AK_LIBDIR/_ak_lib_load 2>/dev/null +_ak_lib_load _ak_datetime + +export AK_LOGSDIR="$AK_WORKDIR/logs" +export AK_LOGSFILE="$AK_LOGSDIR/log" +export AK_LOGTOFILE="no" +if [ -d $AK_SETTINGS ] +then + if [ ! -d $AK_SETTINGS/logging ] + then + mkdir -p $AK_SETTINGS/logging + fi + if [ -f $AK_SETTINGS/logging/logToFile ] + then + export AK_LOGTOFILE="$(cat $AK_SETTINGS/logging/logToFile)" + else + echo -n $AK_LOGTOFILE > $AK_SETTINGS/logging/logToFile + fi +fi + +if [ -n "${AK_WORKDIR}" ] && [ -d "${AK_WORKDIR}" ] +then + if [ ! -d $AK_LOGSDIR ] + then + if [ -f $AK_LOGSDIR ] + then + TEMP="$(mktemp)" + mv $AK_LOGSDIR $TEMP + mkdir -p $AK_LOGSDIR + mv $TEMP $AK_LOGSFILE + _ak_log_info 'Migration of logs was successful!' + else + mkdir -p $AK_LOGSDIR + touch $AK_LOGSFILE + _ak_log_info 'Update was successful!' + fi + fi +else + printf "Can't find AK_WORKDIR to work upon.\n" + exit 1 +fi + function _ak_log_print_log_line(){ if [ -n "$1" ] then @@ -26,7 +68,7 @@ function _ak_log_print_log_line(){ messagetype="$(echo "$*" | awk '{print $3}')" message="$(echo "$*" | cut -d ' ' -f4-)" printf '%s \033[1;32m%s\033[0;00m \033[1;31m%s\033[0;00m %s\n' \ - "$(date --date=@$timestamp +%Y%m%d_%H%M%S 2>/dev/null || date -r $timestamp +%Y%m%d_%H%M%S 2>/dev/null)" \ + "$(_ak_datetime_unix_to_human $timestamp)" \ "$program" \ "$messagetype" \ "$message" @@ -40,6 +82,11 @@ function _ak_log_follow(){ done } +function _ak_log_last_line(){ + p="$(tail -n 1 $AK_LOGSFILE | tr -d '\n')" + _ak_log_print_log_line "$p" +} + function _ak_log_grep(){ if [ ! -z $1 ] && [ -n "$1" ] then @@ -63,50 +110,64 @@ function _ak_log_grep(){ } function _ak_log_rotate(){ + cd $AK_LOGSDIR if [ -f $AK_LOGSFILE ] then - tar cvfz $AK_ARCHIVESDIR/logs_$(date -u +%s).tar.gz $AK_WORKDIR/logs - cat /dev/null > $AK_WORKDIR/logs + gzip -c log > $(_ak_datetime_unix)_log.gz + cat /dev/null > log fi - if [ -f $AK_WORKDIR/akd.log ] + if [ -f $AK_LOGSDIR/akd.log ] then - tar cvfz $AK_ARCHIVESDIR/akd-logs_$(date -u +%s).tar.gz $AK_WORKDIR/akd.log - cat /dev/null > $AK_WORKDIR/akd.log + gzip -c akd.log > $(_ak_datetime_unix)_akd-log.gz + cat /dev/null > akd.log fi } function _ak_log_message(){ - TS="$(date -u +%s)" + TS="$(_ak_datetime_unix)" if [ ! -z $1 ] && [ -n "$1" ] then - if [ ! -z "$2" ] && [ -n "$2" ] + prg="$1" + shift + if [ ! -z "$1" ] && [ -n "$1" ] then - if [ ! -z "$3" ] && [ -n "$3" ] + tp="$1" + shift + if [ ! -z "$*" ] && [ -n "$*" ] then - msg="$(echo -n $*| cut -d ' ' -f 3-)" - echo "$TS" "<$1>" "[$2]" "$msg" >> $AK_LOGSFILE + msg="$*" + if [ ! -z $AK_LOGTOFILE ] && [ -n "$AK_LOGTOFILE" ] && [ "$AK_LOGTOFILE" == "yes" ] + then + echo "$TS" "<$prg>" "[$tp]" "$msg" >> $AK_LOGSFILE + fi if [ ! -z $AK_DEBUG ] && [ -n "$AK_DEBUG" ] && [ "$AK_DEBUG" == "yes" ] then - _ak_log_print_log_line "$TS <$1> [$2] $msg" >&2 + _ak_log_print_log_line "$TS <$prg> [$tp] $msg" >&2 fi else - echo "$TS" "<$1>" "[ERROR]" "No message" >> $AK_LOGSFILE + echo "$TS" "<$prg>" "[ERROR]" "No message" >> $AK_LOGSFILE if [ "$AK_DEBUG" == "yes" ] then - echo "$TS" "<$1>" "[ERROR]" "No message" >&2 + echo "$TS" "<$prg>" "[ERROR]" "No message" >&2 fi return 1 fi else - echo "$TS" "<$1>" "[ERROR]" "No type and message" >> $AK_LOGSFILE + if [ ! -z $AK_LOGTOFILE ] && [ -n "$AK_LOGTOFILE" ] && [ "$AK_LOGTOFILE" == "yes" ] + then + echo "$TS" "<$prg>" "[ERROR]" "No type and message" >> $AK_LOGSFILE + fi if [ "$AK_DEBUG" == "yes" ] then - echo "$TS" "<$1>" "[ERROR]" "No type and message" >&2 + echo "$TS" "<$prg>" "[ERROR]" "No type and message" >&2 fi return 1 fi else - echo "$TS" "<$(basename $0)>" "[ERROR]" "No arguments given" >> $AK_LOGSFILE + if [ ! -z $AK_LOGTOFILE ] && [ -n "$AK_LOGTOFILE" ] && [ "$AK_LOGTOFILE" == "yes" ] + then + echo "$TS" "<$(basename $0)>" "[ERROR]" "No arguments given" >> $AK_LOGSFILE + fi if [ "$AK_DEBUG" == "yes" ] then echo "$TS" "<$(basename $0)>" "[ERROR]" "No arguments given" >&2 @@ -116,23 +177,24 @@ function _ak_log_message(){ } function _ak_log_exit(){ - _ak_log_message "$PROGRAM" "EXIT" "$1" + _ak_log_message "$PROGRAM" "EXIT" "$*" } function _ak_log_warning(){ - _ak_log_message "$PROGRAM" "WARNING" "$1" + _ak_log_message "$PROGRAM" "WARNING" "$*" } function _ak_log_debug(){ |