summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/get-ssh-port/default.nix
blob: 2d106b2b60c01b9afaf15c502bbbbc984526d7cd (plain)
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
{ config, pkgs, ... }: with config.krebs.lib;

pkgs.writeScriptBin "get-ssh-port" ''
  #! ${pkgs.dash}/bin/dash
  set -efu
  if test $# != 1 || test $1 = -h || test $1 = --help; then
    echo "usage: get-ssh-port HOSTNAME" >&2
    exit 23
  fi
  case $1 in
  ${concatMapStringsSep ";;\n"
    (host: toString [
      "(${shell.escape host.name})"
      "echo ${toString host.nets.${config.krebs.search-domain}.ssh.port}"
    ])
    (filter (host: hasAttr config.krebs.search-domain host.nets)
            (attrValues config.krebs.hosts))
  };;
  ${concatMapStringsSep ";;\n"
    (net: toString [
      "(${concatMapStringsSep "|" shell.escape net.aliases})"
      "echo ${toString net.ssh.port}"
    ])
    (concatMap (host: attrValues host.nets) (attrValues config.krebs.hosts))
  };;
  (*) echo "get-ssh-port: don't know ssh port of $1" >&2
      exit 1
  esac
''