From aa167d3e26bd4fd0fedc1ea94b7069cca2100181 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 2 Aug 2016 20:24:45 +0200 Subject: lib: import shell from krebs/4lib --- krebs/4lib/default.nix | 3 +-- krebs/4lib/shell.nix | 22 ---------------------- lib/default.nix | 7 +++++++ lib/shell.nix | 21 +++++++++++++++++++++ 4 files changed, 29 insertions(+), 24 deletions(-) delete mode 100644 krebs/4lib/shell.nix create mode 100644 lib/default.nix create mode 100644 lib/shell.nix diff --git a/krebs/4lib/default.nix b/krebs/4lib/default.nix index 296748333..8a822ed7d 100644 --- a/krebs/4lib/default.nix +++ b/krebs/4lib/default.nix @@ -3,7 +3,7 @@ with builtins; with lib; -let out = rec { +let out = import // rec { eq = x: y: x == y; ne = x: y: x != y; @@ -34,7 +34,6 @@ let out = rec { genid = import ./genid.nix { lib = lib // out; }; git = import ./git.nix { lib = lib // out; }; - shell = import ./shell.nix { inherit lib; }; tree = import ./tree.nix { inherit lib; }; lpad = n: c: s: diff --git a/krebs/4lib/shell.nix b/krebs/4lib/shell.nix deleted file mode 100644 index 5910adacc..000000000 --- a/krebs/4lib/shell.nix +++ /dev/null @@ -1,22 +0,0 @@ -{ lib, ... }: - -with builtins; -with lib; - -rec { - escape = - let - isSafeChar = c: match "[-+./0-9:=A-Z_a-z]" c != null; - in - stringAsChars (c: - if isSafeChar c then c - else if c == "\n" then "'\n'" - else "\\${c}"); - - # - # shell script generators - # - - # example: "${cat (toJSON { foo = "bar"; })} | jq -r .foo" - cat = s: "printf '%s' ${escape s}"; -} diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 000000000..6c607f13b --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,7 @@ +let + lib = import // builtins // { + shell = import ./shell.nix { inherit lib; }; + }; +in + +lib diff --git a/lib/shell.nix b/lib/shell.nix new file mode 100644 index 000000000..a8ff5dbe0 --- /dev/null +++ b/lib/shell.nix @@ -0,0 +1,21 @@ +{ lib, ... }: + +with lib; + +rec { + escape = + let + isSafeChar = c: match "[-+./0-9:=A-Z_a-z]" c != null; + in + stringAsChars (c: + if isSafeChar c then c + else if c == "\n" then "'\n'" + else "\\${c}"); + + # + # shell script generators + # + + # example: "${cat (toJSON { foo = "bar"; })} | jq -r .foo" + cat = s: "printf '%s' ${escape s}"; +} -- cgit v1.2.3 From 1e3e6f7bbc9290aad99a86f33982a8dd7d60caba Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 2 Aug 2016 20:35:34 +0200 Subject: writeJSON: init --- krebs/5pkgs/builders.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/krebs/5pkgs/builders.nix b/krebs/5pkgs/builders.nix index da7052f38..4c087bc44 100644 --- a/krebs/5pkgs/builders.nix +++ b/krebs/5pkgs/builders.nix @@ -242,6 +242,8 @@ rec { cp src.jq "$out" ''; + writeJSON = name: value: pkgs.writeText name (toJSON value); + writeNixFromCabal = trace (toString [ "The function `writeNixFromCabal` has been deprecated in favour of" -- cgit v1.2.3 From 0928cc03a6191640c66c9122159994855527faef Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 2 Aug 2016 20:39:26 +0200 Subject: logf: init at 1.0.0 --- krebs/5pkgs/logf/default.nix | 103 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 krebs/5pkgs/logf/default.nix diff --git a/krebs/5pkgs/logf/default.nix b/krebs/5pkgs/logf/default.nix new file mode 100644 index 000000000..246e4d954 --- /dev/null +++ b/krebs/5pkgs/logf/default.nix @@ -0,0 +1,103 @@ +{ lib, pkgs, ... }: + +with import ; + +let + default-host-colors = pkgs.writeJSON "logf.default-host-colors.json" { + }; + default-prio-colors = pkgs.writeJSON "logf.default-prio-colors.json" { + "0" = 196; # emerg + "1" = 160; # alert + "2" = 124; # crit + "3" = 009; # err + "4" = 011; # warning + "5" = 255; # notice + "6" = 250; # info + "7" = 139; # debug + }; +in + +pkgs.writeDashBin "logf" '' + export LOGF_HOST_COLORS LOGF_PRIO_COLORS + LOGF_HOST_COLORS=$(cat "''${LOGF_HOST_COLORS-${default-host-colors}}") + LOGF_PRIO_COLORS=$(cat "''${LOGF_PRIO_COLORS-${default-prio-colors}}") + printf '%s\0' "$@" \ + | ${pkgs.findutils}/bin/xargs -0 -P 0 -n 1 ${pkgs.writeDash "logf-remote" '' + target=$1 \ + target_host=$(echo "$1" | sed 's/^.*@//;s/\..*//') \ + exec 3>&1; + 2>&1 1>&3 ssh "$target" -T \ + -o PreferredAuthentications=publickey \ + -o StrictHostKeyChecking=yes \ + exec journalctl -af -n 0 -o json \ + | stdbuf -oL jq -Rf ${pkgs.writeJq "logf-remote-error.jq" '' + { + PRIORITY: "4", + MESSAGE: ., + SYSLOG_IDENTIFIER: env.target_host, + } + ''} + sleep 10m + exec "$0" "$@" + ''} \ + | ${pkgs.jq}/bin/jq -rf ${pkgs.writeJq "logf-filter.jq" '' + (env.LOGF_HOST_COLORS | fromjson) as $host_colors | + (env.LOGF_PRIO_COLORS | fromjson) as $prio_colors | + + def when(c; f): if c then f else . end; + + # anaphoric gsub + def agsub(re; f): + gsub("(?\(re))"; .it | f); + + # :: [int] -> sgr + def sgr: "\u001b[\(map(tostring) | join(";"))m"; + + # :: sgr + def rst: [] | sgr; + + # :: int -> sgr + def fg(i): [38,5,i]|sgr; + # TODO def fg(r;g;b): [38,2,r,g,b]|sgr; + # http://cvs.schmorp.de/rxvt-unicode/src/command.C?revision=1.570&view=markup&sortby=log&sortdir=down + + # (sgr; sgr) | (null; any) :: str -> str + def col(a; b): when(a != null; a + . + b); + def col(a): col(a; rst); + + + def p_time: + ._SOURCE_REALTIME_TIMESTAMP + | if . != null then . | fromjson | . / 1000000 else now end + | gmtime + | todateiso8601 + | col(fg(237)); + + def p_host: + ._HOSTNAME + | if . != null then . else "-" end + | col($host_colors[.]|when(. != null; fg(.))); + + def p_ident: + if .SYSLOG_IDENTIFIER != null then .SYSLOG_IDENTIFIER + else ._COMM end + | col(fg(244)); + + def p_message: + fg($prio_colors[if has("PRIORITY") then .PRIORITY else "-" end]) + as $prio_c | + .MESSAGE + | sub("\r$"; "") + | agsub("\\btv@nomic\\b"; "\(.)\u0007" | col(fg(219); $prio_c)) + #| agsub("Start queue"; "\(.)\u0007" | col(fg(42); $prio_c)) + | col($prio_c); + + + [ p_time + , p_host + , p_ident + , p_message + ] + | join(" ") + ''} +'' -- cgit v1.2.3 From 46d73a70ce2df4af2d66228874470cb4c8657f14 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 2 Aug 2016 21:51:39 +0200 Subject: tv.hosts: init --- tv/3modules/default.nix | 1 + tv/3modules/hosts.nix | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 tv/3modules/hosts.nix diff --git a/tv/3modules/default.nix b/tv/3modules/default.nix index 0b0bccab9..397ee8e85 100644 --- a/tv/3modules/default.nix +++ b/tv/3modules/default.nix @@ -4,6 +4,7 @@ _: imports = [ ./charybdis ./ejabberd + ./hosts.nix ./iptables.nix ]; } diff --git a/tv/3modules/hosts.nix b/tv/3modules/hosts.nix new file mode 100644 index 000000000..7bf3267c7 --- /dev/null +++ b/tv/3modules/hosts.nix @@ -0,0 +1,12 @@ +{ config, ... }: + +with config.krebs.lib; + +{ + options.tv.hosts = mkOption { + type = types.attrsOf types.host; + default = + filterAttrs (_: host: host.owner.name == "tv") + config.krebs.hosts; + }; +} -- cgit v1.2.3